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 | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 78 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 79 | #include <linux/splice.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 80 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 81 | #define CREATE_TRACE_POINTS |
| 82 | #include <trace/events/io_uring.h> |
| 83 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | #include <uapi/linux/io_uring.h> |
| 85 | |
| 86 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 87 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 88 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 89 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 90 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 94 | */ |
| 95 | #define IORING_FILE_TABLE_SHIFT 9 |
| 96 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 97 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 98 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 99 | |
| 100 | struct io_uring { |
| 101 | u32 head ____cacheline_aligned_in_smp; |
| 102 | u32 tail ____cacheline_aligned_in_smp; |
| 103 | }; |
| 104 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 105 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 106 | * This data is shared with the application through the mmap at offsets |
| 107 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 108 | * |
| 109 | * The offsets to the member fields are published through struct |
| 110 | * io_sqring_offsets when calling io_uring_setup. |
| 111 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 112 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 113 | /* |
| 114 | * Head and tail offsets into the ring; the offsets need to be |
| 115 | * masked to get valid indices. |
| 116 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 117 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 118 | * and the application controls tail of the sq ring and the head of the |
| 119 | * cq ring. |
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 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 123 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | * ring_entries - 1) |
| 125 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 126 | u32 sq_ring_mask, cq_ring_mask; |
| 127 | /* Ring sizes (constant, power of 2) */ |
| 128 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 129 | /* |
| 130 | * Number of invalid entries dropped by the kernel due to |
| 131 | * invalid index stored in array |
| 132 | * |
| 133 | * Written by the kernel, shouldn't be modified by the |
| 134 | * application (i.e. get number of "new events" by comparing to |
| 135 | * cached value). |
| 136 | * |
| 137 | * After a new SQ head value was read by the application this |
| 138 | * counter includes all submissions that were dropped reaching |
| 139 | * the new SQ head (and possibly more). |
| 140 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 141 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Runtime flags |
| 144 | * |
| 145 | * Written by the kernel, shouldn't be modified by the |
| 146 | * application. |
| 147 | * |
| 148 | * The application needs a full memory barrier before checking |
| 149 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 150 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 151 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Number of completion events lost because the queue was full; |
| 154 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 155 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 156 | * the completion queue. |
| 157 | * |
| 158 | * Written by the kernel, shouldn't be modified by the |
| 159 | * application (i.e. get number of "new events" by comparing to |
| 160 | * cached value). |
| 161 | * |
| 162 | * As completion events come in out of order this counter is not |
| 163 | * ordered with any other data. |
| 164 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 165 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 166 | /* |
| 167 | * Ring buffer of completion events. |
| 168 | * |
| 169 | * The kernel writes completion events fresh every time they are |
| 170 | * produced, so the application is allowed to modify pending |
| 171 | * entries. |
| 172 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 173 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 176 | struct io_mapped_ubuf { |
| 177 | u64 ubuf; |
| 178 | size_t len; |
| 179 | struct bio_vec *bvec; |
| 180 | unsigned int nr_bvecs; |
| 181 | }; |
| 182 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 183 | struct fixed_file_table { |
| 184 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 187 | struct fixed_file_data { |
| 188 | struct fixed_file_table *table; |
| 189 | struct io_ring_ctx *ctx; |
| 190 | |
| 191 | struct percpu_ref refs; |
| 192 | struct llist_head put_llist; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 193 | struct work_struct ref_work; |
| 194 | struct completion done; |
| 195 | }; |
| 196 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 197 | struct io_ring_ctx { |
| 198 | struct { |
| 199 | struct percpu_ref refs; |
| 200 | } ____cacheline_aligned_in_smp; |
| 201 | |
| 202 | struct { |
| 203 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 204 | unsigned int compat: 1; |
| 205 | unsigned int account_mem: 1; |
| 206 | unsigned int cq_overflow_flushed: 1; |
| 207 | unsigned int drain_next: 1; |
| 208 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 209 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 210 | /* |
| 211 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 212 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 213 | * |
| 214 | * This indirection could e.g. be used to assign fixed |
| 215 | * io_uring_sqe entries to operations and only submit them to |
| 216 | * the queue when needed. |
| 217 | * |
| 218 | * The kernel modifies neither the indices array nor the entries |
| 219 | * array. |
| 220 | */ |
| 221 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 222 | unsigned cached_sq_head; |
| 223 | unsigned sq_entries; |
| 224 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 225 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 226 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 227 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 228 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 229 | |
| 230 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 231 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 232 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 233 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 234 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 235 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 236 | } ____cacheline_aligned_in_smp; |
| 237 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 238 | struct io_rings *rings; |
| 239 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 240 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 241 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 242 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 243 | struct mm_struct *sqo_mm; |
| 244 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 245 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 246 | /* |
| 247 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 248 | * readers must ensure that ->refs is alive as long as the file* is |
| 249 | * used. Only updated through io_uring_register(2). |
| 250 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 251 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 252 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 253 | int ring_fd; |
| 254 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 255 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 256 | /* if used, fixed mapped user buffers */ |
| 257 | unsigned nr_user_bufs; |
| 258 | struct io_mapped_ubuf *user_bufs; |
| 259 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 260 | struct user_struct *user; |
| 261 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 262 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 263 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 264 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 265 | struct completion *completions; |
| 266 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 267 | /* if all else fails... */ |
| 268 | struct io_kiocb *fallback_req; |
| 269 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 270 | #if defined(CONFIG_UNIX) |
| 271 | struct socket *ring_sock; |
| 272 | #endif |
| 273 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 274 | struct idr personality_idr; |
| 275 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 276 | struct { |
| 277 | unsigned cached_cq_tail; |
| 278 | unsigned cq_entries; |
| 279 | unsigned cq_mask; |
| 280 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 281 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 282 | struct wait_queue_head cq_wait; |
| 283 | struct fasync_struct *cq_fasync; |
| 284 | struct eventfd_ctx *cq_ev_fd; |
| 285 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 286 | |
| 287 | struct { |
| 288 | struct mutex uring_lock; |
| 289 | wait_queue_head_t wait; |
| 290 | } ____cacheline_aligned_in_smp; |
| 291 | |
| 292 | struct { |
| 293 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 294 | struct llist_head poll_llist; |
| 295 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 296 | /* |
| 297 | * ->poll_list is protected by the ctx->uring_lock for |
| 298 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 299 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 300 | * manipulate the list, hence no extra locking is needed there. |
| 301 | */ |
| 302 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 303 | struct hlist_head *cancel_hash; |
| 304 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 305 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 306 | |
| 307 | spinlock_t inflight_lock; |
| 308 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 309 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 312 | /* |
| 313 | * First field must be the file pointer in all the |
| 314 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 315 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 316 | struct io_poll_iocb { |
| 317 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 318 | union { |
| 319 | struct wait_queue_head *head; |
| 320 | u64 addr; |
| 321 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 322 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 323 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 324 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 325 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 328 | struct io_close { |
| 329 | struct file *file; |
| 330 | struct file *put_file; |
| 331 | int fd; |
| 332 | }; |
| 333 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 334 | struct io_timeout_data { |
| 335 | struct io_kiocb *req; |
| 336 | struct hrtimer timer; |
| 337 | struct timespec64 ts; |
| 338 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 339 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 342 | struct io_accept { |
| 343 | struct file *file; |
| 344 | struct sockaddr __user *addr; |
| 345 | int __user *addr_len; |
| 346 | int flags; |
| 347 | }; |
| 348 | |
| 349 | struct io_sync { |
| 350 | struct file *file; |
| 351 | loff_t len; |
| 352 | loff_t off; |
| 353 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 354 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 357 | struct io_cancel { |
| 358 | struct file *file; |
| 359 | u64 addr; |
| 360 | }; |
| 361 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 362 | struct io_timeout { |
| 363 | struct file *file; |
| 364 | u64 addr; |
| 365 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 366 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 367 | }; |
| 368 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 369 | struct io_rw { |
| 370 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 371 | struct kiocb kiocb; |
| 372 | u64 addr; |
| 373 | u64 len; |
| 374 | }; |
| 375 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 376 | struct io_connect { |
| 377 | struct file *file; |
| 378 | struct sockaddr __user *addr; |
| 379 | int addr_len; |
| 380 | }; |
| 381 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 382 | struct io_sr_msg { |
| 383 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 384 | union { |
| 385 | struct user_msghdr __user *msg; |
| 386 | void __user *buf; |
| 387 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 388 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 389 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 392 | struct io_open { |
| 393 | struct file *file; |
| 394 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 395 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 396 | unsigned mask; |
| 397 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 398 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 399 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 400 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 403 | struct io_files_update { |
| 404 | struct file *file; |
| 405 | u64 arg; |
| 406 | u32 nr_args; |
| 407 | u32 offset; |
| 408 | }; |
| 409 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 410 | struct io_fadvise { |
| 411 | struct file *file; |
| 412 | u64 offset; |
| 413 | u32 len; |
| 414 | u32 advice; |
| 415 | }; |
| 416 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 417 | struct io_madvise { |
| 418 | struct file *file; |
| 419 | u64 addr; |
| 420 | u32 len; |
| 421 | u32 advice; |
| 422 | }; |
| 423 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 424 | struct io_epoll { |
| 425 | struct file *file; |
| 426 | int epfd; |
| 427 | int op; |
| 428 | int fd; |
| 429 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 430 | }; |
| 431 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 432 | struct io_splice { |
| 433 | struct file *file_out; |
| 434 | struct file *file_in; |
| 435 | loff_t off_out; |
| 436 | loff_t off_in; |
| 437 | u64 len; |
| 438 | unsigned int flags; |
| 439 | }; |
| 440 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 441 | struct io_async_connect { |
| 442 | struct sockaddr_storage address; |
| 443 | }; |
| 444 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 445 | struct io_async_msghdr { |
| 446 | struct iovec fast_iov[UIO_FASTIOV]; |
| 447 | struct iovec *iov; |
| 448 | struct sockaddr __user *uaddr; |
| 449 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 450 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 451 | }; |
| 452 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 453 | struct io_async_rw { |
| 454 | struct iovec fast_iov[UIO_FASTIOV]; |
| 455 | struct iovec *iov; |
| 456 | ssize_t nr_segs; |
| 457 | ssize_t size; |
| 458 | }; |
| 459 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 460 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 461 | union { |
| 462 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 463 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 464 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 465 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 466 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 469 | enum { |
| 470 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 471 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 472 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 473 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 474 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 475 | |
| 476 | REQ_F_LINK_NEXT_BIT, |
| 477 | REQ_F_FAIL_LINK_BIT, |
| 478 | REQ_F_INFLIGHT_BIT, |
| 479 | REQ_F_CUR_POS_BIT, |
| 480 | REQ_F_NOWAIT_BIT, |
| 481 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 482 | REQ_F_LINK_TIMEOUT_BIT, |
| 483 | REQ_F_TIMEOUT_BIT, |
| 484 | REQ_F_ISREG_BIT, |
| 485 | REQ_F_MUST_PUNT_BIT, |
| 486 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 487 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 488 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 489 | REQ_F_OVERFLOW_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | enum { |
| 493 | /* ctx owns file */ |
| 494 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 495 | /* drain existing IO first */ |
| 496 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 497 | /* linked sqes */ |
| 498 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 499 | /* doesn't sever on completion < 0 */ |
| 500 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 501 | /* IOSQE_ASYNC */ |
| 502 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 503 | |
| 504 | /* already grabbed next link */ |
| 505 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 506 | /* fail rest of links */ |
| 507 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 508 | /* on inflight list */ |
| 509 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 510 | /* read/write uses file position */ |
| 511 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 512 | /* must not punt to workers */ |
| 513 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 514 | /* polled IO has completed */ |
| 515 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 516 | /* has linked timeout */ |
| 517 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 518 | /* timeout request */ |
| 519 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 520 | /* regular file */ |
| 521 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 522 | /* must be punted even for NONBLOCK */ |
| 523 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 524 | /* no timeout sequence */ |
| 525 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 526 | /* completion under lock */ |
| 527 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 528 | /* needs cleanup */ |
| 529 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 530 | /* in overflow list */ |
| 531 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 532 | }; |
| 533 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 534 | /* |
| 535 | * NOTE! Each of the iocb union members has the file pointer |
| 536 | * as the first entry in their struct definition. So you can |
| 537 | * access the file pointer through any of the sub-structs, |
| 538 | * or directly as just 'ki_filp' in this struct. |
| 539 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 540 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 541 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 542 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 543 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 544 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 545 | struct io_accept accept; |
| 546 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 547 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 548 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 549 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 550 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 551 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 552 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 553 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 554 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 555 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 556 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 557 | struct io_splice splice; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 558 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 559 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 560 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 561 | /* |
| 562 | * llist_node is only used for poll deferred completions |
| 563 | */ |
| 564 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 565 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 566 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 567 | |
| 568 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 569 | union { |
| 570 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 571 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 572 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 573 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 574 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 575 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 576 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 577 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 578 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 579 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 580 | struct list_head inflight_entry; |
| 581 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 582 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 586 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 587 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 588 | struct io_submit_state { |
| 589 | struct blk_plug plug; |
| 590 | |
| 591 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 592 | * io_kiocb alloc cache |
| 593 | */ |
| 594 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 595 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 596 | |
| 597 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 598 | * File reference cache |
| 599 | */ |
| 600 | struct file *file; |
| 601 | unsigned int fd; |
| 602 | unsigned int has_refs; |
| 603 | unsigned int used_refs; |
| 604 | unsigned int ios_left; |
| 605 | }; |
| 606 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 607 | struct io_op_def { |
| 608 | /* needs req->io allocated for deferral/async */ |
| 609 | unsigned async_ctx : 1; |
| 610 | /* needs current->mm setup, does mm access */ |
| 611 | unsigned needs_mm : 1; |
| 612 | /* needs req->file assigned */ |
| 613 | unsigned needs_file : 1; |
| 614 | /* needs req->file assigned IFF fd is >= 0 */ |
| 615 | unsigned fd_non_neg : 1; |
| 616 | /* hash wq insertion if file is a regular file */ |
| 617 | unsigned hash_reg_file : 1; |
| 618 | /* unbound wq insertion if file is a non-regular file */ |
| 619 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 620 | /* opcode is not supported by this kernel */ |
| 621 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 622 | /* needs file table */ |
| 623 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 624 | /* needs ->fs */ |
| 625 | unsigned needs_fs : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 629 | [IORING_OP_NOP] = {}, |
| 630 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 631 | .async_ctx = 1, |
| 632 | .needs_mm = 1, |
| 633 | .needs_file = 1, |
| 634 | .unbound_nonreg_file = 1, |
| 635 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 636 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 637 | .async_ctx = 1, |
| 638 | .needs_mm = 1, |
| 639 | .needs_file = 1, |
| 640 | .hash_reg_file = 1, |
| 641 | .unbound_nonreg_file = 1, |
| 642 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 643 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 644 | .needs_file = 1, |
| 645 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 646 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 647 | .needs_file = 1, |
| 648 | .unbound_nonreg_file = 1, |
| 649 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 650 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 651 | .needs_file = 1, |
| 652 | .hash_reg_file = 1, |
| 653 | .unbound_nonreg_file = 1, |
| 654 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 655 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 656 | .needs_file = 1, |
| 657 | .unbound_nonreg_file = 1, |
| 658 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 659 | [IORING_OP_POLL_REMOVE] = {}, |
| 660 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 661 | .needs_file = 1, |
| 662 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 663 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 664 | .async_ctx = 1, |
| 665 | .needs_mm = 1, |
| 666 | .needs_file = 1, |
| 667 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 668 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 669 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 670 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 671 | .async_ctx = 1, |
| 672 | .needs_mm = 1, |
| 673 | .needs_file = 1, |
| 674 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 675 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 676 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 677 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 678 | .async_ctx = 1, |
| 679 | .needs_mm = 1, |
| 680 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 681 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 682 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 683 | .needs_mm = 1, |
| 684 | .needs_file = 1, |
| 685 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 686 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 687 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 688 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 689 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 690 | .async_ctx = 1, |
| 691 | .needs_mm = 1, |
| 692 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 693 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 694 | .async_ctx = 1, |
| 695 | .needs_mm = 1, |
| 696 | .needs_file = 1, |
| 697 | .unbound_nonreg_file = 1, |
| 698 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 699 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 700 | .needs_file = 1, |
| 701 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 702 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 703 | .needs_file = 1, |
| 704 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 705 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 706 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 707 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 708 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 709 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 710 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 711 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 712 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 713 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 714 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 715 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 716 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 717 | .needs_mm = 1, |
| 718 | .needs_file = 1, |
| 719 | .fd_non_neg = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 720 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 721 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 722 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 723 | .needs_mm = 1, |
| 724 | .needs_file = 1, |
| 725 | .unbound_nonreg_file = 1, |
| 726 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 727 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 728 | .needs_mm = 1, |
| 729 | .needs_file = 1, |
| 730 | .unbound_nonreg_file = 1, |
| 731 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 732 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 733 | .needs_file = 1, |
| 734 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 735 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 736 | .needs_mm = 1, |
| 737 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 738 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 739 | .needs_mm = 1, |
| 740 | .needs_file = 1, |
| 741 | .unbound_nonreg_file = 1, |
| 742 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 743 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 744 | .needs_mm = 1, |
| 745 | .needs_file = 1, |
| 746 | .unbound_nonreg_file = 1, |
| 747 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 748 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 749 | .needs_file = 1, |
| 750 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 751 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 752 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 753 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 754 | [IORING_OP_EPOLL_CTL] = { |
| 755 | .unbound_nonreg_file = 1, |
| 756 | .file_table = 1, |
| 757 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 758 | [IORING_OP_SPLICE] = { |
| 759 | .needs_file = 1, |
| 760 | .hash_reg_file = 1, |
| 761 | .unbound_nonreg_file = 1, |
| 762 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 763 | }; |
| 764 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 765 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 766 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 767 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 768 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 769 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 770 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 771 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 772 | struct io_uring_files_update *ip, |
| 773 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 774 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 775 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 776 | static void io_cleanup_req(struct io_kiocb *req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 777 | static int io_file_get(struct io_submit_state *state, |
| 778 | struct io_kiocb *req, |
| 779 | int fd, struct file **out_file, |
| 780 | bool fixed); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 781 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 782 | static struct kmem_cache *req_cachep; |
| 783 | |
| 784 | static const struct file_operations io_uring_fops; |
| 785 | |
| 786 | struct sock *io_uring_get_socket(struct file *file) |
| 787 | { |
| 788 | #if defined(CONFIG_UNIX) |
| 789 | if (file->f_op == &io_uring_fops) { |
| 790 | struct io_ring_ctx *ctx = file->private_data; |
| 791 | |
| 792 | return ctx->ring_sock->sk; |
| 793 | } |
| 794 | #endif |
| 795 | return NULL; |
| 796 | } |
| 797 | EXPORT_SYMBOL(io_uring_get_socket); |
| 798 | |
| 799 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 800 | { |
| 801 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 802 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 803 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 807 | { |
| 808 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 809 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 810 | |
| 811 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 812 | if (!ctx) |
| 813 | return NULL; |
| 814 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 815 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 816 | if (!ctx->fallback_req) |
| 817 | goto err; |
| 818 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 819 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 820 | if (!ctx->completions) |
| 821 | goto err; |
| 822 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 823 | /* |
| 824 | * Use 5 bits less than the max cq entries, that should give us around |
| 825 | * 32 entries per hash list if totally full and uniformly spread. |
| 826 | */ |
| 827 | hash_bits = ilog2(p->cq_entries); |
| 828 | hash_bits -= 5; |
| 829 | if (hash_bits <= 0) |
| 830 | hash_bits = 1; |
| 831 | ctx->cancel_hash_bits = hash_bits; |
| 832 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 833 | GFP_KERNEL); |
| 834 | if (!ctx->cancel_hash) |
| 835 | goto err; |
| 836 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 837 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 838 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 839 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 840 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 841 | |
| 842 | ctx->flags = p->flags; |
| 843 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 844 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 845 | init_completion(&ctx->completions[0]); |
| 846 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 847 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 848 | mutex_init(&ctx->uring_lock); |
| 849 | init_waitqueue_head(&ctx->wait); |
| 850 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 851 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 852 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 853 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 854 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 855 | init_waitqueue_head(&ctx->inflight_wait); |
| 856 | spin_lock_init(&ctx->inflight_lock); |
| 857 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 858 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 859 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 860 | if (ctx->fallback_req) |
| 861 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 862 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 863 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 864 | kfree(ctx); |
| 865 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 868 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 869 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 870 | struct io_ring_ctx *ctx = req->ctx; |
| 871 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 872 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 873 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 874 | } |
| 875 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 876 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 877 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 878 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 879 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 880 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 881 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | 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] | 885 | { |
| 886 | struct io_kiocb *req; |
| 887 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 888 | 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] | 889 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 890 | list_del_init(&req->list); |
| 891 | return req; |
| 892 | } |
| 893 | |
| 894 | return NULL; |
| 895 | } |
| 896 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 897 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 898 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 899 | struct io_kiocb *req; |
| 900 | |
| 901 | 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] | 902 | if (req) { |
| 903 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 904 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 905 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 906 | list_del_init(&req->list); |
| 907 | return req; |
| 908 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 912 | } |
| 913 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 914 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 915 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 916 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 917 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 918 | /* order cqe stores with ring update */ |
| 919 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 920 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 921 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 922 | wake_up_interruptible(&ctx->cq_wait); |
| 923 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 927 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 928 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 929 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 930 | if (!req->work.mm && def->needs_mm) { |
| 931 | mmgrab(current->mm); |
| 932 | req->work.mm = current->mm; |
| 933 | } |
| 934 | if (!req->work.creds) |
| 935 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 936 | if (!req->work.fs && def->needs_fs) { |
| 937 | spin_lock(¤t->fs->lock); |
| 938 | if (!current->fs->in_exec) { |
| 939 | req->work.fs = current->fs; |
| 940 | req->work.fs->users++; |
| 941 | } else { |
| 942 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 943 | } |
| 944 | spin_unlock(¤t->fs->lock); |
| 945 | } |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 946 | if (!req->work.task_pid) |
| 947 | req->work.task_pid = task_pid_vnr(current); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 951 | { |
| 952 | if (req->work.mm) { |
| 953 | mmdrop(req->work.mm); |
| 954 | req->work.mm = NULL; |
| 955 | } |
| 956 | if (req->work.creds) { |
| 957 | put_cred(req->work.creds); |
| 958 | req->work.creds = NULL; |
| 959 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 960 | if (req->work.fs) { |
| 961 | struct fs_struct *fs = req->work.fs; |
| 962 | |
| 963 | spin_lock(&req->work.fs->lock); |
| 964 | if (--fs->users) |
| 965 | fs = NULL; |
| 966 | spin_unlock(&req->work.fs->lock); |
| 967 | if (fs) |
| 968 | free_fs_struct(fs); |
| 969 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 970 | } |
| 971 | |
Pavel Begunkov | deb6dc0 | 2020-02-24 11:30:17 +0300 | [diff] [blame] | 972 | static inline void io_prep_next_work(struct io_kiocb *req, |
| 973 | struct io_kiocb **link) |
| 974 | { |
| 975 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 976 | |
| 977 | if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file) |
| 978 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 979 | |
| 980 | *link = io_prep_linked_timeout(req); |
| 981 | } |
| 982 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 983 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 984 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 985 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 986 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 987 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 988 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 989 | if (req->flags & REQ_F_ISREG) { |
| 990 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 991 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 992 | } else { |
| 993 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 994 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 995 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 996 | |
| 997 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 998 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 999 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1000 | return do_hashed; |
| 1001 | } |
| 1002 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1003 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1004 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1005 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1006 | struct io_kiocb *link; |
| 1007 | bool do_hashed; |
| 1008 | |
| 1009 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1010 | |
| 1011 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 1012 | req->flags); |
| 1013 | if (!do_hashed) { |
| 1014 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 1015 | } else { |
| 1016 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 1017 | file_inode(req->file)); |
| 1018 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1019 | |
| 1020 | if (link) |
| 1021 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1022 | } |
| 1023 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1024 | static void io_kill_timeout(struct io_kiocb *req) |
| 1025 | { |
| 1026 | int ret; |
| 1027 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1028 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1029 | if (ret != -1) { |
| 1030 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1031 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1032 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1033 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1038 | { |
| 1039 | struct io_kiocb *req, *tmp; |
| 1040 | |
| 1041 | spin_lock_irq(&ctx->completion_lock); |
| 1042 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1043 | io_kill_timeout(req); |
| 1044 | spin_unlock_irq(&ctx->completion_lock); |
| 1045 | } |
| 1046 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1047 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1048 | { |
| 1049 | struct io_kiocb *req; |
| 1050 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1051 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 1052 | io_kill_timeout(req); |
| 1053 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1054 | __io_commit_cqring(ctx); |
| 1055 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1056 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1057 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1058 | } |
| 1059 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1060 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1061 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1062 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1063 | unsigned tail; |
| 1064 | |
| 1065 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1066 | /* |
| 1067 | * writes to the cq entry need to come after reading head; the |
| 1068 | * control dependency is enough as we're using WRITE_ONCE to |
| 1069 | * fill the cq entry |
| 1070 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1071 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1072 | return NULL; |
| 1073 | |
| 1074 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1075 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1078 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1079 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1080 | if (!ctx->cq_ev_fd) |
| 1081 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1082 | if (!ctx->eventfd_async) |
| 1083 | return true; |
| 1084 | return io_wq_current_is_worker() || in_interrupt(); |
| 1085 | } |
| 1086 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1087 | static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1088 | { |
| 1089 | if (waitqueue_active(&ctx->wait)) |
| 1090 | wake_up(&ctx->wait); |
| 1091 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1092 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1093 | if (trigger_ev) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1094 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1095 | } |
| 1096 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1097 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1098 | { |
| 1099 | __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx)); |
| 1100 | } |
| 1101 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1102 | /* Returns true if there are no backlogged entries after the flush */ |
| 1103 | 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] | 1104 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1105 | struct io_rings *rings = ctx->rings; |
| 1106 | struct io_uring_cqe *cqe; |
| 1107 | struct io_kiocb *req; |
| 1108 | unsigned long flags; |
| 1109 | LIST_HEAD(list); |
| 1110 | |
| 1111 | if (!force) { |
| 1112 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1113 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1114 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1115 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1116 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1120 | |
| 1121 | /* if force is set, the ring is going away. always drop after that */ |
| 1122 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1123 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1124 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1125 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1126 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1127 | cqe = io_get_cqring(ctx); |
| 1128 | if (!cqe && !force) |
| 1129 | break; |
| 1130 | |
| 1131 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1132 | list); |
| 1133 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1134 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1135 | if (cqe) { |
| 1136 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1137 | WRITE_ONCE(cqe->res, req->result); |
| 1138 | WRITE_ONCE(cqe->flags, 0); |
| 1139 | } else { |
| 1140 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1141 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1146 | if (cqe) { |
| 1147 | clear_bit(0, &ctx->sq_check_overflow); |
| 1148 | clear_bit(0, &ctx->cq_check_overflow); |
| 1149 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1150 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1151 | io_cqring_ev_posted(ctx); |
| 1152 | |
| 1153 | while (!list_empty(&list)) { |
| 1154 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1155 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1156 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1157 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1158 | |
| 1159 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1162 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1163 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1164 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1165 | struct io_uring_cqe *cqe; |
| 1166 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1167 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1168 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1169 | /* |
| 1170 | * If we can't get a cq entry, userspace overflowed the |
| 1171 | * submission (by quite a lot). Increment the overflow count in |
| 1172 | * the ring. |
| 1173 | */ |
| 1174 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1175 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1176 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1177 | WRITE_ONCE(cqe->res, res); |
| 1178 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1179 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1180 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1181 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1182 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1183 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1184 | set_bit(0, &ctx->sq_check_overflow); |
| 1185 | set_bit(0, &ctx->cq_check_overflow); |
| 1186 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1187 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1188 | refcount_inc(&req->refs); |
| 1189 | req->result = res; |
| 1190 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1194 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1195 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1196 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1197 | unsigned long flags; |
| 1198 | |
| 1199 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1200 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1201 | io_commit_cqring(ctx); |
| 1202 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1203 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1204 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1207 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1208 | { |
| 1209 | return req == (struct io_kiocb *) |
| 1210 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1211 | } |
| 1212 | |
| 1213 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1214 | { |
| 1215 | struct io_kiocb *req; |
| 1216 | |
| 1217 | req = ctx->fallback_req; |
| 1218 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1219 | return req; |
| 1220 | |
| 1221 | return NULL; |
| 1222 | } |
| 1223 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1224 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1225 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1226 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1227 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1228 | struct io_kiocb *req; |
| 1229 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1230 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1231 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1232 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1233 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1234 | } else if (!state->free_reqs) { |
| 1235 | size_t sz; |
| 1236 | int ret; |
| 1237 | |
| 1238 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1239 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1240 | |
| 1241 | /* |
| 1242 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1243 | * retry single alloc to be on the safe side. |
| 1244 | */ |
| 1245 | if (unlikely(ret <= 0)) { |
| 1246 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1247 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1248 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1249 | ret = 1; |
| 1250 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1251 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1252 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1253 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1254 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1255 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1258 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1259 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1260 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1261 | req->ctx = ctx; |
| 1262 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1263 | /* one is dropped after submission, the other at completion */ |
| 1264 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1265 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1266 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1267 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1268 | fallback: |
| 1269 | req = io_get_fallback_req(ctx); |
| 1270 | if (req) |
| 1271 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1272 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1273 | return NULL; |
| 1274 | } |
| 1275 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1276 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1277 | bool fixed) |
| 1278 | { |
| 1279 | if (fixed) |
| 1280 | percpu_ref_put(&req->ctx->file_data->refs); |
| 1281 | else |
| 1282 | fput(file); |
| 1283 | } |
| 1284 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1285 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1286 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1287 | if (likely(!io_is_fallback_req(req))) |
| 1288 | kmem_cache_free(req_cachep, req); |
| 1289 | else |
| 1290 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1291 | } |
| 1292 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1293 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1294 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1295 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1296 | io_cleanup_req(req); |
| 1297 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1298 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1299 | if (req->file) |
| 1300 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1301 | |
| 1302 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static void __io_free_req(struct io_kiocb *req) |
| 1306 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1307 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1308 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1309 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1310 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1311 | unsigned long flags; |
| 1312 | |
| 1313 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1314 | list_del(&req->inflight_entry); |
| 1315 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1316 | wake_up(&ctx->inflight_wait); |
| 1317 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1318 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1319 | |
| 1320 | percpu_ref_put(&req->ctx->refs); |
| 1321 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1322 | } |
| 1323 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1324 | struct req_batch { |
| 1325 | void *reqs[IO_IOPOLL_BATCH]; |
| 1326 | int to_free; |
| 1327 | int need_iter; |
| 1328 | }; |
| 1329 | |
| 1330 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1331 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1332 | int fixed_refs = rb->to_free; |
| 1333 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1334 | if (!rb->to_free) |
| 1335 | return; |
| 1336 | if (rb->need_iter) { |
| 1337 | int i, inflight = 0; |
| 1338 | unsigned long flags; |
| 1339 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1340 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1341 | for (i = 0; i < rb->to_free; i++) { |
| 1342 | struct io_kiocb *req = rb->reqs[i]; |
| 1343 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1344 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1345 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1346 | fixed_refs++; |
| 1347 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1348 | if (req->flags & REQ_F_INFLIGHT) |
| 1349 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1350 | __io_req_aux_free(req); |
| 1351 | } |
| 1352 | if (!inflight) |
| 1353 | goto do_free; |
| 1354 | |
| 1355 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1356 | for (i = 0; i < rb->to_free; i++) { |
| 1357 | struct io_kiocb *req = rb->reqs[i]; |
| 1358 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1359 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1360 | list_del(&req->inflight_entry); |
| 1361 | if (!--inflight) |
| 1362 | break; |
| 1363 | } |
| 1364 | } |
| 1365 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1366 | |
| 1367 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1368 | wake_up(&ctx->inflight_wait); |
| 1369 | } |
| 1370 | do_free: |
| 1371 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1372 | if (fixed_refs) |
| 1373 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1374 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1375 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1376 | } |
| 1377 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1378 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1379 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1380 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1381 | int ret; |
| 1382 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1383 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1384 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1385 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1386 | io_commit_cqring(ctx); |
| 1387 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1388 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1389 | return true; |
| 1390 | } |
| 1391 | |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1395 | 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] | 1396 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1397 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1398 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1399 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1400 | /* Already got next link */ |
| 1401 | if (req->flags & REQ_F_LINK_NEXT) |
| 1402 | return; |
| 1403 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1404 | /* |
| 1405 | * The list should never be empty when we are called here. But could |
| 1406 | * potentially happen if the chain is messed up, check to be on the |
| 1407 | * safe side. |
| 1408 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1409 | while (!list_empty(&req->link_list)) { |
| 1410 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1411 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1412 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1413 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1414 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1415 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1416 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1417 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1418 | continue; |
| 1419 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1420 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1421 | list_del_init(&req->link_list); |
| 1422 | if (!list_empty(&nxt->link_list)) |
| 1423 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1424 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1425 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1426 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1427 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1428 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1429 | if (wake_ev) |
| 1430 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1435 | */ |
| 1436 | static void io_fail_links(struct io_kiocb *req) |
| 1437 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1438 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1439 | unsigned long flags; |
| 1440 | |
| 1441 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1442 | |
| 1443 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1444 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1445 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1446 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1447 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1448 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1449 | |
| 1450 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1451 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1452 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1453 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1454 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1455 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1456 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1457 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1458 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1459 | |
| 1460 | io_commit_cqring(ctx); |
| 1461 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1462 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1463 | } |
| 1464 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1465 | 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] | 1466 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1467 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1468 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1469 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1470 | /* |
| 1471 | * If LINK is set, we have dependent requests in this chain. If we |
| 1472 | * didn't fail this request, queue the first one up, moving any other |
| 1473 | * dependencies to the next request. In case of failure, fail the rest |
| 1474 | * of the chain. |
| 1475 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1476 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1477 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1478 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1479 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1480 | struct io_ring_ctx *ctx = req->ctx; |
| 1481 | unsigned long flags; |
| 1482 | |
| 1483 | /* |
| 1484 | * If this is a timeout link, we could be racing with the |
| 1485 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1486 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1487 | */ |
| 1488 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1489 | io_req_link_next(req, nxt); |
| 1490 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1491 | } else { |
| 1492 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1493 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1494 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1495 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1496 | static void io_free_req(struct io_kiocb *req) |
| 1497 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1498 | struct io_kiocb *nxt = NULL; |
| 1499 | |
| 1500 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1501 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1502 | |
| 1503 | if (nxt) |
| 1504 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1505 | } |
| 1506 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1507 | /* |
| 1508 | * Drop reference to request, return next in chain (if there is one) if this |
| 1509 | * was the last reference to this request. |
| 1510 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1511 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1512 | 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] | 1513 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1514 | if (refcount_dec_and_test(&req->refs)) { |
| 1515 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1516 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1517 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1520 | static void io_put_req(struct io_kiocb *req) |
| 1521 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1522 | if (refcount_dec_and_test(&req->refs)) |
| 1523 | io_free_req(req); |
| 1524 | } |
| 1525 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1526 | /* |
| 1527 | * Must only be used if we don't need to care about links, usually from |
| 1528 | * within the completion handling itself. |
| 1529 | */ |
| 1530 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1531 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1532 | /* drop both submit and complete references */ |
| 1533 | if (refcount_sub_and_test(2, &req->refs)) |
| 1534 | __io_free_req(req); |
| 1535 | } |
| 1536 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1537 | static void io_double_put_req(struct io_kiocb *req) |
| 1538 | { |
| 1539 | /* drop both submit and complete references */ |
| 1540 | if (refcount_sub_and_test(2, &req->refs)) |
| 1541 | io_free_req(req); |
| 1542 | } |
| 1543 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1544 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1545 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1546 | struct io_rings *rings = ctx->rings; |
| 1547 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1548 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1549 | /* |
| 1550 | * noflush == true is from the waitqueue handler, just ensure |
| 1551 | * we wake up the task, and the next invocation will flush the |
| 1552 | * entries. We cannot safely to it from here. |
| 1553 | */ |
| 1554 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1555 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1556 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1557 | io_cqring_overflow_flush(ctx, false); |
| 1558 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1559 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1560 | /* See comment at the top of this file */ |
| 1561 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1562 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1563 | } |
| 1564 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1565 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1566 | { |
| 1567 | struct io_rings *rings = ctx->rings; |
| 1568 | |
| 1569 | /* make sure SQ entry isn't read before tail */ |
| 1570 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1571 | } |
| 1572 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1573 | 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] | 1574 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1575 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1576 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1577 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1578 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1579 | rb->need_iter++; |
| 1580 | |
| 1581 | rb->reqs[rb->to_free++] = req; |
| 1582 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1583 | io_free_req_many(req->ctx, rb); |
| 1584 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1587 | /* |
| 1588 | * Find and free completed poll iocbs |
| 1589 | */ |
| 1590 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1591 | struct list_head *done) |
| 1592 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1593 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1594 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1595 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1596 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1597 | while (!list_empty(done)) { |
| 1598 | req = list_first_entry(done, struct io_kiocb, list); |
| 1599 | list_del(&req->list); |
| 1600 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1601 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1602 | (*nr_events)++; |
| 1603 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1604 | if (refcount_dec_and_test(&req->refs) && |
| 1605 | !io_req_multi_free(&rb, req)) |
| 1606 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1607 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1608 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1609 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1610 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1614 | long min) |
| 1615 | { |
| 1616 | struct io_kiocb *req, *tmp; |
| 1617 | LIST_HEAD(done); |
| 1618 | bool spin; |
| 1619 | int ret; |
| 1620 | |
| 1621 | /* |
| 1622 | * Only spin for completions if we don't have multiple devices hanging |
| 1623 | * off our complete list, and we're under the requested amount. |
| 1624 | */ |
| 1625 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1626 | |
| 1627 | ret = 0; |
| 1628 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1629 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1630 | |
| 1631 | /* |
| 1632 | * Move completed entries to our local list. If we find a |
| 1633 | * request that requires polling, break out and complete |
| 1634 | * the done list first, if we have entries there. |
| 1635 | */ |
| 1636 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1637 | list_move_tail(&req->list, &done); |
| 1638 | continue; |
| 1639 | } |
| 1640 | if (!list_empty(&done)) |
| 1641 | break; |
| 1642 | |
| 1643 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1644 | if (ret < 0) |
| 1645 | break; |
| 1646 | |
| 1647 | if (ret && spin) |
| 1648 | spin = false; |
| 1649 | ret = 0; |
| 1650 | } |
| 1651 | |
| 1652 | if (!list_empty(&done)) |
| 1653 | io_iopoll_complete(ctx, nr_events, &done); |
| 1654 | |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
| 1658 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1659 | * 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] | 1660 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1661 | * as a non-spinning completion check. |
| 1662 | */ |
| 1663 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1664 | long min) |
| 1665 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1666 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1667 | int ret; |
| 1668 | |
| 1669 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1670 | if (ret < 0) |
| 1671 | return ret; |
| 1672 | if (!min || *nr_events >= min) |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * We can't just wait for polled events to come to us, we have to actively |
| 1681 | * find and complete them. |
| 1682 | */ |
| 1683 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1684 | { |
| 1685 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1686 | return; |
| 1687 | |
| 1688 | mutex_lock(&ctx->uring_lock); |
| 1689 | while (!list_empty(&ctx->poll_list)) { |
| 1690 | unsigned int nr_events = 0; |
| 1691 | |
| 1692 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1696 | * in this case we need to ensure that we reap all events. |
| 1697 | */ |
| 1698 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1699 | } |
| 1700 | mutex_unlock(&ctx->uring_lock); |
| 1701 | } |
| 1702 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1703 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1704 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1705 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1706 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1707 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1708 | /* |
| 1709 | * We disallow the app entering submit/complete with polling, but we |
| 1710 | * still need to lock the ring to prevent racing with polled issue |
| 1711 | * that got punted to a workqueue. |
| 1712 | */ |
| 1713 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1714 | do { |
| 1715 | int tmin = 0; |
| 1716 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1717 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1718 | * Don't enter poll loop if we already have events pending. |
| 1719 | * If we do, we can potentially be spinning for commands that |
| 1720 | * already triggered a CQE (eg in error). |
| 1721 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1722 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1723 | break; |
| 1724 | |
| 1725 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1726 | * If a submit got punted to a workqueue, we can have the |
| 1727 | * application entering polling for a command before it gets |
| 1728 | * issued. That app will hold the uring_lock for the duration |
| 1729 | * of the poll right here, so we need to take a breather every |
| 1730 | * now and then to ensure that the issue has a chance to add |
| 1731 | * the poll to the issued list. Otherwise we can spin here |
| 1732 | * forever, while the workqueue is stuck trying to acquire the |
| 1733 | * very same mutex. |
| 1734 | */ |
| 1735 | if (!(++iters & 7)) { |
| 1736 | mutex_unlock(&ctx->uring_lock); |
| 1737 | mutex_lock(&ctx->uring_lock); |
| 1738 | } |
| 1739 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1740 | if (*nr_events < min) |
| 1741 | tmin = min - *nr_events; |
| 1742 | |
| 1743 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1744 | if (ret <= 0) |
| 1745 | break; |
| 1746 | ret = 0; |
| 1747 | } while (min && !*nr_events && !need_resched()); |
| 1748 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1749 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1750 | return ret; |
| 1751 | } |
| 1752 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1753 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1754 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1755 | /* |
| 1756 | * Tell lockdep we inherited freeze protection from submission |
| 1757 | * thread. |
| 1758 | */ |
| 1759 | if (req->flags & REQ_F_ISREG) { |
| 1760 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1761 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1762 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1763 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1764 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1767 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1768 | { |
| 1769 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1770 | req->flags |= REQ_F_FAIL_LINK; |
| 1771 | } |
| 1772 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1773 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1774 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1775 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1776 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1777 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1778 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1779 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1780 | if (res != req->result) |
| 1781 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1782 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1786 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1787 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1788 | |
| 1789 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1790 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1793 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1794 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1795 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1796 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1797 | |
| 1798 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1799 | io_put_req_find_next(req, &nxt); |
| 1800 | |
| 1801 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1804 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1805 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1806 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1807 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1808 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1809 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1810 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1811 | if (res != req->result) |
| 1812 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1813 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1814 | if (res != -EAGAIN) |
| 1815 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1816 | } |
| 1817 | |
| 1818 | /* |
| 1819 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1820 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1821 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1822 | * accessing the kiocb cookie. |
| 1823 | */ |
| 1824 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1825 | { |
| 1826 | struct io_ring_ctx *ctx = req->ctx; |
| 1827 | |
| 1828 | /* |
| 1829 | * Track whether we have multiple files in our lists. This will impact |
| 1830 | * how we do polling eventually, not spinning if we're on potentially |
| 1831 | * different devices. |
| 1832 | */ |
| 1833 | if (list_empty(&ctx->poll_list)) { |
| 1834 | ctx->poll_multi_file = false; |
| 1835 | } else if (!ctx->poll_multi_file) { |
| 1836 | struct io_kiocb *list_req; |
| 1837 | |
| 1838 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1839 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1840 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1841 | ctx->poll_multi_file = true; |
| 1842 | } |
| 1843 | |
| 1844 | /* |
| 1845 | * For fast devices, IO may have already completed. If it has, add |
| 1846 | * it to the front so we find it first. |
| 1847 | */ |
| 1848 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1849 | list_add(&req->list, &ctx->poll_list); |
| 1850 | else |
| 1851 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 1852 | |
| 1853 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 1854 | wq_has_sleeper(&ctx->sqo_wait)) |
| 1855 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1858 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1859 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1860 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1861 | int diff = state->has_refs - state->used_refs; |
| 1862 | |
| 1863 | if (diff) |
| 1864 | fput_many(state->file, diff); |
| 1865 | state->file = NULL; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * Get as many references to a file as we have IOs left in this submission, |
| 1871 | * assuming most submissions are for one file, or at least that each file |
| 1872 | * has more than one submission. |
| 1873 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1874 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1875 | { |
| 1876 | if (!state) |
| 1877 | return fget(fd); |
| 1878 | |
| 1879 | if (state->file) { |
| 1880 | if (state->fd == fd) { |
| 1881 | state->used_refs++; |
| 1882 | state->ios_left--; |
| 1883 | return state->file; |
| 1884 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1885 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1886 | } |
| 1887 | state->file = fget_many(fd, state->ios_left); |
| 1888 | if (!state->file) |
| 1889 | return NULL; |
| 1890 | |
| 1891 | state->fd = fd; |
| 1892 | state->has_refs = state->ios_left; |
| 1893 | state->used_refs = 1; |
| 1894 | state->ios_left--; |
| 1895 | return state->file; |
| 1896 | } |
| 1897 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1898 | /* |
| 1899 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1900 | * any file. For now, just ensure that anything potentially problematic is done |
| 1901 | * inline. |
| 1902 | */ |
| 1903 | static bool io_file_supports_async(struct file *file) |
| 1904 | { |
| 1905 | umode_t mode = file_inode(file)->i_mode; |
| 1906 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1907 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1908 | return true; |
| 1909 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1910 | return true; |
| 1911 | |
| 1912 | return false; |
| 1913 | } |
| 1914 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1915 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1916 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1917 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1918 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1919 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1920 | unsigned ioprio; |
| 1921 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1922 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1923 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1924 | req->flags |= REQ_F_ISREG; |
| 1925 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1926 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1927 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1928 | req->flags |= REQ_F_CUR_POS; |
| 1929 | kiocb->ki_pos = req->file->f_pos; |
| 1930 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1931 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 1932 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1933 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1934 | if (unlikely(ret)) |
| 1935 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1936 | |
| 1937 | ioprio = READ_ONCE(sqe->ioprio); |
| 1938 | if (ioprio) { |
| 1939 | ret = ioprio_check_cap(ioprio); |
| 1940 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1941 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1942 | |
| 1943 | kiocb->ki_ioprio = ioprio; |
| 1944 | } else |
| 1945 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1946 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1947 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1948 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1949 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1950 | req->flags |= REQ_F_NOWAIT; |
| 1951 | |
| 1952 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1953 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1954 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1955 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1956 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1957 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1958 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1959 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1960 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1961 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1962 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1963 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1964 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1965 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1966 | kiocb->ki_complete = io_complete_rw; |
| 1967 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1968 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1969 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1970 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1971 | /* we own ->private, reuse it for the buffer index */ |
| 1972 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1973 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1974 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1978 | { |
| 1979 | switch (ret) { |
| 1980 | case -EIOCBQUEUED: |
| 1981 | break; |
| 1982 | case -ERESTARTSYS: |
| 1983 | case -ERESTARTNOINTR: |
| 1984 | case -ERESTARTNOHAND: |
| 1985 | case -ERESTART_RESTARTBLOCK: |
| 1986 | /* |
| 1987 | * We can't just restart the syscall, since previously |
| 1988 | * submitted sqes may already be in progress. Just fail this |
| 1989 | * IO with EINTR. |
| 1990 | */ |
| 1991 | ret = -EINTR; |
| 1992 | /* fall through */ |
| 1993 | default: |
| 1994 | kiocb->ki_complete(kiocb, ret, 0); |
| 1995 | } |
| 1996 | } |
| 1997 | |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 1998 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1999 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2000 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2001 | |
| 2002 | if (req->flags & REQ_F_CUR_POS) |
| 2003 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2004 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2005 | *nxt = __io_complete_rw(kiocb, ret); |
| 2006 | else |
| 2007 | io_rw_done(kiocb, ret); |
| 2008 | } |
| 2009 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2010 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2011 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2012 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2013 | struct io_ring_ctx *ctx = req->ctx; |
| 2014 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2015 | struct io_mapped_ubuf *imu; |
| 2016 | unsigned index, buf_index; |
| 2017 | size_t offset; |
| 2018 | u64 buf_addr; |
| 2019 | |
| 2020 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2021 | if (unlikely(!ctx->user_bufs)) |
| 2022 | return -EFAULT; |
| 2023 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2024 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2025 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2026 | return -EFAULT; |
| 2027 | |
| 2028 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2029 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2030 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2031 | |
| 2032 | /* overflow */ |
| 2033 | if (buf_addr + len < buf_addr) |
| 2034 | return -EFAULT; |
| 2035 | /* not inside the mapped region */ |
| 2036 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2037 | return -EFAULT; |
| 2038 | |
| 2039 | /* |
| 2040 | * May not be a start of buffer, set size appropriately |
| 2041 | * and advance us to the beginning. |
| 2042 | */ |
| 2043 | offset = buf_addr - imu->ubuf; |
| 2044 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2045 | |
| 2046 | if (offset) { |
| 2047 | /* |
| 2048 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2049 | * using the latter parts of a big fixed buffer - it iterates |
| 2050 | * over each segment manually. We can cheat a bit here, because |
| 2051 | * we know that: |
| 2052 | * |
| 2053 | * 1) it's a BVEC iter, we set it up |
| 2054 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2055 | * first and last bvec |
| 2056 | * |
| 2057 | * So just find our index, and adjust the iterator afterwards. |
| 2058 | * If the offset is within the first bvec (or the whole first |
| 2059 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2060 | * since we can just skip the first segment, which may not |
| 2061 | * be PAGE_SIZE aligned. |
| 2062 | */ |
| 2063 | const struct bio_vec *bvec = imu->bvec; |
| 2064 | |
| 2065 | if (offset <= bvec->bv_len) { |
| 2066 | iov_iter_advance(iter, offset); |
| 2067 | } else { |
| 2068 | unsigned long seg_skip; |
| 2069 | |
| 2070 | /* skip first vec */ |
| 2071 | offset -= bvec->bv_len; |
| 2072 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2073 | |
| 2074 | iter->bvec = bvec + seg_skip; |
| 2075 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2076 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2077 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2081 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2084 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2085 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2086 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2087 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2088 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2089 | u8 opcode; |
| 2090 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2091 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2092 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2093 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2094 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2095 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2096 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2097 | /* buffer index only valid with fixed read/write */ |
| 2098 | if (req->rw.kiocb.private) |
| 2099 | return -EINVAL; |
| 2100 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2101 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2102 | ssize_t ret; |
| 2103 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2104 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2105 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2108 | if (req->io) { |
| 2109 | struct io_async_rw *iorw = &req->io->rw; |
| 2110 | |
| 2111 | *iovec = iorw->iov; |
| 2112 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2113 | if (iorw->iov == iorw->fast_iov) |
| 2114 | *iovec = NULL; |
| 2115 | return iorw->size; |
| 2116 | } |
| 2117 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2118 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2119 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2120 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2121 | iovec, iter); |
| 2122 | #endif |
| 2123 | |
| 2124 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2125 | } |
| 2126 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2127 | /* |
| 2128 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2129 | * by looping over ->read() or ->write() manually. |
| 2130 | */ |
| 2131 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2132 | struct iov_iter *iter) |
| 2133 | { |
| 2134 | ssize_t ret = 0; |
| 2135 | |
| 2136 | /* |
| 2137 | * Don't support polled IO through this interface, and we can't |
| 2138 | * support non-blocking either. For the latter, this just causes |
| 2139 | * the kiocb to be handled from an async context. |
| 2140 | */ |
| 2141 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2142 | return -EOPNOTSUPP; |
| 2143 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2144 | return -EAGAIN; |
| 2145 | |
| 2146 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2147 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2148 | ssize_t nr; |
| 2149 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2150 | if (!iov_iter_is_bvec(iter)) { |
| 2151 | iovec = iov_iter_iovec(iter); |
| 2152 | } else { |
| 2153 | /* fixed buffers import bvec */ |
| 2154 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2155 | + iter->iov_offset; |
| 2156 | iovec.iov_len = min(iter->count, |
| 2157 | iter->bvec->bv_len - iter->iov_offset); |
| 2158 | } |
| 2159 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2160 | if (rw == READ) { |
| 2161 | nr = file->f_op->read(file, iovec.iov_base, |
| 2162 | iovec.iov_len, &kiocb->ki_pos); |
| 2163 | } else { |
| 2164 | nr = file->f_op->write(file, iovec.iov_base, |
| 2165 | iovec.iov_len, &kiocb->ki_pos); |
| 2166 | } |
| 2167 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2168 | if (iov_iter_is_bvec(iter)) |
| 2169 | kunmap(iter->bvec->bv_page); |
| 2170 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2171 | if (nr < 0) { |
| 2172 | if (!ret) |
| 2173 | ret = nr; |
| 2174 | break; |
| 2175 | } |
| 2176 | ret += nr; |
| 2177 | if (nr != iovec.iov_len) |
| 2178 | break; |
| 2179 | iov_iter_advance(iter, nr); |
| 2180 | } |
| 2181 | |
| 2182 | return ret; |
| 2183 | } |
| 2184 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2185 | 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] | 2186 | struct iovec *iovec, struct iovec *fast_iov, |
| 2187 | struct iov_iter *iter) |
| 2188 | { |
| 2189 | req->io->rw.nr_segs = iter->nr_segs; |
| 2190 | req->io->rw.size = io_size; |
| 2191 | req->io->rw.iov = iovec; |
| 2192 | if (!req->io->rw.iov) { |
| 2193 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2194 | memcpy(req->io->rw.iov, fast_iov, |
| 2195 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2196 | } else { |
| 2197 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2201 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2202 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2203 | if (!io_op_defs[req->opcode].async_ctx) |
| 2204 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2205 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2206 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2209 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2210 | struct iovec *iovec, struct iovec *fast_iov, |
| 2211 | struct iov_iter *iter) |
| 2212 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2213 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2214 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2215 | if (!req->io) { |
| 2216 | if (io_alloc_async_ctx(req)) |
| 2217 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2218 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2219 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2220 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2221 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2224 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2225 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2226 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2227 | struct io_async_ctx *io; |
| 2228 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2229 | ssize_t ret; |
| 2230 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2231 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2232 | if (ret) |
| 2233 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2234 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2235 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2236 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2237 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2238 | /* either don't need iovec imported or already have it */ |
| 2239 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2240 | return 0; |
| 2241 | |
| 2242 | io = req->io; |
| 2243 | io->rw.iov = io->rw.fast_iov; |
| 2244 | req->io = NULL; |
| 2245 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2246 | req->io = io; |
| 2247 | if (ret < 0) |
| 2248 | return ret; |
| 2249 | |
| 2250 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2251 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2254 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2255 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2256 | { |
| 2257 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2258 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2259 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2260 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2261 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2262 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2263 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2264 | if (ret < 0) |
| 2265 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2266 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2267 | /* Ensure we clear previously set non-block flag */ |
| 2268 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2269 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2270 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2271 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2272 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2273 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2274 | req->result = io_size; |
| 2275 | |
| 2276 | /* |
| 2277 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2278 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2279 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2280 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2281 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2282 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2283 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2284 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2285 | if (!ret) { |
| 2286 | ssize_t ret2; |
| 2287 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2288 | if (req->file->f_op->read_iter) |
| 2289 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2290 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2291 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2292 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2293 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2294 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2295 | kiocb_done(kiocb, ret2, nxt); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2296 | } else { |
| 2297 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2298 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2299 | inline_vecs, &iter); |
| 2300 | if (ret) |
| 2301 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2302 | /* any defer here is final, must blocking retry */ |
| 2303 | if (!(req->flags & REQ_F_NOWAIT)) |
| 2304 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2305 | return -EAGAIN; |
| 2306 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2307 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2308 | out_free: |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2309 | kfree(iovec); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2310 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2311 | return ret; |
| 2312 | } |
| 2313 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2314 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2315 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2316 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2317 | struct io_async_ctx *io; |
| 2318 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2319 | ssize_t ret; |
| 2320 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2321 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2322 | if (ret) |
| 2323 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2324 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2325 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2326 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2327 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2328 | /* either don't need iovec imported or already have it */ |
| 2329 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2330 | return 0; |
| 2331 | |
| 2332 | io = req->io; |
| 2333 | io->rw.iov = io->rw.fast_iov; |
| 2334 | req->io = NULL; |
| 2335 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2336 | req->io = io; |
| 2337 | if (ret < 0) |
| 2338 | return ret; |
| 2339 | |
| 2340 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2341 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2344 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2345 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2346 | { |
| 2347 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2348 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2349 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2350 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2351 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2352 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2353 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2354 | if (ret < 0) |
| 2355 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2356 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2357 | /* Ensure we clear previously set non-block flag */ |
| 2358 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2359 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2360 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2361 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2362 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2363 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2364 | req->result = io_size; |
| 2365 | |
| 2366 | /* |
| 2367 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2368 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2369 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2370 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2371 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2372 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2373 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2374 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2375 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2376 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2377 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2378 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2379 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2380 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2381 | ssize_t ret2; |
| 2382 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2383 | /* |
| 2384 | * Open-code file_start_write here to grab freeze protection, |
| 2385 | * which will be released by another thread in |
| 2386 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2387 | * released so that it doesn't complain about the held lock when |
| 2388 | * we return to userspace. |
| 2389 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2390 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2391 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2392 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2393 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2394 | SB_FREEZE_WRITE); |
| 2395 | } |
| 2396 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2397 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2398 | if (req->file->f_op->write_iter) |
| 2399 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2400 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2401 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 2402 | /* |
| 2403 | * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 2404 | * retry them without IOCB_NOWAIT. |
| 2405 | */ |
| 2406 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 2407 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2408 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2409 | kiocb_done(kiocb, ret2, nxt); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2410 | } else { |
| 2411 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2412 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2413 | inline_vecs, &iter); |
| 2414 | if (ret) |
| 2415 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2416 | /* any defer here is final, must blocking retry */ |
| 2417 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2418 | return -EAGAIN; |
| 2419 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2420 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2421 | out_free: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2422 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2423 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2424 | return ret; |
| 2425 | } |
| 2426 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2427 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2428 | { |
| 2429 | struct io_splice* sp = &req->splice; |
| 2430 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 2431 | int ret; |
| 2432 | |
| 2433 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2434 | return 0; |
| 2435 | |
| 2436 | sp->file_in = NULL; |
| 2437 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 2438 | sp->off_out = READ_ONCE(sqe->off); |
| 2439 | sp->len = READ_ONCE(sqe->len); |
| 2440 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 2441 | |
| 2442 | if (unlikely(sp->flags & ~valid_flags)) |
| 2443 | return -EINVAL; |
| 2444 | |
| 2445 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 2446 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2447 | if (ret) |
| 2448 | return ret; |
| 2449 | req->flags |= REQ_F_NEED_CLEANUP; |
| 2450 | |
| 2451 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) |
| 2452 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 2453 | |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
| 2457 | static bool io_splice_punt(struct file *file) |
| 2458 | { |
| 2459 | if (get_pipe_info(file)) |
| 2460 | return false; |
| 2461 | if (!io_file_supports_async(file)) |
| 2462 | return true; |
| 2463 | return !(file->f_mode & O_NONBLOCK); |
| 2464 | } |
| 2465 | |
| 2466 | static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2467 | bool force_nonblock) |
| 2468 | { |
| 2469 | struct io_splice *sp = &req->splice; |
| 2470 | struct file *in = sp->file_in; |
| 2471 | struct file *out = sp->file_out; |
| 2472 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 2473 | loff_t *poff_in, *poff_out; |
| 2474 | long ret; |
| 2475 | |
| 2476 | if (force_nonblock) { |
| 2477 | if (io_splice_punt(in) || io_splice_punt(out)) |
| 2478 | return -EAGAIN; |
| 2479 | flags |= SPLICE_F_NONBLOCK; |
| 2480 | } |
| 2481 | |
| 2482 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 2483 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
| 2484 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
| 2485 | if (force_nonblock && ret == -EAGAIN) |
| 2486 | return -EAGAIN; |
| 2487 | |
| 2488 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2489 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 2490 | |
| 2491 | io_cqring_add_event(req, ret); |
| 2492 | if (ret != sp->len) |
| 2493 | req_set_fail_links(req); |
| 2494 | io_put_req_find_next(req, nxt); |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2498 | /* |
| 2499 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2500 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2501 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2502 | { |
| 2503 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2504 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2505 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2506 | return -EINVAL; |
| 2507 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2508 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2509 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2510 | return 0; |
| 2511 | } |
| 2512 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2513 | 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] | 2514 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2515 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2516 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2517 | if (!req->file) |
| 2518 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2519 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2520 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2521 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2522 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2523 | return -EINVAL; |
| 2524 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2525 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2526 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2527 | return -EINVAL; |
| 2528 | |
| 2529 | req->sync.off = READ_ONCE(sqe->off); |
| 2530 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2531 | return 0; |
| 2532 | } |
| 2533 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2534 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2535 | { |
| 2536 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2537 | req_set_fail_links(req); |
| 2538 | io_cqring_add_event(req, -ECANCELED); |
| 2539 | io_put_req(req); |
| 2540 | return true; |
| 2541 | } |
| 2542 | |
| 2543 | return false; |
| 2544 | } |
| 2545 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2546 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2547 | { |
| 2548 | struct io_wq_work *work = *workptr; |
| 2549 | struct io_kiocb *link = work->data; |
| 2550 | |
| 2551 | io_queue_linked_timeout(link); |
| 2552 | work->func = io_wq_submit_work; |
| 2553 | } |
| 2554 | |
| 2555 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2556 | { |
| 2557 | struct io_kiocb *link; |
| 2558 | |
Pavel Begunkov | deb6dc0 | 2020-02-24 11:30:17 +0300 | [diff] [blame] | 2559 | io_prep_next_work(nxt, &link); |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2560 | *workptr = &nxt->work; |
| 2561 | if (link) { |
| 2562 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2563 | nxt->work.func = io_link_work_cb; |
| 2564 | nxt->work.data = link; |
| 2565 | } |
| 2566 | } |
| 2567 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2568 | static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2569 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2570 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2571 | int ret; |
| 2572 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2573 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2574 | end > 0 ? end : LLONG_MAX, |
| 2575 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2576 | if (ret < 0) |
| 2577 | req_set_fail_links(req); |
| 2578 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2579 | io_put_req_find_next(req, nxt); |
| 2580 | } |
| 2581 | |
| 2582 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2583 | { |
| 2584 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2585 | struct io_kiocb *nxt = NULL; |
| 2586 | |
| 2587 | if (io_req_cancelled(req)) |
| 2588 | return; |
| 2589 | __io_fsync(req, &nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2590 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2591 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2594 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2595 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2596 | { |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2597 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2598 | if (force_nonblock) { |
| 2599 | io_put_req(req); |
| 2600 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2601 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2602 | } |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2603 | __io_fsync(req, nxt); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2604 | return 0; |
| 2605 | } |
| 2606 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2607 | static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2608 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2609 | int ret; |
| 2610 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 2611 | if (io_req_cancelled(req)) |
| 2612 | return; |
| 2613 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2614 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2615 | req->sync.len); |
| 2616 | if (ret < 0) |
| 2617 | req_set_fail_links(req); |
| 2618 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2619 | io_put_req_find_next(req, nxt); |
| 2620 | } |
| 2621 | |
| 2622 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2623 | { |
| 2624 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2625 | struct io_kiocb *nxt = NULL; |
| 2626 | |
| 2627 | __io_fallocate(req, &nxt); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2628 | if (nxt) |
| 2629 | io_wq_assign_next(workptr, nxt); |
| 2630 | } |
| 2631 | |
| 2632 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2633 | const struct io_uring_sqe *sqe) |
| 2634 | { |
| 2635 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2636 | return -EINVAL; |
| 2637 | |
| 2638 | req->sync.off = READ_ONCE(sqe->off); |
| 2639 | req->sync.len = READ_ONCE(sqe->addr); |
| 2640 | req->sync.mode = READ_ONCE(sqe->len); |
| 2641 | return 0; |
| 2642 | } |
| 2643 | |
| 2644 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2645 | bool force_nonblock) |
| 2646 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2647 | /* fallocate always requiring blocking context */ |
| 2648 | if (force_nonblock) { |
| 2649 | io_put_req(req); |
| 2650 | req->work.func = io_fallocate_finish; |
| 2651 | return -EAGAIN; |
| 2652 | } |
| 2653 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2654 | __io_fallocate(req, nxt); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2655 | return 0; |
| 2656 | } |
| 2657 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2658 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2659 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2660 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2661 | int ret; |
| 2662 | |
| 2663 | if (sqe->ioprio || sqe->buf_index) |
| 2664 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2665 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2666 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2667 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2668 | return 0; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2669 | |
| 2670 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2671 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2672 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2673 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2674 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2675 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2676 | if (IS_ERR(req->open.filename)) { |
| 2677 | ret = PTR_ERR(req->open.filename); |
| 2678 | req->open.filename = NULL; |
| 2679 | return ret; |
| 2680 | } |
| 2681 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2682 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2683 | return 0; |
| 2684 | } |
| 2685 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2686 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2687 | { |
| 2688 | struct open_how __user *how; |
| 2689 | const char __user *fname; |
| 2690 | size_t len; |
| 2691 | int ret; |
| 2692 | |
| 2693 | if (sqe->ioprio || sqe->buf_index) |
| 2694 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2695 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2696 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2697 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2698 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2699 | |
| 2700 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2701 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2702 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2703 | len = READ_ONCE(sqe->len); |
| 2704 | |
| 2705 | if (len < OPEN_HOW_SIZE_VER0) |
| 2706 | return -EINVAL; |
| 2707 | |
| 2708 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2709 | len); |
| 2710 | if (ret) |
| 2711 | return ret; |
| 2712 | |
| 2713 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2714 | req->open.how.flags |= O_LARGEFILE; |
| 2715 | |
| 2716 | req->open.filename = getname(fname); |
| 2717 | if (IS_ERR(req->open.filename)) { |
| 2718 | ret = PTR_ERR(req->open.filename); |
| 2719 | req->open.filename = NULL; |
| 2720 | return ret; |
| 2721 | } |
| 2722 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2723 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2724 | return 0; |
| 2725 | } |
| 2726 | |
| 2727 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2728 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2729 | { |
| 2730 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2731 | struct file *file; |
| 2732 | int ret; |
| 2733 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2734 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2735 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2736 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2737 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2738 | if (ret) |
| 2739 | goto err; |
| 2740 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2741 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2742 | if (ret < 0) |
| 2743 | goto err; |
| 2744 | |
| 2745 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2746 | if (IS_ERR(file)) { |
| 2747 | put_unused_fd(ret); |
| 2748 | ret = PTR_ERR(file); |
| 2749 | } else { |
| 2750 | fsnotify_open(file); |
| 2751 | fd_install(ret, file); |
| 2752 | } |
| 2753 | err: |
| 2754 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2755 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2756 | if (ret < 0) |
| 2757 | req_set_fail_links(req); |
| 2758 | io_cqring_add_event(req, ret); |
| 2759 | io_put_req_find_next(req, nxt); |
| 2760 | return 0; |
| 2761 | } |
| 2762 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2763 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2764 | bool force_nonblock) |
| 2765 | { |
| 2766 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2767 | return io_openat2(req, nxt, force_nonblock); |
| 2768 | } |
| 2769 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 2770 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 2771 | const struct io_uring_sqe *sqe) |
| 2772 | { |
| 2773 | #if defined(CONFIG_EPOLL) |
| 2774 | if (sqe->ioprio || sqe->buf_index) |
| 2775 | return -EINVAL; |
| 2776 | |
| 2777 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 2778 | req->epoll.op = READ_ONCE(sqe->len); |
| 2779 | req->epoll.fd = READ_ONCE(sqe->off); |
| 2780 | |
| 2781 | if (ep_op_has_event(req->epoll.op)) { |
| 2782 | struct epoll_event __user *ev; |
| 2783 | |
| 2784 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2785 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 2786 | return -EFAULT; |
| 2787 | } |
| 2788 | |
| 2789 | return 0; |
| 2790 | #else |
| 2791 | return -EOPNOTSUPP; |
| 2792 | #endif |
| 2793 | } |
| 2794 | |
| 2795 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2796 | bool force_nonblock) |
| 2797 | { |
| 2798 | #if defined(CONFIG_EPOLL) |
| 2799 | struct io_epoll *ie = &req->epoll; |
| 2800 | int ret; |
| 2801 | |
| 2802 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 2803 | if (force_nonblock && ret == -EAGAIN) |
| 2804 | return -EAGAIN; |
| 2805 | |
| 2806 | if (ret < 0) |
| 2807 | req_set_fail_links(req); |
| 2808 | io_cqring_add_event(req, ret); |
| 2809 | io_put_req_find_next(req, nxt); |
| 2810 | return 0; |
| 2811 | #else |
| 2812 | return -EOPNOTSUPP; |
| 2813 | #endif |
| 2814 | } |
| 2815 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2816 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2817 | { |
| 2818 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2819 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2820 | return -EINVAL; |
| 2821 | |
| 2822 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2823 | req->madvise.len = READ_ONCE(sqe->len); |
| 2824 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2825 | return 0; |
| 2826 | #else |
| 2827 | return -EOPNOTSUPP; |
| 2828 | #endif |
| 2829 | } |
| 2830 | |
| 2831 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2832 | bool force_nonblock) |
| 2833 | { |
| 2834 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2835 | struct io_madvise *ma = &req->madvise; |
| 2836 | int ret; |
| 2837 | |
| 2838 | if (force_nonblock) |
| 2839 | return -EAGAIN; |
| 2840 | |
| 2841 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2842 | if (ret < 0) |
| 2843 | req_set_fail_links(req); |
| 2844 | io_cqring_add_event(req, ret); |
| 2845 | io_put_req_find_next(req, nxt); |
| 2846 | return 0; |
| 2847 | #else |
| 2848 | return -EOPNOTSUPP; |
| 2849 | #endif |
| 2850 | } |
| 2851 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2852 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2853 | { |
| 2854 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2855 | return -EINVAL; |
| 2856 | |
| 2857 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2858 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2859 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2860 | return 0; |
| 2861 | } |
| 2862 | |
| 2863 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2864 | bool force_nonblock) |
| 2865 | { |
| 2866 | struct io_fadvise *fa = &req->fadvise; |
| 2867 | int ret; |
| 2868 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 2869 | if (force_nonblock) { |
| 2870 | switch (fa->advice) { |
| 2871 | case POSIX_FADV_NORMAL: |
| 2872 | case POSIX_FADV_RANDOM: |
| 2873 | case POSIX_FADV_SEQUENTIAL: |
| 2874 | break; |
| 2875 | default: |
| 2876 | return -EAGAIN; |
| 2877 | } |
| 2878 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2879 | |
| 2880 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2881 | if (ret < 0) |
| 2882 | req_set_fail_links(req); |
| 2883 | io_cqring_add_event(req, ret); |
| 2884 | io_put_req_find_next(req, nxt); |
| 2885 | return 0; |
| 2886 | } |
| 2887 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2888 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2889 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2890 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2891 | unsigned lookup_flags; |
| 2892 | int ret; |
| 2893 | |
| 2894 | if (sqe->ioprio || sqe->buf_index) |
| 2895 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2896 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2897 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2898 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2899 | return 0; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2900 | |
| 2901 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2902 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2903 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2904 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2905 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2906 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2907 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2908 | return -EINVAL; |
| 2909 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2910 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2911 | if (IS_ERR(req->open.filename)) { |
| 2912 | ret = PTR_ERR(req->open.filename); |
| 2913 | req->open.filename = NULL; |
| 2914 | return ret; |
| 2915 | } |
| 2916 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2917 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2918 | return 0; |
| 2919 | } |
| 2920 | |
| 2921 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2922 | bool force_nonblock) |
| 2923 | { |
| 2924 | struct io_open *ctx = &req->open; |
| 2925 | unsigned lookup_flags; |
| 2926 | struct path path; |
| 2927 | struct kstat stat; |
| 2928 | int ret; |
| 2929 | |
| 2930 | if (force_nonblock) |
| 2931 | return -EAGAIN; |
| 2932 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2933 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2934 | return -EINVAL; |
| 2935 | |
| 2936 | retry: |
| 2937 | /* filename_lookup() drops it, keep a reference */ |
| 2938 | ctx->filename->refcnt++; |
| 2939 | |
| 2940 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2941 | NULL); |
| 2942 | if (ret) |
| 2943 | goto err; |
| 2944 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2945 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2946 | path_put(&path); |
| 2947 | if (retry_estale(ret, lookup_flags)) { |
| 2948 | lookup_flags |= LOOKUP_REVAL; |
| 2949 | goto retry; |
| 2950 | } |
| 2951 | if (!ret) |
| 2952 | ret = cp_statx(&stat, ctx->buffer); |
| 2953 | err: |
| 2954 | putname(ctx->filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2955 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2956 | if (ret < 0) |
| 2957 | req_set_fail_links(req); |
| 2958 | io_cqring_add_event(req, ret); |
| 2959 | io_put_req_find_next(req, nxt); |
| 2960 | return 0; |
| 2961 | } |
| 2962 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2963 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2964 | { |
| 2965 | /* |
| 2966 | * If we queue this for async, it must not be cancellable. That would |
| 2967 | * leave the 'file' in an undeterminate state. |
| 2968 | */ |
| 2969 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2970 | |
| 2971 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2972 | sqe->rw_flags || sqe->buf_index) |
| 2973 | return -EINVAL; |
| 2974 | if (sqe->flags & IOSQE_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2975 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2976 | |
| 2977 | req->close.fd = READ_ONCE(sqe->fd); |
| 2978 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2979 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2980 | return -EBADF; |
| 2981 | |
| 2982 | return 0; |
| 2983 | } |
| 2984 | |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 2985 | /* only called when __close_fd_get_file() is done */ |
| 2986 | static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt) |
| 2987 | { |
| 2988 | int ret; |
| 2989 | |
| 2990 | ret = filp_close(req->close.put_file, req->work.files); |
| 2991 | if (ret < 0) |
| 2992 | req_set_fail_links(req); |
| 2993 | io_cqring_add_event(req, ret); |
| 2994 | fput(req->close.put_file); |
| 2995 | io_put_req_find_next(req, nxt); |
| 2996 | } |
| 2997 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2998 | static void io_close_finish(struct io_wq_work **workptr) |
| 2999 | { |
| 3000 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3001 | struct io_kiocb *nxt = NULL; |
| 3002 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 3003 | /* not cancellable, don't do io_req_cancelled() */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3004 | __io_close_finish(req, &nxt); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3005 | if (nxt) |
| 3006 | io_wq_assign_next(workptr, nxt); |
| 3007 | } |
| 3008 | |
| 3009 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3010 | bool force_nonblock) |
| 3011 | { |
| 3012 | int ret; |
| 3013 | |
| 3014 | req->close.put_file = NULL; |
| 3015 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 3016 | if (ret < 0) |
| 3017 | return ret; |
| 3018 | |
| 3019 | /* 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] | 3020 | 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] | 3021 | goto eagain; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3022 | |
| 3023 | /* |
| 3024 | * No ->flush(), safely close from here and just punt the |
| 3025 | * fput() to async context. |
| 3026 | */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3027 | __io_close_finish(req, nxt); |
| 3028 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3029 | eagain: |
| 3030 | req->work.func = io_close_finish; |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 3031 | /* |
| 3032 | * Do manual async queue here to avoid grabbing files - we don't |
| 3033 | * need the files, and it'll cause io_close_finish() to close |
| 3034 | * the file again and cause a double CQE entry for this request |
| 3035 | */ |
| 3036 | io_queue_async_work(req); |
| 3037 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3040 | 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] | 3041 | { |
| 3042 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3043 | |
| 3044 | if (!req->file) |
| 3045 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3046 | |
| 3047 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3048 | return -EINVAL; |
| 3049 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3050 | return -EINVAL; |
| 3051 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3052 | req->sync.off = READ_ONCE(sqe->off); |
| 3053 | req->sync.len = READ_ONCE(sqe->len); |
| 3054 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3055 | return 0; |
| 3056 | } |
| 3057 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3058 | static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3059 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3060 | int ret; |
| 3061 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3062 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3063 | req->sync.flags); |
| 3064 | if (ret < 0) |
| 3065 | req_set_fail_links(req); |
| 3066 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3067 | io_put_req_find_next(req, nxt); |
| 3068 | } |
| 3069 | |
| 3070 | |
| 3071 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 3072 | { |
| 3073 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3074 | struct io_kiocb *nxt = NULL; |
| 3075 | |
| 3076 | if (io_req_cancelled(req)) |
| 3077 | return; |
| 3078 | __io_sync_file_range(req, &nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3079 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3080 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3081 | } |
| 3082 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3083 | 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] | 3084 | bool force_nonblock) |
| 3085 | { |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3086 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3087 | if (force_nonblock) { |
| 3088 | io_put_req(req); |
| 3089 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3090 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3091 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3092 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3093 | __io_sync_file_range(req, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3094 | return 0; |
| 3095 | } |
| 3096 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame^] | 3097 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3098 | struct io_async_msghdr *kmsg) |
| 3099 | { |
| 3100 | if (req->io) |
| 3101 | return -EAGAIN; |
| 3102 | if (io_alloc_async_ctx(req)) { |
| 3103 | if (kmsg->iov != kmsg->fast_iov) |
| 3104 | kfree(kmsg->iov); |
| 3105 | return -ENOMEM; |
| 3106 | } |
| 3107 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3108 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3109 | return -EAGAIN; |
| 3110 | } |
| 3111 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3112 | 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] | 3113 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3114 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3115 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3116 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3117 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3118 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3119 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3120 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3121 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3122 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3123 | #ifdef CONFIG_COMPAT |
| 3124 | if (req->ctx->compat) |
| 3125 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3126 | #endif |
| 3127 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3128 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3129 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3130 | /* iovec is already imported */ |
| 3131 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3132 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3133 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3134 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3135 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3136 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3137 | if (!ret) |
| 3138 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3139 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3140 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3141 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3142 | #endif |
| 3143 | } |
| 3144 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3145 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3146 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3147 | { |
| 3148 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3149 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3150 | struct socket *sock; |
| 3151 | int ret; |
| 3152 | |
| 3153 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3154 | return -EINVAL; |
| 3155 | |
| 3156 | sock = sock_from_file(req->file, &ret); |
| 3157 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3158 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3159 | unsigned flags; |
| 3160 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3161 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3162 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3163 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3164 | /* if iov is set, it's allocated already */ |
| 3165 | if (!kmsg->iov) |
| 3166 | kmsg->iov = kmsg->fast_iov; |
| 3167 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3168 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3169 | struct io_sr_msg *sr = &req->sr_msg; |
| 3170 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3171 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3172 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3173 | |
| 3174 | io.msg.iov = io.msg.fast_iov; |
| 3175 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3176 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3177 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3178 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3179 | } |
| 3180 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3181 | flags = req->sr_msg.msg_flags; |
| 3182 | if (flags & MSG_DONTWAIT) |
| 3183 | req->flags |= REQ_F_NOWAIT; |
| 3184 | else if (force_nonblock) |
| 3185 | flags |= MSG_DONTWAIT; |
| 3186 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3187 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame^] | 3188 | if (force_nonblock && ret == -EAGAIN) |
| 3189 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3190 | if (ret == -ERESTARTSYS) |
| 3191 | ret = -EINTR; |
| 3192 | } |
| 3193 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3194 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3195 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3196 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3197 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3198 | if (ret < 0) |
| 3199 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3200 | io_put_req_find_next(req, nxt); |
| 3201 | return 0; |
| 3202 | #else |
| 3203 | return -EOPNOTSUPP; |
| 3204 | #endif |
| 3205 | } |
| 3206 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3207 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3208 | bool force_nonblock) |
| 3209 | { |
| 3210 | #if defined(CONFIG_NET) |
| 3211 | struct socket *sock; |
| 3212 | int ret; |
| 3213 | |
| 3214 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3215 | return -EINVAL; |
| 3216 | |
| 3217 | sock = sock_from_file(req->file, &ret); |
| 3218 | if (sock) { |
| 3219 | struct io_sr_msg *sr = &req->sr_msg; |
| 3220 | struct msghdr msg; |
| 3221 | struct iovec iov; |
| 3222 | unsigned flags; |
| 3223 | |
| 3224 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3225 | &msg.msg_iter); |
| 3226 | if (ret) |
| 3227 | return ret; |
| 3228 | |
| 3229 | msg.msg_name = NULL; |
| 3230 | msg.msg_control = NULL; |
| 3231 | msg.msg_controllen = 0; |
| 3232 | msg.msg_namelen = 0; |
| 3233 | |
| 3234 | flags = req->sr_msg.msg_flags; |
| 3235 | if (flags & MSG_DONTWAIT) |
| 3236 | req->flags |= REQ_F_NOWAIT; |
| 3237 | else if (force_nonblock) |
| 3238 | flags |= MSG_DONTWAIT; |
| 3239 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3240 | msg.msg_flags = flags; |
| 3241 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3242 | if (force_nonblock && ret == -EAGAIN) |
| 3243 | return -EAGAIN; |
| 3244 | if (ret == -ERESTARTSYS) |
| 3245 | ret = -EINTR; |
| 3246 | } |
| 3247 | |
| 3248 | io_cqring_add_event(req, ret); |
| 3249 | if (ret < 0) |
| 3250 | req_set_fail_links(req); |
| 3251 | io_put_req_find_next(req, nxt); |
| 3252 | return 0; |
| 3253 | #else |
| 3254 | return -EOPNOTSUPP; |
| 3255 | #endif |
| 3256 | } |
| 3257 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3258 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3259 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3260 | { |
| 3261 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3262 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3263 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3264 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3265 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3266 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3267 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3268 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3269 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3270 | #ifdef CONFIG_COMPAT |
| 3271 | if (req->ctx->compat) |
| 3272 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3273 | #endif |
| 3274 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3275 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3276 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3277 | /* iovec is already imported */ |
| 3278 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3279 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3280 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3281 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3282 | ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3283 | &io->msg.uaddr, &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3284 | if (!ret) |
| 3285 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3286 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3287 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3288 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3289 | #endif |
| 3290 | } |
| 3291 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3292 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3293 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3294 | { |
| 3295 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3296 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3297 | struct socket *sock; |
| 3298 | int ret; |
| 3299 | |
| 3300 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3301 | return -EINVAL; |
| 3302 | |
| 3303 | sock = sock_from_file(req->file, &ret); |
| 3304 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3305 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3306 | unsigned flags; |
| 3307 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3308 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3309 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3310 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3311 | /* if iov is set, it's allocated already */ |
| 3312 | if (!kmsg->iov) |
| 3313 | kmsg->iov = kmsg->fast_iov; |
| 3314 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3315 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3316 | struct io_sr_msg *sr = &req->sr_msg; |
| 3317 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3318 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3319 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3320 | |
| 3321 | io.msg.iov = io.msg.fast_iov; |
| 3322 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3323 | sr->msg_flags, &io.msg.uaddr, |
| 3324 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3325 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3326 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3327 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3328 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3329 | flags = req->sr_msg.msg_flags; |
| 3330 | if (flags & MSG_DONTWAIT) |
| 3331 | req->flags |= REQ_F_NOWAIT; |
| 3332 | else if (force_nonblock) |
| 3333 | flags |= MSG_DONTWAIT; |
| 3334 | |
| 3335 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3336 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame^] | 3337 | if (force_nonblock && ret == -EAGAIN) |
| 3338 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3339 | if (ret == -ERESTARTSYS) |
| 3340 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3341 | } |
| 3342 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3343 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3344 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3345 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3346 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3347 | if (ret < 0) |
| 3348 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3349 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3350 | return 0; |
| 3351 | #else |
| 3352 | return -EOPNOTSUPP; |
| 3353 | #endif |
| 3354 | } |
| 3355 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3356 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3357 | bool force_nonblock) |
| 3358 | { |
| 3359 | #if defined(CONFIG_NET) |
| 3360 | struct socket *sock; |
| 3361 | int ret; |
| 3362 | |
| 3363 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3364 | return -EINVAL; |
| 3365 | |
| 3366 | sock = sock_from_file(req->file, &ret); |
| 3367 | if (sock) { |
| 3368 | struct io_sr_msg *sr = &req->sr_msg; |
| 3369 | struct msghdr msg; |
| 3370 | struct iovec iov; |
| 3371 | unsigned flags; |
| 3372 | |
| 3373 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3374 | &msg.msg_iter); |
| 3375 | if (ret) |
| 3376 | return ret; |
| 3377 | |
| 3378 | msg.msg_name = NULL; |
| 3379 | msg.msg_control = NULL; |
| 3380 | msg.msg_controllen = 0; |
| 3381 | msg.msg_namelen = 0; |
| 3382 | msg.msg_iocb = NULL; |
| 3383 | msg.msg_flags = 0; |
| 3384 | |
| 3385 | flags = req->sr_msg.msg_flags; |
| 3386 | if (flags & MSG_DONTWAIT) |
| 3387 | req->flags |= REQ_F_NOWAIT; |
| 3388 | else if (force_nonblock) |
| 3389 | flags |= MSG_DONTWAIT; |
| 3390 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3391 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3392 | if (force_nonblock && ret == -EAGAIN) |
| 3393 | return -EAGAIN; |
| 3394 | if (ret == -ERESTARTSYS) |
| 3395 | ret = -EINTR; |
| 3396 | } |
| 3397 | |
| 3398 | io_cqring_add_event(req, ret); |
| 3399 | if (ret < 0) |
| 3400 | req_set_fail_links(req); |
| 3401 | io_put_req_find_next(req, nxt); |
| 3402 | return 0; |
| 3403 | #else |
| 3404 | return -EOPNOTSUPP; |
| 3405 | #endif |
| 3406 | } |
| 3407 | |
| 3408 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3409 | 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] | 3410 | { |
| 3411 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3412 | struct io_accept *accept = &req->accept; |
| 3413 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3414 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3415 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3416 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3417 | return -EINVAL; |
| 3418 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3419 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3420 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3421 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3422 | return 0; |
| 3423 | #else |
| 3424 | return -EOPNOTSUPP; |
| 3425 | #endif |
| 3426 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3427 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3428 | #if defined(CONFIG_NET) |
| 3429 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3430 | bool force_nonblock) |
| 3431 | { |
| 3432 | struct io_accept *accept = &req->accept; |
| 3433 | unsigned file_flags; |
| 3434 | int ret; |
| 3435 | |
| 3436 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3437 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3438 | accept->addr_len, accept->flags); |
| 3439 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3440 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3441 | if (ret == -ERESTARTSYS) |
| 3442 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3443 | if (ret < 0) |
| 3444 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3445 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3446 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3447 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3451 | { |
| 3452 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3453 | struct io_kiocb *nxt = NULL; |
| 3454 | |
Jens Axboe | e441d1c | 2020-02-20 09:59:02 -0700 | [diff] [blame] | 3455 | io_put_req(req); |
| 3456 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3457 | if (io_req_cancelled(req)) |
| 3458 | return; |
| 3459 | __io_accept(req, &nxt, false); |
| 3460 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3461 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3462 | } |
| 3463 | #endif |
| 3464 | |
| 3465 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3466 | bool force_nonblock) |
| 3467 | { |
| 3468 | #if defined(CONFIG_NET) |
| 3469 | int ret; |
| 3470 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3471 | ret = __io_accept(req, nxt, force_nonblock); |
| 3472 | if (ret == -EAGAIN && force_nonblock) { |
| 3473 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3474 | return -EAGAIN; |
| 3475 | } |
| 3476 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3477 | #else |
| 3478 | return -EOPNOTSUPP; |
| 3479 | #endif |
| 3480 | } |
| 3481 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3482 | 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] | 3483 | { |
| 3484 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3485 | struct io_connect *conn = &req->connect; |
| 3486 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3487 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3488 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3489 | return -EINVAL; |
| 3490 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3491 | return -EINVAL; |
| 3492 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3493 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3494 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3495 | |
| 3496 | if (!io) |
| 3497 | return 0; |
| 3498 | |
| 3499 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3500 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3501 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3502 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3503 | #endif |
| 3504 | } |
| 3505 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3506 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3507 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3508 | { |
| 3509 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3510 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3511 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3512 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3513 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3514 | if (req->io) { |
| 3515 | io = req->io; |
| 3516 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3517 | ret = move_addr_to_kernel(req->connect.addr, |
| 3518 | req->connect.addr_len, |
| 3519 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3520 | if (ret) |
| 3521 | goto out; |
| 3522 | io = &__io; |
| 3523 | } |
| 3524 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3525 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3526 | |
| 3527 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3528 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3529 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3530 | if (req->io) |
| 3531 | return -EAGAIN; |
| 3532 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3533 | ret = -ENOMEM; |
| 3534 | goto out; |
| 3535 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3536 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3537 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3538 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3539 | if (ret == -ERESTARTSYS) |
| 3540 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3541 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3542 | if (ret < 0) |
| 3543 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3544 | io_cqring_add_event(req, ret); |
| 3545 | io_put_req_find_next(req, nxt); |
| 3546 | return 0; |
| 3547 | #else |
| 3548 | return -EOPNOTSUPP; |
| 3549 | #endif |
| 3550 | } |
| 3551 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3552 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3553 | { |
| 3554 | struct io_poll_iocb *poll = &req->poll; |
| 3555 | |
| 3556 | spin_lock(&poll->head->lock); |
| 3557 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3558 | if (!list_empty(&poll->wait.entry)) { |
| 3559 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3560 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3561 | } |
| 3562 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3563 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3567 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3568 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3569 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3570 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3571 | |
| 3572 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3573 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3574 | struct hlist_head *list; |
| 3575 | |
| 3576 | list = &ctx->cancel_hash[i]; |
| 3577 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3578 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3579 | } |
| 3580 | spin_unlock_irq(&ctx->completion_lock); |
| 3581 | } |
| 3582 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3583 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3584 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3585 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3586 | struct io_kiocb *req; |
| 3587 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3588 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3589 | hlist_for_each_entry(req, list, hash_node) { |
| 3590 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3591 | io_poll_remove_one(req); |
| 3592 | return 0; |
| 3593 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | return -ENOENT; |
| 3597 | } |
| 3598 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3599 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3600 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3601 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3602 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3603 | return -EINVAL; |
| 3604 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3605 | sqe->poll_events) |
| 3606 | return -EINVAL; |
| 3607 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3608 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3609 | return 0; |
| 3610 | } |
| 3611 | |
| 3612 | /* |
| 3613 | * Find a running poll command that matches one specified in sqe->addr, |
| 3614 | * and remove it if found. |
| 3615 | */ |
| 3616 | static int io_poll_remove(struct io_kiocb *req) |
| 3617 | { |
| 3618 | struct io_ring_ctx *ctx = req->ctx; |
| 3619 | u64 addr; |
| 3620 | int ret; |
| 3621 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3622 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3623 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3624 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3625 | spin_unlock_irq(&ctx->completion_lock); |
| 3626 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3627 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3628 | if (ret < 0) |
| 3629 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3630 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3631 | return 0; |
| 3632 | } |
| 3633 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3634 | 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] | 3635 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3636 | struct io_ring_ctx *ctx = req->ctx; |
| 3637 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3638 | req->poll.done = true; |
Pavel Begunkov | b0a2034 | 2020-02-28 10:36:35 +0300 | [diff] [blame] | 3639 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3640 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3641 | } |
| 3642 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3643 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3644 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3645 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3646 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3647 | struct io_poll_iocb *poll = &req->poll; |
| 3648 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3649 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3650 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3651 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3652 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3653 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3654 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3655 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3656 | ret = -ECANCELED; |
| 3657 | } else if (READ_ONCE(poll->canceled)) { |
| 3658 | ret = -ECANCELED; |
| 3659 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3660 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3661 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3662 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3663 | |
| 3664 | /* |
| 3665 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3666 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3667 | * synchronize with them. In the cancellation case the list_del_init |
| 3668 | * itself is not actually needed, but harmless so we keep it in to |
| 3669 | * avoid further branches in the fast path. |
| 3670 | */ |
| 3671 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3672 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3673 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3674 | spin_unlock_irq(&ctx->completion_lock); |
| 3675 | return; |
| 3676 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3677 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3678 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3679 | spin_unlock_irq(&ctx->completion_lock); |
| 3680 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3681 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3682 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3683 | if (ret < 0) |
| 3684 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3685 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3686 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3687 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3688 | } |
| 3689 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3690 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3691 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3692 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3693 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3694 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3695 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3696 | spin_lock_irq(&ctx->completion_lock); |
| 3697 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3698 | hash_del(&req->hash_node); |
| 3699 | io_poll_complete(req, req->result, 0); |
| 3700 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3701 | if (refcount_dec_and_test(&req->refs) && |
| 3702 | !io_req_multi_free(&rb, req)) { |
| 3703 | req->flags |= REQ_F_COMP_LOCKED; |
| 3704 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3705 | } |
| 3706 | } |
| 3707 | spin_unlock_irq(&ctx->completion_lock); |
| 3708 | |
| 3709 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3710 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3714 | { |
| 3715 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3716 | struct llist_node *nodes; |
| 3717 | |
| 3718 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3719 | if (nodes) |
| 3720 | __io_poll_flush(req->ctx, nodes); |
| 3721 | } |
| 3722 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3723 | static void io_poll_trigger_evfd(struct io_wq_work **workptr) |
| 3724 | { |
| 3725 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3726 | |
| 3727 | eventfd_signal(req->ctx->cq_ev_fd, 1); |
| 3728 | io_put_req(req); |
| 3729 | } |
| 3730 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3731 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3732 | void *key) |
| 3733 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3734 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3735 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3736 | struct io_ring_ctx *ctx = req->ctx; |
| 3737 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3738 | |
| 3739 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3740 | if (mask && !(mask & poll->events)) |
| 3741 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3742 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3743 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3744 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3745 | /* |
| 3746 | * Run completion inline if we can. We're using trylock here because |
| 3747 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3748 | * If we have a link timeout we're going to need the completion_lock |
| 3749 | * for finalizing the request, mark us as having grabbed that already. |
| 3750 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3751 | if (mask) { |
| 3752 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3753 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3754 | if (llist_empty(&ctx->poll_llist) && |
| 3755 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3756 | bool trigger_ev; |
| 3757 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3758 | hash_del(&req->hash_node); |
| 3759 | io_poll_complete(req, mask, 0); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3760 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3761 | trigger_ev = io_should_trigger_evfd(ctx); |
| 3762 | if (trigger_ev && eventfd_signal_count()) { |
| 3763 | trigger_ev = false; |
| 3764 | req->work.func = io_poll_trigger_evfd; |
| 3765 | } else { |
| 3766 | req->flags |= REQ_F_COMP_LOCKED; |
| 3767 | io_put_req(req); |
| 3768 | req = NULL; |
| 3769 | } |
| 3770 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3771 | __io_cqring_ev_posted(ctx, trigger_ev); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3772 | } else { |
| 3773 | req->result = mask; |
| 3774 | req->llist_node.next = NULL; |
| 3775 | /* if the list wasn't empty, we're done */ |
| 3776 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3777 | req = NULL; |
| 3778 | else |
| 3779 | req->work.func = io_poll_flush; |
| 3780 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3781 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3782 | if (req) |
| 3783 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3784 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3785 | return 1; |
| 3786 | } |
| 3787 | |
| 3788 | struct io_poll_table { |
| 3789 | struct poll_table_struct pt; |
| 3790 | struct io_kiocb *req; |
| 3791 | int error; |
| 3792 | }; |
| 3793 | |
| 3794 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3795 | struct poll_table_struct *p) |
| 3796 | { |
| 3797 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3798 | |
| 3799 | if (unlikely(pt->req->poll.head)) { |
| 3800 | pt->error = -EINVAL; |
| 3801 | return; |
| 3802 | } |
| 3803 | |
| 3804 | pt->error = 0; |
| 3805 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3806 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3807 | } |
| 3808 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3809 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3810 | { |
| 3811 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3812 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3813 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3814 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3815 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3816 | } |
| 3817 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3818 | 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] | 3819 | { |
| 3820 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3821 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3822 | |
| 3823 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3824 | return -EINVAL; |
| 3825 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3826 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3827 | if (!poll->file) |
| 3828 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3829 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3830 | events = READ_ONCE(sqe->poll_events); |
| 3831 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3832 | return 0; |
| 3833 | } |
| 3834 | |
| 3835 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3836 | { |
| 3837 | struct io_poll_iocb *poll = &req->poll; |
| 3838 | struct io_ring_ctx *ctx = req->ctx; |
| 3839 | struct io_poll_table ipt; |
| 3840 | bool cancel = false; |
| 3841 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3842 | |
| 3843 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3844 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3845 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3846 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3847 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3848 | poll->canceled = false; |
| 3849 | |
| 3850 | ipt.pt._qproc = io_poll_queue_proc; |
| 3851 | ipt.pt._key = poll->events; |
| 3852 | ipt.req = req; |
| 3853 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3854 | |
| 3855 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3856 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3857 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3858 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3859 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3860 | INIT_LIST_HEAD(&req->list); |
| 3861 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3862 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3863 | |
| 3864 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3865 | if (likely(poll->head)) { |
| 3866 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3867 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3868 | if (ipt.error) |
| 3869 | cancel = true; |
| 3870 | ipt.error = 0; |
| 3871 | mask = 0; |
| 3872 | } |
| 3873 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3874 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3875 | else if (cancel) |
| 3876 | WRITE_ONCE(poll->canceled, true); |
| 3877 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3878 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3879 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3880 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3881 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3882 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3883 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3884 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3885 | spin_unlock_irq(&ctx->completion_lock); |
| 3886 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3887 | if (mask) { |
| 3888 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3889 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3890 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3891 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3894 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3895 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3896 | struct io_timeout_data *data = container_of(timer, |
| 3897 | struct io_timeout_data, timer); |
| 3898 | struct io_kiocb *req = data->req; |
| 3899 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3900 | unsigned long flags; |
| 3901 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3902 | atomic_inc(&ctx->cq_timeouts); |
| 3903 | |
| 3904 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3905 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3906 | * We could be racing with timeout deletion. If the list is empty, |
| 3907 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3908 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3909 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3910 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3911 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3912 | /* |
| 3913 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3914 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3915 | * pointer will be increased, otherwise other timeout reqs may |
| 3916 | * return in advance without waiting for enough wait_nr. |
| 3917 | */ |
| 3918 | prev = req; |
| 3919 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3920 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3921 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3922 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3923 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3924 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3925 | io_commit_cqring(ctx); |
| 3926 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3927 | |
| 3928 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3929 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3930 | io_put_req(req); |
| 3931 | return HRTIMER_NORESTART; |
| 3932 | } |
| 3933 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3934 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3935 | { |
| 3936 | struct io_kiocb *req; |
| 3937 | int ret = -ENOENT; |
| 3938 | |
| 3939 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3940 | if (user_data == req->user_data) { |
| 3941 | list_del_init(&req->list); |
| 3942 | ret = 0; |
| 3943 | break; |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | if (ret == -ENOENT) |
| 3948 | return ret; |
| 3949 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3950 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3951 | if (ret == -1) |
| 3952 | return -EALREADY; |
| 3953 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3954 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3955 | io_cqring_fill_event(req, -ECANCELED); |
| 3956 | io_put_req(req); |
| 3957 | return 0; |
| 3958 | } |
| 3959 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3960 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3961 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3962 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3963 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3964 | return -EINVAL; |
| 3965 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3966 | return -EINVAL; |
| 3967 | |
| 3968 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3969 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3970 | if (req->timeout.flags) |
| 3971 | return -EINVAL; |
| 3972 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3973 | return 0; |
| 3974 | } |
| 3975 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3976 | /* |
| 3977 | * Remove or update an existing timeout command |
| 3978 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3979 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3980 | { |
| 3981 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3982 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3983 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3984 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3985 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3986 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3987 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3988 | io_commit_cqring(ctx); |
| 3989 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3990 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3991 | if (ret < 0) |
| 3992 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3993 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3994 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3995 | } |
| 3996 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3997 | 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] | 3998 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3999 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4000 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4001 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4002 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4003 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4004 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4005 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4006 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4007 | if (sqe->off && is_timeout_link) |
| 4008 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4009 | flags = READ_ONCE(sqe->timeout_flags); |
| 4010 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4011 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 4012 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4013 | req->timeout.count = READ_ONCE(sqe->off); |
| 4014 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4015 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4016 | return -ENOMEM; |
| 4017 | |
| 4018 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4019 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4020 | req->flags |= REQ_F_TIMEOUT; |
| 4021 | |
| 4022 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4023 | return -EFAULT; |
| 4024 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4025 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4026 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4027 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4028 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4029 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4030 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 4031 | return 0; |
| 4032 | } |
| 4033 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4034 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4035 | { |
| 4036 | unsigned count; |
| 4037 | struct io_ring_ctx *ctx = req->ctx; |
| 4038 | struct io_timeout_data *data; |
| 4039 | struct list_head *entry; |
| 4040 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4041 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4042 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4043 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4044 | /* |
| 4045 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4046 | * timeout event to be satisfied. If it isn't set, then this is |
| 4047 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4048 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4049 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4050 | if (!count) { |
| 4051 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 4052 | spin_lock_irq(&ctx->completion_lock); |
| 4053 | entry = ctx->timeout_list.prev; |
| 4054 | goto add; |
| 4055 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4056 | |
| 4057 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4058 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4059 | |
| 4060 | /* |
| 4061 | * Insertion sort, ensuring the first entry in the list is always |
| 4062 | * the one we need first. |
| 4063 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4064 | spin_lock_irq(&ctx->completion_lock); |
| 4065 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 4066 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4067 | unsigned nxt_sq_head; |
| 4068 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4069 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4070 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4071 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 4072 | continue; |
| 4073 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4074 | /* |
| 4075 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 4076 | * long to store it. |
| 4077 | */ |
| 4078 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 4079 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 4080 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4081 | |
| 4082 | /* |
| 4083 | * cached_sq_head may overflow, and it will never overflow twice |
| 4084 | * once there is some timeout req still be valid. |
| 4085 | */ |
| 4086 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 4087 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4088 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4089 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4090 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4091 | |
| 4092 | /* |
| 4093 | * Sequence of reqs after the insert one and itself should |
| 4094 | * be adjusted because each timeout req consumes a slot. |
| 4095 | */ |
| 4096 | span++; |
| 4097 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4098 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4099 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4100 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4101 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4102 | data->timer.function = io_timeout_fn; |
| 4103 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4104 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4105 | return 0; |
| 4106 | } |
| 4107 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4108 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4109 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4110 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4111 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4112 | return req->user_data == (unsigned long) data; |
| 4113 | } |
| 4114 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4115 | 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] | 4116 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4117 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4118 | int ret = 0; |
| 4119 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4120 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 4121 | switch (cancel_ret) { |
| 4122 | case IO_WQ_CANCEL_OK: |
| 4123 | ret = 0; |
| 4124 | break; |
| 4125 | case IO_WQ_CANCEL_RUNNING: |
| 4126 | ret = -EALREADY; |
| 4127 | break; |
| 4128 | case IO_WQ_CANCEL_NOTFOUND: |
| 4129 | ret = -ENOENT; |
| 4130 | break; |
| 4131 | } |
| 4132 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4133 | return ret; |
| 4134 | } |
| 4135 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4136 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 4137 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4138 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4139 | { |
| 4140 | unsigned long flags; |
| 4141 | int ret; |
| 4142 | |
| 4143 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 4144 | if (ret != -ENOENT) { |
| 4145 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4146 | goto done; |
| 4147 | } |
| 4148 | |
| 4149 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4150 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4151 | if (ret != -ENOENT) |
| 4152 | goto done; |
| 4153 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4154 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4155 | if (!ret) |
| 4156 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4157 | io_cqring_fill_event(req, ret); |
| 4158 | io_commit_cqring(ctx); |
| 4159 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4160 | io_cqring_ev_posted(ctx); |
| 4161 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4162 | if (ret < 0) |
| 4163 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4164 | io_put_req_find_next(req, nxt); |
| 4165 | } |
| 4166 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4167 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4168 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4169 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4170 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4171 | return -EINVAL; |
| 4172 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4173 | sqe->cancel_flags) |
| 4174 | return -EINVAL; |
| 4175 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4176 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4177 | return 0; |
| 4178 | } |
| 4179 | |
| 4180 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4181 | { |
| 4182 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4183 | |
| 4184 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4185 | return 0; |
| 4186 | } |
| 4187 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4188 | static int io_files_update_prep(struct io_kiocb *req, |
| 4189 | const struct io_uring_sqe *sqe) |
| 4190 | { |
| 4191 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4192 | return -EINVAL; |
| 4193 | |
| 4194 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4195 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4196 | if (!req->files_update.nr_args) |
| 4197 | return -EINVAL; |
| 4198 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4199 | return 0; |
| 4200 | } |
| 4201 | |
| 4202 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4203 | { |
| 4204 | struct io_ring_ctx *ctx = req->ctx; |
| 4205 | struct io_uring_files_update up; |
| 4206 | int ret; |
| 4207 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4208 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4209 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4210 | |
| 4211 | up.offset = req->files_update.offset; |
| 4212 | up.fds = req->files_update.arg; |
| 4213 | |
| 4214 | mutex_lock(&ctx->uring_lock); |
| 4215 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4216 | mutex_unlock(&ctx->uring_lock); |
| 4217 | |
| 4218 | if (ret < 0) |
| 4219 | req_set_fail_links(req); |
| 4220 | io_cqring_add_event(req, ret); |
| 4221 | io_put_req(req); |
| 4222 | return 0; |
| 4223 | } |
| 4224 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4225 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4226 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4227 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4228 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4229 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4230 | if (io_op_defs[req->opcode].file_table) { |
| 4231 | ret = io_grab_files(req); |
| 4232 | if (unlikely(ret)) |
| 4233 | return ret; |
| 4234 | } |
| 4235 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4236 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4237 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4238 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4239 | case IORING_OP_NOP: |
| 4240 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4241 | case IORING_OP_READV: |
| 4242 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4243 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4244 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4245 | break; |
| 4246 | case IORING_OP_WRITEV: |
| 4247 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4248 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4249 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4250 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4251 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4252 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4253 | break; |
| 4254 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4255 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4256 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4257 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4258 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4259 | break; |
| 4260 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4261 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4262 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4263 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4264 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4265 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4266 | break; |
| 4267 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4268 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4269 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4270 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4271 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4272 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4273 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4274 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4275 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4276 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4277 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4278 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4279 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4280 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4281 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4282 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4283 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4284 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4285 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4286 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4287 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4288 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4289 | case IORING_OP_FALLOCATE: |
| 4290 | ret = io_fallocate_prep(req, sqe); |
| 4291 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4292 | case IORING_OP_OPENAT: |
| 4293 | ret = io_openat_prep(req, sqe); |
| 4294 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4295 | case IORING_OP_CLOSE: |
| 4296 | ret = io_close_prep(req, sqe); |
| 4297 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4298 | case IORING_OP_FILES_UPDATE: |
| 4299 | ret = io_files_update_prep(req, sqe); |
| 4300 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4301 | case IORING_OP_STATX: |
| 4302 | ret = io_statx_prep(req, sqe); |
| 4303 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4304 | case IORING_OP_FADVISE: |
| 4305 | ret = io_fadvise_prep(req, sqe); |
| 4306 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4307 | case IORING_OP_MADVISE: |
| 4308 | ret = io_madvise_prep(req, sqe); |
| 4309 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4310 | case IORING_OP_OPENAT2: |
| 4311 | ret = io_openat2_prep(req, sqe); |
| 4312 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4313 | case IORING_OP_EPOLL_CTL: |
| 4314 | ret = io_epoll_ctl_prep(req, sqe); |
| 4315 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4316 | case IORING_OP_SPLICE: |
| 4317 | ret = io_splice_prep(req, sqe); |
| 4318 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4319 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4320 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4321 | req->opcode); |
| 4322 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4323 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4324 | } |
| 4325 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4326 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4327 | } |
| 4328 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4329 | 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] | 4330 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4331 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4332 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4333 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4334 | /* Still need defer if there is pending req in defer list. */ |
| 4335 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4336 | return 0; |
| 4337 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4338 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4339 | return -EAGAIN; |
| 4340 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4341 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4342 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4343 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4344 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4345 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4346 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4347 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4348 | return 0; |
| 4349 | } |
| 4350 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4351 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4352 | list_add_tail(&req->list, &ctx->defer_list); |
| 4353 | spin_unlock_irq(&ctx->completion_lock); |
| 4354 | return -EIOCBQUEUED; |
| 4355 | } |
| 4356 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4357 | static void io_cleanup_req(struct io_kiocb *req) |
| 4358 | { |
| 4359 | struct io_async_ctx *io = req->io; |
| 4360 | |
| 4361 | switch (req->opcode) { |
| 4362 | case IORING_OP_READV: |
| 4363 | case IORING_OP_READ_FIXED: |
| 4364 | case IORING_OP_READ: |
| 4365 | case IORING_OP_WRITEV: |
| 4366 | case IORING_OP_WRITE_FIXED: |
| 4367 | case IORING_OP_WRITE: |
| 4368 | if (io->rw.iov != io->rw.fast_iov) |
| 4369 | kfree(io->rw.iov); |
| 4370 | break; |
| 4371 | case IORING_OP_SENDMSG: |
| 4372 | case IORING_OP_RECVMSG: |
| 4373 | if (io->msg.iov != io->msg.fast_iov) |
| 4374 | kfree(io->msg.iov); |
| 4375 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4376 | case IORING_OP_OPENAT: |
| 4377 | case IORING_OP_OPENAT2: |
| 4378 | case IORING_OP_STATX: |
| 4379 | putname(req->open.filename); |
| 4380 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4381 | case IORING_OP_SPLICE: |
| 4382 | io_put_file(req, req->splice.file_in, |
| 4383 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 4384 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4388 | } |
| 4389 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4390 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4391 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4392 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4393 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4394 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4395 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4396 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4397 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4398 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4399 | break; |
| 4400 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4401 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4402 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4403 | if (sqe) { |
| 4404 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4405 | if (ret < 0) |
| 4406 | break; |
| 4407 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4408 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4409 | break; |
| 4410 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4411 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4412 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4413 | if (sqe) { |
| 4414 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4415 | if (ret < 0) |
| 4416 | break; |
| 4417 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4418 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4419 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4420 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4421 | if (sqe) { |
| 4422 | ret = io_prep_fsync(req, sqe); |
| 4423 | if (ret < 0) |
| 4424 | break; |
| 4425 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4426 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4427 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4428 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4429 | if (sqe) { |
| 4430 | ret = io_poll_add_prep(req, sqe); |
| 4431 | if (ret) |
| 4432 | break; |
| 4433 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4434 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4435 | break; |
| 4436 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4437 | if (sqe) { |
| 4438 | ret = io_poll_remove_prep(req, sqe); |
| 4439 | if (ret < 0) |
| 4440 | break; |
| 4441 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4442 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4443 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4444 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4445 | if (sqe) { |
| 4446 | ret = io_prep_sfr(req, sqe); |
| 4447 | if (ret < 0) |
| 4448 | break; |
| 4449 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4450 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4451 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4452 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4453 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4454 | if (sqe) { |
| 4455 | ret = io_sendmsg_prep(req, sqe); |
| 4456 | if (ret < 0) |
| 4457 | break; |
| 4458 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4459 | if (req->opcode == IORING_OP_SENDMSG) |
| 4460 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4461 | else |
| 4462 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4463 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4464 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4465 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4466 | if (sqe) { |
| 4467 | ret = io_recvmsg_prep(req, sqe); |
| 4468 | if (ret) |
| 4469 | break; |
| 4470 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4471 | if (req->opcode == IORING_OP_RECVMSG) |
| 4472 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4473 | else |
| 4474 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4475 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4476 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4477 | if (sqe) { |
| 4478 | ret = io_timeout_prep(req, sqe, false); |
| 4479 | if (ret) |
| 4480 | break; |
| 4481 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4482 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4483 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4484 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4485 | if (sqe) { |
| 4486 | ret = io_timeout_remove_prep(req, sqe); |
| 4487 | if (ret) |
| 4488 | break; |
| 4489 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4490 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4491 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4492 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4493 | if (sqe) { |
| 4494 | ret = io_accept_prep(req, sqe); |
| 4495 | if (ret) |
| 4496 | break; |
| 4497 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4498 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4499 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4500 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4501 | if (sqe) { |
| 4502 | ret = io_connect_prep(req, sqe); |
| 4503 | if (ret) |
| 4504 | break; |
| 4505 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4506 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4507 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4508 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4509 | if (sqe) { |
| 4510 | ret = io_async_cancel_prep(req, sqe); |
| 4511 | if (ret) |
| 4512 | break; |
| 4513 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4514 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4515 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4516 | case IORING_OP_FALLOCATE: |
| 4517 | if (sqe) { |
| 4518 | ret = io_fallocate_prep(req, sqe); |
| 4519 | if (ret) |
| 4520 | break; |
| 4521 | } |
| 4522 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4523 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4524 | case IORING_OP_OPENAT: |
| 4525 | if (sqe) { |
| 4526 | ret = io_openat_prep(req, sqe); |
| 4527 | if (ret) |
| 4528 | break; |
| 4529 | } |
| 4530 | ret = io_openat(req, nxt, force_nonblock); |
| 4531 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4532 | case IORING_OP_CLOSE: |
| 4533 | if (sqe) { |
| 4534 | ret = io_close_prep(req, sqe); |
| 4535 | if (ret) |
| 4536 | break; |
| 4537 | } |
| 4538 | ret = io_close(req, nxt, force_nonblock); |
| 4539 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4540 | case IORING_OP_FILES_UPDATE: |
| 4541 | if (sqe) { |
| 4542 | ret = io_files_update_prep(req, sqe); |
| 4543 | if (ret) |
| 4544 | break; |
| 4545 | } |
| 4546 | ret = io_files_update(req, force_nonblock); |
| 4547 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4548 | case IORING_OP_STATX: |
| 4549 | if (sqe) { |
| 4550 | ret = io_statx_prep(req, sqe); |
| 4551 | if (ret) |
| 4552 | break; |
| 4553 | } |
| 4554 | ret = io_statx(req, nxt, force_nonblock); |
| 4555 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4556 | case IORING_OP_FADVISE: |
| 4557 | if (sqe) { |
| 4558 | ret = io_fadvise_prep(req, sqe); |
| 4559 | if (ret) |
| 4560 | break; |
| 4561 | } |
| 4562 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4563 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4564 | case IORING_OP_MADVISE: |
| 4565 | if (sqe) { |
| 4566 | ret = io_madvise_prep(req, sqe); |
| 4567 | if (ret) |
| 4568 | break; |
| 4569 | } |
| 4570 | ret = io_madvise(req, nxt, force_nonblock); |
| 4571 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4572 | case IORING_OP_OPENAT2: |
| 4573 | if (sqe) { |
| 4574 | ret = io_openat2_prep(req, sqe); |
| 4575 | if (ret) |
| 4576 | break; |
| 4577 | } |
| 4578 | ret = io_openat2(req, nxt, force_nonblock); |
| 4579 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4580 | case IORING_OP_EPOLL_CTL: |
| 4581 | if (sqe) { |
| 4582 | ret = io_epoll_ctl_prep(req, sqe); |
| 4583 | if (ret) |
| 4584 | break; |
| 4585 | } |
| 4586 | ret = io_epoll_ctl(req, nxt, force_nonblock); |
| 4587 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4588 | case IORING_OP_SPLICE: |
| 4589 | if (sqe) { |
| 4590 | ret = io_splice_prep(req, sqe); |
| 4591 | if (ret < 0) |
| 4592 | break; |
| 4593 | } |
| 4594 | ret = io_splice(req, nxt, force_nonblock); |
| 4595 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4596 | default: |
| 4597 | ret = -EINVAL; |
| 4598 | break; |
| 4599 | } |
| 4600 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4601 | if (ret) |
| 4602 | return ret; |
| 4603 | |
| 4604 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4605 | const bool in_async = io_wq_current_is_worker(); |
| 4606 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4607 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4608 | return -EAGAIN; |
| 4609 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4610 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4611 | if (in_async) |
| 4612 | mutex_lock(&ctx->uring_lock); |
| 4613 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4614 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4615 | |
| 4616 | if (in_async) |
| 4617 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4621 | } |
| 4622 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4623 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4624 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4625 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4626 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4627 | struct io_kiocb *nxt = NULL; |
| 4628 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4629 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4630 | /* if NO_CANCEL is set, we must still run the work */ |
| 4631 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4632 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4633 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4634 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4635 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4636 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4637 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4638 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4639 | /* |
| 4640 | * We can get EAGAIN for polled IO even though we're |
| 4641 | * forcing a sync submission from here, since we can't |
| 4642 | * wait for request slots on the block side. |
| 4643 | */ |
| 4644 | if (ret != -EAGAIN) |
| 4645 | break; |
| 4646 | cond_resched(); |
| 4647 | } while (1); |
| 4648 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4649 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4650 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4651 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4652 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4653 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4654 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4655 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4656 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4657 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4658 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4659 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4660 | if (!ret && nxt) |
| 4661 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4662 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4663 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4664 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4665 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4666 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4667 | return 0; |
Jens Axboe | 0b5faf6 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 4668 | if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4669 | return 0; |
| 4670 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4671 | } |
| 4672 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4673 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4674 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4675 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4676 | struct fixed_file_table *table; |
| 4677 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4678 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4679 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4680 | } |
| 4681 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4682 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 4683 | int fd, struct file **out_file, bool fixed) |
| 4684 | { |
| 4685 | struct io_ring_ctx *ctx = req->ctx; |
| 4686 | struct file *file; |
| 4687 | |
| 4688 | if (fixed) { |
| 4689 | if (unlikely(!ctx->file_data || |
| 4690 | (unsigned) fd >= ctx->nr_user_files)) |
| 4691 | return -EBADF; |
| 4692 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 4693 | file = io_file_from_index(ctx, fd); |
| 4694 | if (!file) |
| 4695 | return -EBADF; |
| 4696 | percpu_ref_get(&ctx->file_data->refs); |
| 4697 | } else { |
| 4698 | trace_io_uring_file_get(ctx, fd); |
| 4699 | file = __io_file_get(state, fd); |
| 4700 | if (unlikely(!file)) |
| 4701 | return -EBADF; |
| 4702 | } |
| 4703 | |
| 4704 | *out_file = file; |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4708 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4709 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4710 | { |
| 4711 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4712 | int fd; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4713 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4714 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4715 | flags = READ_ONCE(sqe->flags); |
| 4716 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4717 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4718 | if (!io_req_needs_file(req, fd)) |
| 4719 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4720 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4721 | fixed = (flags & IOSQE_FIXED_FILE); |
| 4722 | if (unlikely(!fixed && req->needs_fixed_file)) |
| 4723 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4724 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4725 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4726 | } |
| 4727 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4728 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4729 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4730 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4731 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4732 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4733 | if (req->work.files) |
| 4734 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4735 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4736 | return -EBADF; |
| 4737 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4738 | rcu_read_lock(); |
| 4739 | spin_lock_irq(&ctx->inflight_lock); |
| 4740 | /* |
| 4741 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4742 | * out work accessing these files if the fd is closed. Check if |
| 4743 | * the fd has changed since we started down this path, and disallow |
| 4744 | * this operation if it has. |
| 4745 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4746 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4747 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4748 | req->flags |= REQ_F_INFLIGHT; |
| 4749 | req->work.files = current->files; |
| 4750 | ret = 0; |
| 4751 | } |
| 4752 | spin_unlock_irq(&ctx->inflight_lock); |
| 4753 | rcu_read_unlock(); |
| 4754 | |
| 4755 | return ret; |
| 4756 | } |
| 4757 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4758 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4759 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4760 | struct io_timeout_data *data = container_of(timer, |
| 4761 | struct io_timeout_data, timer); |
| 4762 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4763 | struct io_ring_ctx *ctx = req->ctx; |
| 4764 | struct io_kiocb *prev = NULL; |
| 4765 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4766 | |
| 4767 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4768 | |
| 4769 | /* |
| 4770 | * We don't expect the list to be empty, that will only happen if we |
| 4771 | * race with the completion of the linked work. |
| 4772 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4773 | if (!list_empty(&req->link_list)) { |
| 4774 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4775 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4776 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4777 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4778 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4779 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4780 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4781 | } |
| 4782 | |
| 4783 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4784 | |
| 4785 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4786 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4787 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4788 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4789 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4790 | } else { |
| 4791 | io_cqring_add_event(req, -ETIME); |
| 4792 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4793 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4794 | return HRTIMER_NORESTART; |
| 4795 | } |
| 4796 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4797 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4798 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4799 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4800 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4801 | /* |
| 4802 | * If the list is now empty, then our linked request finished before |
| 4803 | * we got a chance to setup the timer |
| 4804 | */ |
| 4805 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4806 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4807 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4808 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4809 | data->timer.function = io_link_timeout_fn; |
| 4810 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4811 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4812 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4813 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4814 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4815 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4816 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4817 | } |
| 4818 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4819 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4820 | { |
| 4821 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4822 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4823 | if (!(req->flags & REQ_F_LINK)) |
| 4824 | return NULL; |
| 4825 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4826 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4827 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4828 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4829 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4830 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4831 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4832 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4833 | } |
| 4834 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4835 | 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] | 4836 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4837 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4838 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4839 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4840 | int ret; |
| 4841 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4842 | again: |
| 4843 | linked_timeout = io_prep_linked_timeout(req); |
| 4844 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4845 | if (req->work.creds && req->work.creds != current_cred()) { |
| 4846 | if (old_creds) |
| 4847 | revert_creds(old_creds); |
| 4848 | if (old_creds == req->work.creds) |
| 4849 | old_creds = NULL; /* restored original creds */ |
| 4850 | else |
| 4851 | old_creds = override_creds(req->work.creds); |
| 4852 | } |
| 4853 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4854 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4855 | |
| 4856 | /* |
| 4857 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4858 | * doesn't support non-blocking read/write attempts |
| 4859 | */ |
| 4860 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4861 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4862 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4863 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4864 | ret = io_grab_files(req); |
| 4865 | if (ret) |
| 4866 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4867 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4868 | |
| 4869 | /* |
| 4870 | * Queued up for async execution, worker will release |
| 4871 | * submit reference when the iocb is actually submitted. |
| 4872 | */ |
| 4873 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4874 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4875 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4876 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4877 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4878 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 4879 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4880 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4881 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4882 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4883 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4884 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4885 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4886 | } |
| 4887 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4888 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4889 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4890 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4891 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4892 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4893 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4894 | done_req: |
| 4895 | if (nxt) { |
| 4896 | req = nxt; |
| 4897 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4898 | |
| 4899 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4900 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4901 | goto again; |
| 4902 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4903 | if (old_creds) |
| 4904 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4905 | } |
| 4906 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4907 | 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] | 4908 | { |
| 4909 | int ret; |
| 4910 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4911 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4912 | if (ret) { |
| 4913 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4914 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4915 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4916 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4917 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4918 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4919 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4920 | ret = io_req_defer_prep(req, sqe); |
| 4921 | if (unlikely(ret < 0)) |
| 4922 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4923 | /* |
| 4924 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4925 | * to async execution. |
| 4926 | */ |
| 4927 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4928 | io_queue_async_work(req); |
| 4929 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4930 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4931 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4932 | } |
| 4933 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4934 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4935 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4936 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4937 | io_cqring_add_event(req, -ECANCELED); |
| 4938 | io_double_put_req(req); |
| 4939 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4940 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4941 | } |
| 4942 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4943 | #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] | 4944 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4945 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4946 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4947 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4948 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4949 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4950 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4951 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4952 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4953 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4954 | |
| 4955 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4956 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4957 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4958 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4959 | } |
| 4960 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4961 | id = READ_ONCE(sqe->personality); |
| 4962 | if (id) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4963 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 4964 | if (unlikely(!req->work.creds)) { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4965 | ret = -EINVAL; |
| 4966 | goto err_req; |
| 4967 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4968 | get_cred(req->work.creds); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4969 | } |
| 4970 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4971 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4972 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK | |
| 4973 | IOSQE_ASYNC | IOSQE_FIXED_FILE); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4974 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4975 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4976 | if (unlikely(ret)) { |
| 4977 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4978 | io_cqring_add_event(req, ret); |
| 4979 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4980 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4981 | } |
| 4982 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4983 | /* |
| 4984 | * If we already have a head request, queue this one for async |
| 4985 | * submittal once the head completes. If we don't have a head but |
| 4986 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4987 | * submitted sync once the chain is complete. If none of those |
| 4988 | * conditions are true (normal request), then just queue it. |
| 4989 | */ |
| 4990 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4991 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4992 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 4993 | /* |
| 4994 | * Taking sequential execution of a link, draining both sides |
| 4995 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 4996 | * requests in the link. So, it drains the head and the |
| 4997 | * next after the link request. The last one is done via |
| 4998 | * drain_next flag to persist the effect across calls. |
| 4999 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5000 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 5001 | head->flags |= REQ_F_IO_DRAIN; |
| 5002 | ctx->drain_next = 1; |
| 5003 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5004 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5005 | ret = -EAGAIN; |
| 5006 | goto err_req; |
| 5007 | } |
| 5008 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5009 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5010 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5011 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5012 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5013 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5014 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5015 | trace_io_uring_link(ctx, req, head); |
| 5016 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5017 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5018 | /* last request of a link, enqueue the link */ |
| 5019 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 5020 | io_queue_link_head(head); |
| 5021 | *link = NULL; |
| 5022 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5023 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5024 | if (unlikely(ctx->drain_next)) { |
| 5025 | req->flags |= REQ_F_IO_DRAIN; |
| 5026 | req->ctx->drain_next = 0; |
| 5027 | } |
| 5028 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 5029 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5030 | INIT_LIST_HEAD(&req->link_list); |
| 5031 | ret = io_req_defer_prep(req, sqe); |
| 5032 | if (ret) |
| 5033 | req->flags |= REQ_F_FAIL_LINK; |
| 5034 | *link = req; |
| 5035 | } else { |
| 5036 | io_queue_sqe(req, sqe); |
| 5037 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5038 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5039 | |
| 5040 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5041 | } |
| 5042 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5043 | /* |
| 5044 | * Batched submission is done, ensure local IO is flushed out. |
| 5045 | */ |
| 5046 | static void io_submit_state_end(struct io_submit_state *state) |
| 5047 | { |
| 5048 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 5049 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5050 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 5051 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | /* |
| 5055 | * Start submission side cache. |
| 5056 | */ |
| 5057 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5058 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5059 | { |
| 5060 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5061 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5062 | state->file = NULL; |
| 5063 | state->ios_left = max_ios; |
| 5064 | } |
| 5065 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5066 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 5067 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5068 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5069 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 5070 | /* |
| 5071 | * Ensure any loads from the SQEs are done at this point, |
| 5072 | * since once we write the new head, the application could |
| 5073 | * write new data to them. |
| 5074 | */ |
| 5075 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5079 | * 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] | 5080 | * that is mapped by userspace. This means that care needs to be taken to |
| 5081 | * ensure that reads are stable, as we cannot rely on userspace always |
| 5082 | * being a good citizen. If members of the sqe are validated and then later |
| 5083 | * used, it's important that those reads are done through READ_ONCE() to |
| 5084 | * prevent a re-load down the line. |
| 5085 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5086 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 5087 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5088 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5089 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5090 | unsigned head; |
| 5091 | |
| 5092 | /* |
| 5093 | * The cached sq head (or cq tail) serves two purposes: |
| 5094 | * |
| 5095 | * 1) allows us to batch the cost of updating the user visible |
| 5096 | * head updates. |
| 5097 | * 2) allows the kernel side to track the head on its own, even |
| 5098 | * though the application is the one updating it. |
| 5099 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5100 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 5101 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5102 | /* |
| 5103 | * All io need record the previous position, if LINK vs DARIN, |
| 5104 | * it can be used to mark the position of the first IO in the |
| 5105 | * link list. |
| 5106 | */ |
| 5107 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5108 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 5109 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 5110 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5111 | ctx->cached_sq_head++; |
| 5112 | return true; |
| 5113 | } |
| 5114 | |
| 5115 | /* drop invalid entries */ |
| 5116 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 5117 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5118 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5119 | return false; |
| 5120 | } |
| 5121 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5122 | 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] | 5123 | struct file *ring_file, int ring_fd, |
| 5124 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5125 | { |
| 5126 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5127 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5128 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5129 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5130 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 5131 | /* 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] | 5132 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 5133 | if (!list_empty(&ctx->cq_overflow_list) && |
| 5134 | !io_cqring_overflow_flush(ctx, false)) |
| 5135 | return -EBUSY; |
| 5136 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5137 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5138 | /* make sure SQ entry isn't read before tail */ |
| 5139 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 5140 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5141 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 5142 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5143 | |
| 5144 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5145 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5146 | statep = &state; |
| 5147 | } |
| 5148 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5149 | ctx->ring_fd = ring_fd; |
| 5150 | ctx->ring_file = ring_file; |
| 5151 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5152 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5153 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5154 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5155 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5156 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5157 | req = io_get_req(ctx, statep); |
| 5158 | if (unlikely(!req)) { |
| 5159 | if (!submitted) |
| 5160 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5161 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5162 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5163 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5164 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5165 | break; |
| 5166 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5167 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5168 | /* will complete beyond this point, count as submitted */ |
| 5169 | submitted++; |
| 5170 | |
| 5171 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5172 | err = -EINVAL; |
| 5173 | fail_req: |
| 5174 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5175 | io_double_put_req(req); |
| 5176 | break; |
| 5177 | } |
| 5178 | |
| 5179 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5180 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5181 | if (unlikely(mm_fault)) { |
| 5182 | err = -EFAULT; |
| 5183 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5184 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5185 | use_mm(ctx->sqo_mm); |
| 5186 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5187 | } |
| 5188 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5189 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 5190 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 5191 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5192 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5193 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5194 | } |
| 5195 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 5196 | if (unlikely(submitted != nr)) { |
| 5197 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 5198 | |
| 5199 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 5200 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5201 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5202 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5203 | if (statep) |
| 5204 | io_submit_state_end(&state); |
| 5205 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5206 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5207 | io_commit_sqring(ctx); |
| 5208 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5209 | return submitted; |
| 5210 | } |
| 5211 | |
| 5212 | static int io_sq_thread(void *data) |
| 5213 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5214 | struct io_ring_ctx *ctx = data; |
| 5215 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5216 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5217 | mm_segment_t old_fs; |
| 5218 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5219 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5220 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5221 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5222 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5223 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5224 | old_fs = get_fs(); |
| 5225 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5226 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5227 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5228 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5229 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5230 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5231 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5232 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5233 | unsigned nr_events = 0; |
| 5234 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5235 | mutex_lock(&ctx->uring_lock); |
| 5236 | if (!list_empty(&ctx->poll_list)) |
| 5237 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 5238 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5239 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5240 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5241 | } |
| 5242 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5243 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5244 | |
| 5245 | /* |
| 5246 | * If submit got -EBUSY, flag us as needing the application |
| 5247 | * to enter the kernel to reap and flush events. |
| 5248 | */ |
| 5249 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5250 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 5251 | * Drop cur_mm before scheduling, we can't hold it for |
| 5252 | * long periods (or over schedule()). Do this before |
| 5253 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5254 | * may sleep. |
| 5255 | */ |
| 5256 | if (cur_mm) { |
| 5257 | unuse_mm(cur_mm); |
| 5258 | mmput(cur_mm); |
| 5259 | cur_mm = NULL; |
| 5260 | } |
| 5261 | |
| 5262 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5263 | * We're polling. If we're within the defined idle |
| 5264 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5265 | * to sleep. The exception is if we got EBUSY doing |
| 5266 | * more IO, we should wait for the application to |
| 5267 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5268 | */ |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5269 | if (!list_empty(&ctx->poll_list) || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5270 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5271 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5272 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5273 | continue; |
| 5274 | } |
| 5275 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5276 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5277 | TASK_INTERRUPTIBLE); |
| 5278 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5279 | /* |
| 5280 | * While doing polled IO, before going to sleep, we need |
| 5281 | * to check if there are new reqs added to poll_list, it |
| 5282 | * is because reqs may have been punted to io worker and |
| 5283 | * will be added to poll_list later, hence check the |
| 5284 | * poll_list again. |
| 5285 | */ |
| 5286 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 5287 | !list_empty_careful(&ctx->poll_list)) { |
| 5288 | finish_wait(&ctx->sqo_wait, &wait); |
| 5289 | continue; |
| 5290 | } |
| 5291 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5292 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5293 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5294 | /* make sure to read SQ tail after writing flags */ |
| 5295 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5296 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5297 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5298 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5299 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5300 | finish_wait(&ctx->sqo_wait, &wait); |
| 5301 | break; |
| 5302 | } |
| 5303 | if (signal_pending(current)) |
| 5304 | flush_signals(current); |
| 5305 | schedule(); |
| 5306 | finish_wait(&ctx->sqo_wait, &wait); |
| 5307 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5308 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5309 | continue; |
| 5310 | } |
| 5311 | finish_wait(&ctx->sqo_wait, &wait); |
| 5312 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5313 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5314 | } |
| 5315 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5316 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5317 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5318 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5319 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | set_fs(old_fs); |
| 5323 | if (cur_mm) { |
| 5324 | unuse_mm(cur_mm); |
| 5325 | mmput(cur_mm); |
| 5326 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5327 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5328 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5329 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5330 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5331 | return 0; |
| 5332 | } |
| 5333 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5334 | struct io_wait_queue { |
| 5335 | struct wait_queue_entry wq; |
| 5336 | struct io_ring_ctx *ctx; |
| 5337 | unsigned to_wait; |
| 5338 | unsigned nr_timeouts; |
| 5339 | }; |
| 5340 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5341 | 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] | 5342 | { |
| 5343 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5344 | |
| 5345 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5346 | * 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] | 5347 | * started waiting. For timeouts, we always want to return to userspace, |
| 5348 | * regardless of event count. |
| 5349 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5350 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5351 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5352 | } |
| 5353 | |
| 5354 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5355 | int wake_flags, void *key) |
| 5356 | { |
| 5357 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5358 | wq); |
| 5359 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5360 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5361 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5362 | return -1; |
| 5363 | |
| 5364 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5365 | } |
| 5366 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5367 | /* |
| 5368 | * Wait until events become available, if we don't already have some. The |
| 5369 | * application must reap them itself, as they reside on the shared cq ring. |
| 5370 | */ |
| 5371 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5372 | const sigset_t __user *sig, size_t sigsz) |
| 5373 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5374 | struct io_wait_queue iowq = { |
| 5375 | .wq = { |
| 5376 | .private = current, |
| 5377 | .func = io_wake_function, |
| 5378 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5379 | }, |
| 5380 | .ctx = ctx, |
| 5381 | .to_wait = min_events, |
| 5382 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5383 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5384 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5385 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5386 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5387 | return 0; |
| 5388 | |
| 5389 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5390 | #ifdef CONFIG_COMPAT |
| 5391 | if (in_compat_syscall()) |
| 5392 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5393 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5394 | else |
| 5395 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5396 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5397 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5398 | if (ret) |
| 5399 | return ret; |
| 5400 | } |
| 5401 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5402 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5403 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5404 | do { |
| 5405 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5406 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5407 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5408 | break; |
| 5409 | schedule(); |
| 5410 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5411 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5412 | break; |
| 5413 | } |
| 5414 | } while (1); |
| 5415 | finish_wait(&ctx->wait, &iowq.wq); |
| 5416 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5417 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5418 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5419 | 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] | 5420 | } |
| 5421 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5422 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5423 | { |
| 5424 | #if defined(CONFIG_UNIX) |
| 5425 | if (ctx->ring_sock) { |
| 5426 | struct sock *sock = ctx->ring_sock->sk; |
| 5427 | struct sk_buff *skb; |
| 5428 | |
| 5429 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5430 | kfree_skb(skb); |
| 5431 | } |
| 5432 | #else |
| 5433 | int i; |
| 5434 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5435 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5436 | struct file *file; |
| 5437 | |
| 5438 | file = io_file_from_index(ctx, i); |
| 5439 | if (file) |
| 5440 | fput(file); |
| 5441 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5442 | #endif |
| 5443 | } |
| 5444 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5445 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5446 | { |
| 5447 | struct fixed_file_data *data; |
| 5448 | |
| 5449 | data = container_of(ref, struct fixed_file_data, refs); |
| 5450 | complete(&data->done); |
| 5451 | } |
| 5452 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5453 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5454 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5455 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5456 | unsigned nr_tables, i; |
| 5457 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5458 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5459 | return -ENXIO; |
| 5460 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5461 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5462 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5463 | wait_for_completion(&data->done); |
| 5464 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5465 | percpu_ref_exit(&data->refs); |
| 5466 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5467 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5468 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5469 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5470 | kfree(data->table[i].files); |
| 5471 | kfree(data->table); |
| 5472 | kfree(data); |
| 5473 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5474 | ctx->nr_user_files = 0; |
| 5475 | return 0; |
| 5476 | } |
| 5477 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5478 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5479 | { |
| 5480 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5481 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5482 | /* |
| 5483 | * The park is a bit of a work-around, without it we get |
| 5484 | * warning spews on shutdown with SQPOLL set and affinity |
| 5485 | * set to a single CPU. |
| 5486 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5487 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5488 | kthread_stop(ctx->sqo_thread); |
| 5489 | ctx->sqo_thread = NULL; |
| 5490 | } |
| 5491 | } |
| 5492 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5493 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5494 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5495 | io_sq_thread_stop(ctx); |
| 5496 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5497 | if (ctx->io_wq) { |
| 5498 | io_wq_destroy(ctx->io_wq); |
| 5499 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5500 | } |
| 5501 | } |
| 5502 | |
| 5503 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5504 | /* |
| 5505 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5506 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5507 | * loops in the file referencing. |
| 5508 | */ |
| 5509 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5510 | { |
| 5511 | struct sock *sk = ctx->ring_sock->sk; |
| 5512 | struct scm_fp_list *fpl; |
| 5513 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5514 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5515 | |
| 5516 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5517 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5518 | |
| 5519 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5520 | return -EMFILE; |
| 5521 | } |
| 5522 | |
| 5523 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5524 | if (!fpl) |
| 5525 | return -ENOMEM; |
| 5526 | |
| 5527 | skb = alloc_skb(0, GFP_KERNEL); |
| 5528 | if (!skb) { |
| 5529 | kfree(fpl); |
| 5530 | return -ENOMEM; |
| 5531 | } |
| 5532 | |
| 5533 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5534 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5535 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5536 | fpl->user = get_uid(ctx->user); |
| 5537 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5538 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5539 | |
| 5540 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5541 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5542 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5543 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5544 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5545 | } |
| 5546 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5547 | if (nr_files) { |
| 5548 | fpl->max = SCM_MAX_FD; |
| 5549 | fpl->count = nr_files; |
| 5550 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5551 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5552 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5553 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5554 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5555 | for (i = 0; i < nr_files; i++) |
| 5556 | fput(fpl->fp[i]); |
| 5557 | } else { |
| 5558 | kfree_skb(skb); |
| 5559 | kfree(fpl); |
| 5560 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5561 | |
| 5562 | return 0; |
| 5563 | } |
| 5564 | |
| 5565 | /* |
| 5566 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5567 | * causes regular reference counting to break down. We rely on the UNIX |
| 5568 | * garbage collection to take care of this problem for us. |
| 5569 | */ |
| 5570 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5571 | { |
| 5572 | unsigned left, total; |
| 5573 | int ret = 0; |
| 5574 | |
| 5575 | total = 0; |
| 5576 | left = ctx->nr_user_files; |
| 5577 | while (left) { |
| 5578 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5579 | |
| 5580 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5581 | if (ret) |
| 5582 | break; |
| 5583 | left -= this_files; |
| 5584 | total += this_files; |
| 5585 | } |
| 5586 | |
| 5587 | if (!ret) |
| 5588 | return 0; |
| 5589 | |
| 5590 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5591 | struct file *file = io_file_from_index(ctx, total); |
| 5592 | |
| 5593 | if (file) |
| 5594 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5595 | total++; |
| 5596 | } |
| 5597 | |
| 5598 | return ret; |
| 5599 | } |
| 5600 | #else |
| 5601 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5602 | { |
| 5603 | return 0; |
| 5604 | } |
| 5605 | #endif |
| 5606 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5607 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5608 | unsigned nr_files) |
| 5609 | { |
| 5610 | int i; |
| 5611 | |
| 5612 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5613 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5614 | unsigned this_files; |
| 5615 | |
| 5616 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5617 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5618 | GFP_KERNEL); |
| 5619 | if (!table->files) |
| 5620 | break; |
| 5621 | nr_files -= this_files; |
| 5622 | } |
| 5623 | |
| 5624 | if (i == nr_tables) |
| 5625 | return 0; |
| 5626 | |
| 5627 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5628 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5629 | kfree(table->files); |
| 5630 | } |
| 5631 | return 1; |
| 5632 | } |
| 5633 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5634 | 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] | 5635 | { |
| 5636 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5637 | struct sock *sock = ctx->ring_sock->sk; |
| 5638 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5639 | struct sk_buff *skb; |
| 5640 | int i; |
| 5641 | |
| 5642 | __skb_queue_head_init(&list); |
| 5643 | |
| 5644 | /* |
| 5645 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5646 | * remove this entry and rearrange the file array. |
| 5647 | */ |
| 5648 | skb = skb_dequeue(head); |
| 5649 | while (skb) { |
| 5650 | struct scm_fp_list *fp; |
| 5651 | |
| 5652 | fp = UNIXCB(skb).fp; |
| 5653 | for (i = 0; i < fp->count; i++) { |
| 5654 | int left; |
| 5655 | |
| 5656 | if (fp->fp[i] != file) |
| 5657 | continue; |
| 5658 | |
| 5659 | unix_notinflight(fp->user, fp->fp[i]); |
| 5660 | left = fp->count - 1 - i; |
| 5661 | if (left) { |
| 5662 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5663 | left * sizeof(struct file *)); |
| 5664 | } |
| 5665 | fp->count--; |
| 5666 | if (!fp->count) { |
| 5667 | kfree_skb(skb); |
| 5668 | skb = NULL; |
| 5669 | } else { |
| 5670 | __skb_queue_tail(&list, skb); |
| 5671 | } |
| 5672 | fput(file); |
| 5673 | file = NULL; |
| 5674 | break; |
| 5675 | } |
| 5676 | |
| 5677 | if (!file) |
| 5678 | break; |
| 5679 | |
| 5680 | __skb_queue_tail(&list, skb); |
| 5681 | |
| 5682 | skb = skb_dequeue(head); |
| 5683 | } |
| 5684 | |
| 5685 | if (skb_peek(&list)) { |
| 5686 | spin_lock_irq(&head->lock); |
| 5687 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5688 | __skb_queue_tail(head, skb); |
| 5689 | spin_unlock_irq(&head->lock); |
| 5690 | } |
| 5691 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5692 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5693 | #endif |
| 5694 | } |
| 5695 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5696 | struct io_file_put { |
| 5697 | struct llist_node llist; |
| 5698 | struct file *file; |
| 5699 | struct completion *done; |
| 5700 | }; |
| 5701 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5702 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5703 | { |
| 5704 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5705 | struct llist_node *node; |
| 5706 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5707 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5708 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5709 | io_ring_file_put(data->ctx, pfile->file); |
| 5710 | if (pfile->done) |
| 5711 | complete(pfile->done); |
| 5712 | else |
| 5713 | kfree(pfile); |
| 5714 | } |
| 5715 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5716 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5717 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5718 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5719 | { |
| 5720 | struct fixed_file_data *data; |
| 5721 | |
| 5722 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5723 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5724 | percpu_ref_switch_to_percpu(&data->refs); |
| 5725 | } |
| 5726 | |
| 5727 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5728 | { |
| 5729 | struct fixed_file_data *data; |
| 5730 | |
| 5731 | data = container_of(ref, struct fixed_file_data, refs); |
| 5732 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5733 | /* |
| 5734 | * We can't safely switch from inside this context, punt to wq. If |
| 5735 | * the table ref is going away, the table is being unregistered. |
| 5736 | * Don't queue up the async work for that case, the caller will |
| 5737 | * handle it. |
| 5738 | */ |
| 5739 | if (!percpu_ref_is_dying(&data->refs)) |
| 5740 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5744 | unsigned nr_args) |
| 5745 | { |
| 5746 | __s32 __user *fds = (__s32 __user *) arg; |
| 5747 | unsigned nr_tables; |
| 5748 | struct file *file; |
| 5749 | int fd, ret = 0; |
| 5750 | unsigned i; |
| 5751 | |
| 5752 | if (ctx->file_data) |
| 5753 | return -EBUSY; |
| 5754 | if (!nr_args) |
| 5755 | return -EINVAL; |
| 5756 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5757 | return -EMFILE; |
| 5758 | |
| 5759 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5760 | if (!ctx->file_data) |
| 5761 | return -ENOMEM; |
| 5762 | ctx->file_data->ctx = ctx; |
| 5763 | init_completion(&ctx->file_data->done); |
| 5764 | |
| 5765 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5766 | ctx->file_data->table = kcalloc(nr_tables, |
| 5767 | sizeof(struct fixed_file_table), |
| 5768 | GFP_KERNEL); |
| 5769 | if (!ctx->file_data->table) { |
| 5770 | kfree(ctx->file_data); |
| 5771 | ctx->file_data = NULL; |
| 5772 | return -ENOMEM; |
| 5773 | } |
| 5774 | |
| 5775 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5776 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5777 | kfree(ctx->file_data->table); |
| 5778 | kfree(ctx->file_data); |
| 5779 | ctx->file_data = NULL; |
| 5780 | return -ENOMEM; |
| 5781 | } |
| 5782 | ctx->file_data->put_llist.first = NULL; |
| 5783 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5784 | |
| 5785 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5786 | percpu_ref_exit(&ctx->file_data->refs); |
| 5787 | kfree(ctx->file_data->table); |
| 5788 | kfree(ctx->file_data); |
| 5789 | ctx->file_data = NULL; |
| 5790 | return -ENOMEM; |
| 5791 | } |
| 5792 | |
| 5793 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5794 | struct fixed_file_table *table; |
| 5795 | unsigned index; |
| 5796 | |
| 5797 | ret = -EFAULT; |
| 5798 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5799 | break; |
| 5800 | /* allow sparse sets */ |
| 5801 | if (fd == -1) { |
| 5802 | ret = 0; |
| 5803 | continue; |
| 5804 | } |
| 5805 | |
| 5806 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5807 | index = i & IORING_FILE_TABLE_MASK; |
| 5808 | file = fget(fd); |
| 5809 | |
| 5810 | ret = -EBADF; |
| 5811 | if (!file) |
| 5812 | break; |
| 5813 | |
| 5814 | /* |
| 5815 | * Don't allow io_uring instances to be registered. If UNIX |
| 5816 | * isn't enabled, then this causes a reference cycle and this |
| 5817 | * instance can never get freed. If UNIX is enabled we'll |
| 5818 | * handle it just fine, but there's still no point in allowing |
| 5819 | * a ring fd as it doesn't support regular read/write anyway. |
| 5820 | */ |
| 5821 | if (file->f_op == &io_uring_fops) { |
| 5822 | fput(file); |
| 5823 | break; |
| 5824 | } |
| 5825 | ret = 0; |
| 5826 | table->files[index] = file; |
| 5827 | } |
| 5828 | |
| 5829 | if (ret) { |
| 5830 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5831 | file = io_file_from_index(ctx, i); |
| 5832 | if (file) |
| 5833 | fput(file); |
| 5834 | } |
| 5835 | for (i = 0; i < nr_tables; i++) |
| 5836 | kfree(ctx->file_data->table[i].files); |
| 5837 | |
| 5838 | kfree(ctx->file_data->table); |
| 5839 | kfree(ctx->file_data); |
| 5840 | ctx->file_data = NULL; |
| 5841 | ctx->nr_user_files = 0; |
| 5842 | return ret; |
| 5843 | } |
| 5844 | |
| 5845 | ret = io_sqe_files_scm(ctx); |
| 5846 | if (ret) |
| 5847 | io_sqe_files_unregister(ctx); |
| 5848 | |
| 5849 | return ret; |
| 5850 | } |
| 5851 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5852 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5853 | int index) |
| 5854 | { |
| 5855 | #if defined(CONFIG_UNIX) |
| 5856 | struct sock *sock = ctx->ring_sock->sk; |
| 5857 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5858 | struct sk_buff *skb; |
| 5859 | |
| 5860 | /* |
| 5861 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5862 | * file set. If there's no room, fall back to allocating a new skb |
| 5863 | * and filling it in. |
| 5864 | */ |
| 5865 | spin_lock_irq(&head->lock); |
| 5866 | skb = skb_peek(head); |
| 5867 | if (skb) { |
| 5868 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5869 | |
| 5870 | if (fpl->count < SCM_MAX_FD) { |
| 5871 | __skb_unlink(skb, head); |
| 5872 | spin_unlock_irq(&head->lock); |
| 5873 | fpl->fp[fpl->count] = get_file(file); |
| 5874 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5875 | fpl->count++; |
| 5876 | spin_lock_irq(&head->lock); |
| 5877 | __skb_queue_head(head, skb); |
| 5878 | } else { |
| 5879 | skb = NULL; |
| 5880 | } |
| 5881 | } |
| 5882 | spin_unlock_irq(&head->lock); |
| 5883 | |
| 5884 | if (skb) { |
| 5885 | fput(file); |
| 5886 | return 0; |
| 5887 | } |
| 5888 | |
| 5889 | return __io_sqe_files_scm(ctx, 1, index); |
| 5890 | #else |
| 5891 | return 0; |
| 5892 | #endif |
| 5893 | } |
| 5894 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5895 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5896 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5897 | struct fixed_file_data *data; |
| 5898 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5899 | /* |
| 5900 | * Juggle reference to ensure we hit zero, if needed, so we can |
| 5901 | * switch back to percpu mode |
| 5902 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5903 | data = container_of(ref, struct fixed_file_data, refs); |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5904 | percpu_ref_put(&data->refs); |
| 5905 | percpu_ref_get(&data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5906 | } |
| 5907 | |
| 5908 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5909 | struct file *file) |
| 5910 | { |
| 5911 | struct io_file_put *pfile, pfile_stack; |
| 5912 | DECLARE_COMPLETION_ONSTACK(done); |
| 5913 | |
| 5914 | /* |
| 5915 | * If we fail allocating the struct we need for doing async reomval |
| 5916 | * of this file, just punt to sync and wait for it. |
| 5917 | */ |
| 5918 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5919 | if (!pfile) { |
| 5920 | pfile = &pfile_stack; |
| 5921 | pfile->done = &done; |
| 5922 | } |
| 5923 | |
| 5924 | pfile->file = file; |
| 5925 | llist_add(&pfile->llist, &data->put_llist); |
| 5926 | |
| 5927 | if (pfile == &pfile_stack) { |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5928 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5929 | wait_for_completion(&done); |
| 5930 | flush_work(&data->ref_work); |
| 5931 | return false; |
| 5932 | } |
| 5933 | |
| 5934 | return true; |
| 5935 | } |
| 5936 | |
| 5937 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5938 | struct io_uring_files_update *up, |
| 5939 | unsigned nr_args) |
| 5940 | { |
| 5941 | struct fixed_file_data *data = ctx->file_data; |
| 5942 | bool ref_switch = false; |
| 5943 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5944 | __s32 __user *fds; |
| 5945 | int fd, i, err; |
| 5946 | __u32 done; |
| 5947 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5948 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5949 | return -EOVERFLOW; |
| 5950 | if (done > ctx->nr_user_files) |
| 5951 | return -EINVAL; |
| 5952 | |
| 5953 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5954 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5955 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5956 | struct fixed_file_table *table; |
| 5957 | unsigned index; |
| 5958 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5959 | err = 0; |
| 5960 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5961 | err = -EFAULT; |
| 5962 | break; |
| 5963 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5964 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5965 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5966 | index = i & IORING_FILE_TABLE_MASK; |
| 5967 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5968 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5969 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5970 | if (io_queue_file_removal(data, file)) |
| 5971 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5972 | } |
| 5973 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5974 | file = fget(fd); |
| 5975 | if (!file) { |
| 5976 | err = -EBADF; |
| 5977 | break; |
| 5978 | } |
| 5979 | /* |
| 5980 | * Don't allow io_uring instances to be registered. If |
| 5981 | * UNIX isn't enabled, then this causes a reference |
| 5982 | * cycle and this instance can never get freed. If UNIX |
| 5983 | * is enabled we'll handle it just fine, but there's |
| 5984 | * still no point in allowing a ring fd as it doesn't |
| 5985 | * support regular read/write anyway. |
| 5986 | */ |
| 5987 | if (file->f_op == &io_uring_fops) { |
| 5988 | fput(file); |
| 5989 | err = -EBADF; |
| 5990 | break; |
| 5991 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5992 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5993 | err = io_sqe_file_register(ctx, file, i); |
| 5994 | if (err) |
| 5995 | break; |
| 5996 | } |
| 5997 | nr_args--; |
| 5998 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5999 | up->offset++; |
| 6000 | } |
| 6001 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6002 | if (ref_switch) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6003 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6004 | |
| 6005 | return done ? done : err; |
| 6006 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6007 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 6008 | unsigned nr_args) |
| 6009 | { |
| 6010 | struct io_uring_files_update up; |
| 6011 | |
| 6012 | if (!ctx->file_data) |
| 6013 | return -ENXIO; |
| 6014 | if (!nr_args) |
| 6015 | return -EINVAL; |
| 6016 | if (copy_from_user(&up, arg, sizeof(up))) |
| 6017 | return -EFAULT; |
| 6018 | if (up.resv) |
| 6019 | return -EINVAL; |
| 6020 | |
| 6021 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 6022 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6023 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 6024 | static void io_put_work(struct io_wq_work *work) |
| 6025 | { |
| 6026 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6027 | |
| 6028 | io_put_req(req); |
| 6029 | } |
| 6030 | |
| 6031 | static void io_get_work(struct io_wq_work *work) |
| 6032 | { |
| 6033 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6034 | |
| 6035 | refcount_inc(&req->refs); |
| 6036 | } |
| 6037 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6038 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 6039 | struct io_uring_params *p) |
| 6040 | { |
| 6041 | struct io_wq_data data; |
| 6042 | struct fd f; |
| 6043 | struct io_ring_ctx *ctx_attach; |
| 6044 | unsigned int concurrency; |
| 6045 | int ret = 0; |
| 6046 | |
| 6047 | data.user = ctx->user; |
| 6048 | data.get_work = io_get_work; |
| 6049 | data.put_work = io_put_work; |
| 6050 | |
| 6051 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 6052 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 6053 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 6054 | |
| 6055 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 6056 | if (IS_ERR(ctx->io_wq)) { |
| 6057 | ret = PTR_ERR(ctx->io_wq); |
| 6058 | ctx->io_wq = NULL; |
| 6059 | } |
| 6060 | return ret; |
| 6061 | } |
| 6062 | |
| 6063 | f = fdget(p->wq_fd); |
| 6064 | if (!f.file) |
| 6065 | return -EBADF; |
| 6066 | |
| 6067 | if (f.file->f_op != &io_uring_fops) { |
| 6068 | ret = -EINVAL; |
| 6069 | goto out_fput; |
| 6070 | } |
| 6071 | |
| 6072 | ctx_attach = f.file->private_data; |
| 6073 | /* @io_wq is protected by holding the fd */ |
| 6074 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 6075 | ret = -EINVAL; |
| 6076 | goto out_fput; |
| 6077 | } |
| 6078 | |
| 6079 | ctx->io_wq = ctx_attach->io_wq; |
| 6080 | out_fput: |
| 6081 | fdput(f); |
| 6082 | return ret; |
| 6083 | } |
| 6084 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6085 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 6086 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6087 | { |
| 6088 | int ret; |
| 6089 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6090 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6091 | mmgrab(current->mm); |
| 6092 | ctx->sqo_mm = current->mm; |
| 6093 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6094 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 6095 | ret = -EPERM; |
| 6096 | if (!capable(CAP_SYS_ADMIN)) |
| 6097 | goto err; |
| 6098 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6099 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 6100 | if (!ctx->sq_thread_idle) |
| 6101 | ctx->sq_thread_idle = HZ; |
| 6102 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6103 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6104 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6105 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6106 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6107 | if (cpu >= nr_cpu_ids) |
| 6108 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 6109 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6110 | goto err; |
| 6111 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6112 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 6113 | ctx, cpu, |
| 6114 | "io_uring-sq"); |
| 6115 | } else { |
| 6116 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 6117 | "io_uring-sq"); |
| 6118 | } |
| 6119 | if (IS_ERR(ctx->sqo_thread)) { |
| 6120 | ret = PTR_ERR(ctx->sqo_thread); |
| 6121 | ctx->sqo_thread = NULL; |
| 6122 | goto err; |
| 6123 | } |
| 6124 | wake_up_process(ctx->sqo_thread); |
| 6125 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 6126 | /* Can't have SQ_AFF without SQPOLL */ |
| 6127 | ret = -EINVAL; |
| 6128 | goto err; |
| 6129 | } |
| 6130 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6131 | ret = io_init_wq_offload(ctx, p); |
| 6132 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6133 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6134 | |
| 6135 | return 0; |
| 6136 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 6137 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6138 | mmdrop(ctx->sqo_mm); |
| 6139 | ctx->sqo_mm = NULL; |
| 6140 | return ret; |
| 6141 | } |
| 6142 | |
| 6143 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 6144 | { |
| 6145 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 6146 | } |
| 6147 | |
| 6148 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 6149 | { |
| 6150 | unsigned long page_limit, cur_pages, new_pages; |
| 6151 | |
| 6152 | /* Don't allow more pages than we can safely lock */ |
| 6153 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 6154 | |
| 6155 | do { |
| 6156 | cur_pages = atomic_long_read(&user->locked_vm); |
| 6157 | new_pages = cur_pages + nr_pages; |
| 6158 | if (new_pages > page_limit) |
| 6159 | return -ENOMEM; |
| 6160 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 6161 | new_pages) != cur_pages); |
| 6162 | |
| 6163 | return 0; |
| 6164 | } |
| 6165 | |
| 6166 | static void io_mem_free(void *ptr) |
| 6167 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6168 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6169 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6170 | if (!ptr) |
| 6171 | return; |
| 6172 | |
| 6173 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6174 | if (put_page_testzero(page)) |
| 6175 | free_compound_page(page); |
| 6176 | } |
| 6177 | |
| 6178 | static void *io_mem_alloc(size_t size) |
| 6179 | { |
| 6180 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 6181 | __GFP_NORETRY; |
| 6182 | |
| 6183 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 6184 | } |
| 6185 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6186 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 6187 | size_t *sq_offset) |
| 6188 | { |
| 6189 | struct io_rings *rings; |
| 6190 | size_t off, sq_array_size; |
| 6191 | |
| 6192 | off = struct_size(rings, cqes, cq_entries); |
| 6193 | if (off == SIZE_MAX) |
| 6194 | return SIZE_MAX; |
| 6195 | |
| 6196 | #ifdef CONFIG_SMP |
| 6197 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6198 | if (off == 0) |
| 6199 | return SIZE_MAX; |
| 6200 | #endif |
| 6201 | |
| 6202 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6203 | if (sq_array_size == SIZE_MAX) |
| 6204 | return SIZE_MAX; |
| 6205 | |
| 6206 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6207 | return SIZE_MAX; |
| 6208 | |
| 6209 | if (sq_offset) |
| 6210 | *sq_offset = off; |
| 6211 | |
| 6212 | return off; |
| 6213 | } |
| 6214 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6215 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6216 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6217 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6218 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6219 | pages = (size_t)1 << get_order( |
| 6220 | rings_size(sq_entries, cq_entries, NULL)); |
| 6221 | pages += (size_t)1 << get_order( |
| 6222 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6223 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6224 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6225 | } |
| 6226 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6227 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6228 | { |
| 6229 | int i, j; |
| 6230 | |
| 6231 | if (!ctx->user_bufs) |
| 6232 | return -ENXIO; |
| 6233 | |
| 6234 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6235 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6236 | |
| 6237 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6238 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6239 | |
| 6240 | if (ctx->account_mem) |
| 6241 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6242 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6243 | imu->nr_bvecs = 0; |
| 6244 | } |
| 6245 | |
| 6246 | kfree(ctx->user_bufs); |
| 6247 | ctx->user_bufs = NULL; |
| 6248 | ctx->nr_user_bufs = 0; |
| 6249 | return 0; |
| 6250 | } |
| 6251 | |
| 6252 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6253 | void __user *arg, unsigned index) |
| 6254 | { |
| 6255 | struct iovec __user *src; |
| 6256 | |
| 6257 | #ifdef CONFIG_COMPAT |
| 6258 | if (ctx->compat) { |
| 6259 | struct compat_iovec __user *ciovs; |
| 6260 | struct compat_iovec ciov; |
| 6261 | |
| 6262 | ciovs = (struct compat_iovec __user *) arg; |
| 6263 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6264 | return -EFAULT; |
| 6265 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6266 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6267 | dst->iov_len = ciov.iov_len; |
| 6268 | return 0; |
| 6269 | } |
| 6270 | #endif |
| 6271 | src = (struct iovec __user *) arg; |
| 6272 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6273 | return -EFAULT; |
| 6274 | return 0; |
| 6275 | } |
| 6276 | |
| 6277 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6278 | unsigned nr_args) |
| 6279 | { |
| 6280 | struct vm_area_struct **vmas = NULL; |
| 6281 | struct page **pages = NULL; |
| 6282 | int i, j, got_pages = 0; |
| 6283 | int ret = -EINVAL; |
| 6284 | |
| 6285 | if (ctx->user_bufs) |
| 6286 | return -EBUSY; |
| 6287 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6288 | return -EINVAL; |
| 6289 | |
| 6290 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6291 | GFP_KERNEL); |
| 6292 | if (!ctx->user_bufs) |
| 6293 | return -ENOMEM; |
| 6294 | |
| 6295 | for (i = 0; i < nr_args; i++) { |
| 6296 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6297 | unsigned long off, start, end, ubuf; |
| 6298 | int pret, nr_pages; |
| 6299 | struct iovec iov; |
| 6300 | size_t size; |
| 6301 | |
| 6302 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6303 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6304 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6305 | |
| 6306 | /* |
| 6307 | * Don't impose further limits on the size and buffer |
| 6308 | * constraints here, we'll -EINVAL later when IO is |
| 6309 | * submitted if they are wrong. |
| 6310 | */ |
| 6311 | ret = -EFAULT; |
| 6312 | if (!iov.iov_base || !iov.iov_len) |
| 6313 | goto err; |
| 6314 | |
| 6315 | /* arbitrary limit, but we need something */ |
| 6316 | if (iov.iov_len > SZ_1G) |
| 6317 | goto err; |
| 6318 | |
| 6319 | ubuf = (unsigned long) iov.iov_base; |
| 6320 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6321 | start = ubuf >> PAGE_SHIFT; |
| 6322 | nr_pages = end - start; |
| 6323 | |
| 6324 | if (ctx->account_mem) { |
| 6325 | ret = io_account_mem(ctx->user, nr_pages); |
| 6326 | if (ret) |
| 6327 | goto err; |
| 6328 | } |
| 6329 | |
| 6330 | ret = 0; |
| 6331 | if (!pages || nr_pages > got_pages) { |
| 6332 | kfree(vmas); |
| 6333 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6334 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6335 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6336 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6337 | sizeof(struct vm_area_struct *), |
| 6338 | GFP_KERNEL); |
| 6339 | if (!pages || !vmas) { |
| 6340 | ret = -ENOMEM; |
| 6341 | if (ctx->account_mem) |
| 6342 | io_unaccount_mem(ctx->user, nr_pages); |
| 6343 | goto err; |
| 6344 | } |
| 6345 | got_pages = nr_pages; |
| 6346 | } |
| 6347 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6348 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6349 | GFP_KERNEL); |
| 6350 | ret = -ENOMEM; |
| 6351 | if (!imu->bvec) { |
| 6352 | if (ctx->account_mem) |
| 6353 | io_unaccount_mem(ctx->user, nr_pages); |
| 6354 | goto err; |
| 6355 | } |
| 6356 | |
| 6357 | ret = 0; |
| 6358 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6359 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6360 | FOLL_WRITE | FOLL_LONGTERM, |
| 6361 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6362 | if (pret == nr_pages) { |
| 6363 | /* don't support file backed memory */ |
| 6364 | for (j = 0; j < nr_pages; j++) { |
| 6365 | struct vm_area_struct *vma = vmas[j]; |
| 6366 | |
| 6367 | if (vma->vm_file && |
| 6368 | !is_file_hugepages(vma->vm_file)) { |
| 6369 | ret = -EOPNOTSUPP; |
| 6370 | break; |
| 6371 | } |
| 6372 | } |
| 6373 | } else { |
| 6374 | ret = pret < 0 ? pret : -EFAULT; |
| 6375 | } |
| 6376 | up_read(¤t->mm->mmap_sem); |
| 6377 | if (ret) { |
| 6378 | /* |
| 6379 | * if we did partial map, or found file backed vmas, |
| 6380 | * release any pages we did get |
| 6381 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6382 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6383 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6384 | if (ctx->account_mem) |
| 6385 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6386 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6387 | goto err; |
| 6388 | } |
| 6389 | |
| 6390 | off = ubuf & ~PAGE_MASK; |
| 6391 | size = iov.iov_len; |
| 6392 | for (j = 0; j < nr_pages; j++) { |
| 6393 | size_t vec_len; |
| 6394 | |
| 6395 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6396 | imu->bvec[j].bv_page = pages[j]; |
| 6397 | imu->bvec[j].bv_len = vec_len; |
| 6398 | imu->bvec[j].bv_offset = off; |
| 6399 | off = 0; |
| 6400 | size -= vec_len; |
| 6401 | } |
| 6402 | /* store original address for later verification */ |
| 6403 | imu->ubuf = ubuf; |
| 6404 | imu->len = iov.iov_len; |
| 6405 | imu->nr_bvecs = nr_pages; |
| 6406 | |
| 6407 | ctx->nr_user_bufs++; |
| 6408 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6409 | kvfree(pages); |
| 6410 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6411 | return 0; |
| 6412 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6413 | kvfree(pages); |
| 6414 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6415 | io_sqe_buffer_unregister(ctx); |
| 6416 | return ret; |
| 6417 | } |
| 6418 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6419 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6420 | { |
| 6421 | __s32 __user *fds = arg; |
| 6422 | int fd; |
| 6423 | |
| 6424 | if (ctx->cq_ev_fd) |
| 6425 | return -EBUSY; |
| 6426 | |
| 6427 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6428 | return -EFAULT; |
| 6429 | |
| 6430 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6431 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6432 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6433 | ctx->cq_ev_fd = NULL; |
| 6434 | return ret; |
| 6435 | } |
| 6436 | |
| 6437 | return 0; |
| 6438 | } |
| 6439 | |
| 6440 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6441 | { |
| 6442 | if (ctx->cq_ev_fd) { |
| 6443 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6444 | ctx->cq_ev_fd = NULL; |
| 6445 | return 0; |
| 6446 | } |
| 6447 | |
| 6448 | return -ENXIO; |
| 6449 | } |
| 6450 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6451 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6452 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6453 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6454 | if (ctx->sqo_mm) |
| 6455 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6456 | |
| 6457 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6458 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6459 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6460 | io_eventfd_unregister(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 6461 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6462 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6463 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6464 | if (ctx->ring_sock) { |
| 6465 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6466 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6467 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6468 | #endif |
| 6469 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6470 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6471 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6472 | |
| 6473 | percpu_ref_exit(&ctx->refs); |
| 6474 | if (ctx->account_mem) |
| 6475 | io_unaccount_mem(ctx->user, |
| 6476 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6477 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6478 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6479 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6480 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6481 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6482 | kfree(ctx); |
| 6483 | } |
| 6484 | |
| 6485 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6486 | { |
| 6487 | struct io_ring_ctx *ctx = file->private_data; |
| 6488 | __poll_t mask = 0; |
| 6489 | |
| 6490 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6491 | /* |
| 6492 | * synchronizes with barrier from wq_has_sleeper call in |
| 6493 | * io_commit_cqring |
| 6494 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6495 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6496 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6497 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6498 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 6499 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6500 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6501 | |
| 6502 | return mask; |
| 6503 | } |
| 6504 | |
| 6505 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6506 | { |
| 6507 | struct io_ring_ctx *ctx = file->private_data; |
| 6508 | |
| 6509 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6510 | } |
| 6511 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6512 | static int io_remove_personalities(int id, void *p, void *data) |
| 6513 | { |
| 6514 | struct io_ring_ctx *ctx = data; |
| 6515 | const struct cred *cred; |
| 6516 | |
| 6517 | cred = idr_remove(&ctx->personality_idr, id); |
| 6518 | if (cred) |
| 6519 | put_cred(cred); |
| 6520 | return 0; |
| 6521 | } |
| 6522 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6523 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6524 | { |
| 6525 | mutex_lock(&ctx->uring_lock); |
| 6526 | percpu_ref_kill(&ctx->refs); |
| 6527 | mutex_unlock(&ctx->uring_lock); |
| 6528 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6529 | /* |
| 6530 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 6531 | * work after we've killed the ctx ref above. This is important to do |
| 6532 | * before we cancel existing commands, as the thread could otherwise |
| 6533 | * be queueing new work post that. If that's work we need to cancel, |
| 6534 | * it could cause shutdown to hang. |
| 6535 | */ |
| 6536 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 6537 | cpu_relax(); |
| 6538 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6539 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6540 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6541 | |
| 6542 | if (ctx->io_wq) |
| 6543 | io_wq_cancel_all(ctx->io_wq); |
| 6544 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6545 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6546 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6547 | if (ctx->rings) |
| 6548 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6549 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6550 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6551 | io_ring_ctx_free(ctx); |
| 6552 | } |
| 6553 | |
| 6554 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6555 | { |
| 6556 | struct io_ring_ctx *ctx = file->private_data; |
| 6557 | |
| 6558 | file->private_data = NULL; |
| 6559 | io_ring_ctx_wait_and_kill(ctx); |
| 6560 | return 0; |
| 6561 | } |
| 6562 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6563 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6564 | struct files_struct *files) |
| 6565 | { |
| 6566 | struct io_kiocb *req; |
| 6567 | DEFINE_WAIT(wait); |
| 6568 | |
| 6569 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6570 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6571 | |
| 6572 | spin_lock_irq(&ctx->inflight_lock); |
| 6573 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6574 | if (req->work.files != files) |
| 6575 | continue; |
| 6576 | /* req is being completed, ignore */ |
| 6577 | if (!refcount_inc_not_zero(&req->refs)) |
| 6578 | continue; |
| 6579 | cancel_req = req; |
| 6580 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6581 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6582 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6583 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6584 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6585 | spin_unlock_irq(&ctx->inflight_lock); |
| 6586 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6587 | /* We need to keep going until we don't find a matching req */ |
| 6588 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6589 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6590 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 6591 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 6592 | spin_lock_irq(&ctx->completion_lock); |
| 6593 | list_del(&cancel_req->list); |
| 6594 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 6595 | if (list_empty(&ctx->cq_overflow_list)) { |
| 6596 | clear_bit(0, &ctx->sq_check_overflow); |
| 6597 | clear_bit(0, &ctx->cq_check_overflow); |
| 6598 | } |
| 6599 | spin_unlock_irq(&ctx->completion_lock); |
| 6600 | |
| 6601 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 6602 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 6603 | |
| 6604 | /* |
| 6605 | * Put inflight ref and overflow ref. If that's |
| 6606 | * all we had, then we're done with this request. |
| 6607 | */ |
| 6608 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
| 6609 | io_put_req(cancel_req); |
| 6610 | continue; |
| 6611 | } |
| 6612 | } |
| 6613 | |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6614 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6615 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6616 | schedule(); |
| 6617 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6618 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6619 | } |
| 6620 | |
| 6621 | static int io_uring_flush(struct file *file, void *data) |
| 6622 | { |
| 6623 | struct io_ring_ctx *ctx = file->private_data; |
| 6624 | |
| 6625 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 6626 | |
| 6627 | /* |
| 6628 | * If the task is going away, cancel work it may have pending |
| 6629 | */ |
| 6630 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 6631 | io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current)); |
| 6632 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6633 | return 0; |
| 6634 | } |
| 6635 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6636 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6637 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6638 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6639 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6640 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6641 | struct page *page; |
| 6642 | void *ptr; |
| 6643 | |
| 6644 | switch (offset) { |
| 6645 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6646 | case IORING_OFF_CQ_RING: |
| 6647 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6648 | break; |
| 6649 | case IORING_OFF_SQES: |
| 6650 | ptr = ctx->sq_sqes; |
| 6651 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6652 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6653 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6654 | } |
| 6655 | |
| 6656 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6657 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6658 | return ERR_PTR(-EINVAL); |
| 6659 | |
| 6660 | return ptr; |
| 6661 | } |
| 6662 | |
| 6663 | #ifdef CONFIG_MMU |
| 6664 | |
| 6665 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6666 | { |
| 6667 | size_t sz = vma->vm_end - vma->vm_start; |
| 6668 | unsigned long pfn; |
| 6669 | void *ptr; |
| 6670 | |
| 6671 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6672 | if (IS_ERR(ptr)) |
| 6673 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6674 | |
| 6675 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6676 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6677 | } |
| 6678 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6679 | #else /* !CONFIG_MMU */ |
| 6680 | |
| 6681 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6682 | { |
| 6683 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6684 | } |
| 6685 | |
| 6686 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6687 | { |
| 6688 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6689 | } |
| 6690 | |
| 6691 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6692 | unsigned long addr, unsigned long len, |
| 6693 | unsigned long pgoff, unsigned long flags) |
| 6694 | { |
| 6695 | void *ptr; |
| 6696 | |
| 6697 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6698 | if (IS_ERR(ptr)) |
| 6699 | return PTR_ERR(ptr); |
| 6700 | |
| 6701 | return (unsigned long) ptr; |
| 6702 | } |
| 6703 | |
| 6704 | #endif /* !CONFIG_MMU */ |
| 6705 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6706 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6707 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6708 | size_t, sigsz) |
| 6709 | { |
| 6710 | struct io_ring_ctx *ctx; |
| 6711 | long ret = -EBADF; |
| 6712 | int submitted = 0; |
| 6713 | struct fd f; |
| 6714 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6715 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6716 | return -EINVAL; |
| 6717 | |
| 6718 | f = fdget(fd); |
| 6719 | if (!f.file) |
| 6720 | return -EBADF; |
| 6721 | |
| 6722 | ret = -EOPNOTSUPP; |
| 6723 | if (f.file->f_op != &io_uring_fops) |
| 6724 | goto out_fput; |
| 6725 | |
| 6726 | ret = -ENXIO; |
| 6727 | ctx = f.file->private_data; |
| 6728 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6729 | goto out_fput; |
| 6730 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6731 | /* |
| 6732 | * For SQ polling, the thread will do all submissions and completions. |
| 6733 | * Just return the requested submit count, and wake the thread if |
| 6734 | * we were asked to. |
| 6735 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6736 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6737 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6738 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6739 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6740 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6741 | wake_up(&ctx->sqo_wait); |
| 6742 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6743 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6744 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6745 | |
| 6746 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6747 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6748 | cur_mm = ctx->sqo_mm; |
| 6749 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6750 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6751 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6752 | |
| 6753 | if (submitted != to_submit) |
| 6754 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6755 | } |
| 6756 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6757 | unsigned nr_events = 0; |
| 6758 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6759 | min_complete = min(min_complete, ctx->cq_entries); |
| 6760 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6761 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6762 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6763 | } else { |
| 6764 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6765 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6766 | } |
| 6767 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6768 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6769 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6770 | out_fput: |
| 6771 | fdput(f); |
| 6772 | return submitted ? submitted : ret; |
| 6773 | } |
| 6774 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6775 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6776 | static int io_uring_show_cred(int id, void *p, void *data) |
| 6777 | { |
| 6778 | const struct cred *cred = p; |
| 6779 | struct seq_file *m = data; |
| 6780 | struct user_namespace *uns = seq_user_ns(m); |
| 6781 | struct group_info *gi; |
| 6782 | kernel_cap_t cap; |
| 6783 | unsigned __capi; |
| 6784 | int g; |
| 6785 | |
| 6786 | seq_printf(m, "%5d\n", id); |
| 6787 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 6788 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 6789 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 6790 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 6791 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 6792 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 6793 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 6794 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 6795 | seq_puts(m, "\n\tGroups:\t"); |
| 6796 | gi = cred->group_info; |
| 6797 | for (g = 0; g < gi->ngroups; g++) { |
| 6798 | seq_put_decimal_ull(m, g ? " " : "", |
| 6799 | from_kgid_munged(uns, gi->gid[g])); |
| 6800 | } |
| 6801 | seq_puts(m, "\n\tCapEff:\t"); |
| 6802 | cap = cred->cap_effective; |
| 6803 | CAP_FOR_EACH_U32(__capi) |
| 6804 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 6805 | seq_putc(m, '\n'); |
| 6806 | return 0; |
| 6807 | } |
| 6808 | |
| 6809 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 6810 | { |
| 6811 | int i; |
| 6812 | |
| 6813 | mutex_lock(&ctx->uring_lock); |
| 6814 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 6815 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6816 | struct fixed_file_table *table; |
| 6817 | struct file *f; |
| 6818 | |
| 6819 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6820 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 6821 | if (f) |
| 6822 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 6823 | else |
| 6824 | seq_printf(m, "%5u: <none>\n", i); |
| 6825 | } |
| 6826 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 6827 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6828 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 6829 | |
| 6830 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 6831 | (unsigned int) buf->len); |
| 6832 | } |
| 6833 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 6834 | seq_printf(m, "Personalities:\n"); |
| 6835 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 6836 | } |
| 6837 | mutex_unlock(&ctx->uring_lock); |
| 6838 | } |
| 6839 | |
| 6840 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 6841 | { |
| 6842 | struct io_ring_ctx *ctx = f->private_data; |
| 6843 | |
| 6844 | if (percpu_ref_tryget(&ctx->refs)) { |
| 6845 | __io_uring_show_fdinfo(ctx, m); |
| 6846 | percpu_ref_put(&ctx->refs); |
| 6847 | } |
| 6848 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6849 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6850 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6851 | static const struct file_operations io_uring_fops = { |
| 6852 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6853 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6854 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6855 | #ifndef CONFIG_MMU |
| 6856 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6857 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6858 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6859 | .poll = io_uring_poll, |
| 6860 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6861 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6862 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6863 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6864 | }; |
| 6865 | |
| 6866 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6867 | struct io_uring_params *p) |
| 6868 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6869 | struct io_rings *rings; |
| 6870 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6871 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6872 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6873 | if (size == SIZE_MAX) |
| 6874 | return -EOVERFLOW; |
| 6875 | |
| 6876 | rings = io_mem_alloc(size); |
| 6877 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6878 | return -ENOMEM; |
| 6879 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6880 | ctx->rings = rings; |
| 6881 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6882 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6883 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6884 | rings->sq_ring_entries = p->sq_entries; |
| 6885 | rings->cq_ring_entries = p->cq_entries; |
| 6886 | ctx->sq_mask = rings->sq_ring_mask; |
| 6887 | ctx->cq_mask = rings->cq_ring_mask; |
| 6888 | ctx->sq_entries = rings->sq_ring_entries; |
| 6889 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6890 | |
| 6891 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6892 | if (size == SIZE_MAX) { |
| 6893 | io_mem_free(ctx->rings); |
| 6894 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6895 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6896 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6897 | |
| 6898 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6899 | if (!ctx->sq_sqes) { |
| 6900 | io_mem_free(ctx->rings); |
| 6901 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6902 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6903 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6904 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6905 | return 0; |
| 6906 | } |
| 6907 | |
| 6908 | /* |
| 6909 | * Allocate an anonymous fd, this is what constitutes the application |
| 6910 | * visible backing of an io_uring instance. The application mmaps this |
| 6911 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6912 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6913 | */ |
| 6914 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6915 | { |
| 6916 | struct file *file; |
| 6917 | int ret; |
| 6918 | |
| 6919 | #if defined(CONFIG_UNIX) |
| 6920 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6921 | &ctx->ring_sock); |
| 6922 | if (ret) |
| 6923 | return ret; |
| 6924 | #endif |
| 6925 | |
| 6926 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6927 | if (ret < 0) |
| 6928 | goto err; |
| 6929 | |
| 6930 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6931 | O_RDWR | O_CLOEXEC); |
| 6932 | if (IS_ERR(file)) { |
| 6933 | put_unused_fd(ret); |
| 6934 | ret = PTR_ERR(file); |
| 6935 | goto err; |
| 6936 | } |
| 6937 | |
| 6938 | #if defined(CONFIG_UNIX) |
| 6939 | ctx->ring_sock->file = file; |
| 6940 | #endif |
| 6941 | fd_install(ret, file); |
| 6942 | return ret; |
| 6943 | err: |
| 6944 | #if defined(CONFIG_UNIX) |
| 6945 | sock_release(ctx->ring_sock); |
| 6946 | ctx->ring_sock = NULL; |
| 6947 | #endif |
| 6948 | return ret; |
| 6949 | } |
| 6950 | |
| 6951 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6952 | { |
| 6953 | struct user_struct *user = NULL; |
| 6954 | struct io_ring_ctx *ctx; |
| 6955 | bool account_mem; |
| 6956 | int ret; |
| 6957 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6958 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6959 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6960 | if (entries > IORING_MAX_ENTRIES) { |
| 6961 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6962 | return -EINVAL; |
| 6963 | entries = IORING_MAX_ENTRIES; |
| 6964 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6965 | |
| 6966 | /* |
| 6967 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6968 | * application to drive a higher depth than the size of the SQ ring, |
| 6969 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6970 | * some flexibility in overcommitting a bit. If the application has |
| 6971 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6972 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6973 | */ |
| 6974 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6975 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6976 | /* |
| 6977 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6978 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6979 | * any cq vs sq ring sizing. |
| 6980 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6981 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6982 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6983 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6984 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6985 | return -EINVAL; |
| 6986 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6987 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6988 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6989 | } else { |
| 6990 | p->cq_entries = 2 * p->sq_entries; |
| 6991 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6992 | |
| 6993 | user = get_uid(current_user()); |
| 6994 | account_mem = !capable(CAP_IPC_LOCK); |
| 6995 | |
| 6996 | if (account_mem) { |
| 6997 | ret = io_account_mem(user, |
| 6998 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6999 | if (ret) { |
| 7000 | free_uid(user); |
| 7001 | return ret; |
| 7002 | } |
| 7003 | } |
| 7004 | |
| 7005 | ctx = io_ring_ctx_alloc(p); |
| 7006 | if (!ctx) { |
| 7007 | if (account_mem) |
| 7008 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 7009 | p->cq_entries)); |
| 7010 | free_uid(user); |
| 7011 | return -ENOMEM; |
| 7012 | } |
| 7013 | ctx->compat = in_compat_syscall(); |
| 7014 | ctx->account_mem = account_mem; |
| 7015 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 7016 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7017 | |
| 7018 | ret = io_allocate_scq_urings(ctx, p); |
| 7019 | if (ret) |
| 7020 | goto err; |
| 7021 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7022 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7023 | if (ret) |
| 7024 | goto err; |
| 7025 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7026 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7027 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 7028 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 7029 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 7030 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 7031 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 7032 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 7033 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7034 | |
| 7035 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7036 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 7037 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 7038 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 7039 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 7040 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 7041 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 7042 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 7043 | /* |
| 7044 | * Install ring fd as the very last thing, so we don't risk someone |
| 7045 | * having closed it before we finish setup |
| 7046 | */ |
| 7047 | ret = io_uring_get_fd(ctx); |
| 7048 | if (ret < 0) |
| 7049 | goto err; |
| 7050 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 7051 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 7052 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
| 7053 | IORING_FEAT_CUR_PERSONALITY; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7054 | 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] | 7055 | return ret; |
| 7056 | err: |
| 7057 | io_ring_ctx_wait_and_kill(ctx); |
| 7058 | return ret; |
| 7059 | } |
| 7060 | |
| 7061 | /* |
| 7062 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 7063 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 7064 | * params structure passed in. |
| 7065 | */ |
| 7066 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 7067 | { |
| 7068 | struct io_uring_params p; |
| 7069 | long ret; |
| 7070 | int i; |
| 7071 | |
| 7072 | if (copy_from_user(&p, params, sizeof(p))) |
| 7073 | return -EFAULT; |
| 7074 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 7075 | if (p.resv[i]) |
| 7076 | return -EINVAL; |
| 7077 | } |
| 7078 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7079 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7080 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7081 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7082 | return -EINVAL; |
| 7083 | |
| 7084 | ret = io_uring_create(entries, &p); |
| 7085 | if (ret < 0) |
| 7086 | return ret; |
| 7087 | |
| 7088 | if (copy_to_user(params, &p, sizeof(p))) |
| 7089 | return -EFAULT; |
| 7090 | |
| 7091 | return ret; |
| 7092 | } |
| 7093 | |
| 7094 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 7095 | struct io_uring_params __user *, params) |
| 7096 | { |
| 7097 | return io_uring_setup(entries, params); |
| 7098 | } |
| 7099 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7100 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 7101 | { |
| 7102 | struct io_uring_probe *p; |
| 7103 | size_t size; |
| 7104 | int i, ret; |
| 7105 | |
| 7106 | size = struct_size(p, ops, nr_args); |
| 7107 | if (size == SIZE_MAX) |
| 7108 | return -EOVERFLOW; |
| 7109 | p = kzalloc(size, GFP_KERNEL); |
| 7110 | if (!p) |
| 7111 | return -ENOMEM; |
| 7112 | |
| 7113 | ret = -EFAULT; |
| 7114 | if (copy_from_user(p, arg, size)) |
| 7115 | goto out; |
| 7116 | ret = -EINVAL; |
| 7117 | if (memchr_inv(p, 0, size)) |
| 7118 | goto out; |
| 7119 | |
| 7120 | p->last_op = IORING_OP_LAST - 1; |
| 7121 | if (nr_args > IORING_OP_LAST) |
| 7122 | nr_args = IORING_OP_LAST; |
| 7123 | |
| 7124 | for (i = 0; i < nr_args; i++) { |
| 7125 | p->ops[i].op = i; |
| 7126 | if (!io_op_defs[i].not_supported) |
| 7127 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 7128 | } |
| 7129 | p->ops_len = i; |
| 7130 | |
| 7131 | ret = 0; |
| 7132 | if (copy_to_user(arg, p, size)) |
| 7133 | ret = -EFAULT; |
| 7134 | out: |
| 7135 | kfree(p); |
| 7136 | return ret; |
| 7137 | } |
| 7138 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7139 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 7140 | { |
| 7141 | const struct cred *creds = get_current_cred(); |
| 7142 | int id; |
| 7143 | |
| 7144 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 7145 | USHRT_MAX, GFP_KERNEL); |
| 7146 | if (id < 0) |
| 7147 | put_cred(creds); |
| 7148 | return id; |
| 7149 | } |
| 7150 | |
| 7151 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 7152 | { |
| 7153 | const struct cred *old_creds; |
| 7154 | |
| 7155 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 7156 | if (old_creds) { |
| 7157 | put_cred(old_creds); |
| 7158 | return 0; |
| 7159 | } |
| 7160 | |
| 7161 | return -EINVAL; |
| 7162 | } |
| 7163 | |
| 7164 | static bool io_register_op_must_quiesce(int op) |
| 7165 | { |
| 7166 | switch (op) { |
| 7167 | case IORING_UNREGISTER_FILES: |
| 7168 | case IORING_REGISTER_FILES_UPDATE: |
| 7169 | case IORING_REGISTER_PROBE: |
| 7170 | case IORING_REGISTER_PERSONALITY: |
| 7171 | case IORING_UNREGISTER_PERSONALITY: |
| 7172 | return false; |
| 7173 | default: |
| 7174 | return true; |
| 7175 | } |
| 7176 | } |
| 7177 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7178 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 7179 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7180 | __releases(ctx->uring_lock) |
| 7181 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7182 | { |
| 7183 | int ret; |
| 7184 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 7185 | /* |
| 7186 | * We're inside the ring mutex, if the ref is already dying, then |
| 7187 | * someone else killed the ctx or is already going through |
| 7188 | * io_uring_register(). |
| 7189 | */ |
| 7190 | if (percpu_ref_is_dying(&ctx->refs)) |
| 7191 | return -ENXIO; |
| 7192 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7193 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7194 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7195 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7196 | /* |
| 7197 | * Drop uring mutex before waiting for references to exit. If |
| 7198 | * another thread is currently inside io_uring_enter() it might |
| 7199 | * need to grab the uring_lock to make progress. If we hold it |
| 7200 | * here across the drain wait, then we can deadlock. It's safe |
| 7201 | * to drop the mutex here, since no new references will come in |
| 7202 | * after we've killed the percpu ref. |
| 7203 | */ |
| 7204 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7205 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7206 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7207 | if (ret) { |
| 7208 | percpu_ref_resurrect(&ctx->refs); |
| 7209 | ret = -EINTR; |
| 7210 | goto out; |
| 7211 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7212 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7213 | |
| 7214 | switch (opcode) { |
| 7215 | case IORING_REGISTER_BUFFERS: |
| 7216 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 7217 | break; |
| 7218 | case IORING_UNREGISTER_BUFFERS: |
| 7219 | ret = -EINVAL; |
| 7220 | if (arg || nr_args) |
| 7221 | break; |
| 7222 | ret = io_sqe_buffer_unregister(ctx); |
| 7223 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7224 | case IORING_REGISTER_FILES: |
| 7225 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 7226 | break; |
| 7227 | case IORING_UNREGISTER_FILES: |
| 7228 | ret = -EINVAL; |
| 7229 | if (arg || nr_args) |
| 7230 | break; |
| 7231 | ret = io_sqe_files_unregister(ctx); |
| 7232 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7233 | case IORING_REGISTER_FILES_UPDATE: |
| 7234 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7235 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7236 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7237 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7238 | ret = -EINVAL; |
| 7239 | if (nr_args != 1) |
| 7240 | break; |
| 7241 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7242 | if (ret) |
| 7243 | break; |
| 7244 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7245 | ctx->eventfd_async = 1; |
| 7246 | else |
| 7247 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7248 | break; |
| 7249 | case IORING_UNREGISTER_EVENTFD: |
| 7250 | ret = -EINVAL; |
| 7251 | if (arg || nr_args) |
| 7252 | break; |
| 7253 | ret = io_eventfd_unregister(ctx); |
| 7254 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7255 | case IORING_REGISTER_PROBE: |
| 7256 | ret = -EINVAL; |
| 7257 | if (!arg || nr_args > 256) |
| 7258 | break; |
| 7259 | ret = io_probe(ctx, arg, nr_args); |
| 7260 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7261 | case IORING_REGISTER_PERSONALITY: |
| 7262 | ret = -EINVAL; |
| 7263 | if (arg || nr_args) |
| 7264 | break; |
| 7265 | ret = io_register_personality(ctx); |
| 7266 | break; |
| 7267 | case IORING_UNREGISTER_PERSONALITY: |
| 7268 | ret = -EINVAL; |
| 7269 | if (arg) |
| 7270 | break; |
| 7271 | ret = io_unregister_personality(ctx, nr_args); |
| 7272 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7273 | default: |
| 7274 | ret = -EINVAL; |
| 7275 | break; |
| 7276 | } |
| 7277 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7278 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7279 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7280 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7281 | out: |
| 7282 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7283 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7284 | return ret; |
| 7285 | } |
| 7286 | |
| 7287 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7288 | void __user *, arg, unsigned int, nr_args) |
| 7289 | { |
| 7290 | struct io_ring_ctx *ctx; |
| 7291 | long ret = -EBADF; |
| 7292 | struct fd f; |
| 7293 | |
| 7294 | f = fdget(fd); |
| 7295 | if (!f.file) |
| 7296 | return -EBADF; |
| 7297 | |
| 7298 | ret = -EOPNOTSUPP; |
| 7299 | if (f.file->f_op != &io_uring_fops) |
| 7300 | goto out_fput; |
| 7301 | |
| 7302 | ctx = f.file->private_data; |
| 7303 | |
| 7304 | mutex_lock(&ctx->uring_lock); |
| 7305 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7306 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7307 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7308 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7309 | out_fput: |
| 7310 | fdput(f); |
| 7311 | return ret; |
| 7312 | } |
| 7313 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7314 | static int __init io_uring_init(void) |
| 7315 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7316 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7317 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7318 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7319 | } while (0) |
| 7320 | |
| 7321 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7322 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7323 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7324 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7325 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7326 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7327 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7328 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7329 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7330 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7331 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7332 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7333 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7334 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7335 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7336 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7337 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7338 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7339 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7340 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7341 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7342 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7343 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7344 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7345 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7346 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7347 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7348 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7349 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7350 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7351 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7352 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7353 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 7354 | return 0; |
| 7355 | }; |
| 7356 | __initcall(io_uring_init); |