Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 49 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 50 | |
| 51 | #include <linux/sched/signal.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/file.h> |
| 54 | #include <linux/fdtable.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/mman.h> |
| 57 | #include <linux/mmu_context.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 78 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 79 | #define CREATE_TRACE_POINTS |
| 80 | #include <trace/events/io_uring.h> |
| 81 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 82 | #include <uapi/linux/io_uring.h> |
| 83 | |
| 84 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 85 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 86 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 87 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 88 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 92 | */ |
| 93 | #define IORING_FILE_TABLE_SHIFT 9 |
| 94 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 95 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 96 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 97 | |
| 98 | struct io_uring { |
| 99 | u32 head ____cacheline_aligned_in_smp; |
| 100 | u32 tail ____cacheline_aligned_in_smp; |
| 101 | }; |
| 102 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 103 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 104 | * This data is shared with the application through the mmap at offsets |
| 105 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | * |
| 107 | * The offsets to the member fields are published through struct |
| 108 | * io_sqring_offsets when calling io_uring_setup. |
| 109 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 110 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Head and tail offsets into the ring; the offsets need to be |
| 113 | * masked to get valid indices. |
| 114 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 115 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 116 | * and the application controls tail of the sq ring and the head of the |
| 117 | * cq ring. |
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 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 120 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 121 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | * ring_entries - 1) |
| 123 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | u32 sq_ring_mask, cq_ring_mask; |
| 125 | /* Ring sizes (constant, power of 2) */ |
| 126 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Number of invalid entries dropped by the kernel due to |
| 129 | * invalid index stored in array |
| 130 | * |
| 131 | * Written by the kernel, shouldn't be modified by the |
| 132 | * application (i.e. get number of "new events" by comparing to |
| 133 | * cached value). |
| 134 | * |
| 135 | * After a new SQ head value was read by the application this |
| 136 | * counter includes all submissions that were dropped reaching |
| 137 | * the new SQ head (and possibly more). |
| 138 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 139 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Runtime flags |
| 142 | * |
| 143 | * Written by the kernel, shouldn't be modified by the |
| 144 | * application. |
| 145 | * |
| 146 | * The application needs a full memory barrier before checking |
| 147 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 148 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 149 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 150 | /* |
| 151 | * Number of completion events lost because the queue was full; |
| 152 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 153 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | * the completion queue. |
| 155 | * |
| 156 | * Written by the kernel, shouldn't be modified by the |
| 157 | * application (i.e. get number of "new events" by comparing to |
| 158 | * cached value). |
| 159 | * |
| 160 | * As completion events come in out of order this counter is not |
| 161 | * ordered with any other data. |
| 162 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 163 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 164 | /* |
| 165 | * Ring buffer of completion events. |
| 166 | * |
| 167 | * The kernel writes completion events fresh every time they are |
| 168 | * produced, so the application is allowed to modify pending |
| 169 | * entries. |
| 170 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 171 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 174 | struct io_mapped_ubuf { |
| 175 | u64 ubuf; |
| 176 | size_t len; |
| 177 | struct bio_vec *bvec; |
| 178 | unsigned int nr_bvecs; |
| 179 | }; |
| 180 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 181 | struct fixed_file_table { |
| 182 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 185 | enum { |
| 186 | FFD_F_ATOMIC, |
| 187 | }; |
| 188 | |
| 189 | struct fixed_file_data { |
| 190 | struct fixed_file_table *table; |
| 191 | struct io_ring_ctx *ctx; |
| 192 | |
| 193 | struct percpu_ref refs; |
| 194 | struct llist_head put_llist; |
| 195 | unsigned long state; |
| 196 | struct work_struct ref_work; |
| 197 | struct completion done; |
| 198 | }; |
| 199 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 200 | struct io_ring_ctx { |
| 201 | struct { |
| 202 | struct percpu_ref refs; |
| 203 | } ____cacheline_aligned_in_smp; |
| 204 | |
| 205 | struct { |
| 206 | unsigned int flags; |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 207 | int compat: 1; |
| 208 | int account_mem: 1; |
| 209 | int cq_overflow_flushed: 1; |
| 210 | int drain_next: 1; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 211 | int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 212 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 213 | /* |
| 214 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 215 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 216 | * |
| 217 | * This indirection could e.g. be used to assign fixed |
| 218 | * io_uring_sqe entries to operations and only submit them to |
| 219 | * the queue when needed. |
| 220 | * |
| 221 | * The kernel modifies neither the indices array nor the entries |
| 222 | * array. |
| 223 | */ |
| 224 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 225 | unsigned cached_sq_head; |
| 226 | unsigned sq_entries; |
| 227 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 228 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 229 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 230 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 231 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 232 | |
| 233 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 234 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 235 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 236 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 237 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 238 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 239 | } ____cacheline_aligned_in_smp; |
| 240 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 241 | struct io_rings *rings; |
| 242 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 243 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 244 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 245 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 246 | struct mm_struct *sqo_mm; |
| 247 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 248 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 249 | /* |
| 250 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 251 | * readers must ensure that ->refs is alive as long as the file* is |
| 252 | * used. Only updated through io_uring_register(2). |
| 253 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 254 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 255 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 256 | int ring_fd; |
| 257 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 258 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 259 | /* if used, fixed mapped user buffers */ |
| 260 | unsigned nr_user_bufs; |
| 261 | struct io_mapped_ubuf *user_bufs; |
| 262 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 263 | struct user_struct *user; |
| 264 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 265 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 266 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 267 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 268 | struct completion *completions; |
| 269 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 270 | /* if all else fails... */ |
| 271 | struct io_kiocb *fallback_req; |
| 272 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 273 | #if defined(CONFIG_UNIX) |
| 274 | struct socket *ring_sock; |
| 275 | #endif |
| 276 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 277 | struct idr personality_idr; |
| 278 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 279 | struct { |
| 280 | unsigned cached_cq_tail; |
| 281 | unsigned cq_entries; |
| 282 | unsigned cq_mask; |
| 283 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 284 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 285 | struct wait_queue_head cq_wait; |
| 286 | struct fasync_struct *cq_fasync; |
| 287 | struct eventfd_ctx *cq_ev_fd; |
| 288 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 289 | |
| 290 | struct { |
| 291 | struct mutex uring_lock; |
| 292 | wait_queue_head_t wait; |
| 293 | } ____cacheline_aligned_in_smp; |
| 294 | |
| 295 | struct { |
| 296 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 297 | struct llist_head poll_llist; |
| 298 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 299 | /* |
| 300 | * ->poll_list is protected by the ctx->uring_lock for |
| 301 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 302 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 303 | * manipulate the list, hence no extra locking is needed there. |
| 304 | */ |
| 305 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 306 | struct hlist_head *cancel_hash; |
| 307 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 308 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 309 | |
| 310 | spinlock_t inflight_lock; |
| 311 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 312 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 315 | /* |
| 316 | * First field must be the file pointer in all the |
| 317 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 318 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 319 | struct io_poll_iocb { |
| 320 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 321 | union { |
| 322 | struct wait_queue_head *head; |
| 323 | u64 addr; |
| 324 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 325 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 326 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 327 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 328 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 331 | struct io_close { |
| 332 | struct file *file; |
| 333 | struct file *put_file; |
| 334 | int fd; |
| 335 | }; |
| 336 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 337 | struct io_timeout_data { |
| 338 | struct io_kiocb *req; |
| 339 | struct hrtimer timer; |
| 340 | struct timespec64 ts; |
| 341 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 342 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 343 | }; |
| 344 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 345 | struct io_accept { |
| 346 | struct file *file; |
| 347 | struct sockaddr __user *addr; |
| 348 | int __user *addr_len; |
| 349 | int flags; |
| 350 | }; |
| 351 | |
| 352 | struct io_sync { |
| 353 | struct file *file; |
| 354 | loff_t len; |
| 355 | loff_t off; |
| 356 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 357 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 358 | }; |
| 359 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 360 | struct io_cancel { |
| 361 | struct file *file; |
| 362 | u64 addr; |
| 363 | }; |
| 364 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 365 | struct io_timeout { |
| 366 | struct file *file; |
| 367 | u64 addr; |
| 368 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 369 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 370 | }; |
| 371 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 372 | struct io_rw { |
| 373 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 374 | struct kiocb kiocb; |
| 375 | u64 addr; |
| 376 | u64 len; |
| 377 | }; |
| 378 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 379 | struct io_connect { |
| 380 | struct file *file; |
| 381 | struct sockaddr __user *addr; |
| 382 | int addr_len; |
| 383 | }; |
| 384 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 385 | struct io_sr_msg { |
| 386 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 387 | union { |
| 388 | struct user_msghdr __user *msg; |
| 389 | void __user *buf; |
| 390 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 391 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 392 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 393 | }; |
| 394 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 395 | struct io_open { |
| 396 | struct file *file; |
| 397 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 398 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 399 | unsigned mask; |
| 400 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 401 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 402 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 403 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 406 | struct io_files_update { |
| 407 | struct file *file; |
| 408 | u64 arg; |
| 409 | u32 nr_args; |
| 410 | u32 offset; |
| 411 | }; |
| 412 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 413 | struct io_fadvise { |
| 414 | struct file *file; |
| 415 | u64 offset; |
| 416 | u32 len; |
| 417 | u32 advice; |
| 418 | }; |
| 419 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 420 | struct io_madvise { |
| 421 | struct file *file; |
| 422 | u64 addr; |
| 423 | u32 len; |
| 424 | u32 advice; |
| 425 | }; |
| 426 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 427 | struct io_epoll { |
| 428 | struct file *file; |
| 429 | int epfd; |
| 430 | int op; |
| 431 | int fd; |
| 432 | struct epoll_event event; |
| 433 | }; |
| 434 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 435 | struct io_async_connect { |
| 436 | struct sockaddr_storage address; |
| 437 | }; |
| 438 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 439 | struct io_async_msghdr { |
| 440 | struct iovec fast_iov[UIO_FASTIOV]; |
| 441 | struct iovec *iov; |
| 442 | struct sockaddr __user *uaddr; |
| 443 | struct msghdr msg; |
| 444 | }; |
| 445 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 446 | struct io_async_rw { |
| 447 | struct iovec fast_iov[UIO_FASTIOV]; |
| 448 | struct iovec *iov; |
| 449 | ssize_t nr_segs; |
| 450 | ssize_t size; |
| 451 | }; |
| 452 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 453 | struct io_async_open { |
| 454 | struct filename *filename; |
| 455 | }; |
| 456 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 457 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 458 | union { |
| 459 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 460 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 461 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 462 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 463 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 464 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 467 | enum { |
| 468 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 469 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 470 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 471 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 472 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 473 | |
| 474 | REQ_F_LINK_NEXT_BIT, |
| 475 | REQ_F_FAIL_LINK_BIT, |
| 476 | REQ_F_INFLIGHT_BIT, |
| 477 | REQ_F_CUR_POS_BIT, |
| 478 | REQ_F_NOWAIT_BIT, |
| 479 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 480 | REQ_F_LINK_TIMEOUT_BIT, |
| 481 | REQ_F_TIMEOUT_BIT, |
| 482 | REQ_F_ISREG_BIT, |
| 483 | REQ_F_MUST_PUNT_BIT, |
| 484 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 485 | REQ_F_COMP_LOCKED_BIT, |
| 486 | }; |
| 487 | |
| 488 | enum { |
| 489 | /* ctx owns file */ |
| 490 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 491 | /* drain existing IO first */ |
| 492 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 493 | /* linked sqes */ |
| 494 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 495 | /* doesn't sever on completion < 0 */ |
| 496 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 497 | /* IOSQE_ASYNC */ |
| 498 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 499 | |
| 500 | /* already grabbed next link */ |
| 501 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 502 | /* fail rest of links */ |
| 503 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 504 | /* on inflight list */ |
| 505 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 506 | /* read/write uses file position */ |
| 507 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 508 | /* must not punt to workers */ |
| 509 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 510 | /* polled IO has completed */ |
| 511 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 512 | /* has linked timeout */ |
| 513 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 514 | /* timeout request */ |
| 515 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 516 | /* regular file */ |
| 517 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 518 | /* must be punted even for NONBLOCK */ |
| 519 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 520 | /* no timeout sequence */ |
| 521 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 522 | /* completion under lock */ |
| 523 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
| 524 | }; |
| 525 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 526 | /* |
| 527 | * NOTE! Each of the iocb union members has the file pointer |
| 528 | * as the first entry in their struct definition. So you can |
| 529 | * access the file pointer through any of the sub-structs, |
| 530 | * or directly as just 'ki_filp' in this struct. |
| 531 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 532 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 533 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 534 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 535 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 536 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 537 | struct io_accept accept; |
| 538 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 539 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 540 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 541 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 542 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 543 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 544 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 545 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 546 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 547 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 548 | struct io_epoll epoll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 549 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 550 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 551 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 552 | /* |
| 553 | * llist_node is only used for poll deferred completions |
| 554 | */ |
| 555 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 556 | bool has_user; |
| 557 | bool in_async; |
| 558 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 559 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 560 | |
| 561 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 562 | union { |
| 563 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 564 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 565 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 566 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 567 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 568 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 569 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 570 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 571 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 572 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 573 | struct list_head inflight_entry; |
| 574 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 575 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 579 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 580 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 581 | struct io_submit_state { |
| 582 | struct blk_plug plug; |
| 583 | |
| 584 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 585 | * io_kiocb alloc cache |
| 586 | */ |
| 587 | void *reqs[IO_IOPOLL_BATCH]; |
| 588 | unsigned int free_reqs; |
| 589 | unsigned int cur_req; |
| 590 | |
| 591 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 592 | * File reference cache |
| 593 | */ |
| 594 | struct file *file; |
| 595 | unsigned int fd; |
| 596 | unsigned int has_refs; |
| 597 | unsigned int used_refs; |
| 598 | unsigned int ios_left; |
| 599 | }; |
| 600 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 601 | struct io_op_def { |
| 602 | /* needs req->io allocated for deferral/async */ |
| 603 | unsigned async_ctx : 1; |
| 604 | /* needs current->mm setup, does mm access */ |
| 605 | unsigned needs_mm : 1; |
| 606 | /* needs req->file assigned */ |
| 607 | unsigned needs_file : 1; |
| 608 | /* needs req->file assigned IFF fd is >= 0 */ |
| 609 | unsigned fd_non_neg : 1; |
| 610 | /* hash wq insertion if file is a regular file */ |
| 611 | unsigned hash_reg_file : 1; |
| 612 | /* unbound wq insertion if file is a non-regular file */ |
| 613 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 614 | /* opcode is not supported by this kernel */ |
| 615 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 616 | /* needs file table */ |
| 617 | unsigned file_table : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 621 | [IORING_OP_NOP] = {}, |
| 622 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 623 | .async_ctx = 1, |
| 624 | .needs_mm = 1, |
| 625 | .needs_file = 1, |
| 626 | .unbound_nonreg_file = 1, |
| 627 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 628 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 629 | .async_ctx = 1, |
| 630 | .needs_mm = 1, |
| 631 | .needs_file = 1, |
| 632 | .hash_reg_file = 1, |
| 633 | .unbound_nonreg_file = 1, |
| 634 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 635 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 636 | .needs_file = 1, |
| 637 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 638 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 639 | .needs_file = 1, |
| 640 | .unbound_nonreg_file = 1, |
| 641 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 642 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 643 | .needs_file = 1, |
| 644 | .hash_reg_file = 1, |
| 645 | .unbound_nonreg_file = 1, |
| 646 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 647 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 648 | .needs_file = 1, |
| 649 | .unbound_nonreg_file = 1, |
| 650 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 651 | [IORING_OP_POLL_REMOVE] = {}, |
| 652 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 653 | .needs_file = 1, |
| 654 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 655 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 656 | .async_ctx = 1, |
| 657 | .needs_mm = 1, |
| 658 | .needs_file = 1, |
| 659 | .unbound_nonreg_file = 1, |
| 660 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 661 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 662 | .async_ctx = 1, |
| 663 | .needs_mm = 1, |
| 664 | .needs_file = 1, |
| 665 | .unbound_nonreg_file = 1, |
| 666 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 667 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 668 | .async_ctx = 1, |
| 669 | .needs_mm = 1, |
| 670 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 671 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 672 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 673 | .needs_mm = 1, |
| 674 | .needs_file = 1, |
| 675 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 676 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 677 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 678 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 679 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 680 | .async_ctx = 1, |
| 681 | .needs_mm = 1, |
| 682 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 683 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 684 | .async_ctx = 1, |
| 685 | .needs_mm = 1, |
| 686 | .needs_file = 1, |
| 687 | .unbound_nonreg_file = 1, |
| 688 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 689 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 690 | .needs_file = 1, |
| 691 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 692 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 693 | .needs_file = 1, |
| 694 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 695 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 696 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 697 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 698 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 699 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 700 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 701 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 702 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 703 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 704 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 705 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 706 | .needs_mm = 1, |
| 707 | .needs_file = 1, |
| 708 | .fd_non_neg = 1, |
| 709 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 710 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 711 | .needs_mm = 1, |
| 712 | .needs_file = 1, |
| 713 | .unbound_nonreg_file = 1, |
| 714 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 715 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 716 | .needs_mm = 1, |
| 717 | .needs_file = 1, |
| 718 | .unbound_nonreg_file = 1, |
| 719 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 720 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 721 | .needs_file = 1, |
| 722 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 723 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 724 | .needs_mm = 1, |
| 725 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 726 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 727 | .needs_mm = 1, |
| 728 | .needs_file = 1, |
| 729 | .unbound_nonreg_file = 1, |
| 730 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 731 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 732 | .needs_mm = 1, |
| 733 | .needs_file = 1, |
| 734 | .unbound_nonreg_file = 1, |
| 735 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 736 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 737 | .needs_file = 1, |
| 738 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 739 | .file_table = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 740 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 741 | [IORING_OP_EPOLL_CTL] = { |
| 742 | .unbound_nonreg_file = 1, |
| 743 | .file_table = 1, |
| 744 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 745 | }; |
| 746 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 747 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 748 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 749 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 750 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 751 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 752 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 753 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 754 | struct io_uring_files_update *ip, |
| 755 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 756 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 757 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 758 | static struct kmem_cache *req_cachep; |
| 759 | |
| 760 | static const struct file_operations io_uring_fops; |
| 761 | |
| 762 | struct sock *io_uring_get_socket(struct file *file) |
| 763 | { |
| 764 | #if defined(CONFIG_UNIX) |
| 765 | if (file->f_op == &io_uring_fops) { |
| 766 | struct io_ring_ctx *ctx = file->private_data; |
| 767 | |
| 768 | return ctx->ring_sock->sk; |
| 769 | } |
| 770 | #endif |
| 771 | return NULL; |
| 772 | } |
| 773 | EXPORT_SYMBOL(io_uring_get_socket); |
| 774 | |
| 775 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 776 | { |
| 777 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 778 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 779 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 783 | { |
| 784 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 785 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 786 | |
| 787 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 788 | if (!ctx) |
| 789 | return NULL; |
| 790 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 791 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 792 | if (!ctx->fallback_req) |
| 793 | goto err; |
| 794 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 795 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 796 | if (!ctx->completions) |
| 797 | goto err; |
| 798 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 799 | /* |
| 800 | * Use 5 bits less than the max cq entries, that should give us around |
| 801 | * 32 entries per hash list if totally full and uniformly spread. |
| 802 | */ |
| 803 | hash_bits = ilog2(p->cq_entries); |
| 804 | hash_bits -= 5; |
| 805 | if (hash_bits <= 0) |
| 806 | hash_bits = 1; |
| 807 | ctx->cancel_hash_bits = hash_bits; |
| 808 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 809 | GFP_KERNEL); |
| 810 | if (!ctx->cancel_hash) |
| 811 | goto err; |
| 812 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 813 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 814 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 815 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 816 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 817 | |
| 818 | ctx->flags = p->flags; |
| 819 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 820 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 821 | init_completion(&ctx->completions[0]); |
| 822 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 823 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 824 | mutex_init(&ctx->uring_lock); |
| 825 | init_waitqueue_head(&ctx->wait); |
| 826 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 827 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 828 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 829 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 830 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 831 | init_waitqueue_head(&ctx->inflight_wait); |
| 832 | spin_lock_init(&ctx->inflight_lock); |
| 833 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 834 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 835 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 836 | if (ctx->fallback_req) |
| 837 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 838 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 839 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 840 | kfree(ctx); |
| 841 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 844 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 845 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 846 | struct io_ring_ctx *ctx = req->ctx; |
| 847 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 848 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 849 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 850 | } |
| 851 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 852 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 853 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 854 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 855 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 856 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 857 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | 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] | 861 | { |
| 862 | struct io_kiocb *req; |
| 863 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 864 | 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] | 865 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 866 | list_del_init(&req->list); |
| 867 | return req; |
| 868 | } |
| 869 | |
| 870 | return NULL; |
| 871 | } |
| 872 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 873 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 874 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 875 | struct io_kiocb *req; |
| 876 | |
| 877 | 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] | 878 | if (req) { |
| 879 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 880 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 881 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 882 | list_del_init(&req->list); |
| 883 | return req; |
| 884 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 888 | } |
| 889 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 890 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 891 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 892 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 893 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 894 | /* order cqe stores with ring update */ |
| 895 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 896 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 897 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 898 | wake_up_interruptible(&ctx->cq_wait); |
| 899 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 903 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 904 | const struct io_op_def *def) |
| 905 | { |
| 906 | if (!req->work.mm && def->needs_mm) { |
| 907 | mmgrab(current->mm); |
| 908 | req->work.mm = current->mm; |
| 909 | } |
| 910 | if (!req->work.creds) |
| 911 | req->work.creds = get_current_cred(); |
| 912 | } |
| 913 | |
| 914 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 915 | { |
| 916 | if (req->work.mm) { |
| 917 | mmdrop(req->work.mm); |
| 918 | req->work.mm = NULL; |
| 919 | } |
| 920 | if (req->work.creds) { |
| 921 | put_cred(req->work.creds); |
| 922 | req->work.creds = NULL; |
| 923 | } |
| 924 | } |
| 925 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 926 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 927 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 928 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 929 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 930 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 931 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 932 | if (req->flags & REQ_F_ISREG) { |
| 933 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 934 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 935 | } else { |
| 936 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 937 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 938 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 939 | |
| 940 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 941 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 942 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 943 | return do_hashed; |
| 944 | } |
| 945 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 946 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 947 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 948 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 949 | struct io_kiocb *link; |
| 950 | bool do_hashed; |
| 951 | |
| 952 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 953 | |
| 954 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 955 | req->flags); |
| 956 | if (!do_hashed) { |
| 957 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 958 | } else { |
| 959 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 960 | file_inode(req->file)); |
| 961 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 962 | |
| 963 | if (link) |
| 964 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 965 | } |
| 966 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 967 | static void io_kill_timeout(struct io_kiocb *req) |
| 968 | { |
| 969 | int ret; |
| 970 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 971 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 972 | if (ret != -1) { |
| 973 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 974 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 975 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 976 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 981 | { |
| 982 | struct io_kiocb *req, *tmp; |
| 983 | |
| 984 | spin_lock_irq(&ctx->completion_lock); |
| 985 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 986 | io_kill_timeout(req); |
| 987 | spin_unlock_irq(&ctx->completion_lock); |
| 988 | } |
| 989 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 990 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 991 | { |
| 992 | struct io_kiocb *req; |
| 993 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 994 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 995 | io_kill_timeout(req); |
| 996 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 997 | __io_commit_cqring(ctx); |
| 998 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 999 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1000 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1001 | } |
| 1002 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1003 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1004 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1005 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1006 | unsigned tail; |
| 1007 | |
| 1008 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * writes to the cq entry need to come after reading head; the |
| 1011 | * control dependency is enough as we're using WRITE_ONCE to |
| 1012 | * fill the cq entry |
| 1013 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1014 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1015 | return NULL; |
| 1016 | |
| 1017 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1018 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1021 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1022 | { |
| 1023 | if (!ctx->eventfd_async) |
| 1024 | return true; |
| 1025 | return io_wq_current_is_worker() || in_interrupt(); |
| 1026 | } |
| 1027 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1028 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1029 | { |
| 1030 | if (waitqueue_active(&ctx->wait)) |
| 1031 | wake_up(&ctx->wait); |
| 1032 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1033 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1034 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1035 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1036 | } |
| 1037 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1038 | /* Returns true if there are no backlogged entries after the flush */ |
| 1039 | 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] | 1040 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1041 | struct io_rings *rings = ctx->rings; |
| 1042 | struct io_uring_cqe *cqe; |
| 1043 | struct io_kiocb *req; |
| 1044 | unsigned long flags; |
| 1045 | LIST_HEAD(list); |
| 1046 | |
| 1047 | if (!force) { |
| 1048 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1049 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1050 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1051 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1052 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1056 | |
| 1057 | /* if force is set, the ring is going away. always drop after that */ |
| 1058 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1059 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1060 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1061 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1062 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1063 | cqe = io_get_cqring(ctx); |
| 1064 | if (!cqe && !force) |
| 1065 | break; |
| 1066 | |
| 1067 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1068 | list); |
| 1069 | list_move(&req->list, &list); |
| 1070 | if (cqe) { |
| 1071 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1072 | WRITE_ONCE(cqe->res, req->result); |
| 1073 | WRITE_ONCE(cqe->flags, 0); |
| 1074 | } else { |
| 1075 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1076 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1081 | if (cqe) { |
| 1082 | clear_bit(0, &ctx->sq_check_overflow); |
| 1083 | clear_bit(0, &ctx->cq_check_overflow); |
| 1084 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1085 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1086 | io_cqring_ev_posted(ctx); |
| 1087 | |
| 1088 | while (!list_empty(&list)) { |
| 1089 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1090 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1091 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1092 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1093 | |
| 1094 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1097 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1098 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1099 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1100 | struct io_uring_cqe *cqe; |
| 1101 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1102 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1103 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1104 | /* |
| 1105 | * If we can't get a cq entry, userspace overflowed the |
| 1106 | * submission (by quite a lot). Increment the overflow count in |
| 1107 | * the ring. |
| 1108 | */ |
| 1109 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1110 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1111 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1112 | WRITE_ONCE(cqe->res, res); |
| 1113 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1114 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1115 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1116 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1117 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1118 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1119 | set_bit(0, &ctx->sq_check_overflow); |
| 1120 | set_bit(0, &ctx->cq_check_overflow); |
| 1121 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1122 | refcount_inc(&req->refs); |
| 1123 | req->result = res; |
| 1124 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1128 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1129 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1130 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1131 | unsigned long flags; |
| 1132 | |
| 1133 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1134 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1135 | io_commit_cqring(ctx); |
| 1136 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1137 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1138 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1141 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1142 | { |
| 1143 | return req == (struct io_kiocb *) |
| 1144 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1145 | } |
| 1146 | |
| 1147 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1148 | { |
| 1149 | struct io_kiocb *req; |
| 1150 | |
| 1151 | req = ctx->fallback_req; |
| 1152 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1153 | return req; |
| 1154 | |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1158 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1159 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1160 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1161 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1162 | struct io_kiocb *req; |
| 1163 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1164 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1165 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1166 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1167 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1168 | } else if (!state->free_reqs) { |
| 1169 | size_t sz; |
| 1170 | int ret; |
| 1171 | |
| 1172 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1173 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1174 | |
| 1175 | /* |
| 1176 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1177 | * retry single alloc to be on the safe side. |
| 1178 | */ |
| 1179 | if (unlikely(ret <= 0)) { |
| 1180 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1181 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1182 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1183 | ret = 1; |
| 1184 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1185 | state->free_reqs = ret - 1; |
| 1186 | state->cur_req = 1; |
| 1187 | req = state->reqs[0]; |
| 1188 | } else { |
| 1189 | req = state->reqs[state->cur_req]; |
| 1190 | state->free_reqs--; |
| 1191 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1194 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1195 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1196 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1197 | req->ctx = ctx; |
| 1198 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1199 | /* one is dropped after submission, the other at completion */ |
| 1200 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1201 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1202 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1203 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1204 | fallback: |
| 1205 | req = io_get_fallback_req(ctx); |
| 1206 | if (req) |
| 1207 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1208 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1209 | return NULL; |
| 1210 | } |
| 1211 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1212 | static void __io_req_do_free(struct io_kiocb *req) |
| 1213 | { |
| 1214 | if (likely(!io_is_fallback_req(req))) |
| 1215 | kmem_cache_free(req_cachep, req); |
| 1216 | else |
| 1217 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1218 | } |
| 1219 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1220 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1221 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1222 | struct io_ring_ctx *ctx = req->ctx; |
| 1223 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1224 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1225 | if (req->file) { |
| 1226 | if (req->flags & REQ_F_FIXED_FILE) |
| 1227 | percpu_ref_put(&ctx->file_data->refs); |
| 1228 | else |
| 1229 | fput(req->file); |
| 1230 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1231 | |
| 1232 | io_req_work_drop_env(req); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static void __io_free_req(struct io_kiocb *req) |
| 1236 | { |
| 1237 | __io_req_aux_free(req); |
| 1238 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1239 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1240 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1241 | unsigned long flags; |
| 1242 | |
| 1243 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1244 | list_del(&req->inflight_entry); |
| 1245 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1246 | wake_up(&ctx->inflight_wait); |
| 1247 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1248 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1249 | |
| 1250 | percpu_ref_put(&req->ctx->refs); |
| 1251 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1252 | } |
| 1253 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1254 | struct req_batch { |
| 1255 | void *reqs[IO_IOPOLL_BATCH]; |
| 1256 | int to_free; |
| 1257 | int need_iter; |
| 1258 | }; |
| 1259 | |
| 1260 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1261 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1262 | int fixed_refs = rb->to_free; |
| 1263 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1264 | if (!rb->to_free) |
| 1265 | return; |
| 1266 | if (rb->need_iter) { |
| 1267 | int i, inflight = 0; |
| 1268 | unsigned long flags; |
| 1269 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1270 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1271 | for (i = 0; i < rb->to_free; i++) { |
| 1272 | struct io_kiocb *req = rb->reqs[i]; |
| 1273 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1274 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1275 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1276 | fixed_refs++; |
| 1277 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1278 | if (req->flags & REQ_F_INFLIGHT) |
| 1279 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1280 | __io_req_aux_free(req); |
| 1281 | } |
| 1282 | if (!inflight) |
| 1283 | goto do_free; |
| 1284 | |
| 1285 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1286 | for (i = 0; i < rb->to_free; i++) { |
| 1287 | struct io_kiocb *req = rb->reqs[i]; |
| 1288 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1289 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1290 | list_del(&req->inflight_entry); |
| 1291 | if (!--inflight) |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1296 | |
| 1297 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1298 | wake_up(&ctx->inflight_wait); |
| 1299 | } |
| 1300 | do_free: |
| 1301 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1302 | if (fixed_refs) |
| 1303 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1304 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1305 | rb->to_free = rb->need_iter = 0; |
| 1306 | } |
| 1307 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1308 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1309 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1310 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1311 | int ret; |
| 1312 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1313 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1314 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1315 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1316 | io_commit_cqring(ctx); |
| 1317 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1318 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1319 | return true; |
| 1320 | } |
| 1321 | |
| 1322 | return false; |
| 1323 | } |
| 1324 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1325 | 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] | 1326 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1327 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1328 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1329 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1330 | /* Already got next link */ |
| 1331 | if (req->flags & REQ_F_LINK_NEXT) |
| 1332 | return; |
| 1333 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1334 | /* |
| 1335 | * The list should never be empty when we are called here. But could |
| 1336 | * potentially happen if the chain is messed up, check to be on the |
| 1337 | * safe side. |
| 1338 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1339 | while (!list_empty(&req->link_list)) { |
| 1340 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1341 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1342 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1343 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1344 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1345 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1346 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1347 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1348 | continue; |
| 1349 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1350 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1351 | list_del_init(&req->link_list); |
| 1352 | if (!list_empty(&nxt->link_list)) |
| 1353 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1354 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1355 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1356 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1357 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1358 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1359 | if (wake_ev) |
| 1360 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1365 | */ |
| 1366 | static void io_fail_links(struct io_kiocb *req) |
| 1367 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1368 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1369 | unsigned long flags; |
| 1370 | |
| 1371 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1372 | |
| 1373 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1374 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1375 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1376 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1377 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1378 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1379 | |
| 1380 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1381 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1382 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1383 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1384 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1385 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1386 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1387 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1388 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1389 | |
| 1390 | io_commit_cqring(ctx); |
| 1391 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1392 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1393 | } |
| 1394 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1395 | 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] | 1396 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1397 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1398 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1399 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1400 | /* |
| 1401 | * If LINK is set, we have dependent requests in this chain. If we |
| 1402 | * didn't fail this request, queue the first one up, moving any other |
| 1403 | * dependencies to the next request. In case of failure, fail the rest |
| 1404 | * of the chain. |
| 1405 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1406 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1407 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1408 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1409 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1410 | struct io_ring_ctx *ctx = req->ctx; |
| 1411 | unsigned long flags; |
| 1412 | |
| 1413 | /* |
| 1414 | * If this is a timeout link, we could be racing with the |
| 1415 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1416 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1417 | */ |
| 1418 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1419 | io_req_link_next(req, nxt); |
| 1420 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1421 | } else { |
| 1422 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1423 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1424 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1425 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1426 | static void io_free_req(struct io_kiocb *req) |
| 1427 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1428 | struct io_kiocb *nxt = NULL; |
| 1429 | |
| 1430 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1431 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1432 | |
| 1433 | if (nxt) |
| 1434 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1435 | } |
| 1436 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1437 | /* |
| 1438 | * Drop reference to request, return next in chain (if there is one) if this |
| 1439 | * was the last reference to this request. |
| 1440 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1441 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1442 | 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] | 1443 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1444 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1445 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1446 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1447 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1450 | static void io_put_req(struct io_kiocb *req) |
| 1451 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1452 | if (refcount_dec_and_test(&req->refs)) |
| 1453 | io_free_req(req); |
| 1454 | } |
| 1455 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1456 | /* |
| 1457 | * Must only be used if we don't need to care about links, usually from |
| 1458 | * within the completion handling itself. |
| 1459 | */ |
| 1460 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1461 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1462 | /* drop both submit and complete references */ |
| 1463 | if (refcount_sub_and_test(2, &req->refs)) |
| 1464 | __io_free_req(req); |
| 1465 | } |
| 1466 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1467 | static void io_double_put_req(struct io_kiocb *req) |
| 1468 | { |
| 1469 | /* drop both submit and complete references */ |
| 1470 | if (refcount_sub_and_test(2, &req->refs)) |
| 1471 | io_free_req(req); |
| 1472 | } |
| 1473 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1474 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1475 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1476 | struct io_rings *rings = ctx->rings; |
| 1477 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1478 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1479 | /* |
| 1480 | * noflush == true is from the waitqueue handler, just ensure |
| 1481 | * we wake up the task, and the next invocation will flush the |
| 1482 | * entries. We cannot safely to it from here. |
| 1483 | */ |
| 1484 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1485 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1486 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1487 | io_cqring_overflow_flush(ctx, false); |
| 1488 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1489 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1490 | /* See comment at the top of this file */ |
| 1491 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1492 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1493 | } |
| 1494 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1495 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1496 | { |
| 1497 | struct io_rings *rings = ctx->rings; |
| 1498 | |
| 1499 | /* make sure SQ entry isn't read before tail */ |
| 1500 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1501 | } |
| 1502 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1503 | 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] | 1504 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1505 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1506 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1507 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1508 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1509 | rb->need_iter++; |
| 1510 | |
| 1511 | rb->reqs[rb->to_free++] = req; |
| 1512 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1513 | io_free_req_many(req->ctx, rb); |
| 1514 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1517 | /* |
| 1518 | * Find and free completed poll iocbs |
| 1519 | */ |
| 1520 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1521 | struct list_head *done) |
| 1522 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1523 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1524 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1525 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1526 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1527 | while (!list_empty(done)) { |
| 1528 | req = list_first_entry(done, struct io_kiocb, list); |
| 1529 | list_del(&req->list); |
| 1530 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1531 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1532 | (*nr_events)++; |
| 1533 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1534 | if (refcount_dec_and_test(&req->refs) && |
| 1535 | !io_req_multi_free(&rb, req)) |
| 1536 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1537 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1538 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1539 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1540 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1544 | long min) |
| 1545 | { |
| 1546 | struct io_kiocb *req, *tmp; |
| 1547 | LIST_HEAD(done); |
| 1548 | bool spin; |
| 1549 | int ret; |
| 1550 | |
| 1551 | /* |
| 1552 | * Only spin for completions if we don't have multiple devices hanging |
| 1553 | * off our complete list, and we're under the requested amount. |
| 1554 | */ |
| 1555 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1556 | |
| 1557 | ret = 0; |
| 1558 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1559 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1560 | |
| 1561 | /* |
| 1562 | * Move completed entries to our local list. If we find a |
| 1563 | * request that requires polling, break out and complete |
| 1564 | * the done list first, if we have entries there. |
| 1565 | */ |
| 1566 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1567 | list_move_tail(&req->list, &done); |
| 1568 | continue; |
| 1569 | } |
| 1570 | if (!list_empty(&done)) |
| 1571 | break; |
| 1572 | |
| 1573 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1574 | if (ret < 0) |
| 1575 | break; |
| 1576 | |
| 1577 | if (ret && spin) |
| 1578 | spin = false; |
| 1579 | ret = 0; |
| 1580 | } |
| 1581 | |
| 1582 | if (!list_empty(&done)) |
| 1583 | io_iopoll_complete(ctx, nr_events, &done); |
| 1584 | |
| 1585 | return ret; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1589 | * 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] | 1590 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1591 | * as a non-spinning completion check. |
| 1592 | */ |
| 1593 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1594 | long min) |
| 1595 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1596 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1597 | int ret; |
| 1598 | |
| 1599 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1600 | if (ret < 0) |
| 1601 | return ret; |
| 1602 | if (!min || *nr_events >= min) |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | return 1; |
| 1607 | } |
| 1608 | |
| 1609 | /* |
| 1610 | * We can't just wait for polled events to come to us, we have to actively |
| 1611 | * find and complete them. |
| 1612 | */ |
| 1613 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1614 | { |
| 1615 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1616 | return; |
| 1617 | |
| 1618 | mutex_lock(&ctx->uring_lock); |
| 1619 | while (!list_empty(&ctx->poll_list)) { |
| 1620 | unsigned int nr_events = 0; |
| 1621 | |
| 1622 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1623 | |
| 1624 | /* |
| 1625 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1626 | * in this case we need to ensure that we reap all events. |
| 1627 | */ |
| 1628 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1629 | } |
| 1630 | mutex_unlock(&ctx->uring_lock); |
| 1631 | } |
| 1632 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1633 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1634 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1635 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1636 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1637 | |
| 1638 | do { |
| 1639 | int tmin = 0; |
| 1640 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1641 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1642 | * Don't enter poll loop if we already have events pending. |
| 1643 | * If we do, we can potentially be spinning for commands that |
| 1644 | * already triggered a CQE (eg in error). |
| 1645 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1646 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1647 | break; |
| 1648 | |
| 1649 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1650 | * If a submit got punted to a workqueue, we can have the |
| 1651 | * application entering polling for a command before it gets |
| 1652 | * issued. That app will hold the uring_lock for the duration |
| 1653 | * of the poll right here, so we need to take a breather every |
| 1654 | * now and then to ensure that the issue has a chance to add |
| 1655 | * the poll to the issued list. Otherwise we can spin here |
| 1656 | * forever, while the workqueue is stuck trying to acquire the |
| 1657 | * very same mutex. |
| 1658 | */ |
| 1659 | if (!(++iters & 7)) { |
| 1660 | mutex_unlock(&ctx->uring_lock); |
| 1661 | mutex_lock(&ctx->uring_lock); |
| 1662 | } |
| 1663 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1664 | if (*nr_events < min) |
| 1665 | tmin = min - *nr_events; |
| 1666 | |
| 1667 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1668 | if (ret <= 0) |
| 1669 | break; |
| 1670 | ret = 0; |
| 1671 | } while (min && !*nr_events && !need_resched()); |
| 1672 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1673 | return ret; |
| 1674 | } |
| 1675 | |
| 1676 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1677 | long min) |
| 1678 | { |
| 1679 | int ret; |
| 1680 | |
| 1681 | /* |
| 1682 | * We disallow the app entering submit/complete with polling, but we |
| 1683 | * still need to lock the ring to prevent racing with polled issue |
| 1684 | * that got punted to a workqueue. |
| 1685 | */ |
| 1686 | mutex_lock(&ctx->uring_lock); |
| 1687 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1688 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1689 | return ret; |
| 1690 | } |
| 1691 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1692 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1693 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1694 | /* |
| 1695 | * Tell lockdep we inherited freeze protection from submission |
| 1696 | * thread. |
| 1697 | */ |
| 1698 | if (req->flags & REQ_F_ISREG) { |
| 1699 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1700 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1701 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1702 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1703 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1706 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1707 | { |
| 1708 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1709 | req->flags |= REQ_F_FAIL_LINK; |
| 1710 | } |
| 1711 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1712 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1713 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1714 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1715 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1716 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1717 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1718 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1719 | if (res != req->result) |
| 1720 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1721 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1725 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1726 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1727 | |
| 1728 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1729 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1732 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1733 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1734 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1735 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1736 | |
| 1737 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1738 | io_put_req_find_next(req, &nxt); |
| 1739 | |
| 1740 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1743 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1744 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1745 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1746 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1747 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1748 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1749 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1750 | if (res != req->result) |
| 1751 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1752 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1753 | if (res != -EAGAIN) |
| 1754 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1759 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1760 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1761 | * accessing the kiocb cookie. |
| 1762 | */ |
| 1763 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1764 | { |
| 1765 | struct io_ring_ctx *ctx = req->ctx; |
| 1766 | |
| 1767 | /* |
| 1768 | * Track whether we have multiple files in our lists. This will impact |
| 1769 | * how we do polling eventually, not spinning if we're on potentially |
| 1770 | * different devices. |
| 1771 | */ |
| 1772 | if (list_empty(&ctx->poll_list)) { |
| 1773 | ctx->poll_multi_file = false; |
| 1774 | } else if (!ctx->poll_multi_file) { |
| 1775 | struct io_kiocb *list_req; |
| 1776 | |
| 1777 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1778 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1779 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1780 | ctx->poll_multi_file = true; |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * For fast devices, IO may have already completed. If it has, add |
| 1785 | * it to the front so we find it first. |
| 1786 | */ |
| 1787 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1788 | list_add(&req->list, &ctx->poll_list); |
| 1789 | else |
| 1790 | list_add_tail(&req->list, &ctx->poll_list); |
| 1791 | } |
| 1792 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1793 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1794 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1795 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1796 | int diff = state->has_refs - state->used_refs; |
| 1797 | |
| 1798 | if (diff) |
| 1799 | fput_many(state->file, diff); |
| 1800 | state->file = NULL; |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * Get as many references to a file as we have IOs left in this submission, |
| 1806 | * assuming most submissions are for one file, or at least that each file |
| 1807 | * has more than one submission. |
| 1808 | */ |
| 1809 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1810 | { |
| 1811 | if (!state) |
| 1812 | return fget(fd); |
| 1813 | |
| 1814 | if (state->file) { |
| 1815 | if (state->fd == fd) { |
| 1816 | state->used_refs++; |
| 1817 | state->ios_left--; |
| 1818 | return state->file; |
| 1819 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1820 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1821 | } |
| 1822 | state->file = fget_many(fd, state->ios_left); |
| 1823 | if (!state->file) |
| 1824 | return NULL; |
| 1825 | |
| 1826 | state->fd = fd; |
| 1827 | state->has_refs = state->ios_left; |
| 1828 | state->used_refs = 1; |
| 1829 | state->ios_left--; |
| 1830 | return state->file; |
| 1831 | } |
| 1832 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1833 | /* |
| 1834 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1835 | * any file. For now, just ensure that anything potentially problematic is done |
| 1836 | * inline. |
| 1837 | */ |
| 1838 | static bool io_file_supports_async(struct file *file) |
| 1839 | { |
| 1840 | umode_t mode = file_inode(file)->i_mode; |
| 1841 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1842 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1843 | return true; |
| 1844 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1845 | return true; |
| 1846 | |
| 1847 | return false; |
| 1848 | } |
| 1849 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1850 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1851 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1852 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1853 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1854 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1855 | unsigned ioprio; |
| 1856 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1857 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1858 | if (!req->file) |
| 1859 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1860 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1861 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1862 | req->flags |= REQ_F_ISREG; |
| 1863 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1864 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1865 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1866 | req->flags |= REQ_F_CUR_POS; |
| 1867 | kiocb->ki_pos = req->file->f_pos; |
| 1868 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1869 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1870 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1871 | |
| 1872 | ioprio = READ_ONCE(sqe->ioprio); |
| 1873 | if (ioprio) { |
| 1874 | ret = ioprio_check_cap(ioprio); |
| 1875 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1876 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1877 | |
| 1878 | kiocb->ki_ioprio = ioprio; |
| 1879 | } else |
| 1880 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1881 | |
| 1882 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1883 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1884 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1885 | |
| 1886 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1887 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1888 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1889 | req->flags |= REQ_F_NOWAIT; |
| 1890 | |
| 1891 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1892 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1893 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1894 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1895 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1896 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1897 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1898 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1899 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1900 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1901 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1902 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1903 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1904 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1905 | kiocb->ki_complete = io_complete_rw; |
| 1906 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1907 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1908 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1909 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1910 | /* we own ->private, reuse it for the buffer index */ |
| 1911 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1912 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1913 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1917 | { |
| 1918 | switch (ret) { |
| 1919 | case -EIOCBQUEUED: |
| 1920 | break; |
| 1921 | case -ERESTARTSYS: |
| 1922 | case -ERESTARTNOINTR: |
| 1923 | case -ERESTARTNOHAND: |
| 1924 | case -ERESTART_RESTARTBLOCK: |
| 1925 | /* |
| 1926 | * We can't just restart the syscall, since previously |
| 1927 | * submitted sqes may already be in progress. Just fail this |
| 1928 | * IO with EINTR. |
| 1929 | */ |
| 1930 | ret = -EINTR; |
| 1931 | /* fall through */ |
| 1932 | default: |
| 1933 | kiocb->ki_complete(kiocb, ret, 0); |
| 1934 | } |
| 1935 | } |
| 1936 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1937 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1938 | bool in_async) |
| 1939 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1940 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1941 | |
| 1942 | if (req->flags & REQ_F_CUR_POS) |
| 1943 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1944 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1945 | *nxt = __io_complete_rw(kiocb, ret); |
| 1946 | else |
| 1947 | io_rw_done(kiocb, ret); |
| 1948 | } |
| 1949 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1950 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1951 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1952 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1953 | struct io_ring_ctx *ctx = req->ctx; |
| 1954 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1955 | struct io_mapped_ubuf *imu; |
| 1956 | unsigned index, buf_index; |
| 1957 | size_t offset; |
| 1958 | u64 buf_addr; |
| 1959 | |
| 1960 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1961 | if (unlikely(!ctx->user_bufs)) |
| 1962 | return -EFAULT; |
| 1963 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1964 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1965 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1966 | return -EFAULT; |
| 1967 | |
| 1968 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1969 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1970 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1971 | |
| 1972 | /* overflow */ |
| 1973 | if (buf_addr + len < buf_addr) |
| 1974 | return -EFAULT; |
| 1975 | /* not inside the mapped region */ |
| 1976 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1977 | return -EFAULT; |
| 1978 | |
| 1979 | /* |
| 1980 | * May not be a start of buffer, set size appropriately |
| 1981 | * and advance us to the beginning. |
| 1982 | */ |
| 1983 | offset = buf_addr - imu->ubuf; |
| 1984 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1985 | |
| 1986 | if (offset) { |
| 1987 | /* |
| 1988 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1989 | * using the latter parts of a big fixed buffer - it iterates |
| 1990 | * over each segment manually. We can cheat a bit here, because |
| 1991 | * we know that: |
| 1992 | * |
| 1993 | * 1) it's a BVEC iter, we set it up |
| 1994 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1995 | * first and last bvec |
| 1996 | * |
| 1997 | * So just find our index, and adjust the iterator afterwards. |
| 1998 | * If the offset is within the first bvec (or the whole first |
| 1999 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2000 | * since we can just skip the first segment, which may not |
| 2001 | * be PAGE_SIZE aligned. |
| 2002 | */ |
| 2003 | const struct bio_vec *bvec = imu->bvec; |
| 2004 | |
| 2005 | if (offset <= bvec->bv_len) { |
| 2006 | iov_iter_advance(iter, offset); |
| 2007 | } else { |
| 2008 | unsigned long seg_skip; |
| 2009 | |
| 2010 | /* skip first vec */ |
| 2011 | offset -= bvec->bv_len; |
| 2012 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2013 | |
| 2014 | iter->bvec = bvec + seg_skip; |
| 2015 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2016 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2017 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2021 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2024 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2025 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2026 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2027 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2028 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2029 | u8 opcode; |
| 2030 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2031 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2032 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2033 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2034 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2035 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2036 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2037 | /* buffer index only valid with fixed read/write */ |
| 2038 | if (req->rw.kiocb.private) |
| 2039 | return -EINVAL; |
| 2040 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2041 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2042 | ssize_t ret; |
| 2043 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2044 | *iovec = NULL; |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2048 | if (req->io) { |
| 2049 | struct io_async_rw *iorw = &req->io->rw; |
| 2050 | |
| 2051 | *iovec = iorw->iov; |
| 2052 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2053 | if (iorw->iov == iorw->fast_iov) |
| 2054 | *iovec = NULL; |
| 2055 | return iorw->size; |
| 2056 | } |
| 2057 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2058 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2059 | return -EFAULT; |
| 2060 | |
| 2061 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2062 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2063 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2064 | iovec, iter); |
| 2065 | #endif |
| 2066 | |
| 2067 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2068 | } |
| 2069 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2070 | /* |
| 2071 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2072 | * by looping over ->read() or ->write() manually. |
| 2073 | */ |
| 2074 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2075 | struct iov_iter *iter) |
| 2076 | { |
| 2077 | ssize_t ret = 0; |
| 2078 | |
| 2079 | /* |
| 2080 | * Don't support polled IO through this interface, and we can't |
| 2081 | * support non-blocking either. For the latter, this just causes |
| 2082 | * the kiocb to be handled from an async context. |
| 2083 | */ |
| 2084 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2085 | return -EOPNOTSUPP; |
| 2086 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2087 | return -EAGAIN; |
| 2088 | |
| 2089 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2090 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2091 | ssize_t nr; |
| 2092 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2093 | if (!iov_iter_is_bvec(iter)) { |
| 2094 | iovec = iov_iter_iovec(iter); |
| 2095 | } else { |
| 2096 | /* fixed buffers import bvec */ |
| 2097 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2098 | + iter->iov_offset; |
| 2099 | iovec.iov_len = min(iter->count, |
| 2100 | iter->bvec->bv_len - iter->iov_offset); |
| 2101 | } |
| 2102 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2103 | if (rw == READ) { |
| 2104 | nr = file->f_op->read(file, iovec.iov_base, |
| 2105 | iovec.iov_len, &kiocb->ki_pos); |
| 2106 | } else { |
| 2107 | nr = file->f_op->write(file, iovec.iov_base, |
| 2108 | iovec.iov_len, &kiocb->ki_pos); |
| 2109 | } |
| 2110 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2111 | if (iov_iter_is_bvec(iter)) |
| 2112 | kunmap(iter->bvec->bv_page); |
| 2113 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2114 | if (nr < 0) { |
| 2115 | if (!ret) |
| 2116 | ret = nr; |
| 2117 | break; |
| 2118 | } |
| 2119 | ret += nr; |
| 2120 | if (nr != iovec.iov_len) |
| 2121 | break; |
| 2122 | iov_iter_advance(iter, nr); |
| 2123 | } |
| 2124 | |
| 2125 | return ret; |
| 2126 | } |
| 2127 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2128 | 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] | 2129 | struct iovec *iovec, struct iovec *fast_iov, |
| 2130 | struct iov_iter *iter) |
| 2131 | { |
| 2132 | req->io->rw.nr_segs = iter->nr_segs; |
| 2133 | req->io->rw.size = io_size; |
| 2134 | req->io->rw.iov = iovec; |
| 2135 | if (!req->io->rw.iov) { |
| 2136 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2137 | memcpy(req->io->rw.iov, fast_iov, |
| 2138 | sizeof(struct iovec) * iter->nr_segs); |
| 2139 | } |
| 2140 | } |
| 2141 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2142 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2143 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2144 | if (!io_op_defs[req->opcode].async_ctx) |
| 2145 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2146 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2147 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | static void io_rw_async(struct io_wq_work **workptr) |
| 2151 | { |
| 2152 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2153 | struct iovec *iov = NULL; |
| 2154 | |
| 2155 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2156 | iov = req->io->rw.iov; |
| 2157 | io_wq_submit_work(workptr); |
| 2158 | kfree(iov); |
| 2159 | } |
| 2160 | |
| 2161 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2162 | struct iovec *iovec, struct iovec *fast_iov, |
| 2163 | struct iov_iter *iter) |
| 2164 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2165 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2166 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2167 | if (!req->io && io_alloc_async_ctx(req)) |
| 2168 | return -ENOMEM; |
| 2169 | |
| 2170 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2171 | req->work.func = io_rw_async; |
| 2172 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2175 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2176 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2177 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2178 | struct io_async_ctx *io; |
| 2179 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2180 | ssize_t ret; |
| 2181 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2182 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2183 | if (ret) |
| 2184 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2185 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2186 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2187 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2188 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2189 | if (!req->io) |
| 2190 | return 0; |
| 2191 | |
| 2192 | io = req->io; |
| 2193 | io->rw.iov = io->rw.fast_iov; |
| 2194 | req->io = NULL; |
| 2195 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2196 | req->io = io; |
| 2197 | if (ret < 0) |
| 2198 | return ret; |
| 2199 | |
| 2200 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2201 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2204 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2205 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2206 | { |
| 2207 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2208 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2209 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2210 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2211 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2212 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2213 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2214 | if (ret < 0) |
| 2215 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2216 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2217 | /* Ensure we clear previously set non-block flag */ |
| 2218 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2219 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2220 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2221 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2222 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2223 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2224 | req->result = io_size; |
| 2225 | |
| 2226 | /* |
| 2227 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2228 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2229 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2230 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2231 | req->flags |= REQ_F_MUST_PUNT; |
| 2232 | goto copy_iov; |
| 2233 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2234 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2235 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2236 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2237 | if (!ret) { |
| 2238 | ssize_t ret2; |
| 2239 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2240 | if (req->file->f_op->read_iter) |
| 2241 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2242 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2243 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2244 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2245 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2246 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2247 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2248 | } else { |
| 2249 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2250 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2251 | inline_vecs, &iter); |
| 2252 | if (ret) |
| 2253 | goto out_free; |
| 2254 | return -EAGAIN; |
| 2255 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2256 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2257 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2258 | if (!io_wq_current_is_worker()) |
| 2259 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2260 | return ret; |
| 2261 | } |
| 2262 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2263 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2264 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2265 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2266 | struct io_async_ctx *io; |
| 2267 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2268 | ssize_t ret; |
| 2269 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2270 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2271 | if (ret) |
| 2272 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2273 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2274 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2275 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2276 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2277 | if (!req->io) |
| 2278 | return 0; |
| 2279 | |
| 2280 | io = req->io; |
| 2281 | io->rw.iov = io->rw.fast_iov; |
| 2282 | req->io = NULL; |
| 2283 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2284 | req->io = io; |
| 2285 | if (ret < 0) |
| 2286 | return ret; |
| 2287 | |
| 2288 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2289 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2292 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2293 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2294 | { |
| 2295 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2296 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2297 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2298 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2299 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2300 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2301 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2302 | if (ret < 0) |
| 2303 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2304 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2305 | /* Ensure we clear previously set non-block flag */ |
| 2306 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2307 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2308 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2309 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2310 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2311 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2312 | req->result = io_size; |
| 2313 | |
| 2314 | /* |
| 2315 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2316 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2317 | */ |
| 2318 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2319 | req->flags |= REQ_F_MUST_PUNT; |
| 2320 | goto copy_iov; |
| 2321 | } |
| 2322 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2323 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2324 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2325 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2326 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2327 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2328 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2329 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2330 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2331 | ssize_t ret2; |
| 2332 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2333 | /* |
| 2334 | * Open-code file_start_write here to grab freeze protection, |
| 2335 | * which will be released by another thread in |
| 2336 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2337 | * released so that it doesn't complain about the held lock when |
| 2338 | * we return to userspace. |
| 2339 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2340 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2341 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2342 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2343 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2344 | SB_FREEZE_WRITE); |
| 2345 | } |
| 2346 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2347 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2348 | if (req->file->f_op->write_iter) |
| 2349 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2350 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2351 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2352 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2353 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2354 | } else { |
| 2355 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2356 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2357 | inline_vecs, &iter); |
| 2358 | if (ret) |
| 2359 | goto out_free; |
| 2360 | return -EAGAIN; |
| 2361 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2362 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2363 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2364 | if (!io_wq_current_is_worker()) |
| 2365 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2366 | return ret; |
| 2367 | } |
| 2368 | |
| 2369 | /* |
| 2370 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2371 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2372 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2373 | { |
| 2374 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2375 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2376 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2377 | return -EINVAL; |
| 2378 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2379 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2380 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2381 | return 0; |
| 2382 | } |
| 2383 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2384 | 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] | 2385 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2386 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2387 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2388 | if (!req->file) |
| 2389 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2390 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2391 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2392 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2393 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2394 | return -EINVAL; |
| 2395 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2396 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2397 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2398 | return -EINVAL; |
| 2399 | |
| 2400 | req->sync.off = READ_ONCE(sqe->off); |
| 2401 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2402 | return 0; |
| 2403 | } |
| 2404 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2405 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2406 | { |
| 2407 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2408 | req_set_fail_links(req); |
| 2409 | io_cqring_add_event(req, -ECANCELED); |
| 2410 | io_put_req(req); |
| 2411 | return true; |
| 2412 | } |
| 2413 | |
| 2414 | return false; |
| 2415 | } |
| 2416 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2417 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2418 | { |
| 2419 | struct io_wq_work *work = *workptr; |
| 2420 | struct io_kiocb *link = work->data; |
| 2421 | |
| 2422 | io_queue_linked_timeout(link); |
| 2423 | work->func = io_wq_submit_work; |
| 2424 | } |
| 2425 | |
| 2426 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2427 | { |
| 2428 | struct io_kiocb *link; |
| 2429 | |
| 2430 | io_prep_async_work(nxt, &link); |
| 2431 | *workptr = &nxt->work; |
| 2432 | if (link) { |
| 2433 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2434 | nxt->work.func = io_link_work_cb; |
| 2435 | nxt->work.data = link; |
| 2436 | } |
| 2437 | } |
| 2438 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2439 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2440 | { |
| 2441 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2442 | loff_t end = req->sync.off + req->sync.len; |
| 2443 | struct io_kiocb *nxt = NULL; |
| 2444 | int ret; |
| 2445 | |
| 2446 | if (io_req_cancelled(req)) |
| 2447 | return; |
| 2448 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2449 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2450 | end > 0 ? end : LLONG_MAX, |
| 2451 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2452 | if (ret < 0) |
| 2453 | req_set_fail_links(req); |
| 2454 | io_cqring_add_event(req, ret); |
| 2455 | io_put_req_find_next(req, &nxt); |
| 2456 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2457 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2460 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2461 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2462 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2463 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2464 | |
| 2465 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2466 | if (force_nonblock) { |
| 2467 | io_put_req(req); |
| 2468 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2469 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2470 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2471 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2472 | work = old_work = &req->work; |
| 2473 | io_fsync_finish(&work); |
| 2474 | if (work && work != old_work) |
| 2475 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2476 | return 0; |
| 2477 | } |
| 2478 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2479 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2480 | { |
| 2481 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2482 | struct io_kiocb *nxt = NULL; |
| 2483 | int ret; |
| 2484 | |
| 2485 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2486 | req->sync.len); |
| 2487 | if (ret < 0) |
| 2488 | req_set_fail_links(req); |
| 2489 | io_cqring_add_event(req, ret); |
| 2490 | io_put_req_find_next(req, &nxt); |
| 2491 | if (nxt) |
| 2492 | io_wq_assign_next(workptr, nxt); |
| 2493 | } |
| 2494 | |
| 2495 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2496 | const struct io_uring_sqe *sqe) |
| 2497 | { |
| 2498 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2499 | return -EINVAL; |
| 2500 | |
| 2501 | req->sync.off = READ_ONCE(sqe->off); |
| 2502 | req->sync.len = READ_ONCE(sqe->addr); |
| 2503 | req->sync.mode = READ_ONCE(sqe->len); |
| 2504 | return 0; |
| 2505 | } |
| 2506 | |
| 2507 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2508 | bool force_nonblock) |
| 2509 | { |
| 2510 | struct io_wq_work *work, *old_work; |
| 2511 | |
| 2512 | /* fallocate always requiring blocking context */ |
| 2513 | if (force_nonblock) { |
| 2514 | io_put_req(req); |
| 2515 | req->work.func = io_fallocate_finish; |
| 2516 | return -EAGAIN; |
| 2517 | } |
| 2518 | |
| 2519 | work = old_work = &req->work; |
| 2520 | io_fallocate_finish(&work); |
| 2521 | if (work && work != old_work) |
| 2522 | *nxt = container_of(work, struct io_kiocb, work); |
| 2523 | |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2527 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2528 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2529 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2530 | int ret; |
| 2531 | |
| 2532 | if (sqe->ioprio || sqe->buf_index) |
| 2533 | return -EINVAL; |
| 2534 | |
| 2535 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2536 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2537 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2538 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2539 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2540 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2541 | if (IS_ERR(req->open.filename)) { |
| 2542 | ret = PTR_ERR(req->open.filename); |
| 2543 | req->open.filename = NULL; |
| 2544 | return ret; |
| 2545 | } |
| 2546 | |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2550 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2551 | { |
| 2552 | struct open_how __user *how; |
| 2553 | const char __user *fname; |
| 2554 | size_t len; |
| 2555 | int ret; |
| 2556 | |
| 2557 | if (sqe->ioprio || sqe->buf_index) |
| 2558 | return -EINVAL; |
| 2559 | |
| 2560 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2561 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2562 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2563 | len = READ_ONCE(sqe->len); |
| 2564 | |
| 2565 | if (len < OPEN_HOW_SIZE_VER0) |
| 2566 | return -EINVAL; |
| 2567 | |
| 2568 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2569 | len); |
| 2570 | if (ret) |
| 2571 | return ret; |
| 2572 | |
| 2573 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2574 | req->open.how.flags |= O_LARGEFILE; |
| 2575 | |
| 2576 | req->open.filename = getname(fname); |
| 2577 | if (IS_ERR(req->open.filename)) { |
| 2578 | ret = PTR_ERR(req->open.filename); |
| 2579 | req->open.filename = NULL; |
| 2580 | return ret; |
| 2581 | } |
| 2582 | |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
| 2586 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2587 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2588 | { |
| 2589 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2590 | struct file *file; |
| 2591 | int ret; |
| 2592 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2593 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2594 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2595 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2596 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2597 | if (ret) |
| 2598 | goto err; |
| 2599 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2600 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2601 | if (ret < 0) |
| 2602 | goto err; |
| 2603 | |
| 2604 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2605 | if (IS_ERR(file)) { |
| 2606 | put_unused_fd(ret); |
| 2607 | ret = PTR_ERR(file); |
| 2608 | } else { |
| 2609 | fsnotify_open(file); |
| 2610 | fd_install(ret, file); |
| 2611 | } |
| 2612 | err: |
| 2613 | putname(req->open.filename); |
| 2614 | if (ret < 0) |
| 2615 | req_set_fail_links(req); |
| 2616 | io_cqring_add_event(req, ret); |
| 2617 | io_put_req_find_next(req, nxt); |
| 2618 | return 0; |
| 2619 | } |
| 2620 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2621 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2622 | bool force_nonblock) |
| 2623 | { |
| 2624 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2625 | return io_openat2(req, nxt, force_nonblock); |
| 2626 | } |
| 2627 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 2628 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 2629 | const struct io_uring_sqe *sqe) |
| 2630 | { |
| 2631 | #if defined(CONFIG_EPOLL) |
| 2632 | if (sqe->ioprio || sqe->buf_index) |
| 2633 | return -EINVAL; |
| 2634 | |
| 2635 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 2636 | req->epoll.op = READ_ONCE(sqe->len); |
| 2637 | req->epoll.fd = READ_ONCE(sqe->off); |
| 2638 | |
| 2639 | if (ep_op_has_event(req->epoll.op)) { |
| 2640 | struct epoll_event __user *ev; |
| 2641 | |
| 2642 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2643 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 2644 | return -EFAULT; |
| 2645 | } |
| 2646 | |
| 2647 | return 0; |
| 2648 | #else |
| 2649 | return -EOPNOTSUPP; |
| 2650 | #endif |
| 2651 | } |
| 2652 | |
| 2653 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2654 | bool force_nonblock) |
| 2655 | { |
| 2656 | #if defined(CONFIG_EPOLL) |
| 2657 | struct io_epoll *ie = &req->epoll; |
| 2658 | int ret; |
| 2659 | |
| 2660 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 2661 | if (force_nonblock && ret == -EAGAIN) |
| 2662 | return -EAGAIN; |
| 2663 | |
| 2664 | if (ret < 0) |
| 2665 | req_set_fail_links(req); |
| 2666 | io_cqring_add_event(req, ret); |
| 2667 | io_put_req_find_next(req, nxt); |
| 2668 | return 0; |
| 2669 | #else |
| 2670 | return -EOPNOTSUPP; |
| 2671 | #endif |
| 2672 | } |
| 2673 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2674 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2675 | { |
| 2676 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2677 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2678 | return -EINVAL; |
| 2679 | |
| 2680 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2681 | req->madvise.len = READ_ONCE(sqe->len); |
| 2682 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2683 | return 0; |
| 2684 | #else |
| 2685 | return -EOPNOTSUPP; |
| 2686 | #endif |
| 2687 | } |
| 2688 | |
| 2689 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2690 | bool force_nonblock) |
| 2691 | { |
| 2692 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2693 | struct io_madvise *ma = &req->madvise; |
| 2694 | int ret; |
| 2695 | |
| 2696 | if (force_nonblock) |
| 2697 | return -EAGAIN; |
| 2698 | |
| 2699 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2700 | if (ret < 0) |
| 2701 | req_set_fail_links(req); |
| 2702 | io_cqring_add_event(req, ret); |
| 2703 | io_put_req_find_next(req, nxt); |
| 2704 | return 0; |
| 2705 | #else |
| 2706 | return -EOPNOTSUPP; |
| 2707 | #endif |
| 2708 | } |
| 2709 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2710 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2711 | { |
| 2712 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2713 | return -EINVAL; |
| 2714 | |
| 2715 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2716 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2717 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2718 | return 0; |
| 2719 | } |
| 2720 | |
| 2721 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2722 | bool force_nonblock) |
| 2723 | { |
| 2724 | struct io_fadvise *fa = &req->fadvise; |
| 2725 | int ret; |
| 2726 | |
| 2727 | /* DONTNEED may block, others _should_ not */ |
| 2728 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2729 | return -EAGAIN; |
| 2730 | |
| 2731 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2732 | if (ret < 0) |
| 2733 | req_set_fail_links(req); |
| 2734 | io_cqring_add_event(req, ret); |
| 2735 | io_put_req_find_next(req, nxt); |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2739 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2740 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2741 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2742 | unsigned lookup_flags; |
| 2743 | int ret; |
| 2744 | |
| 2745 | if (sqe->ioprio || sqe->buf_index) |
| 2746 | return -EINVAL; |
| 2747 | |
| 2748 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2749 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2750 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2751 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2752 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2753 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2754 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2755 | return -EINVAL; |
| 2756 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2757 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2758 | if (IS_ERR(req->open.filename)) { |
| 2759 | ret = PTR_ERR(req->open.filename); |
| 2760 | req->open.filename = NULL; |
| 2761 | return ret; |
| 2762 | } |
| 2763 | |
| 2764 | return 0; |
| 2765 | } |
| 2766 | |
| 2767 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2768 | bool force_nonblock) |
| 2769 | { |
| 2770 | struct io_open *ctx = &req->open; |
| 2771 | unsigned lookup_flags; |
| 2772 | struct path path; |
| 2773 | struct kstat stat; |
| 2774 | int ret; |
| 2775 | |
| 2776 | if (force_nonblock) |
| 2777 | return -EAGAIN; |
| 2778 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2779 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2780 | return -EINVAL; |
| 2781 | |
| 2782 | retry: |
| 2783 | /* filename_lookup() drops it, keep a reference */ |
| 2784 | ctx->filename->refcnt++; |
| 2785 | |
| 2786 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2787 | NULL); |
| 2788 | if (ret) |
| 2789 | goto err; |
| 2790 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2791 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2792 | path_put(&path); |
| 2793 | if (retry_estale(ret, lookup_flags)) { |
| 2794 | lookup_flags |= LOOKUP_REVAL; |
| 2795 | goto retry; |
| 2796 | } |
| 2797 | if (!ret) |
| 2798 | ret = cp_statx(&stat, ctx->buffer); |
| 2799 | err: |
| 2800 | putname(ctx->filename); |
| 2801 | if (ret < 0) |
| 2802 | req_set_fail_links(req); |
| 2803 | io_cqring_add_event(req, ret); |
| 2804 | io_put_req_find_next(req, nxt); |
| 2805 | return 0; |
| 2806 | } |
| 2807 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2808 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2809 | { |
| 2810 | /* |
| 2811 | * If we queue this for async, it must not be cancellable. That would |
| 2812 | * leave the 'file' in an undeterminate state. |
| 2813 | */ |
| 2814 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2815 | |
| 2816 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2817 | sqe->rw_flags || sqe->buf_index) |
| 2818 | return -EINVAL; |
| 2819 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2820 | return -EINVAL; |
| 2821 | |
| 2822 | req->close.fd = READ_ONCE(sqe->fd); |
| 2823 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2824 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2825 | return -EBADF; |
| 2826 | |
| 2827 | return 0; |
| 2828 | } |
| 2829 | |
| 2830 | static void io_close_finish(struct io_wq_work **workptr) |
| 2831 | { |
| 2832 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2833 | struct io_kiocb *nxt = NULL; |
| 2834 | |
| 2835 | /* Invoked with files, we need to do the close */ |
| 2836 | if (req->work.files) { |
| 2837 | int ret; |
| 2838 | |
| 2839 | ret = filp_close(req->close.put_file, req->work.files); |
| 2840 | if (ret < 0) { |
| 2841 | req_set_fail_links(req); |
| 2842 | } |
| 2843 | io_cqring_add_event(req, ret); |
| 2844 | } |
| 2845 | |
| 2846 | fput(req->close.put_file); |
| 2847 | |
| 2848 | /* we bypassed the re-issue, drop the submission reference */ |
| 2849 | io_put_req(req); |
| 2850 | io_put_req_find_next(req, &nxt); |
| 2851 | if (nxt) |
| 2852 | io_wq_assign_next(workptr, nxt); |
| 2853 | } |
| 2854 | |
| 2855 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2856 | bool force_nonblock) |
| 2857 | { |
| 2858 | int ret; |
| 2859 | |
| 2860 | req->close.put_file = NULL; |
| 2861 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2862 | if (ret < 0) |
| 2863 | return ret; |
| 2864 | |
| 2865 | /* if the file has a flush method, be safe and punt to async */ |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2866 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2867 | goto eagain; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2868 | |
| 2869 | /* |
| 2870 | * No ->flush(), safely close from here and just punt the |
| 2871 | * fput() to async context. |
| 2872 | */ |
| 2873 | ret = filp_close(req->close.put_file, current->files); |
| 2874 | |
| 2875 | if (ret < 0) |
| 2876 | req_set_fail_links(req); |
| 2877 | io_cqring_add_event(req, ret); |
| 2878 | |
| 2879 | if (io_wq_current_is_worker()) { |
| 2880 | struct io_wq_work *old_work, *work; |
| 2881 | |
| 2882 | old_work = work = &req->work; |
| 2883 | io_close_finish(&work); |
| 2884 | if (work && work != old_work) |
| 2885 | *nxt = container_of(work, struct io_kiocb, work); |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | eagain: |
| 2890 | req->work.func = io_close_finish; |
| 2891 | return -EAGAIN; |
| 2892 | } |
| 2893 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2894 | 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] | 2895 | { |
| 2896 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2897 | |
| 2898 | if (!req->file) |
| 2899 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2900 | |
| 2901 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2902 | return -EINVAL; |
| 2903 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2904 | return -EINVAL; |
| 2905 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2906 | req->sync.off = READ_ONCE(sqe->off); |
| 2907 | req->sync.len = READ_ONCE(sqe->len); |
| 2908 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2909 | return 0; |
| 2910 | } |
| 2911 | |
| 2912 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2913 | { |
| 2914 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2915 | struct io_kiocb *nxt = NULL; |
| 2916 | int ret; |
| 2917 | |
| 2918 | if (io_req_cancelled(req)) |
| 2919 | return; |
| 2920 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2921 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2922 | req->sync.flags); |
| 2923 | if (ret < 0) |
| 2924 | req_set_fail_links(req); |
| 2925 | io_cqring_add_event(req, ret); |
| 2926 | io_put_req_find_next(req, &nxt); |
| 2927 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2928 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2929 | } |
| 2930 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2931 | 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] | 2932 | bool force_nonblock) |
| 2933 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2934 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2935 | |
| 2936 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2937 | if (force_nonblock) { |
| 2938 | io_put_req(req); |
| 2939 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2940 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2941 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2942 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2943 | work = old_work = &req->work; |
| 2944 | io_sync_file_range_finish(&work); |
| 2945 | if (work && work != old_work) |
| 2946 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2947 | return 0; |
| 2948 | } |
| 2949 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2950 | #if defined(CONFIG_NET) |
| 2951 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2952 | { |
| 2953 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2954 | struct iovec *iov = NULL; |
| 2955 | |
| 2956 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2957 | iov = req->io->msg.iov; |
| 2958 | io_wq_submit_work(workptr); |
| 2959 | kfree(iov); |
| 2960 | } |
| 2961 | #endif |
| 2962 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2963 | 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] | 2964 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2965 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2966 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2967 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2968 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2969 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2970 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2971 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2972 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2973 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2974 | return 0; |
| 2975 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2976 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2977 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2978 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2979 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2980 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2981 | #endif |
| 2982 | } |
| 2983 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2984 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2985 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2986 | { |
| 2987 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2988 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2989 | struct socket *sock; |
| 2990 | int ret; |
| 2991 | |
| 2992 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2993 | return -EINVAL; |
| 2994 | |
| 2995 | sock = sock_from_file(req->file, &ret); |
| 2996 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2997 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2998 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2999 | unsigned flags; |
| 3000 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3001 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3002 | kmsg = &req->io->msg; |
| 3003 | kmsg->msg.msg_name = &addr; |
| 3004 | /* if iov is set, it's allocated already */ |
| 3005 | if (!kmsg->iov) |
| 3006 | kmsg->iov = kmsg->fast_iov; |
| 3007 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3008 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3009 | struct io_sr_msg *sr = &req->sr_msg; |
| 3010 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3011 | kmsg = &io.msg; |
| 3012 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3013 | |
| 3014 | io.msg.iov = io.msg.fast_iov; |
| 3015 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3016 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3017 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3018 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3019 | } |
| 3020 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3021 | flags = req->sr_msg.msg_flags; |
| 3022 | if (flags & MSG_DONTWAIT) |
| 3023 | req->flags |= REQ_F_NOWAIT; |
| 3024 | else if (force_nonblock) |
| 3025 | flags |= MSG_DONTWAIT; |
| 3026 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3027 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3028 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3029 | if (req->io) |
| 3030 | return -EAGAIN; |
| 3031 | if (io_alloc_async_ctx(req)) |
| 3032 | return -ENOMEM; |
| 3033 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3034 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3035 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3036 | } |
| 3037 | if (ret == -ERESTARTSYS) |
| 3038 | ret = -EINTR; |
| 3039 | } |
| 3040 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3041 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3042 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3043 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3044 | if (ret < 0) |
| 3045 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3046 | io_put_req_find_next(req, nxt); |
| 3047 | return 0; |
| 3048 | #else |
| 3049 | return -EOPNOTSUPP; |
| 3050 | #endif |
| 3051 | } |
| 3052 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3053 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3054 | bool force_nonblock) |
| 3055 | { |
| 3056 | #if defined(CONFIG_NET) |
| 3057 | struct socket *sock; |
| 3058 | int ret; |
| 3059 | |
| 3060 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3061 | return -EINVAL; |
| 3062 | |
| 3063 | sock = sock_from_file(req->file, &ret); |
| 3064 | if (sock) { |
| 3065 | struct io_sr_msg *sr = &req->sr_msg; |
| 3066 | struct msghdr msg; |
| 3067 | struct iovec iov; |
| 3068 | unsigned flags; |
| 3069 | |
| 3070 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3071 | &msg.msg_iter); |
| 3072 | if (ret) |
| 3073 | return ret; |
| 3074 | |
| 3075 | msg.msg_name = NULL; |
| 3076 | msg.msg_control = NULL; |
| 3077 | msg.msg_controllen = 0; |
| 3078 | msg.msg_namelen = 0; |
| 3079 | |
| 3080 | flags = req->sr_msg.msg_flags; |
| 3081 | if (flags & MSG_DONTWAIT) |
| 3082 | req->flags |= REQ_F_NOWAIT; |
| 3083 | else if (force_nonblock) |
| 3084 | flags |= MSG_DONTWAIT; |
| 3085 | |
| 3086 | ret = __sys_sendmsg_sock(sock, &msg, flags); |
| 3087 | if (force_nonblock && ret == -EAGAIN) |
| 3088 | return -EAGAIN; |
| 3089 | if (ret == -ERESTARTSYS) |
| 3090 | ret = -EINTR; |
| 3091 | } |
| 3092 | |
| 3093 | io_cqring_add_event(req, ret); |
| 3094 | if (ret < 0) |
| 3095 | req_set_fail_links(req); |
| 3096 | io_put_req_find_next(req, nxt); |
| 3097 | return 0; |
| 3098 | #else |
| 3099 | return -EOPNOTSUPP; |
| 3100 | #endif |
| 3101 | } |
| 3102 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3103 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3104 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3105 | { |
| 3106 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3107 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3108 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3109 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3110 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3111 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3112 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3113 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3114 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3115 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3116 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3117 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3118 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3119 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3120 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3121 | #endif |
| 3122 | } |
| 3123 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3124 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3125 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3126 | { |
| 3127 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3128 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3129 | struct socket *sock; |
| 3130 | int ret; |
| 3131 | |
| 3132 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3133 | return -EINVAL; |
| 3134 | |
| 3135 | sock = sock_from_file(req->file, &ret); |
| 3136 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3137 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3138 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3139 | unsigned flags; |
| 3140 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3141 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3142 | kmsg = &req->io->msg; |
| 3143 | kmsg->msg.msg_name = &addr; |
| 3144 | /* if iov is set, it's allocated already */ |
| 3145 | if (!kmsg->iov) |
| 3146 | kmsg->iov = kmsg->fast_iov; |
| 3147 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3148 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3149 | struct io_sr_msg *sr = &req->sr_msg; |
| 3150 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3151 | kmsg = &io.msg; |
| 3152 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3153 | |
| 3154 | io.msg.iov = io.msg.fast_iov; |
| 3155 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3156 | sr->msg_flags, &io.msg.uaddr, |
| 3157 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3158 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3159 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3160 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3161 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3162 | flags = req->sr_msg.msg_flags; |
| 3163 | if (flags & MSG_DONTWAIT) |
| 3164 | req->flags |= REQ_F_NOWAIT; |
| 3165 | else if (force_nonblock) |
| 3166 | flags |= MSG_DONTWAIT; |
| 3167 | |
| 3168 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3169 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3170 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3171 | if (req->io) |
| 3172 | return -EAGAIN; |
| 3173 | if (io_alloc_async_ctx(req)) |
| 3174 | return -ENOMEM; |
| 3175 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3176 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3177 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3178 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3179 | if (ret == -ERESTARTSYS) |
| 3180 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3181 | } |
| 3182 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3183 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3184 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3185 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3186 | if (ret < 0) |
| 3187 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3188 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3189 | return 0; |
| 3190 | #else |
| 3191 | return -EOPNOTSUPP; |
| 3192 | #endif |
| 3193 | } |
| 3194 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3195 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3196 | bool force_nonblock) |
| 3197 | { |
| 3198 | #if defined(CONFIG_NET) |
| 3199 | struct socket *sock; |
| 3200 | int ret; |
| 3201 | |
| 3202 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3203 | return -EINVAL; |
| 3204 | |
| 3205 | sock = sock_from_file(req->file, &ret); |
| 3206 | if (sock) { |
| 3207 | struct io_sr_msg *sr = &req->sr_msg; |
| 3208 | struct msghdr msg; |
| 3209 | struct iovec iov; |
| 3210 | unsigned flags; |
| 3211 | |
| 3212 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3213 | &msg.msg_iter); |
| 3214 | if (ret) |
| 3215 | return ret; |
| 3216 | |
| 3217 | msg.msg_name = NULL; |
| 3218 | msg.msg_control = NULL; |
| 3219 | msg.msg_controllen = 0; |
| 3220 | msg.msg_namelen = 0; |
| 3221 | msg.msg_iocb = NULL; |
| 3222 | msg.msg_flags = 0; |
| 3223 | |
| 3224 | flags = req->sr_msg.msg_flags; |
| 3225 | if (flags & MSG_DONTWAIT) |
| 3226 | req->flags |= REQ_F_NOWAIT; |
| 3227 | else if (force_nonblock) |
| 3228 | flags |= MSG_DONTWAIT; |
| 3229 | |
| 3230 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); |
| 3231 | if (force_nonblock && ret == -EAGAIN) |
| 3232 | return -EAGAIN; |
| 3233 | if (ret == -ERESTARTSYS) |
| 3234 | ret = -EINTR; |
| 3235 | } |
| 3236 | |
| 3237 | io_cqring_add_event(req, ret); |
| 3238 | if (ret < 0) |
| 3239 | req_set_fail_links(req); |
| 3240 | io_put_req_find_next(req, nxt); |
| 3241 | return 0; |
| 3242 | #else |
| 3243 | return -EOPNOTSUPP; |
| 3244 | #endif |
| 3245 | } |
| 3246 | |
| 3247 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3248 | 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] | 3249 | { |
| 3250 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3251 | struct io_accept *accept = &req->accept; |
| 3252 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3253 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3254 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3255 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3256 | return -EINVAL; |
| 3257 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3258 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3259 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3260 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3261 | return 0; |
| 3262 | #else |
| 3263 | return -EOPNOTSUPP; |
| 3264 | #endif |
| 3265 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3266 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3267 | #if defined(CONFIG_NET) |
| 3268 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3269 | bool force_nonblock) |
| 3270 | { |
| 3271 | struct io_accept *accept = &req->accept; |
| 3272 | unsigned file_flags; |
| 3273 | int ret; |
| 3274 | |
| 3275 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3276 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3277 | accept->addr_len, accept->flags); |
| 3278 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3279 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3280 | if (ret == -ERESTARTSYS) |
| 3281 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3282 | if (ret < 0) |
| 3283 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3284 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3285 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3286 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3290 | { |
| 3291 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3292 | struct io_kiocb *nxt = NULL; |
| 3293 | |
| 3294 | if (io_req_cancelled(req)) |
| 3295 | return; |
| 3296 | __io_accept(req, &nxt, false); |
| 3297 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3298 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3299 | } |
| 3300 | #endif |
| 3301 | |
| 3302 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3303 | bool force_nonblock) |
| 3304 | { |
| 3305 | #if defined(CONFIG_NET) |
| 3306 | int ret; |
| 3307 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3308 | ret = __io_accept(req, nxt, force_nonblock); |
| 3309 | if (ret == -EAGAIN && force_nonblock) { |
| 3310 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3311 | io_put_req(req); |
| 3312 | return -EAGAIN; |
| 3313 | } |
| 3314 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3315 | #else |
| 3316 | return -EOPNOTSUPP; |
| 3317 | #endif |
| 3318 | } |
| 3319 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3320 | 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] | 3321 | { |
| 3322 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3323 | struct io_connect *conn = &req->connect; |
| 3324 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3325 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3326 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3327 | return -EINVAL; |
| 3328 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3329 | return -EINVAL; |
| 3330 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3331 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3332 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3333 | |
| 3334 | if (!io) |
| 3335 | return 0; |
| 3336 | |
| 3337 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3338 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3339 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3340 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3341 | #endif |
| 3342 | } |
| 3343 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3344 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3345 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3346 | { |
| 3347 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3348 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3349 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3350 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3351 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3352 | if (req->io) { |
| 3353 | io = req->io; |
| 3354 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3355 | ret = move_addr_to_kernel(req->connect.addr, |
| 3356 | req->connect.addr_len, |
| 3357 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3358 | if (ret) |
| 3359 | goto out; |
| 3360 | io = &__io; |
| 3361 | } |
| 3362 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3363 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3364 | |
| 3365 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3366 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3367 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3368 | if (req->io) |
| 3369 | return -EAGAIN; |
| 3370 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3371 | ret = -ENOMEM; |
| 3372 | goto out; |
| 3373 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3374 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3375 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3376 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3377 | if (ret == -ERESTARTSYS) |
| 3378 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3379 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3380 | if (ret < 0) |
| 3381 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3382 | io_cqring_add_event(req, ret); |
| 3383 | io_put_req_find_next(req, nxt); |
| 3384 | return 0; |
| 3385 | #else |
| 3386 | return -EOPNOTSUPP; |
| 3387 | #endif |
| 3388 | } |
| 3389 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3390 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3391 | { |
| 3392 | struct io_poll_iocb *poll = &req->poll; |
| 3393 | |
| 3394 | spin_lock(&poll->head->lock); |
| 3395 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3396 | if (!list_empty(&poll->wait.entry)) { |
| 3397 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3398 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3399 | } |
| 3400 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3401 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3405 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3406 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3407 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3408 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3409 | |
| 3410 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3411 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3412 | struct hlist_head *list; |
| 3413 | |
| 3414 | list = &ctx->cancel_hash[i]; |
| 3415 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3416 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3417 | } |
| 3418 | spin_unlock_irq(&ctx->completion_lock); |
| 3419 | } |
| 3420 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3421 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3422 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3423 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3424 | struct io_kiocb *req; |
| 3425 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3426 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3427 | hlist_for_each_entry(req, list, hash_node) { |
| 3428 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3429 | io_poll_remove_one(req); |
| 3430 | return 0; |
| 3431 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | return -ENOENT; |
| 3435 | } |
| 3436 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3437 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3438 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3439 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3440 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3441 | return -EINVAL; |
| 3442 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3443 | sqe->poll_events) |
| 3444 | return -EINVAL; |
| 3445 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3446 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | /* |
| 3451 | * Find a running poll command that matches one specified in sqe->addr, |
| 3452 | * and remove it if found. |
| 3453 | */ |
| 3454 | static int io_poll_remove(struct io_kiocb *req) |
| 3455 | { |
| 3456 | struct io_ring_ctx *ctx = req->ctx; |
| 3457 | u64 addr; |
| 3458 | int ret; |
| 3459 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3460 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3461 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3462 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3463 | spin_unlock_irq(&ctx->completion_lock); |
| 3464 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3465 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3466 | if (ret < 0) |
| 3467 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3468 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3469 | return 0; |
| 3470 | } |
| 3471 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3472 | 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] | 3473 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3474 | struct io_ring_ctx *ctx = req->ctx; |
| 3475 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3476 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3477 | if (error) |
| 3478 | io_cqring_fill_event(req, error); |
| 3479 | else |
| 3480 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3481 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3484 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3485 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3486 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3487 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3488 | struct io_poll_iocb *poll = &req->poll; |
| 3489 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3490 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3491 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3492 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3493 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3494 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3495 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3496 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3497 | ret = -ECANCELED; |
| 3498 | } else if (READ_ONCE(poll->canceled)) { |
| 3499 | ret = -ECANCELED; |
| 3500 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3501 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3502 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3503 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3504 | |
| 3505 | /* |
| 3506 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3507 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3508 | * synchronize with them. In the cancellation case the list_del_init |
| 3509 | * itself is not actually needed, but harmless so we keep it in to |
| 3510 | * avoid further branches in the fast path. |
| 3511 | */ |
| 3512 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3513 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3514 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3515 | spin_unlock_irq(&ctx->completion_lock); |
| 3516 | return; |
| 3517 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3518 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3519 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3520 | spin_unlock_irq(&ctx->completion_lock); |
| 3521 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3522 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3523 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3524 | if (ret < 0) |
| 3525 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3526 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3527 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3528 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3529 | } |
| 3530 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3531 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3532 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3533 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3534 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3535 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3536 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3537 | spin_lock_irq(&ctx->completion_lock); |
| 3538 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3539 | hash_del(&req->hash_node); |
| 3540 | io_poll_complete(req, req->result, 0); |
| 3541 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3542 | if (refcount_dec_and_test(&req->refs) && |
| 3543 | !io_req_multi_free(&rb, req)) { |
| 3544 | req->flags |= REQ_F_COMP_LOCKED; |
| 3545 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | spin_unlock_irq(&ctx->completion_lock); |
| 3549 | |
| 3550 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3551 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3555 | { |
| 3556 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3557 | struct llist_node *nodes; |
| 3558 | |
| 3559 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3560 | if (nodes) |
| 3561 | __io_poll_flush(req->ctx, nodes); |
| 3562 | } |
| 3563 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3564 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3565 | void *key) |
| 3566 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3567 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3568 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3569 | struct io_ring_ctx *ctx = req->ctx; |
| 3570 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3571 | |
| 3572 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3573 | if (mask && !(mask & poll->events)) |
| 3574 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3575 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3576 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3577 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3578 | /* |
| 3579 | * Run completion inline if we can. We're using trylock here because |
| 3580 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3581 | * If we have a link timeout we're going to need the completion_lock |
| 3582 | * for finalizing the request, mark us as having grabbed that already. |
| 3583 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3584 | if (mask) { |
| 3585 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3586 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3587 | if (llist_empty(&ctx->poll_llist) && |
| 3588 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3589 | hash_del(&req->hash_node); |
| 3590 | io_poll_complete(req, mask, 0); |
| 3591 | req->flags |= REQ_F_COMP_LOCKED; |
| 3592 | io_put_req(req); |
| 3593 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3594 | |
| 3595 | io_cqring_ev_posted(ctx); |
| 3596 | req = NULL; |
| 3597 | } else { |
| 3598 | req->result = mask; |
| 3599 | req->llist_node.next = NULL; |
| 3600 | /* if the list wasn't empty, we're done */ |
| 3601 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3602 | req = NULL; |
| 3603 | else |
| 3604 | req->work.func = io_poll_flush; |
| 3605 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3606 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3607 | if (req) |
| 3608 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3609 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3610 | return 1; |
| 3611 | } |
| 3612 | |
| 3613 | struct io_poll_table { |
| 3614 | struct poll_table_struct pt; |
| 3615 | struct io_kiocb *req; |
| 3616 | int error; |
| 3617 | }; |
| 3618 | |
| 3619 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3620 | struct poll_table_struct *p) |
| 3621 | { |
| 3622 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3623 | |
| 3624 | if (unlikely(pt->req->poll.head)) { |
| 3625 | pt->error = -EINVAL; |
| 3626 | return; |
| 3627 | } |
| 3628 | |
| 3629 | pt->error = 0; |
| 3630 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3631 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3634 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3635 | { |
| 3636 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3637 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3638 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3639 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3640 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3641 | } |
| 3642 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3643 | 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] | 3644 | { |
| 3645 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3646 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3647 | |
| 3648 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3649 | return -EINVAL; |
| 3650 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3651 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3652 | if (!poll->file) |
| 3653 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3654 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3655 | events = READ_ONCE(sqe->poll_events); |
| 3656 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3657 | return 0; |
| 3658 | } |
| 3659 | |
| 3660 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3661 | { |
| 3662 | struct io_poll_iocb *poll = &req->poll; |
| 3663 | struct io_ring_ctx *ctx = req->ctx; |
| 3664 | struct io_poll_table ipt; |
| 3665 | bool cancel = false; |
| 3666 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3667 | |
| 3668 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3669 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3670 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3671 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3672 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3673 | poll->canceled = false; |
| 3674 | |
| 3675 | ipt.pt._qproc = io_poll_queue_proc; |
| 3676 | ipt.pt._key = poll->events; |
| 3677 | ipt.req = req; |
| 3678 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3679 | |
| 3680 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3681 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3682 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3683 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3684 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3685 | INIT_LIST_HEAD(&req->list); |
| 3686 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3687 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3688 | |
| 3689 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3690 | if (likely(poll->head)) { |
| 3691 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3692 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3693 | if (ipt.error) |
| 3694 | cancel = true; |
| 3695 | ipt.error = 0; |
| 3696 | mask = 0; |
| 3697 | } |
| 3698 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3699 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3700 | else if (cancel) |
| 3701 | WRITE_ONCE(poll->canceled, true); |
| 3702 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3703 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3704 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3705 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3706 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3707 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3708 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3709 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3710 | spin_unlock_irq(&ctx->completion_lock); |
| 3711 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3712 | if (mask) { |
| 3713 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3714 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3715 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3716 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3719 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3720 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3721 | struct io_timeout_data *data = container_of(timer, |
| 3722 | struct io_timeout_data, timer); |
| 3723 | struct io_kiocb *req = data->req; |
| 3724 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3725 | unsigned long flags; |
| 3726 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3727 | atomic_inc(&ctx->cq_timeouts); |
| 3728 | |
| 3729 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3730 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3731 | * We could be racing with timeout deletion. If the list is empty, |
| 3732 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3733 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3734 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3735 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3736 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3737 | /* |
| 3738 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3739 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3740 | * pointer will be increased, otherwise other timeout reqs may |
| 3741 | * return in advance without waiting for enough wait_nr. |
| 3742 | */ |
| 3743 | prev = req; |
| 3744 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3745 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3746 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3747 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3748 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3749 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3750 | io_commit_cqring(ctx); |
| 3751 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3752 | |
| 3753 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3754 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3755 | io_put_req(req); |
| 3756 | return HRTIMER_NORESTART; |
| 3757 | } |
| 3758 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3759 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3760 | { |
| 3761 | struct io_kiocb *req; |
| 3762 | int ret = -ENOENT; |
| 3763 | |
| 3764 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3765 | if (user_data == req->user_data) { |
| 3766 | list_del_init(&req->list); |
| 3767 | ret = 0; |
| 3768 | break; |
| 3769 | } |
| 3770 | } |
| 3771 | |
| 3772 | if (ret == -ENOENT) |
| 3773 | return ret; |
| 3774 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3775 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3776 | if (ret == -1) |
| 3777 | return -EALREADY; |
| 3778 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3779 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3780 | io_cqring_fill_event(req, -ECANCELED); |
| 3781 | io_put_req(req); |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3785 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3786 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3787 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3788 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3789 | return -EINVAL; |
| 3790 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3791 | return -EINVAL; |
| 3792 | |
| 3793 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3794 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3795 | if (req->timeout.flags) |
| 3796 | return -EINVAL; |
| 3797 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3798 | return 0; |
| 3799 | } |
| 3800 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3801 | /* |
| 3802 | * Remove or update an existing timeout command |
| 3803 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3804 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3805 | { |
| 3806 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3807 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3808 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3809 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3810 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3811 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3812 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3813 | io_commit_cqring(ctx); |
| 3814 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3815 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3816 | if (ret < 0) |
| 3817 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3818 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3819 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3820 | } |
| 3821 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3822 | 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] | 3823 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3824 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3825 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3826 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3827 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3828 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3829 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3830 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3831 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3832 | if (sqe->off && is_timeout_link) |
| 3833 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3834 | flags = READ_ONCE(sqe->timeout_flags); |
| 3835 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3836 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3837 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3838 | req->timeout.count = READ_ONCE(sqe->off); |
| 3839 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3840 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3841 | return -ENOMEM; |
| 3842 | |
| 3843 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3844 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3845 | req->flags |= REQ_F_TIMEOUT; |
| 3846 | |
| 3847 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3848 | return -EFAULT; |
| 3849 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3850 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3851 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3852 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3853 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3854 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3855 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3856 | return 0; |
| 3857 | } |
| 3858 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3859 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3860 | { |
| 3861 | unsigned count; |
| 3862 | struct io_ring_ctx *ctx = req->ctx; |
| 3863 | struct io_timeout_data *data; |
| 3864 | struct list_head *entry; |
| 3865 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3866 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3867 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3868 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3869 | /* |
| 3870 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3871 | * timeout event to be satisfied. If it isn't set, then this is |
| 3872 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3873 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3874 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3875 | if (!count) { |
| 3876 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3877 | spin_lock_irq(&ctx->completion_lock); |
| 3878 | entry = ctx->timeout_list.prev; |
| 3879 | goto add; |
| 3880 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3881 | |
| 3882 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3883 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3884 | |
| 3885 | /* |
| 3886 | * Insertion sort, ensuring the first entry in the list is always |
| 3887 | * the one we need first. |
| 3888 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3889 | spin_lock_irq(&ctx->completion_lock); |
| 3890 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3891 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3892 | unsigned nxt_sq_head; |
| 3893 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3894 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3895 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3896 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3897 | continue; |
| 3898 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3899 | /* |
| 3900 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3901 | * long to store it. |
| 3902 | */ |
| 3903 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3904 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3905 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3906 | |
| 3907 | /* |
| 3908 | * cached_sq_head may overflow, and it will never overflow twice |
| 3909 | * once there is some timeout req still be valid. |
| 3910 | */ |
| 3911 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3912 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3913 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3914 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3915 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3916 | |
| 3917 | /* |
| 3918 | * Sequence of reqs after the insert one and itself should |
| 3919 | * be adjusted because each timeout req consumes a slot. |
| 3920 | */ |
| 3921 | span++; |
| 3922 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3923 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3924 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3925 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3926 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3927 | data->timer.function = io_timeout_fn; |
| 3928 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3929 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3930 | return 0; |
| 3931 | } |
| 3932 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3933 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3934 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3935 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3936 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3937 | return req->user_data == (unsigned long) data; |
| 3938 | } |
| 3939 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3940 | 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] | 3941 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3942 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3943 | int ret = 0; |
| 3944 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3945 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3946 | switch (cancel_ret) { |
| 3947 | case IO_WQ_CANCEL_OK: |
| 3948 | ret = 0; |
| 3949 | break; |
| 3950 | case IO_WQ_CANCEL_RUNNING: |
| 3951 | ret = -EALREADY; |
| 3952 | break; |
| 3953 | case IO_WQ_CANCEL_NOTFOUND: |
| 3954 | ret = -ENOENT; |
| 3955 | break; |
| 3956 | } |
| 3957 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3958 | return ret; |
| 3959 | } |
| 3960 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3961 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3962 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3963 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3964 | { |
| 3965 | unsigned long flags; |
| 3966 | int ret; |
| 3967 | |
| 3968 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3969 | if (ret != -ENOENT) { |
| 3970 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3971 | goto done; |
| 3972 | } |
| 3973 | |
| 3974 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3975 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3976 | if (ret != -ENOENT) |
| 3977 | goto done; |
| 3978 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3979 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3980 | if (!ret) |
| 3981 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3982 | io_cqring_fill_event(req, ret); |
| 3983 | io_commit_cqring(ctx); |
| 3984 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3985 | io_cqring_ev_posted(ctx); |
| 3986 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3987 | if (ret < 0) |
| 3988 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3989 | io_put_req_find_next(req, nxt); |
| 3990 | } |
| 3991 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3992 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3993 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3994 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3995 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3996 | return -EINVAL; |
| 3997 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3998 | sqe->cancel_flags) |
| 3999 | return -EINVAL; |
| 4000 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4001 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
| 4005 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4006 | { |
| 4007 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4008 | |
| 4009 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4010 | return 0; |
| 4011 | } |
| 4012 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4013 | static int io_files_update_prep(struct io_kiocb *req, |
| 4014 | const struct io_uring_sqe *sqe) |
| 4015 | { |
| 4016 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4017 | return -EINVAL; |
| 4018 | |
| 4019 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4020 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4021 | if (!req->files_update.nr_args) |
| 4022 | return -EINVAL; |
| 4023 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4024 | return 0; |
| 4025 | } |
| 4026 | |
| 4027 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4028 | { |
| 4029 | struct io_ring_ctx *ctx = req->ctx; |
| 4030 | struct io_uring_files_update up; |
| 4031 | int ret; |
| 4032 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4033 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4034 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4035 | |
| 4036 | up.offset = req->files_update.offset; |
| 4037 | up.fds = req->files_update.arg; |
| 4038 | |
| 4039 | mutex_lock(&ctx->uring_lock); |
| 4040 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4041 | mutex_unlock(&ctx->uring_lock); |
| 4042 | |
| 4043 | if (ret < 0) |
| 4044 | req_set_fail_links(req); |
| 4045 | io_cqring_add_event(req, ret); |
| 4046 | io_put_req(req); |
| 4047 | return 0; |
| 4048 | } |
| 4049 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4050 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4051 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4052 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4053 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4054 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4055 | if (io_op_defs[req->opcode].file_table) { |
| 4056 | ret = io_grab_files(req); |
| 4057 | if (unlikely(ret)) |
| 4058 | return ret; |
| 4059 | } |
| 4060 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4061 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4062 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4063 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4064 | case IORING_OP_NOP: |
| 4065 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4066 | case IORING_OP_READV: |
| 4067 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4068 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4069 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4070 | break; |
| 4071 | case IORING_OP_WRITEV: |
| 4072 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4073 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4074 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4075 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4076 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4077 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4078 | break; |
| 4079 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4080 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4081 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4082 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4083 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4084 | break; |
| 4085 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4086 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4087 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4088 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4089 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4090 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4091 | break; |
| 4092 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4093 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4094 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4095 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4096 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4097 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4098 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4099 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4100 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4101 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4102 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4103 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4104 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4105 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4106 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4107 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4108 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4109 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4110 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4111 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4112 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4113 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4114 | case IORING_OP_FALLOCATE: |
| 4115 | ret = io_fallocate_prep(req, sqe); |
| 4116 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4117 | case IORING_OP_OPENAT: |
| 4118 | ret = io_openat_prep(req, sqe); |
| 4119 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4120 | case IORING_OP_CLOSE: |
| 4121 | ret = io_close_prep(req, sqe); |
| 4122 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4123 | case IORING_OP_FILES_UPDATE: |
| 4124 | ret = io_files_update_prep(req, sqe); |
| 4125 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4126 | case IORING_OP_STATX: |
| 4127 | ret = io_statx_prep(req, sqe); |
| 4128 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4129 | case IORING_OP_FADVISE: |
| 4130 | ret = io_fadvise_prep(req, sqe); |
| 4131 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4132 | case IORING_OP_MADVISE: |
| 4133 | ret = io_madvise_prep(req, sqe); |
| 4134 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4135 | case IORING_OP_OPENAT2: |
| 4136 | ret = io_openat2_prep(req, sqe); |
| 4137 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 4138 | case IORING_OP_EPOLL_CTL: |
| 4139 | ret = io_epoll_ctl_prep(req, sqe); |
| 4140 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4141 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4142 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4143 | req->opcode); |
| 4144 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4145 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4146 | } |
| 4147 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4148 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4149 | } |
| 4150 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4151 | 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] | 4152 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4153 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4154 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4155 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4156 | /* Still need defer if there is pending req in defer list. */ |
| 4157 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4158 | return 0; |
| 4159 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4160 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4161 | return -EAGAIN; |
| 4162 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4163 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4164 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4165 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4166 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4167 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4168 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4169 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4170 | return 0; |
| 4171 | } |
| 4172 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4173 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4174 | list_add_tail(&req->list, &ctx->defer_list); |
| 4175 | spin_unlock_irq(&ctx->completion_lock); |
| 4176 | return -EIOCBQUEUED; |
| 4177 | } |
| 4178 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4179 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4180 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4181 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4182 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4183 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4184 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4185 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4186 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4187 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4188 | break; |
| 4189 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4190 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4191 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4192 | if (sqe) { |
| 4193 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4194 | if (ret < 0) |
| 4195 | break; |
| 4196 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4197 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4198 | break; |
| 4199 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4200 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4201 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4202 | if (sqe) { |
| 4203 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4204 | if (ret < 0) |
| 4205 | break; |
| 4206 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4207 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4208 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4209 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4210 | if (sqe) { |
| 4211 | ret = io_prep_fsync(req, sqe); |
| 4212 | if (ret < 0) |
| 4213 | break; |
| 4214 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4215 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4216 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4217 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4218 | if (sqe) { |
| 4219 | ret = io_poll_add_prep(req, sqe); |
| 4220 | if (ret) |
| 4221 | break; |
| 4222 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4223 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4224 | break; |
| 4225 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4226 | if (sqe) { |
| 4227 | ret = io_poll_remove_prep(req, sqe); |
| 4228 | if (ret < 0) |
| 4229 | break; |
| 4230 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4231 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4232 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4233 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4234 | if (sqe) { |
| 4235 | ret = io_prep_sfr(req, sqe); |
| 4236 | if (ret < 0) |
| 4237 | break; |
| 4238 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4239 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4240 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4241 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4242 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4243 | if (sqe) { |
| 4244 | ret = io_sendmsg_prep(req, sqe); |
| 4245 | if (ret < 0) |
| 4246 | break; |
| 4247 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4248 | if (req->opcode == IORING_OP_SENDMSG) |
| 4249 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4250 | else |
| 4251 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4252 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4253 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4254 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4255 | if (sqe) { |
| 4256 | ret = io_recvmsg_prep(req, sqe); |
| 4257 | if (ret) |
| 4258 | break; |
| 4259 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4260 | if (req->opcode == IORING_OP_RECVMSG) |
| 4261 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4262 | else |
| 4263 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4264 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4265 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4266 | if (sqe) { |
| 4267 | ret = io_timeout_prep(req, sqe, false); |
| 4268 | if (ret) |
| 4269 | break; |
| 4270 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4271 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4272 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4273 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4274 | if (sqe) { |
| 4275 | ret = io_timeout_remove_prep(req, sqe); |
| 4276 | if (ret) |
| 4277 | break; |
| 4278 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4279 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4280 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4281 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4282 | if (sqe) { |
| 4283 | ret = io_accept_prep(req, sqe); |
| 4284 | if (ret) |
| 4285 | break; |
| 4286 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4287 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4288 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4289 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4290 | if (sqe) { |
| 4291 | ret = io_connect_prep(req, sqe); |
| 4292 | if (ret) |
| 4293 | break; |
| 4294 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4295 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4296 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4297 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4298 | if (sqe) { |
| 4299 | ret = io_async_cancel_prep(req, sqe); |
| 4300 | if (ret) |
| 4301 | break; |
| 4302 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4303 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4304 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4305 | case IORING_OP_FALLOCATE: |
| 4306 | if (sqe) { |
| 4307 | ret = io_fallocate_prep(req, sqe); |
| 4308 | if (ret) |
| 4309 | break; |
| 4310 | } |
| 4311 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4312 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4313 | case IORING_OP_OPENAT: |
| 4314 | if (sqe) { |
| 4315 | ret = io_openat_prep(req, sqe); |
| 4316 | if (ret) |
| 4317 | break; |
| 4318 | } |
| 4319 | ret = io_openat(req, nxt, force_nonblock); |
| 4320 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4321 | case IORING_OP_CLOSE: |
| 4322 | if (sqe) { |
| 4323 | ret = io_close_prep(req, sqe); |
| 4324 | if (ret) |
| 4325 | break; |
| 4326 | } |
| 4327 | ret = io_close(req, nxt, force_nonblock); |
| 4328 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4329 | case IORING_OP_FILES_UPDATE: |
| 4330 | if (sqe) { |
| 4331 | ret = io_files_update_prep(req, sqe); |
| 4332 | if (ret) |
| 4333 | break; |
| 4334 | } |
| 4335 | ret = io_files_update(req, force_nonblock); |
| 4336 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4337 | case IORING_OP_STATX: |
| 4338 | if (sqe) { |
| 4339 | ret = io_statx_prep(req, sqe); |
| 4340 | if (ret) |
| 4341 | break; |
| 4342 | } |
| 4343 | ret = io_statx(req, nxt, force_nonblock); |
| 4344 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4345 | case IORING_OP_FADVISE: |
| 4346 | if (sqe) { |
| 4347 | ret = io_fadvise_prep(req, sqe); |
| 4348 | if (ret) |
| 4349 | break; |
| 4350 | } |
| 4351 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4352 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4353 | case IORING_OP_MADVISE: |
| 4354 | if (sqe) { |
| 4355 | ret = io_madvise_prep(req, sqe); |
| 4356 | if (ret) |
| 4357 | break; |
| 4358 | } |
| 4359 | ret = io_madvise(req, nxt, force_nonblock); |
| 4360 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4361 | case IORING_OP_OPENAT2: |
| 4362 | if (sqe) { |
| 4363 | ret = io_openat2_prep(req, sqe); |
| 4364 | if (ret) |
| 4365 | break; |
| 4366 | } |
| 4367 | ret = io_openat2(req, nxt, force_nonblock); |
| 4368 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame^] | 4369 | case IORING_OP_EPOLL_CTL: |
| 4370 | if (sqe) { |
| 4371 | ret = io_epoll_ctl_prep(req, sqe); |
| 4372 | if (ret) |
| 4373 | break; |
| 4374 | } |
| 4375 | ret = io_epoll_ctl(req, nxt, force_nonblock); |
| 4376 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4377 | default: |
| 4378 | ret = -EINVAL; |
| 4379 | break; |
| 4380 | } |
| 4381 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4382 | if (ret) |
| 4383 | return ret; |
| 4384 | |
| 4385 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4386 | const bool in_async = io_wq_current_is_worker(); |
| 4387 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4388 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4389 | return -EAGAIN; |
| 4390 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4391 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4392 | if (in_async) |
| 4393 | mutex_lock(&ctx->uring_lock); |
| 4394 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4395 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4396 | |
| 4397 | if (in_async) |
| 4398 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4404 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4405 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4406 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4407 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4408 | struct io_kiocb *nxt = NULL; |
| 4409 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4410 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4411 | /* if NO_CANCEL is set, we must still run the work */ |
| 4412 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4413 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4414 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4415 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4416 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4417 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4418 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4419 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4420 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4421 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4422 | /* |
| 4423 | * We can get EAGAIN for polled IO even though we're |
| 4424 | * forcing a sync submission from here, since we can't |
| 4425 | * wait for request slots on the block side. |
| 4426 | */ |
| 4427 | if (ret != -EAGAIN) |
| 4428 | break; |
| 4429 | cond_resched(); |
| 4430 | } while (1); |
| 4431 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4432 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4433 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4434 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4435 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4436 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4437 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4438 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4439 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4440 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4441 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4442 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4443 | if (!ret && nxt) |
| 4444 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4445 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4446 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4447 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4448 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4449 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4450 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4451 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4452 | return 0; |
| 4453 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4454 | } |
| 4455 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4456 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4457 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4458 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4459 | struct fixed_file_table *table; |
| 4460 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4461 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4462 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4463 | } |
| 4464 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4465 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4466 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4467 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4468 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4469 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4470 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4471 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4472 | flags = READ_ONCE(sqe->flags); |
| 4473 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4474 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4475 | if (!io_req_needs_file(req, fd)) |
| 4476 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4477 | |
| 4478 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4479 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4480 | (unsigned) fd >= ctx->nr_user_files)) |
| 4481 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4482 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4483 | req->file = io_file_from_index(ctx, fd); |
| 4484 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4485 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4486 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4487 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4488 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4489 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4490 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4491 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4492 | req->file = io_file_get(state, fd); |
| 4493 | if (unlikely(!req->file)) |
| 4494 | return -EBADF; |
| 4495 | } |
| 4496 | |
| 4497 | return 0; |
| 4498 | } |
| 4499 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4500 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4501 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4502 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4503 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4504 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4505 | if (req->work.files) |
| 4506 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4507 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4508 | return -EBADF; |
| 4509 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4510 | rcu_read_lock(); |
| 4511 | spin_lock_irq(&ctx->inflight_lock); |
| 4512 | /* |
| 4513 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4514 | * out work accessing these files if the fd is closed. Check if |
| 4515 | * the fd has changed since we started down this path, and disallow |
| 4516 | * this operation if it has. |
| 4517 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4518 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4519 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4520 | req->flags |= REQ_F_INFLIGHT; |
| 4521 | req->work.files = current->files; |
| 4522 | ret = 0; |
| 4523 | } |
| 4524 | spin_unlock_irq(&ctx->inflight_lock); |
| 4525 | rcu_read_unlock(); |
| 4526 | |
| 4527 | return ret; |
| 4528 | } |
| 4529 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4530 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4531 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4532 | struct io_timeout_data *data = container_of(timer, |
| 4533 | struct io_timeout_data, timer); |
| 4534 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4535 | struct io_ring_ctx *ctx = req->ctx; |
| 4536 | struct io_kiocb *prev = NULL; |
| 4537 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4538 | |
| 4539 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4540 | |
| 4541 | /* |
| 4542 | * We don't expect the list to be empty, that will only happen if we |
| 4543 | * race with the completion of the linked work. |
| 4544 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4545 | if (!list_empty(&req->link_list)) { |
| 4546 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4547 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4548 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4549 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4550 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4551 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4552 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4556 | |
| 4557 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4558 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4559 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4560 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4561 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4562 | } else { |
| 4563 | io_cqring_add_event(req, -ETIME); |
| 4564 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4565 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4566 | return HRTIMER_NORESTART; |
| 4567 | } |
| 4568 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4569 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4570 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4571 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4572 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4573 | /* |
| 4574 | * If the list is now empty, then our linked request finished before |
| 4575 | * we got a chance to setup the timer |
| 4576 | */ |
| 4577 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4578 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4579 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4580 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4581 | data->timer.function = io_link_timeout_fn; |
| 4582 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4583 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4584 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4585 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4586 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4587 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4588 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4589 | } |
| 4590 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4591 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4592 | { |
| 4593 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4594 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4595 | if (!(req->flags & REQ_F_LINK)) |
| 4596 | return NULL; |
| 4597 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4598 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4599 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4600 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4601 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4602 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4603 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4604 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4605 | } |
| 4606 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4607 | 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] | 4608 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4609 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4610 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4611 | int ret; |
| 4612 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4613 | again: |
| 4614 | linked_timeout = io_prep_linked_timeout(req); |
| 4615 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4616 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4617 | |
| 4618 | /* |
| 4619 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4620 | * doesn't support non-blocking read/write attempts |
| 4621 | */ |
| 4622 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4623 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4624 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4625 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4626 | ret = io_grab_files(req); |
| 4627 | if (ret) |
| 4628 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4629 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4630 | |
| 4631 | /* |
| 4632 | * Queued up for async execution, worker will release |
| 4633 | * submit reference when the iocb is actually submitted. |
| 4634 | */ |
| 4635 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4636 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4637 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4638 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4639 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4640 | /* drop submission reference */ |
| 4641 | io_put_req(req); |
| 4642 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4643 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4644 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4645 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4646 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4647 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4650 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4651 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4652 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4653 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4654 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4655 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4656 | done_req: |
| 4657 | if (nxt) { |
| 4658 | req = nxt; |
| 4659 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4660 | |
| 4661 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4662 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4663 | goto again; |
| 4664 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4665 | } |
| 4666 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4667 | 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] | 4668 | { |
| 4669 | int ret; |
| 4670 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4671 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4672 | if (ret) { |
| 4673 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4674 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4675 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4676 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4677 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4678 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4679 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4680 | ret = io_req_defer_prep(req, sqe); |
| 4681 | if (unlikely(ret < 0)) |
| 4682 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4683 | /* |
| 4684 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4685 | * to async execution. |
| 4686 | */ |
| 4687 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4688 | io_queue_async_work(req); |
| 4689 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4690 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4691 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4692 | } |
| 4693 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4694 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4695 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4696 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4697 | io_cqring_add_event(req, -ECANCELED); |
| 4698 | io_double_put_req(req); |
| 4699 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4700 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4701 | } |
| 4702 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4703 | #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] | 4704 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4705 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4706 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4707 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4708 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4709 | const struct cred *old_creds = NULL; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4710 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4711 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4712 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4713 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4714 | sqe_flags = READ_ONCE(sqe->flags); |
| 4715 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4716 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4717 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4718 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4719 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4720 | } |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4721 | |
| 4722 | id = READ_ONCE(sqe->personality); |
| 4723 | if (id) { |
| 4724 | const struct cred *personality_creds; |
| 4725 | |
| 4726 | personality_creds = idr_find(&ctx->personality_idr, id); |
| 4727 | if (unlikely(!personality_creds)) { |
| 4728 | ret = -EINVAL; |
| 4729 | goto err_req; |
| 4730 | } |
| 4731 | old_creds = override_creds(personality_creds); |
| 4732 | } |
| 4733 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4734 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 4735 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| |
| 4736 | IOSQE_ASYNC); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4737 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4738 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4739 | if (unlikely(ret)) { |
| 4740 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4741 | io_cqring_add_event(req, ret); |
| 4742 | io_double_put_req(req); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4743 | if (old_creds) |
| 4744 | revert_creds(old_creds); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4745 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4746 | } |
| 4747 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4748 | /* |
| 4749 | * If we already have a head request, queue this one for async |
| 4750 | * submittal once the head completes. If we don't have a head but |
| 4751 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4752 | * submitted sync once the chain is complete. If none of those |
| 4753 | * conditions are true (normal request), then just queue it. |
| 4754 | */ |
| 4755 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4756 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4757 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 4758 | /* |
| 4759 | * Taking sequential execution of a link, draining both sides |
| 4760 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 4761 | * requests in the link. So, it drains the head and the |
| 4762 | * next after the link request. The last one is done via |
| 4763 | * drain_next flag to persist the effect across calls. |
| 4764 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4765 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4766 | head->flags |= REQ_F_IO_DRAIN; |
| 4767 | ctx->drain_next = 1; |
| 4768 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4769 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4770 | ret = -EAGAIN; |
| 4771 | goto err_req; |
| 4772 | } |
| 4773 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4774 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4775 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4776 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4777 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4778 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4779 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4780 | trace_io_uring_link(ctx, req, head); |
| 4781 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4782 | |
| 4783 | /* last request of a link, enqueue the link */ |
| 4784 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4785 | io_queue_link_head(head); |
| 4786 | *link = NULL; |
| 4787 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4788 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4789 | if (unlikely(ctx->drain_next)) { |
| 4790 | req->flags |= REQ_F_IO_DRAIN; |
| 4791 | req->ctx->drain_next = 0; |
| 4792 | } |
| 4793 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4794 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4795 | INIT_LIST_HEAD(&req->link_list); |
| 4796 | ret = io_req_defer_prep(req, sqe); |
| 4797 | if (ret) |
| 4798 | req->flags |= REQ_F_FAIL_LINK; |
| 4799 | *link = req; |
| 4800 | } else { |
| 4801 | io_queue_sqe(req, sqe); |
| 4802 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4803 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4804 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4805 | if (old_creds) |
| 4806 | revert_creds(old_creds); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4807 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4808 | } |
| 4809 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4810 | /* |
| 4811 | * Batched submission is done, ensure local IO is flushed out. |
| 4812 | */ |
| 4813 | static void io_submit_state_end(struct io_submit_state *state) |
| 4814 | { |
| 4815 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4816 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4817 | if (state->free_reqs) |
| 4818 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4819 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /* |
| 4823 | * Start submission side cache. |
| 4824 | */ |
| 4825 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4826 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4827 | { |
| 4828 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4829 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4830 | state->file = NULL; |
| 4831 | state->ios_left = max_ios; |
| 4832 | } |
| 4833 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4834 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4835 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4836 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4837 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4838 | /* |
| 4839 | * Ensure any loads from the SQEs are done at this point, |
| 4840 | * since once we write the new head, the application could |
| 4841 | * write new data to them. |
| 4842 | */ |
| 4843 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4844 | } |
| 4845 | |
| 4846 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4847 | * 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] | 4848 | * that is mapped by userspace. This means that care needs to be taken to |
| 4849 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4850 | * being a good citizen. If members of the sqe are validated and then later |
| 4851 | * used, it's important that those reads are done through READ_ONCE() to |
| 4852 | * prevent a re-load down the line. |
| 4853 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4854 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4855 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4856 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4857 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4858 | unsigned head; |
| 4859 | |
| 4860 | /* |
| 4861 | * The cached sq head (or cq tail) serves two purposes: |
| 4862 | * |
| 4863 | * 1) allows us to batch the cost of updating the user visible |
| 4864 | * head updates. |
| 4865 | * 2) allows the kernel side to track the head on its own, even |
| 4866 | * though the application is the one updating it. |
| 4867 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4868 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4869 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4870 | /* |
| 4871 | * All io need record the previous position, if LINK vs DARIN, |
| 4872 | * it can be used to mark the position of the first IO in the |
| 4873 | * link list. |
| 4874 | */ |
| 4875 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4876 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4877 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4878 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4879 | ctx->cached_sq_head++; |
| 4880 | return true; |
| 4881 | } |
| 4882 | |
| 4883 | /* drop invalid entries */ |
| 4884 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4885 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4886 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4887 | return false; |
| 4888 | } |
| 4889 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4890 | 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] | 4891 | struct file *ring_file, int ring_fd, |
| 4892 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4893 | { |
| 4894 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4895 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4896 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4897 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4898 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4899 | /* 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] | 4900 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4901 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4902 | !io_cqring_overflow_flush(ctx, false)) |
| 4903 | return -EBUSY; |
| 4904 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4905 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4906 | /* make sure SQ entry isn't read before tail */ |
| 4907 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4908 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4909 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4910 | return -EAGAIN; |
| 4911 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4912 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4913 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4914 | statep = &state; |
| 4915 | } |
| 4916 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4917 | ctx->ring_fd = ring_fd; |
| 4918 | ctx->ring_file = ring_file; |
| 4919 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4920 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4921 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4922 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4923 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4924 | req = io_get_req(ctx, statep); |
| 4925 | if (unlikely(!req)) { |
| 4926 | if (!submitted) |
| 4927 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4928 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4929 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4930 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4931 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4932 | break; |
| 4933 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4934 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4935 | /* will complete beyond this point, count as submitted */ |
| 4936 | submitted++; |
| 4937 | |
| 4938 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4939 | io_cqring_add_event(req, -EINVAL); |
| 4940 | io_double_put_req(req); |
| 4941 | break; |
| 4942 | } |
| 4943 | |
| 4944 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4945 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4946 | if (!mm_fault) { |
| 4947 | use_mm(ctx->sqo_mm); |
| 4948 | *mm = ctx->sqo_mm; |
| 4949 | } |
| 4950 | } |
| 4951 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4952 | req->has_user = *mm != NULL; |
| 4953 | req->in_async = async; |
| 4954 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 4955 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 4956 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4957 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4958 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4959 | } |
| 4960 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 4961 | if (unlikely(submitted != nr)) { |
| 4962 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 4963 | |
| 4964 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 4965 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4966 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4967 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4968 | if (statep) |
| 4969 | io_submit_state_end(&state); |
| 4970 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4971 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4972 | io_commit_sqring(ctx); |
| 4973 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4974 | return submitted; |
| 4975 | } |
| 4976 | |
| 4977 | static int io_sq_thread(void *data) |
| 4978 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4979 | struct io_ring_ctx *ctx = data; |
| 4980 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4981 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4982 | mm_segment_t old_fs; |
| 4983 | DEFINE_WAIT(wait); |
| 4984 | unsigned inflight; |
| 4985 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4986 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4987 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4988 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4989 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4990 | old_fs = get_fs(); |
| 4991 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4992 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4993 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4994 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4995 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4996 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4997 | |
| 4998 | if (inflight) { |
| 4999 | unsigned nr_events = 0; |
| 5000 | |
| 5001 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 5002 | /* |
| 5003 | * inflight is the count of the maximum possible |
| 5004 | * entries we submitted, but it can be smaller |
| 5005 | * if we dropped some of them. If we don't have |
| 5006 | * poll entries available, then we know that we |
| 5007 | * have nothing left to poll for. Reset the |
| 5008 | * inflight count to zero in that case. |
| 5009 | */ |
| 5010 | mutex_lock(&ctx->uring_lock); |
| 5011 | if (!list_empty(&ctx->poll_list)) |
| 5012 | __io_iopoll_check(ctx, &nr_events, 0); |
| 5013 | else |
| 5014 | inflight = 0; |
| 5015 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5016 | } else { |
| 5017 | /* |
| 5018 | * Normal IO, just pretend everything completed. |
| 5019 | * We don't have to poll completions for that. |
| 5020 | */ |
| 5021 | nr_events = inflight; |
| 5022 | } |
| 5023 | |
| 5024 | inflight -= nr_events; |
| 5025 | if (!inflight) |
| 5026 | timeout = jiffies + ctx->sq_thread_idle; |
| 5027 | } |
| 5028 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5029 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5030 | |
| 5031 | /* |
| 5032 | * If submit got -EBUSY, flag us as needing the application |
| 5033 | * to enter the kernel to reap and flush events. |
| 5034 | */ |
| 5035 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5036 | /* |
| 5037 | * We're polling. If we're within the defined idle |
| 5038 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5039 | * to sleep. The exception is if we got EBUSY doing |
| 5040 | * more IO, we should wait for the application to |
| 5041 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5042 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5043 | if (inflight || |
| 5044 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5045 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5046 | continue; |
| 5047 | } |
| 5048 | |
| 5049 | /* |
| 5050 | * Drop cur_mm before scheduling, we can't hold it for |
| 5051 | * long periods (or over schedule()). Do this before |
| 5052 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5053 | * may sleep. |
| 5054 | */ |
| 5055 | if (cur_mm) { |
| 5056 | unuse_mm(cur_mm); |
| 5057 | mmput(cur_mm); |
| 5058 | cur_mm = NULL; |
| 5059 | } |
| 5060 | |
| 5061 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5062 | TASK_INTERRUPTIBLE); |
| 5063 | |
| 5064 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5065 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5066 | /* make sure to read SQ tail after writing flags */ |
| 5067 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5068 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5069 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5070 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5071 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5072 | finish_wait(&ctx->sqo_wait, &wait); |
| 5073 | break; |
| 5074 | } |
| 5075 | if (signal_pending(current)) |
| 5076 | flush_signals(current); |
| 5077 | schedule(); |
| 5078 | finish_wait(&ctx->sqo_wait, &wait); |
| 5079 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5080 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5081 | continue; |
| 5082 | } |
| 5083 | finish_wait(&ctx->sqo_wait, &wait); |
| 5084 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5085 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5086 | } |
| 5087 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5088 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5089 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5090 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5091 | if (ret > 0) |
| 5092 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | set_fs(old_fs); |
| 5096 | if (cur_mm) { |
| 5097 | unuse_mm(cur_mm); |
| 5098 | mmput(cur_mm); |
| 5099 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5100 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5101 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5102 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5103 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5104 | return 0; |
| 5105 | } |
| 5106 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5107 | struct io_wait_queue { |
| 5108 | struct wait_queue_entry wq; |
| 5109 | struct io_ring_ctx *ctx; |
| 5110 | unsigned to_wait; |
| 5111 | unsigned nr_timeouts; |
| 5112 | }; |
| 5113 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5114 | 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] | 5115 | { |
| 5116 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5117 | |
| 5118 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5119 | * 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] | 5120 | * started waiting. For timeouts, we always want to return to userspace, |
| 5121 | * regardless of event count. |
| 5122 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5123 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5124 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5125 | } |
| 5126 | |
| 5127 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5128 | int wake_flags, void *key) |
| 5129 | { |
| 5130 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5131 | wq); |
| 5132 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5133 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5134 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5135 | return -1; |
| 5136 | |
| 5137 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5138 | } |
| 5139 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5140 | /* |
| 5141 | * Wait until events become available, if we don't already have some. The |
| 5142 | * application must reap them itself, as they reside on the shared cq ring. |
| 5143 | */ |
| 5144 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5145 | const sigset_t __user *sig, size_t sigsz) |
| 5146 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5147 | struct io_wait_queue iowq = { |
| 5148 | .wq = { |
| 5149 | .private = current, |
| 5150 | .func = io_wake_function, |
| 5151 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5152 | }, |
| 5153 | .ctx = ctx, |
| 5154 | .to_wait = min_events, |
| 5155 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5156 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5157 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5158 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5159 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5160 | return 0; |
| 5161 | |
| 5162 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5163 | #ifdef CONFIG_COMPAT |
| 5164 | if (in_compat_syscall()) |
| 5165 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5166 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5167 | else |
| 5168 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5169 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5170 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5171 | if (ret) |
| 5172 | return ret; |
| 5173 | } |
| 5174 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5175 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5176 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5177 | do { |
| 5178 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5179 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5180 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5181 | break; |
| 5182 | schedule(); |
| 5183 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5184 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5185 | break; |
| 5186 | } |
| 5187 | } while (1); |
| 5188 | finish_wait(&ctx->wait, &iowq.wq); |
| 5189 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5190 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5191 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5192 | 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] | 5193 | } |
| 5194 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5195 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5196 | { |
| 5197 | #if defined(CONFIG_UNIX) |
| 5198 | if (ctx->ring_sock) { |
| 5199 | struct sock *sock = ctx->ring_sock->sk; |
| 5200 | struct sk_buff *skb; |
| 5201 | |
| 5202 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5203 | kfree_skb(skb); |
| 5204 | } |
| 5205 | #else |
| 5206 | int i; |
| 5207 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5208 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5209 | struct file *file; |
| 5210 | |
| 5211 | file = io_file_from_index(ctx, i); |
| 5212 | if (file) |
| 5213 | fput(file); |
| 5214 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5215 | #endif |
| 5216 | } |
| 5217 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5218 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5219 | { |
| 5220 | struct fixed_file_data *data; |
| 5221 | |
| 5222 | data = container_of(ref, struct fixed_file_data, refs); |
| 5223 | complete(&data->done); |
| 5224 | } |
| 5225 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5226 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5227 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5228 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5229 | unsigned nr_tables, i; |
| 5230 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5231 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5232 | return -ENXIO; |
| 5233 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5234 | /* protect against inflight atomic switch, which drops the ref */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5235 | percpu_ref_get(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5236 | /* wait for existing switches */ |
| 5237 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5238 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 5239 | wait_for_completion(&data->done); |
| 5240 | percpu_ref_put(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5241 | /* flush potential new switch */ |
| 5242 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5243 | percpu_ref_exit(&data->refs); |
| 5244 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5245 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5246 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5247 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5248 | kfree(data->table[i].files); |
| 5249 | kfree(data->table); |
| 5250 | kfree(data); |
| 5251 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5252 | ctx->nr_user_files = 0; |
| 5253 | return 0; |
| 5254 | } |
| 5255 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5256 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5257 | { |
| 5258 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5259 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5260 | /* |
| 5261 | * The park is a bit of a work-around, without it we get |
| 5262 | * warning spews on shutdown with SQPOLL set and affinity |
| 5263 | * set to a single CPU. |
| 5264 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5265 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5266 | kthread_stop(ctx->sqo_thread); |
| 5267 | ctx->sqo_thread = NULL; |
| 5268 | } |
| 5269 | } |
| 5270 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5271 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5272 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5273 | io_sq_thread_stop(ctx); |
| 5274 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5275 | if (ctx->io_wq) { |
| 5276 | io_wq_destroy(ctx->io_wq); |
| 5277 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5278 | } |
| 5279 | } |
| 5280 | |
| 5281 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5282 | /* |
| 5283 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5284 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5285 | * loops in the file referencing. |
| 5286 | */ |
| 5287 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5288 | { |
| 5289 | struct sock *sk = ctx->ring_sock->sk; |
| 5290 | struct scm_fp_list *fpl; |
| 5291 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5292 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5293 | |
| 5294 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5295 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5296 | |
| 5297 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5298 | return -EMFILE; |
| 5299 | } |
| 5300 | |
| 5301 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5302 | if (!fpl) |
| 5303 | return -ENOMEM; |
| 5304 | |
| 5305 | skb = alloc_skb(0, GFP_KERNEL); |
| 5306 | if (!skb) { |
| 5307 | kfree(fpl); |
| 5308 | return -ENOMEM; |
| 5309 | } |
| 5310 | |
| 5311 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5312 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5313 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5314 | fpl->user = get_uid(ctx->user); |
| 5315 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5316 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5317 | |
| 5318 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5319 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5320 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5321 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5322 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5323 | } |
| 5324 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5325 | if (nr_files) { |
| 5326 | fpl->max = SCM_MAX_FD; |
| 5327 | fpl->count = nr_files; |
| 5328 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5329 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5330 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5331 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5332 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5333 | for (i = 0; i < nr_files; i++) |
| 5334 | fput(fpl->fp[i]); |
| 5335 | } else { |
| 5336 | kfree_skb(skb); |
| 5337 | kfree(fpl); |
| 5338 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5339 | |
| 5340 | return 0; |
| 5341 | } |
| 5342 | |
| 5343 | /* |
| 5344 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5345 | * causes regular reference counting to break down. We rely on the UNIX |
| 5346 | * garbage collection to take care of this problem for us. |
| 5347 | */ |
| 5348 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5349 | { |
| 5350 | unsigned left, total; |
| 5351 | int ret = 0; |
| 5352 | |
| 5353 | total = 0; |
| 5354 | left = ctx->nr_user_files; |
| 5355 | while (left) { |
| 5356 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5357 | |
| 5358 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5359 | if (ret) |
| 5360 | break; |
| 5361 | left -= this_files; |
| 5362 | total += this_files; |
| 5363 | } |
| 5364 | |
| 5365 | if (!ret) |
| 5366 | return 0; |
| 5367 | |
| 5368 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5369 | struct file *file = io_file_from_index(ctx, total); |
| 5370 | |
| 5371 | if (file) |
| 5372 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5373 | total++; |
| 5374 | } |
| 5375 | |
| 5376 | return ret; |
| 5377 | } |
| 5378 | #else |
| 5379 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5380 | { |
| 5381 | return 0; |
| 5382 | } |
| 5383 | #endif |
| 5384 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5385 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5386 | unsigned nr_files) |
| 5387 | { |
| 5388 | int i; |
| 5389 | |
| 5390 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5391 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5392 | unsigned this_files; |
| 5393 | |
| 5394 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5395 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5396 | GFP_KERNEL); |
| 5397 | if (!table->files) |
| 5398 | break; |
| 5399 | nr_files -= this_files; |
| 5400 | } |
| 5401 | |
| 5402 | if (i == nr_tables) |
| 5403 | return 0; |
| 5404 | |
| 5405 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5406 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5407 | kfree(table->files); |
| 5408 | } |
| 5409 | return 1; |
| 5410 | } |
| 5411 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5412 | 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] | 5413 | { |
| 5414 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5415 | struct sock *sock = ctx->ring_sock->sk; |
| 5416 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5417 | struct sk_buff *skb; |
| 5418 | int i; |
| 5419 | |
| 5420 | __skb_queue_head_init(&list); |
| 5421 | |
| 5422 | /* |
| 5423 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5424 | * remove this entry and rearrange the file array. |
| 5425 | */ |
| 5426 | skb = skb_dequeue(head); |
| 5427 | while (skb) { |
| 5428 | struct scm_fp_list *fp; |
| 5429 | |
| 5430 | fp = UNIXCB(skb).fp; |
| 5431 | for (i = 0; i < fp->count; i++) { |
| 5432 | int left; |
| 5433 | |
| 5434 | if (fp->fp[i] != file) |
| 5435 | continue; |
| 5436 | |
| 5437 | unix_notinflight(fp->user, fp->fp[i]); |
| 5438 | left = fp->count - 1 - i; |
| 5439 | if (left) { |
| 5440 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5441 | left * sizeof(struct file *)); |
| 5442 | } |
| 5443 | fp->count--; |
| 5444 | if (!fp->count) { |
| 5445 | kfree_skb(skb); |
| 5446 | skb = NULL; |
| 5447 | } else { |
| 5448 | __skb_queue_tail(&list, skb); |
| 5449 | } |
| 5450 | fput(file); |
| 5451 | file = NULL; |
| 5452 | break; |
| 5453 | } |
| 5454 | |
| 5455 | if (!file) |
| 5456 | break; |
| 5457 | |
| 5458 | __skb_queue_tail(&list, skb); |
| 5459 | |
| 5460 | skb = skb_dequeue(head); |
| 5461 | } |
| 5462 | |
| 5463 | if (skb_peek(&list)) { |
| 5464 | spin_lock_irq(&head->lock); |
| 5465 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5466 | __skb_queue_tail(head, skb); |
| 5467 | spin_unlock_irq(&head->lock); |
| 5468 | } |
| 5469 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5470 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5471 | #endif |
| 5472 | } |
| 5473 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5474 | struct io_file_put { |
| 5475 | struct llist_node llist; |
| 5476 | struct file *file; |
| 5477 | struct completion *done; |
| 5478 | }; |
| 5479 | |
| 5480 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5481 | { |
| 5482 | struct io_file_put *pfile, *tmp; |
| 5483 | struct fixed_file_data *data; |
| 5484 | struct llist_node *node; |
| 5485 | |
| 5486 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5487 | |
| 5488 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5489 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5490 | io_ring_file_put(data->ctx, pfile->file); |
| 5491 | if (pfile->done) |
| 5492 | complete(pfile->done); |
| 5493 | else |
| 5494 | kfree(pfile); |
| 5495 | } |
| 5496 | } |
| 5497 | |
| 5498 | percpu_ref_get(&data->refs); |
| 5499 | percpu_ref_switch_to_percpu(&data->refs); |
| 5500 | } |
| 5501 | |
| 5502 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5503 | { |
| 5504 | struct fixed_file_data *data; |
| 5505 | |
| 5506 | data = container_of(ref, struct fixed_file_data, refs); |
| 5507 | |
| 5508 | /* we can't safely switch from inside this context, punt to wq */ |
| 5509 | queue_work(system_wq, &data->ref_work); |
| 5510 | } |
| 5511 | |
| 5512 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5513 | unsigned nr_args) |
| 5514 | { |
| 5515 | __s32 __user *fds = (__s32 __user *) arg; |
| 5516 | unsigned nr_tables; |
| 5517 | struct file *file; |
| 5518 | int fd, ret = 0; |
| 5519 | unsigned i; |
| 5520 | |
| 5521 | if (ctx->file_data) |
| 5522 | return -EBUSY; |
| 5523 | if (!nr_args) |
| 5524 | return -EINVAL; |
| 5525 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5526 | return -EMFILE; |
| 5527 | |
| 5528 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5529 | if (!ctx->file_data) |
| 5530 | return -ENOMEM; |
| 5531 | ctx->file_data->ctx = ctx; |
| 5532 | init_completion(&ctx->file_data->done); |
| 5533 | |
| 5534 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5535 | ctx->file_data->table = kcalloc(nr_tables, |
| 5536 | sizeof(struct fixed_file_table), |
| 5537 | GFP_KERNEL); |
| 5538 | if (!ctx->file_data->table) { |
| 5539 | kfree(ctx->file_data); |
| 5540 | ctx->file_data = NULL; |
| 5541 | return -ENOMEM; |
| 5542 | } |
| 5543 | |
| 5544 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5545 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5546 | kfree(ctx->file_data->table); |
| 5547 | kfree(ctx->file_data); |
| 5548 | ctx->file_data = NULL; |
| 5549 | return -ENOMEM; |
| 5550 | } |
| 5551 | ctx->file_data->put_llist.first = NULL; |
| 5552 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5553 | |
| 5554 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5555 | percpu_ref_exit(&ctx->file_data->refs); |
| 5556 | kfree(ctx->file_data->table); |
| 5557 | kfree(ctx->file_data); |
| 5558 | ctx->file_data = NULL; |
| 5559 | return -ENOMEM; |
| 5560 | } |
| 5561 | |
| 5562 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5563 | struct fixed_file_table *table; |
| 5564 | unsigned index; |
| 5565 | |
| 5566 | ret = -EFAULT; |
| 5567 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5568 | break; |
| 5569 | /* allow sparse sets */ |
| 5570 | if (fd == -1) { |
| 5571 | ret = 0; |
| 5572 | continue; |
| 5573 | } |
| 5574 | |
| 5575 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5576 | index = i & IORING_FILE_TABLE_MASK; |
| 5577 | file = fget(fd); |
| 5578 | |
| 5579 | ret = -EBADF; |
| 5580 | if (!file) |
| 5581 | break; |
| 5582 | |
| 5583 | /* |
| 5584 | * Don't allow io_uring instances to be registered. If UNIX |
| 5585 | * isn't enabled, then this causes a reference cycle and this |
| 5586 | * instance can never get freed. If UNIX is enabled we'll |
| 5587 | * handle it just fine, but there's still no point in allowing |
| 5588 | * a ring fd as it doesn't support regular read/write anyway. |
| 5589 | */ |
| 5590 | if (file->f_op == &io_uring_fops) { |
| 5591 | fput(file); |
| 5592 | break; |
| 5593 | } |
| 5594 | ret = 0; |
| 5595 | table->files[index] = file; |
| 5596 | } |
| 5597 | |
| 5598 | if (ret) { |
| 5599 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5600 | file = io_file_from_index(ctx, i); |
| 5601 | if (file) |
| 5602 | fput(file); |
| 5603 | } |
| 5604 | for (i = 0; i < nr_tables; i++) |
| 5605 | kfree(ctx->file_data->table[i].files); |
| 5606 | |
| 5607 | kfree(ctx->file_data->table); |
| 5608 | kfree(ctx->file_data); |
| 5609 | ctx->file_data = NULL; |
| 5610 | ctx->nr_user_files = 0; |
| 5611 | return ret; |
| 5612 | } |
| 5613 | |
| 5614 | ret = io_sqe_files_scm(ctx); |
| 5615 | if (ret) |
| 5616 | io_sqe_files_unregister(ctx); |
| 5617 | |
| 5618 | return ret; |
| 5619 | } |
| 5620 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5621 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5622 | int index) |
| 5623 | { |
| 5624 | #if defined(CONFIG_UNIX) |
| 5625 | struct sock *sock = ctx->ring_sock->sk; |
| 5626 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5627 | struct sk_buff *skb; |
| 5628 | |
| 5629 | /* |
| 5630 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5631 | * file set. If there's no room, fall back to allocating a new skb |
| 5632 | * and filling it in. |
| 5633 | */ |
| 5634 | spin_lock_irq(&head->lock); |
| 5635 | skb = skb_peek(head); |
| 5636 | if (skb) { |
| 5637 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5638 | |
| 5639 | if (fpl->count < SCM_MAX_FD) { |
| 5640 | __skb_unlink(skb, head); |
| 5641 | spin_unlock_irq(&head->lock); |
| 5642 | fpl->fp[fpl->count] = get_file(file); |
| 5643 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5644 | fpl->count++; |
| 5645 | spin_lock_irq(&head->lock); |
| 5646 | __skb_queue_head(head, skb); |
| 5647 | } else { |
| 5648 | skb = NULL; |
| 5649 | } |
| 5650 | } |
| 5651 | spin_unlock_irq(&head->lock); |
| 5652 | |
| 5653 | if (skb) { |
| 5654 | fput(file); |
| 5655 | return 0; |
| 5656 | } |
| 5657 | |
| 5658 | return __io_sqe_files_scm(ctx, 1, index); |
| 5659 | #else |
| 5660 | return 0; |
| 5661 | #endif |
| 5662 | } |
| 5663 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5664 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5665 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5666 | struct fixed_file_data *data; |
| 5667 | |
| 5668 | data = container_of(ref, struct fixed_file_data, refs); |
| 5669 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5670 | } |
| 5671 | |
| 5672 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5673 | struct file *file) |
| 5674 | { |
| 5675 | struct io_file_put *pfile, pfile_stack; |
| 5676 | DECLARE_COMPLETION_ONSTACK(done); |
| 5677 | |
| 5678 | /* |
| 5679 | * If we fail allocating the struct we need for doing async reomval |
| 5680 | * of this file, just punt to sync and wait for it. |
| 5681 | */ |
| 5682 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5683 | if (!pfile) { |
| 5684 | pfile = &pfile_stack; |
| 5685 | pfile->done = &done; |
| 5686 | } |
| 5687 | |
| 5688 | pfile->file = file; |
| 5689 | llist_add(&pfile->llist, &data->put_llist); |
| 5690 | |
| 5691 | if (pfile == &pfile_stack) { |
| 5692 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5693 | percpu_ref_put(&data->refs); |
| 5694 | percpu_ref_switch_to_atomic(&data->refs, |
| 5695 | io_atomic_switch); |
| 5696 | } |
| 5697 | wait_for_completion(&done); |
| 5698 | flush_work(&data->ref_work); |
| 5699 | return false; |
| 5700 | } |
| 5701 | |
| 5702 | return true; |
| 5703 | } |
| 5704 | |
| 5705 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5706 | struct io_uring_files_update *up, |
| 5707 | unsigned nr_args) |
| 5708 | { |
| 5709 | struct fixed_file_data *data = ctx->file_data; |
| 5710 | bool ref_switch = false; |
| 5711 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5712 | __s32 __user *fds; |
| 5713 | int fd, i, err; |
| 5714 | __u32 done; |
| 5715 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5716 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5717 | return -EOVERFLOW; |
| 5718 | if (done > ctx->nr_user_files) |
| 5719 | return -EINVAL; |
| 5720 | |
| 5721 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5722 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5723 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5724 | struct fixed_file_table *table; |
| 5725 | unsigned index; |
| 5726 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5727 | err = 0; |
| 5728 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5729 | err = -EFAULT; |
| 5730 | break; |
| 5731 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5732 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5733 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5734 | index = i & IORING_FILE_TABLE_MASK; |
| 5735 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5736 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5737 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5738 | if (io_queue_file_removal(data, file)) |
| 5739 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5740 | } |
| 5741 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5742 | file = fget(fd); |
| 5743 | if (!file) { |
| 5744 | err = -EBADF; |
| 5745 | break; |
| 5746 | } |
| 5747 | /* |
| 5748 | * Don't allow io_uring instances to be registered. If |
| 5749 | * UNIX isn't enabled, then this causes a reference |
| 5750 | * cycle and this instance can never get freed. If UNIX |
| 5751 | * is enabled we'll handle it just fine, but there's |
| 5752 | * still no point in allowing a ring fd as it doesn't |
| 5753 | * support regular read/write anyway. |
| 5754 | */ |
| 5755 | if (file->f_op == &io_uring_fops) { |
| 5756 | fput(file); |
| 5757 | err = -EBADF; |
| 5758 | break; |
| 5759 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5760 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5761 | err = io_sqe_file_register(ctx, file, i); |
| 5762 | if (err) |
| 5763 | break; |
| 5764 | } |
| 5765 | nr_args--; |
| 5766 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5767 | up->offset++; |
| 5768 | } |
| 5769 | |
| 5770 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5771 | percpu_ref_put(&data->refs); |
| 5772 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | return done ? done : err; |
| 5776 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5777 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5778 | unsigned nr_args) |
| 5779 | { |
| 5780 | struct io_uring_files_update up; |
| 5781 | |
| 5782 | if (!ctx->file_data) |
| 5783 | return -ENXIO; |
| 5784 | if (!nr_args) |
| 5785 | return -EINVAL; |
| 5786 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5787 | return -EFAULT; |
| 5788 | if (up.resv) |
| 5789 | return -EINVAL; |
| 5790 | |
| 5791 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5792 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5793 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5794 | static void io_put_work(struct io_wq_work *work) |
| 5795 | { |
| 5796 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5797 | |
| 5798 | io_put_req(req); |
| 5799 | } |
| 5800 | |
| 5801 | static void io_get_work(struct io_wq_work *work) |
| 5802 | { |
| 5803 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5804 | |
| 5805 | refcount_inc(&req->refs); |
| 5806 | } |
| 5807 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5808 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 5809 | struct io_uring_params *p) |
| 5810 | { |
| 5811 | struct io_wq_data data; |
| 5812 | struct fd f; |
| 5813 | struct io_ring_ctx *ctx_attach; |
| 5814 | unsigned int concurrency; |
| 5815 | int ret = 0; |
| 5816 | |
| 5817 | data.user = ctx->user; |
| 5818 | data.get_work = io_get_work; |
| 5819 | data.put_work = io_put_work; |
| 5820 | |
| 5821 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 5822 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5823 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 5824 | |
| 5825 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 5826 | if (IS_ERR(ctx->io_wq)) { |
| 5827 | ret = PTR_ERR(ctx->io_wq); |
| 5828 | ctx->io_wq = NULL; |
| 5829 | } |
| 5830 | return ret; |
| 5831 | } |
| 5832 | |
| 5833 | f = fdget(p->wq_fd); |
| 5834 | if (!f.file) |
| 5835 | return -EBADF; |
| 5836 | |
| 5837 | if (f.file->f_op != &io_uring_fops) { |
| 5838 | ret = -EINVAL; |
| 5839 | goto out_fput; |
| 5840 | } |
| 5841 | |
| 5842 | ctx_attach = f.file->private_data; |
| 5843 | /* @io_wq is protected by holding the fd */ |
| 5844 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 5845 | ret = -EINVAL; |
| 5846 | goto out_fput; |
| 5847 | } |
| 5848 | |
| 5849 | ctx->io_wq = ctx_attach->io_wq; |
| 5850 | out_fput: |
| 5851 | fdput(f); |
| 5852 | return ret; |
| 5853 | } |
| 5854 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5855 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5856 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5857 | { |
| 5858 | int ret; |
| 5859 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5860 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5861 | mmgrab(current->mm); |
| 5862 | ctx->sqo_mm = current->mm; |
| 5863 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5864 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5865 | ret = -EPERM; |
| 5866 | if (!capable(CAP_SYS_ADMIN)) |
| 5867 | goto err; |
| 5868 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5869 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5870 | if (!ctx->sq_thread_idle) |
| 5871 | ctx->sq_thread_idle = HZ; |
| 5872 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5873 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5874 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5875 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5876 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5877 | if (cpu >= nr_cpu_ids) |
| 5878 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5879 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5880 | goto err; |
| 5881 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5882 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5883 | ctx, cpu, |
| 5884 | "io_uring-sq"); |
| 5885 | } else { |
| 5886 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5887 | "io_uring-sq"); |
| 5888 | } |
| 5889 | if (IS_ERR(ctx->sqo_thread)) { |
| 5890 | ret = PTR_ERR(ctx->sqo_thread); |
| 5891 | ctx->sqo_thread = NULL; |
| 5892 | goto err; |
| 5893 | } |
| 5894 | wake_up_process(ctx->sqo_thread); |
| 5895 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5896 | /* Can't have SQ_AFF without SQPOLL */ |
| 5897 | ret = -EINVAL; |
| 5898 | goto err; |
| 5899 | } |
| 5900 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5901 | ret = io_init_wq_offload(ctx, p); |
| 5902 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5903 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5904 | |
| 5905 | return 0; |
| 5906 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5907 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5908 | mmdrop(ctx->sqo_mm); |
| 5909 | ctx->sqo_mm = NULL; |
| 5910 | return ret; |
| 5911 | } |
| 5912 | |
| 5913 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5914 | { |
| 5915 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5916 | } |
| 5917 | |
| 5918 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5919 | { |
| 5920 | unsigned long page_limit, cur_pages, new_pages; |
| 5921 | |
| 5922 | /* Don't allow more pages than we can safely lock */ |
| 5923 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5924 | |
| 5925 | do { |
| 5926 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5927 | new_pages = cur_pages + nr_pages; |
| 5928 | if (new_pages > page_limit) |
| 5929 | return -ENOMEM; |
| 5930 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5931 | new_pages) != cur_pages); |
| 5932 | |
| 5933 | return 0; |
| 5934 | } |
| 5935 | |
| 5936 | static void io_mem_free(void *ptr) |
| 5937 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5938 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5939 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5940 | if (!ptr) |
| 5941 | return; |
| 5942 | |
| 5943 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5944 | if (put_page_testzero(page)) |
| 5945 | free_compound_page(page); |
| 5946 | } |
| 5947 | |
| 5948 | static void *io_mem_alloc(size_t size) |
| 5949 | { |
| 5950 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5951 | __GFP_NORETRY; |
| 5952 | |
| 5953 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5954 | } |
| 5955 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5956 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5957 | size_t *sq_offset) |
| 5958 | { |
| 5959 | struct io_rings *rings; |
| 5960 | size_t off, sq_array_size; |
| 5961 | |
| 5962 | off = struct_size(rings, cqes, cq_entries); |
| 5963 | if (off == SIZE_MAX) |
| 5964 | return SIZE_MAX; |
| 5965 | |
| 5966 | #ifdef CONFIG_SMP |
| 5967 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5968 | if (off == 0) |
| 5969 | return SIZE_MAX; |
| 5970 | #endif |
| 5971 | |
| 5972 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5973 | if (sq_array_size == SIZE_MAX) |
| 5974 | return SIZE_MAX; |
| 5975 | |
| 5976 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5977 | return SIZE_MAX; |
| 5978 | |
| 5979 | if (sq_offset) |
| 5980 | *sq_offset = off; |
| 5981 | |
| 5982 | return off; |
| 5983 | } |
| 5984 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5985 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5986 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5987 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5988 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5989 | pages = (size_t)1 << get_order( |
| 5990 | rings_size(sq_entries, cq_entries, NULL)); |
| 5991 | pages += (size_t)1 << get_order( |
| 5992 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5993 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5994 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5995 | } |
| 5996 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5997 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5998 | { |
| 5999 | int i, j; |
| 6000 | |
| 6001 | if (!ctx->user_bufs) |
| 6002 | return -ENXIO; |
| 6003 | |
| 6004 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6005 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6006 | |
| 6007 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6008 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6009 | |
| 6010 | if (ctx->account_mem) |
| 6011 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6012 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6013 | imu->nr_bvecs = 0; |
| 6014 | } |
| 6015 | |
| 6016 | kfree(ctx->user_bufs); |
| 6017 | ctx->user_bufs = NULL; |
| 6018 | ctx->nr_user_bufs = 0; |
| 6019 | return 0; |
| 6020 | } |
| 6021 | |
| 6022 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6023 | void __user *arg, unsigned index) |
| 6024 | { |
| 6025 | struct iovec __user *src; |
| 6026 | |
| 6027 | #ifdef CONFIG_COMPAT |
| 6028 | if (ctx->compat) { |
| 6029 | struct compat_iovec __user *ciovs; |
| 6030 | struct compat_iovec ciov; |
| 6031 | |
| 6032 | ciovs = (struct compat_iovec __user *) arg; |
| 6033 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6034 | return -EFAULT; |
| 6035 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6036 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6037 | dst->iov_len = ciov.iov_len; |
| 6038 | return 0; |
| 6039 | } |
| 6040 | #endif |
| 6041 | src = (struct iovec __user *) arg; |
| 6042 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6043 | return -EFAULT; |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
| 6047 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6048 | unsigned nr_args) |
| 6049 | { |
| 6050 | struct vm_area_struct **vmas = NULL; |
| 6051 | struct page **pages = NULL; |
| 6052 | int i, j, got_pages = 0; |
| 6053 | int ret = -EINVAL; |
| 6054 | |
| 6055 | if (ctx->user_bufs) |
| 6056 | return -EBUSY; |
| 6057 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6058 | return -EINVAL; |
| 6059 | |
| 6060 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6061 | GFP_KERNEL); |
| 6062 | if (!ctx->user_bufs) |
| 6063 | return -ENOMEM; |
| 6064 | |
| 6065 | for (i = 0; i < nr_args; i++) { |
| 6066 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6067 | unsigned long off, start, end, ubuf; |
| 6068 | int pret, nr_pages; |
| 6069 | struct iovec iov; |
| 6070 | size_t size; |
| 6071 | |
| 6072 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6073 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6074 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6075 | |
| 6076 | /* |
| 6077 | * Don't impose further limits on the size and buffer |
| 6078 | * constraints here, we'll -EINVAL later when IO is |
| 6079 | * submitted if they are wrong. |
| 6080 | */ |
| 6081 | ret = -EFAULT; |
| 6082 | if (!iov.iov_base || !iov.iov_len) |
| 6083 | goto err; |
| 6084 | |
| 6085 | /* arbitrary limit, but we need something */ |
| 6086 | if (iov.iov_len > SZ_1G) |
| 6087 | goto err; |
| 6088 | |
| 6089 | ubuf = (unsigned long) iov.iov_base; |
| 6090 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6091 | start = ubuf >> PAGE_SHIFT; |
| 6092 | nr_pages = end - start; |
| 6093 | |
| 6094 | if (ctx->account_mem) { |
| 6095 | ret = io_account_mem(ctx->user, nr_pages); |
| 6096 | if (ret) |
| 6097 | goto err; |
| 6098 | } |
| 6099 | |
| 6100 | ret = 0; |
| 6101 | if (!pages || nr_pages > got_pages) { |
| 6102 | kfree(vmas); |
| 6103 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6104 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6105 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6106 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6107 | sizeof(struct vm_area_struct *), |
| 6108 | GFP_KERNEL); |
| 6109 | if (!pages || !vmas) { |
| 6110 | ret = -ENOMEM; |
| 6111 | if (ctx->account_mem) |
| 6112 | io_unaccount_mem(ctx->user, nr_pages); |
| 6113 | goto err; |
| 6114 | } |
| 6115 | got_pages = nr_pages; |
| 6116 | } |
| 6117 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6118 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6119 | GFP_KERNEL); |
| 6120 | ret = -ENOMEM; |
| 6121 | if (!imu->bvec) { |
| 6122 | if (ctx->account_mem) |
| 6123 | io_unaccount_mem(ctx->user, nr_pages); |
| 6124 | goto err; |
| 6125 | } |
| 6126 | |
| 6127 | ret = 0; |
| 6128 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6129 | pret = get_user_pages(ubuf, nr_pages, |
| 6130 | FOLL_WRITE | FOLL_LONGTERM, |
| 6131 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6132 | if (pret == nr_pages) { |
| 6133 | /* don't support file backed memory */ |
| 6134 | for (j = 0; j < nr_pages; j++) { |
| 6135 | struct vm_area_struct *vma = vmas[j]; |
| 6136 | |
| 6137 | if (vma->vm_file && |
| 6138 | !is_file_hugepages(vma->vm_file)) { |
| 6139 | ret = -EOPNOTSUPP; |
| 6140 | break; |
| 6141 | } |
| 6142 | } |
| 6143 | } else { |
| 6144 | ret = pret < 0 ? pret : -EFAULT; |
| 6145 | } |
| 6146 | up_read(¤t->mm->mmap_sem); |
| 6147 | if (ret) { |
| 6148 | /* |
| 6149 | * if we did partial map, or found file backed vmas, |
| 6150 | * release any pages we did get |
| 6151 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6152 | if (pret > 0) |
| 6153 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6154 | if (ctx->account_mem) |
| 6155 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6156 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6157 | goto err; |
| 6158 | } |
| 6159 | |
| 6160 | off = ubuf & ~PAGE_MASK; |
| 6161 | size = iov.iov_len; |
| 6162 | for (j = 0; j < nr_pages; j++) { |
| 6163 | size_t vec_len; |
| 6164 | |
| 6165 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6166 | imu->bvec[j].bv_page = pages[j]; |
| 6167 | imu->bvec[j].bv_len = vec_len; |
| 6168 | imu->bvec[j].bv_offset = off; |
| 6169 | off = 0; |
| 6170 | size -= vec_len; |
| 6171 | } |
| 6172 | /* store original address for later verification */ |
| 6173 | imu->ubuf = ubuf; |
| 6174 | imu->len = iov.iov_len; |
| 6175 | imu->nr_bvecs = nr_pages; |
| 6176 | |
| 6177 | ctx->nr_user_bufs++; |
| 6178 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6179 | kvfree(pages); |
| 6180 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6181 | return 0; |
| 6182 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6183 | kvfree(pages); |
| 6184 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6185 | io_sqe_buffer_unregister(ctx); |
| 6186 | return ret; |
| 6187 | } |
| 6188 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6189 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6190 | { |
| 6191 | __s32 __user *fds = arg; |
| 6192 | int fd; |
| 6193 | |
| 6194 | if (ctx->cq_ev_fd) |
| 6195 | return -EBUSY; |
| 6196 | |
| 6197 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6198 | return -EFAULT; |
| 6199 | |
| 6200 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6201 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6202 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6203 | ctx->cq_ev_fd = NULL; |
| 6204 | return ret; |
| 6205 | } |
| 6206 | |
| 6207 | return 0; |
| 6208 | } |
| 6209 | |
| 6210 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6211 | { |
| 6212 | if (ctx->cq_ev_fd) { |
| 6213 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6214 | ctx->cq_ev_fd = NULL; |
| 6215 | return 0; |
| 6216 | } |
| 6217 | |
| 6218 | return -ENXIO; |
| 6219 | } |
| 6220 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6221 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6222 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6223 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6224 | if (ctx->sqo_mm) |
| 6225 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6226 | |
| 6227 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6228 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6229 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6230 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6231 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6232 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6233 | if (ctx->ring_sock) { |
| 6234 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6235 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6236 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6237 | #endif |
| 6238 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6239 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6240 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6241 | |
| 6242 | percpu_ref_exit(&ctx->refs); |
| 6243 | if (ctx->account_mem) |
| 6244 | io_unaccount_mem(ctx->user, |
| 6245 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6246 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6247 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6248 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6249 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6250 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6251 | kfree(ctx); |
| 6252 | } |
| 6253 | |
| 6254 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6255 | { |
| 6256 | struct io_ring_ctx *ctx = file->private_data; |
| 6257 | __poll_t mask = 0; |
| 6258 | |
| 6259 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6260 | /* |
| 6261 | * synchronizes with barrier from wq_has_sleeper call in |
| 6262 | * io_commit_cqring |
| 6263 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6264 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6265 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6266 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6267 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6268 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6269 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6270 | |
| 6271 | return mask; |
| 6272 | } |
| 6273 | |
| 6274 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6275 | { |
| 6276 | struct io_ring_ctx *ctx = file->private_data; |
| 6277 | |
| 6278 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6279 | } |
| 6280 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6281 | static int io_remove_personalities(int id, void *p, void *data) |
| 6282 | { |
| 6283 | struct io_ring_ctx *ctx = data; |
| 6284 | const struct cred *cred; |
| 6285 | |
| 6286 | cred = idr_remove(&ctx->personality_idr, id); |
| 6287 | if (cred) |
| 6288 | put_cred(cred); |
| 6289 | return 0; |
| 6290 | } |
| 6291 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6292 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6293 | { |
| 6294 | mutex_lock(&ctx->uring_lock); |
| 6295 | percpu_ref_kill(&ctx->refs); |
| 6296 | mutex_unlock(&ctx->uring_lock); |
| 6297 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6298 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6299 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6300 | |
| 6301 | if (ctx->io_wq) |
| 6302 | io_wq_cancel_all(ctx->io_wq); |
| 6303 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6304 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6305 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6306 | if (ctx->rings) |
| 6307 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6308 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6309 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6310 | io_ring_ctx_free(ctx); |
| 6311 | } |
| 6312 | |
| 6313 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6314 | { |
| 6315 | struct io_ring_ctx *ctx = file->private_data; |
| 6316 | |
| 6317 | file->private_data = NULL; |
| 6318 | io_ring_ctx_wait_and_kill(ctx); |
| 6319 | return 0; |
| 6320 | } |
| 6321 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6322 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6323 | struct files_struct *files) |
| 6324 | { |
| 6325 | struct io_kiocb *req; |
| 6326 | DEFINE_WAIT(wait); |
| 6327 | |
| 6328 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6329 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6330 | |
| 6331 | spin_lock_irq(&ctx->inflight_lock); |
| 6332 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6333 | if (req->work.files != files) |
| 6334 | continue; |
| 6335 | /* req is being completed, ignore */ |
| 6336 | if (!refcount_inc_not_zero(&req->refs)) |
| 6337 | continue; |
| 6338 | cancel_req = req; |
| 6339 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6340 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6341 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6342 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6343 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6344 | spin_unlock_irq(&ctx->inflight_lock); |
| 6345 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6346 | /* We need to keep going until we don't find a matching req */ |
| 6347 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6348 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6349 | |
| 6350 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6351 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6352 | schedule(); |
| 6353 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6354 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | static int io_uring_flush(struct file *file, void *data) |
| 6358 | { |
| 6359 | struct io_ring_ctx *ctx = file->private_data; |
| 6360 | |
| 6361 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6362 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 6363 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6364 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6365 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6366 | return 0; |
| 6367 | } |
| 6368 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6369 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6370 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6371 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6372 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6373 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6374 | struct page *page; |
| 6375 | void *ptr; |
| 6376 | |
| 6377 | switch (offset) { |
| 6378 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6379 | case IORING_OFF_CQ_RING: |
| 6380 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6381 | break; |
| 6382 | case IORING_OFF_SQES: |
| 6383 | ptr = ctx->sq_sqes; |
| 6384 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6385 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6386 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6387 | } |
| 6388 | |
| 6389 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6390 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6391 | return ERR_PTR(-EINVAL); |
| 6392 | |
| 6393 | return ptr; |
| 6394 | } |
| 6395 | |
| 6396 | #ifdef CONFIG_MMU |
| 6397 | |
| 6398 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6399 | { |
| 6400 | size_t sz = vma->vm_end - vma->vm_start; |
| 6401 | unsigned long pfn; |
| 6402 | void *ptr; |
| 6403 | |
| 6404 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6405 | if (IS_ERR(ptr)) |
| 6406 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6407 | |
| 6408 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6409 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6410 | } |
| 6411 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6412 | #else /* !CONFIG_MMU */ |
| 6413 | |
| 6414 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6415 | { |
| 6416 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6417 | } |
| 6418 | |
| 6419 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6420 | { |
| 6421 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6422 | } |
| 6423 | |
| 6424 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6425 | unsigned long addr, unsigned long len, |
| 6426 | unsigned long pgoff, unsigned long flags) |
| 6427 | { |
| 6428 | void *ptr; |
| 6429 | |
| 6430 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6431 | if (IS_ERR(ptr)) |
| 6432 | return PTR_ERR(ptr); |
| 6433 | |
| 6434 | return (unsigned long) ptr; |
| 6435 | } |
| 6436 | |
| 6437 | #endif /* !CONFIG_MMU */ |
| 6438 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6439 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6440 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6441 | size_t, sigsz) |
| 6442 | { |
| 6443 | struct io_ring_ctx *ctx; |
| 6444 | long ret = -EBADF; |
| 6445 | int submitted = 0; |
| 6446 | struct fd f; |
| 6447 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6448 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6449 | return -EINVAL; |
| 6450 | |
| 6451 | f = fdget(fd); |
| 6452 | if (!f.file) |
| 6453 | return -EBADF; |
| 6454 | |
| 6455 | ret = -EOPNOTSUPP; |
| 6456 | if (f.file->f_op != &io_uring_fops) |
| 6457 | goto out_fput; |
| 6458 | |
| 6459 | ret = -ENXIO; |
| 6460 | ctx = f.file->private_data; |
| 6461 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6462 | goto out_fput; |
| 6463 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6464 | /* |
| 6465 | * For SQ polling, the thread will do all submissions and completions. |
| 6466 | * Just return the requested submit count, and wake the thread if |
| 6467 | * we were asked to. |
| 6468 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6469 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6470 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6471 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6472 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6473 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6474 | wake_up(&ctx->sqo_wait); |
| 6475 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6476 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6477 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6478 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 6479 | if (current->mm != ctx->sqo_mm || |
| 6480 | current_cred() != ctx->creds) { |
| 6481 | ret = -EPERM; |
| 6482 | goto out; |
| 6483 | } |
| 6484 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6485 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6486 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6487 | cur_mm = ctx->sqo_mm; |
| 6488 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6489 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6490 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6491 | |
| 6492 | if (submitted != to_submit) |
| 6493 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6494 | } |
| 6495 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6496 | unsigned nr_events = 0; |
| 6497 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6498 | min_complete = min(min_complete, ctx->cq_entries); |
| 6499 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6500 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6501 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6502 | } else { |
| 6503 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6504 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6505 | } |
| 6506 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6507 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6508 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6509 | out_fput: |
| 6510 | fdput(f); |
| 6511 | return submitted ? submitted : ret; |
| 6512 | } |
| 6513 | |
| 6514 | static const struct file_operations io_uring_fops = { |
| 6515 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6516 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6517 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6518 | #ifndef CONFIG_MMU |
| 6519 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6520 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6521 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6522 | .poll = io_uring_poll, |
| 6523 | .fasync = io_uring_fasync, |
| 6524 | }; |
| 6525 | |
| 6526 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6527 | struct io_uring_params *p) |
| 6528 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6529 | struct io_rings *rings; |
| 6530 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6531 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6532 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6533 | if (size == SIZE_MAX) |
| 6534 | return -EOVERFLOW; |
| 6535 | |
| 6536 | rings = io_mem_alloc(size); |
| 6537 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6538 | return -ENOMEM; |
| 6539 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6540 | ctx->rings = rings; |
| 6541 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6542 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6543 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6544 | rings->sq_ring_entries = p->sq_entries; |
| 6545 | rings->cq_ring_entries = p->cq_entries; |
| 6546 | ctx->sq_mask = rings->sq_ring_mask; |
| 6547 | ctx->cq_mask = rings->cq_ring_mask; |
| 6548 | ctx->sq_entries = rings->sq_ring_entries; |
| 6549 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6550 | |
| 6551 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6552 | if (size == SIZE_MAX) { |
| 6553 | io_mem_free(ctx->rings); |
| 6554 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6555 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6556 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6557 | |
| 6558 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6559 | if (!ctx->sq_sqes) { |
| 6560 | io_mem_free(ctx->rings); |
| 6561 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6562 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6563 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6564 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6565 | return 0; |
| 6566 | } |
| 6567 | |
| 6568 | /* |
| 6569 | * Allocate an anonymous fd, this is what constitutes the application |
| 6570 | * visible backing of an io_uring instance. The application mmaps this |
| 6571 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6572 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6573 | */ |
| 6574 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6575 | { |
| 6576 | struct file *file; |
| 6577 | int ret; |
| 6578 | |
| 6579 | #if defined(CONFIG_UNIX) |
| 6580 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6581 | &ctx->ring_sock); |
| 6582 | if (ret) |
| 6583 | return ret; |
| 6584 | #endif |
| 6585 | |
| 6586 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6587 | if (ret < 0) |
| 6588 | goto err; |
| 6589 | |
| 6590 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6591 | O_RDWR | O_CLOEXEC); |
| 6592 | if (IS_ERR(file)) { |
| 6593 | put_unused_fd(ret); |
| 6594 | ret = PTR_ERR(file); |
| 6595 | goto err; |
| 6596 | } |
| 6597 | |
| 6598 | #if defined(CONFIG_UNIX) |
| 6599 | ctx->ring_sock->file = file; |
| 6600 | #endif |
| 6601 | fd_install(ret, file); |
| 6602 | return ret; |
| 6603 | err: |
| 6604 | #if defined(CONFIG_UNIX) |
| 6605 | sock_release(ctx->ring_sock); |
| 6606 | ctx->ring_sock = NULL; |
| 6607 | #endif |
| 6608 | return ret; |
| 6609 | } |
| 6610 | |
| 6611 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6612 | { |
| 6613 | struct user_struct *user = NULL; |
| 6614 | struct io_ring_ctx *ctx; |
| 6615 | bool account_mem; |
| 6616 | int ret; |
| 6617 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6618 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6619 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6620 | if (entries > IORING_MAX_ENTRIES) { |
| 6621 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6622 | return -EINVAL; |
| 6623 | entries = IORING_MAX_ENTRIES; |
| 6624 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6625 | |
| 6626 | /* |
| 6627 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6628 | * application to drive a higher depth than the size of the SQ ring, |
| 6629 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6630 | * some flexibility in overcommitting a bit. If the application has |
| 6631 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6632 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6633 | */ |
| 6634 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6635 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6636 | /* |
| 6637 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6638 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6639 | * any cq vs sq ring sizing. |
| 6640 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6641 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6642 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6643 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6644 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6645 | return -EINVAL; |
| 6646 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6647 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6648 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6649 | } else { |
| 6650 | p->cq_entries = 2 * p->sq_entries; |
| 6651 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6652 | |
| 6653 | user = get_uid(current_user()); |
| 6654 | account_mem = !capable(CAP_IPC_LOCK); |
| 6655 | |
| 6656 | if (account_mem) { |
| 6657 | ret = io_account_mem(user, |
| 6658 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6659 | if (ret) { |
| 6660 | free_uid(user); |
| 6661 | return ret; |
| 6662 | } |
| 6663 | } |
| 6664 | |
| 6665 | ctx = io_ring_ctx_alloc(p); |
| 6666 | if (!ctx) { |
| 6667 | if (account_mem) |
| 6668 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6669 | p->cq_entries)); |
| 6670 | free_uid(user); |
| 6671 | return -ENOMEM; |
| 6672 | } |
| 6673 | ctx->compat = in_compat_syscall(); |
| 6674 | ctx->account_mem = account_mem; |
| 6675 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6676 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6677 | |
| 6678 | ret = io_allocate_scq_urings(ctx, p); |
| 6679 | if (ret) |
| 6680 | goto err; |
| 6681 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6682 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6683 | if (ret) |
| 6684 | goto err; |
| 6685 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6686 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6687 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6688 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6689 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6690 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6691 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6692 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6693 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6694 | |
| 6695 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6696 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6697 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6698 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6699 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6700 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6701 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6702 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6703 | /* |
| 6704 | * Install ring fd as the very last thing, so we don't risk someone |
| 6705 | * having closed it before we finish setup |
| 6706 | */ |
| 6707 | ret = io_uring_get_fd(ctx); |
| 6708 | if (ret < 0) |
| 6709 | goto err; |
| 6710 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6711 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 6712 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
| 6713 | IORING_FEAT_CUR_PERSONALITY; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6714 | 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] | 6715 | return ret; |
| 6716 | err: |
| 6717 | io_ring_ctx_wait_and_kill(ctx); |
| 6718 | return ret; |
| 6719 | } |
| 6720 | |
| 6721 | /* |
| 6722 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6723 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6724 | * params structure passed in. |
| 6725 | */ |
| 6726 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6727 | { |
| 6728 | struct io_uring_params p; |
| 6729 | long ret; |
| 6730 | int i; |
| 6731 | |
| 6732 | if (copy_from_user(&p, params, sizeof(p))) |
| 6733 | return -EFAULT; |
| 6734 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6735 | if (p.resv[i]) |
| 6736 | return -EINVAL; |
| 6737 | } |
| 6738 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6739 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6740 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6741 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6742 | return -EINVAL; |
| 6743 | |
| 6744 | ret = io_uring_create(entries, &p); |
| 6745 | if (ret < 0) |
| 6746 | return ret; |
| 6747 | |
| 6748 | if (copy_to_user(params, &p, sizeof(p))) |
| 6749 | return -EFAULT; |
| 6750 | |
| 6751 | return ret; |
| 6752 | } |
| 6753 | |
| 6754 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6755 | struct io_uring_params __user *, params) |
| 6756 | { |
| 6757 | return io_uring_setup(entries, params); |
| 6758 | } |
| 6759 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6760 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 6761 | { |
| 6762 | struct io_uring_probe *p; |
| 6763 | size_t size; |
| 6764 | int i, ret; |
| 6765 | |
| 6766 | size = struct_size(p, ops, nr_args); |
| 6767 | if (size == SIZE_MAX) |
| 6768 | return -EOVERFLOW; |
| 6769 | p = kzalloc(size, GFP_KERNEL); |
| 6770 | if (!p) |
| 6771 | return -ENOMEM; |
| 6772 | |
| 6773 | ret = -EFAULT; |
| 6774 | if (copy_from_user(p, arg, size)) |
| 6775 | goto out; |
| 6776 | ret = -EINVAL; |
| 6777 | if (memchr_inv(p, 0, size)) |
| 6778 | goto out; |
| 6779 | |
| 6780 | p->last_op = IORING_OP_LAST - 1; |
| 6781 | if (nr_args > IORING_OP_LAST) |
| 6782 | nr_args = IORING_OP_LAST; |
| 6783 | |
| 6784 | for (i = 0; i < nr_args; i++) { |
| 6785 | p->ops[i].op = i; |
| 6786 | if (!io_op_defs[i].not_supported) |
| 6787 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 6788 | } |
| 6789 | p->ops_len = i; |
| 6790 | |
| 6791 | ret = 0; |
| 6792 | if (copy_to_user(arg, p, size)) |
| 6793 | ret = -EFAULT; |
| 6794 | out: |
| 6795 | kfree(p); |
| 6796 | return ret; |
| 6797 | } |
| 6798 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6799 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 6800 | { |
| 6801 | const struct cred *creds = get_current_cred(); |
| 6802 | int id; |
| 6803 | |
| 6804 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 6805 | USHRT_MAX, GFP_KERNEL); |
| 6806 | if (id < 0) |
| 6807 | put_cred(creds); |
| 6808 | return id; |
| 6809 | } |
| 6810 | |
| 6811 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 6812 | { |
| 6813 | const struct cred *old_creds; |
| 6814 | |
| 6815 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 6816 | if (old_creds) { |
| 6817 | put_cred(old_creds); |
| 6818 | return 0; |
| 6819 | } |
| 6820 | |
| 6821 | return -EINVAL; |
| 6822 | } |
| 6823 | |
| 6824 | static bool io_register_op_must_quiesce(int op) |
| 6825 | { |
| 6826 | switch (op) { |
| 6827 | case IORING_UNREGISTER_FILES: |
| 6828 | case IORING_REGISTER_FILES_UPDATE: |
| 6829 | case IORING_REGISTER_PROBE: |
| 6830 | case IORING_REGISTER_PERSONALITY: |
| 6831 | case IORING_UNREGISTER_PERSONALITY: |
| 6832 | return false; |
| 6833 | default: |
| 6834 | return true; |
| 6835 | } |
| 6836 | } |
| 6837 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6838 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6839 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6840 | __releases(ctx->uring_lock) |
| 6841 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6842 | { |
| 6843 | int ret; |
| 6844 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6845 | /* |
| 6846 | * We're inside the ring mutex, if the ref is already dying, then |
| 6847 | * someone else killed the ctx or is already going through |
| 6848 | * io_uring_register(). |
| 6849 | */ |
| 6850 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6851 | return -ENXIO; |
| 6852 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6853 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6854 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6855 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6856 | /* |
| 6857 | * Drop uring mutex before waiting for references to exit. If |
| 6858 | * another thread is currently inside io_uring_enter() it might |
| 6859 | * need to grab the uring_lock to make progress. If we hold it |
| 6860 | * here across the drain wait, then we can deadlock. It's safe |
| 6861 | * to drop the mutex here, since no new references will come in |
| 6862 | * after we've killed the percpu ref. |
| 6863 | */ |
| 6864 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6865 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6866 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6867 | if (ret) { |
| 6868 | percpu_ref_resurrect(&ctx->refs); |
| 6869 | ret = -EINTR; |
| 6870 | goto out; |
| 6871 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6872 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6873 | |
| 6874 | switch (opcode) { |
| 6875 | case IORING_REGISTER_BUFFERS: |
| 6876 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6877 | break; |
| 6878 | case IORING_UNREGISTER_BUFFERS: |
| 6879 | ret = -EINVAL; |
| 6880 | if (arg || nr_args) |
| 6881 | break; |
| 6882 | ret = io_sqe_buffer_unregister(ctx); |
| 6883 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6884 | case IORING_REGISTER_FILES: |
| 6885 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6886 | break; |
| 6887 | case IORING_UNREGISTER_FILES: |
| 6888 | ret = -EINVAL; |
| 6889 | if (arg || nr_args) |
| 6890 | break; |
| 6891 | ret = io_sqe_files_unregister(ctx); |
| 6892 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6893 | case IORING_REGISTER_FILES_UPDATE: |
| 6894 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6895 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6896 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6897 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6898 | ret = -EINVAL; |
| 6899 | if (nr_args != 1) |
| 6900 | break; |
| 6901 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6902 | if (ret) |
| 6903 | break; |
| 6904 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 6905 | ctx->eventfd_async = 1; |
| 6906 | else |
| 6907 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6908 | break; |
| 6909 | case IORING_UNREGISTER_EVENTFD: |
| 6910 | ret = -EINVAL; |
| 6911 | if (arg || nr_args) |
| 6912 | break; |
| 6913 | ret = io_eventfd_unregister(ctx); |
| 6914 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6915 | case IORING_REGISTER_PROBE: |
| 6916 | ret = -EINVAL; |
| 6917 | if (!arg || nr_args > 256) |
| 6918 | break; |
| 6919 | ret = io_probe(ctx, arg, nr_args); |
| 6920 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6921 | case IORING_REGISTER_PERSONALITY: |
| 6922 | ret = -EINVAL; |
| 6923 | if (arg || nr_args) |
| 6924 | break; |
| 6925 | ret = io_register_personality(ctx); |
| 6926 | break; |
| 6927 | case IORING_UNREGISTER_PERSONALITY: |
| 6928 | ret = -EINVAL; |
| 6929 | if (arg) |
| 6930 | break; |
| 6931 | ret = io_unregister_personality(ctx, nr_args); |
| 6932 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6933 | default: |
| 6934 | ret = -EINVAL; |
| 6935 | break; |
| 6936 | } |
| 6937 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6938 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6939 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6940 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6941 | out: |
| 6942 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6943 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6944 | return ret; |
| 6945 | } |
| 6946 | |
| 6947 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6948 | void __user *, arg, unsigned int, nr_args) |
| 6949 | { |
| 6950 | struct io_ring_ctx *ctx; |
| 6951 | long ret = -EBADF; |
| 6952 | struct fd f; |
| 6953 | |
| 6954 | f = fdget(fd); |
| 6955 | if (!f.file) |
| 6956 | return -EBADF; |
| 6957 | |
| 6958 | ret = -EOPNOTSUPP; |
| 6959 | if (f.file->f_op != &io_uring_fops) |
| 6960 | goto out_fput; |
| 6961 | |
| 6962 | ctx = f.file->private_data; |
| 6963 | |
| 6964 | mutex_lock(&ctx->uring_lock); |
| 6965 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6966 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6967 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6968 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6969 | out_fput: |
| 6970 | fdput(f); |
| 6971 | return ret; |
| 6972 | } |
| 6973 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6974 | static int __init io_uring_init(void) |
| 6975 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6976 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6977 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6978 | return 0; |
| 6979 | }; |
| 6980 | __initcall(io_uring_init); |