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 | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 80 | #include <linux/task_work.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 81 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 82 | #define CREATE_TRACE_POINTS |
| 83 | #include <trace/events/io_uring.h> |
| 84 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | #include <uapi/linux/io_uring.h> |
| 86 | |
| 87 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 88 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 89 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 90 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 91 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 95 | */ |
| 96 | #define IORING_FILE_TABLE_SHIFT 9 |
| 97 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 98 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 99 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 100 | |
| 101 | struct io_uring { |
| 102 | u32 head ____cacheline_aligned_in_smp; |
| 103 | u32 tail ____cacheline_aligned_in_smp; |
| 104 | }; |
| 105 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 107 | * This data is shared with the application through the mmap at offsets |
| 108 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 109 | * |
| 110 | * The offsets to the member fields are published through struct |
| 111 | * io_sqring_offsets when calling io_uring_setup. |
| 112 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 114 | /* |
| 115 | * Head and tail offsets into the ring; the offsets need to be |
| 116 | * masked to get valid indices. |
| 117 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 118 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 119 | * and the application controls tail of the sq ring and the head of the |
| 120 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 123 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | * ring_entries - 1) |
| 126 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 127 | u32 sq_ring_mask, cq_ring_mask; |
| 128 | /* Ring sizes (constant, power of 2) */ |
| 129 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 130 | /* |
| 131 | * Number of invalid entries dropped by the kernel due to |
| 132 | * invalid index stored in array |
| 133 | * |
| 134 | * Written by the kernel, shouldn't be modified by the |
| 135 | * application (i.e. get number of "new events" by comparing to |
| 136 | * cached value). |
| 137 | * |
| 138 | * After a new SQ head value was read by the application this |
| 139 | * counter includes all submissions that were dropped reaching |
| 140 | * the new SQ head (and possibly more). |
| 141 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 142 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 143 | /* |
| 144 | * Runtime flags |
| 145 | * |
| 146 | * Written by the kernel, shouldn't be modified by the |
| 147 | * application. |
| 148 | * |
| 149 | * The application needs a full memory barrier before checking |
| 150 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 151 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 152 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 153 | /* |
| 154 | * Number of completion events lost because the queue was full; |
| 155 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 156 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 157 | * the completion queue. |
| 158 | * |
| 159 | * Written by the kernel, shouldn't be modified by the |
| 160 | * application (i.e. get number of "new events" by comparing to |
| 161 | * cached value). |
| 162 | * |
| 163 | * As completion events come in out of order this counter is not |
| 164 | * ordered with any other data. |
| 165 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 166 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Ring buffer of completion events. |
| 169 | * |
| 170 | * The kernel writes completion events fresh every time they are |
| 171 | * produced, so the application is allowed to modify pending |
| 172 | * entries. |
| 173 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 174 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 177 | struct io_mapped_ubuf { |
| 178 | u64 ubuf; |
| 179 | size_t len; |
| 180 | struct bio_vec *bvec; |
| 181 | unsigned int nr_bvecs; |
| 182 | }; |
| 183 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 184 | struct fixed_file_table { |
| 185 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 188 | struct fixed_file_data { |
| 189 | struct fixed_file_table *table; |
| 190 | struct io_ring_ctx *ctx; |
| 191 | |
| 192 | struct percpu_ref refs; |
| 193 | struct llist_head put_llist; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 194 | struct work_struct ref_work; |
| 195 | struct completion done; |
| 196 | }; |
| 197 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 198 | struct io_ring_ctx { |
| 199 | struct { |
| 200 | struct percpu_ref refs; |
| 201 | } ____cacheline_aligned_in_smp; |
| 202 | |
| 203 | struct { |
| 204 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 205 | unsigned int compat: 1; |
| 206 | unsigned int account_mem: 1; |
| 207 | unsigned int cq_overflow_flushed: 1; |
| 208 | unsigned int drain_next: 1; |
| 209 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 210 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 213 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 214 | * |
| 215 | * This indirection could e.g. be used to assign fixed |
| 216 | * io_uring_sqe entries to operations and only submit them to |
| 217 | * the queue when needed. |
| 218 | * |
| 219 | * The kernel modifies neither the indices array nor the entries |
| 220 | * array. |
| 221 | */ |
| 222 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 223 | unsigned cached_sq_head; |
| 224 | unsigned sq_entries; |
| 225 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 226 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 227 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 228 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 229 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 230 | |
| 231 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 232 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 233 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 234 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 235 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 236 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 237 | } ____cacheline_aligned_in_smp; |
| 238 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 239 | struct io_rings *rings; |
| 240 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 241 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 242 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 243 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 244 | struct mm_struct *sqo_mm; |
| 245 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 246 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 247 | /* |
| 248 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 249 | * readers must ensure that ->refs is alive as long as the file* is |
| 250 | * used. Only updated through io_uring_register(2). |
| 251 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 252 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 253 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 254 | int ring_fd; |
| 255 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 256 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 257 | /* if used, fixed mapped user buffers */ |
| 258 | unsigned nr_user_bufs; |
| 259 | struct io_mapped_ubuf *user_bufs; |
| 260 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 261 | struct user_struct *user; |
| 262 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 263 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 264 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 265 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 266 | struct completion *completions; |
| 267 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 268 | /* if all else fails... */ |
| 269 | struct io_kiocb *fallback_req; |
| 270 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 271 | #if defined(CONFIG_UNIX) |
| 272 | struct socket *ring_sock; |
| 273 | #endif |
| 274 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 275 | struct idr personality_idr; |
| 276 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 277 | struct { |
| 278 | unsigned cached_cq_tail; |
| 279 | unsigned cq_entries; |
| 280 | unsigned cq_mask; |
| 281 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 282 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 283 | struct wait_queue_head cq_wait; |
| 284 | struct fasync_struct *cq_fasync; |
| 285 | struct eventfd_ctx *cq_ev_fd; |
| 286 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 287 | |
| 288 | struct { |
| 289 | struct mutex uring_lock; |
| 290 | wait_queue_head_t wait; |
| 291 | } ____cacheline_aligned_in_smp; |
| 292 | |
| 293 | struct { |
| 294 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 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, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 490 | REQ_F_POLLED_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | enum { |
| 494 | /* ctx owns file */ |
| 495 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 496 | /* drain existing IO first */ |
| 497 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 498 | /* linked sqes */ |
| 499 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 500 | /* doesn't sever on completion < 0 */ |
| 501 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 502 | /* IOSQE_ASYNC */ |
| 503 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 504 | |
| 505 | /* already grabbed next link */ |
| 506 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 507 | /* fail rest of links */ |
| 508 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 509 | /* on inflight list */ |
| 510 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 511 | /* read/write uses file position */ |
| 512 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 513 | /* must not punt to workers */ |
| 514 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 515 | /* polled IO has completed */ |
| 516 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 517 | /* has linked timeout */ |
| 518 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 519 | /* timeout request */ |
| 520 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 521 | /* regular file */ |
| 522 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 523 | /* must be punted even for NONBLOCK */ |
| 524 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 525 | /* no timeout sequence */ |
| 526 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 527 | /* completion under lock */ |
| 528 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 529 | /* needs cleanup */ |
| 530 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 531 | /* in overflow list */ |
| 532 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 533 | /* already went through poll handler */ |
| 534 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
| 535 | }; |
| 536 | |
| 537 | struct async_poll { |
| 538 | struct io_poll_iocb poll; |
| 539 | struct io_wq_work work; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 540 | }; |
| 541 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 542 | /* |
| 543 | * NOTE! Each of the iocb union members has the file pointer |
| 544 | * as the first entry in their struct definition. So you can |
| 545 | * access the file pointer through any of the sub-structs, |
| 546 | * or directly as just 'ki_filp' in this struct. |
| 547 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 548 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 549 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 550 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 551 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 552 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 553 | struct io_accept accept; |
| 554 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 555 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 556 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 557 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 558 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 559 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 560 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 561 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 562 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 563 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 564 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 565 | struct io_splice splice; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 566 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 567 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 568 | struct io_async_ctx *io; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 569 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 570 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 571 | |
| 572 | struct io_ring_ctx *ctx; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 573 | struct list_head 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 | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 576 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 577 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 578 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 579 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 580 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 581 | struct list_head link_list; |
| 582 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 583 | struct list_head inflight_entry; |
| 584 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 585 | union { |
| 586 | /* |
| 587 | * Only commands that never go async can use the below fields, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 588 | * obviously. Right now only IORING_OP_POLL_ADD uses them, and |
| 589 | * async armed poll handlers for regular commands. The latter |
| 590 | * restore the work, if needed. |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 591 | */ |
| 592 | struct { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 593 | struct callback_head task_work; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 594 | struct hlist_node hash_node; |
| 595 | struct async_poll *apoll; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 596 | }; |
| 597 | struct io_wq_work work; |
| 598 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 602 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 603 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 604 | struct io_submit_state { |
| 605 | struct blk_plug plug; |
| 606 | |
| 607 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 608 | * io_kiocb alloc cache |
| 609 | */ |
| 610 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 611 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 612 | |
| 613 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 614 | * File reference cache |
| 615 | */ |
| 616 | struct file *file; |
| 617 | unsigned int fd; |
| 618 | unsigned int has_refs; |
| 619 | unsigned int used_refs; |
| 620 | unsigned int ios_left; |
| 621 | }; |
| 622 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 623 | struct io_op_def { |
| 624 | /* needs req->io allocated for deferral/async */ |
| 625 | unsigned async_ctx : 1; |
| 626 | /* needs current->mm setup, does mm access */ |
| 627 | unsigned needs_mm : 1; |
| 628 | /* needs req->file assigned */ |
| 629 | unsigned needs_file : 1; |
| 630 | /* needs req->file assigned IFF fd is >= 0 */ |
| 631 | unsigned fd_non_neg : 1; |
| 632 | /* hash wq insertion if file is a regular file */ |
| 633 | unsigned hash_reg_file : 1; |
| 634 | /* unbound wq insertion if file is a non-regular file */ |
| 635 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 636 | /* opcode is not supported by this kernel */ |
| 637 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 638 | /* needs file table */ |
| 639 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 640 | /* needs ->fs */ |
| 641 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 642 | /* set if opcode supports polled "wait" */ |
| 643 | unsigned pollin : 1; |
| 644 | unsigned pollout : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 645 | }; |
| 646 | |
| 647 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 648 | [IORING_OP_NOP] = {}, |
| 649 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 650 | .async_ctx = 1, |
| 651 | .needs_mm = 1, |
| 652 | .needs_file = 1, |
| 653 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 654 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 655 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 656 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 657 | .async_ctx = 1, |
| 658 | .needs_mm = 1, |
| 659 | .needs_file = 1, |
| 660 | .hash_reg_file = 1, |
| 661 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 662 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 663 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 664 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 665 | .needs_file = 1, |
| 666 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 667 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 668 | .needs_file = 1, |
| 669 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 670 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 671 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 672 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 673 | .needs_file = 1, |
| 674 | .hash_reg_file = 1, |
| 675 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 676 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 677 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 678 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 679 | .needs_file = 1, |
| 680 | .unbound_nonreg_file = 1, |
| 681 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 682 | [IORING_OP_POLL_REMOVE] = {}, |
| 683 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 684 | .needs_file = 1, |
| 685 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 686 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 687 | .async_ctx = 1, |
| 688 | .needs_mm = 1, |
| 689 | .needs_file = 1, |
| 690 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 691 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 692 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 693 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 694 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 695 | .async_ctx = 1, |
| 696 | .needs_mm = 1, |
| 697 | .needs_file = 1, |
| 698 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 699 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 700 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 701 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 702 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 703 | .async_ctx = 1, |
| 704 | .needs_mm = 1, |
| 705 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 706 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 707 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 708 | .needs_mm = 1, |
| 709 | .needs_file = 1, |
| 710 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 711 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 712 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 713 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 714 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 715 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 716 | .async_ctx = 1, |
| 717 | .needs_mm = 1, |
| 718 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 719 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 720 | .async_ctx = 1, |
| 721 | .needs_mm = 1, |
| 722 | .needs_file = 1, |
| 723 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 724 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 725 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 726 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 727 | .needs_file = 1, |
| 728 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 729 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 730 | .needs_file = 1, |
| 731 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 732 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 733 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 734 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 735 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 736 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 737 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 738 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 739 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 740 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 741 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 742 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 743 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 744 | .needs_mm = 1, |
| 745 | .needs_file = 1, |
| 746 | .fd_non_neg = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 747 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 748 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 749 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 750 | .needs_mm = 1, |
| 751 | .needs_file = 1, |
| 752 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 753 | .pollin = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 754 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 755 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 756 | .needs_mm = 1, |
| 757 | .needs_file = 1, |
| 758 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 759 | .pollout = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 760 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 761 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 762 | .needs_file = 1, |
| 763 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 764 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 765 | .needs_mm = 1, |
| 766 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 767 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 768 | .needs_mm = 1, |
| 769 | .needs_file = 1, |
| 770 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 771 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 772 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 773 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 774 | .needs_mm = 1, |
| 775 | .needs_file = 1, |
| 776 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 777 | .pollin = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 778 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 779 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 780 | .needs_file = 1, |
| 781 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 782 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 783 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 784 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 785 | [IORING_OP_EPOLL_CTL] = { |
| 786 | .unbound_nonreg_file = 1, |
| 787 | .file_table = 1, |
| 788 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 789 | [IORING_OP_SPLICE] = { |
| 790 | .needs_file = 1, |
| 791 | .hash_reg_file = 1, |
| 792 | .unbound_nonreg_file = 1, |
| 793 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 794 | }; |
| 795 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 796 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 797 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 798 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 799 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 800 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 801 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 802 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 803 | struct io_uring_files_update *ip, |
| 804 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 805 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 806 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 807 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 808 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 809 | int fd, struct file **out_file, bool fixed); |
| 810 | static void __io_queue_sqe(struct io_kiocb *req, |
| 811 | const struct io_uring_sqe *sqe); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 812 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 813 | static struct kmem_cache *req_cachep; |
| 814 | |
| 815 | static const struct file_operations io_uring_fops; |
| 816 | |
| 817 | struct sock *io_uring_get_socket(struct file *file) |
| 818 | { |
| 819 | #if defined(CONFIG_UNIX) |
| 820 | if (file->f_op == &io_uring_fops) { |
| 821 | struct io_ring_ctx *ctx = file->private_data; |
| 822 | |
| 823 | return ctx->ring_sock->sk; |
| 824 | } |
| 825 | #endif |
| 826 | return NULL; |
| 827 | } |
| 828 | EXPORT_SYMBOL(io_uring_get_socket); |
| 829 | |
| 830 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 831 | { |
| 832 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 833 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 834 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 838 | { |
| 839 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 840 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 841 | |
| 842 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 843 | if (!ctx) |
| 844 | return NULL; |
| 845 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 846 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 847 | if (!ctx->fallback_req) |
| 848 | goto err; |
| 849 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 850 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 851 | if (!ctx->completions) |
| 852 | goto err; |
| 853 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 854 | /* |
| 855 | * Use 5 bits less than the max cq entries, that should give us around |
| 856 | * 32 entries per hash list if totally full and uniformly spread. |
| 857 | */ |
| 858 | hash_bits = ilog2(p->cq_entries); |
| 859 | hash_bits -= 5; |
| 860 | if (hash_bits <= 0) |
| 861 | hash_bits = 1; |
| 862 | ctx->cancel_hash_bits = hash_bits; |
| 863 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 864 | GFP_KERNEL); |
| 865 | if (!ctx->cancel_hash) |
| 866 | goto err; |
| 867 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 868 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 869 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 870 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 871 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 872 | |
| 873 | ctx->flags = p->flags; |
| 874 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 875 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 876 | init_completion(&ctx->completions[0]); |
| 877 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 878 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 879 | mutex_init(&ctx->uring_lock); |
| 880 | init_waitqueue_head(&ctx->wait); |
| 881 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 882 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 883 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 884 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 885 | init_waitqueue_head(&ctx->inflight_wait); |
| 886 | spin_lock_init(&ctx->inflight_lock); |
| 887 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 888 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 889 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 890 | if (ctx->fallback_req) |
| 891 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 892 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 893 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 894 | kfree(ctx); |
| 895 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 898 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 899 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 900 | struct io_ring_ctx *ctx = req->ctx; |
| 901 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 902 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 903 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 904 | } |
| 905 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 906 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 907 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 908 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 909 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 910 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 911 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | 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] | 915 | { |
| 916 | struct io_kiocb *req; |
| 917 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 918 | 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] | 919 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 920 | list_del_init(&req->list); |
| 921 | return req; |
| 922 | } |
| 923 | |
| 924 | return NULL; |
| 925 | } |
| 926 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 927 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 928 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 929 | struct io_kiocb *req; |
| 930 | |
| 931 | 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] | 932 | if (req) { |
| 933 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 934 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 935 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 936 | list_del_init(&req->list); |
| 937 | return req; |
| 938 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 942 | } |
| 943 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 944 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 945 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 946 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 947 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 948 | /* order cqe stores with ring update */ |
| 949 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 950 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 951 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 952 | wake_up_interruptible(&ctx->cq_wait); |
| 953 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 957 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 958 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 959 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 960 | if (!req->work.mm && def->needs_mm) { |
| 961 | mmgrab(current->mm); |
| 962 | req->work.mm = current->mm; |
| 963 | } |
| 964 | if (!req->work.creds) |
| 965 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 966 | if (!req->work.fs && def->needs_fs) { |
| 967 | spin_lock(¤t->fs->lock); |
| 968 | if (!current->fs->in_exec) { |
| 969 | req->work.fs = current->fs; |
| 970 | req->work.fs->users++; |
| 971 | } else { |
| 972 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 973 | } |
| 974 | spin_unlock(¤t->fs->lock); |
| 975 | } |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 976 | if (!req->work.task_pid) |
| 977 | req->work.task_pid = task_pid_vnr(current); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 981 | { |
| 982 | if (req->work.mm) { |
| 983 | mmdrop(req->work.mm); |
| 984 | req->work.mm = NULL; |
| 985 | } |
| 986 | if (req->work.creds) { |
| 987 | put_cred(req->work.creds); |
| 988 | req->work.creds = NULL; |
| 989 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 990 | if (req->work.fs) { |
| 991 | struct fs_struct *fs = req->work.fs; |
| 992 | |
| 993 | spin_lock(&req->work.fs->lock); |
| 994 | if (--fs->users) |
| 995 | fs = NULL; |
| 996 | spin_unlock(&req->work.fs->lock); |
| 997 | if (fs) |
| 998 | free_fs_struct(fs); |
| 999 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1000 | } |
| 1001 | |
Pavel Begunkov | deb6dc0 | 2020-02-24 11:30:17 +0300 | [diff] [blame] | 1002 | static inline void io_prep_next_work(struct io_kiocb *req, |
| 1003 | struct io_kiocb **link) |
| 1004 | { |
| 1005 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 1006 | |
| 1007 | if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file) |
| 1008 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1009 | |
| 1010 | *link = io_prep_linked_timeout(req); |
| 1011 | } |
| 1012 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1013 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 1014 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1015 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1016 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1017 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1018 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1019 | if (req->flags & REQ_F_ISREG) { |
| 1020 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1021 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1022 | } else { |
| 1023 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1024 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1025 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1026 | |
| 1027 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1028 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1029 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1030 | return do_hashed; |
| 1031 | } |
| 1032 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1033 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1034 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1035 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1036 | struct io_kiocb *link; |
| 1037 | bool do_hashed; |
| 1038 | |
| 1039 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1040 | |
| 1041 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 1042 | req->flags); |
| 1043 | if (!do_hashed) { |
| 1044 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 1045 | } else { |
| 1046 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 1047 | file_inode(req->file)); |
| 1048 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1049 | |
| 1050 | if (link) |
| 1051 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1052 | } |
| 1053 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1054 | static void io_kill_timeout(struct io_kiocb *req) |
| 1055 | { |
| 1056 | int ret; |
| 1057 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1058 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1059 | if (ret != -1) { |
| 1060 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1061 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1062 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1063 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1068 | { |
| 1069 | struct io_kiocb *req, *tmp; |
| 1070 | |
| 1071 | spin_lock_irq(&ctx->completion_lock); |
| 1072 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1073 | io_kill_timeout(req); |
| 1074 | spin_unlock_irq(&ctx->completion_lock); |
| 1075 | } |
| 1076 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1077 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1078 | { |
| 1079 | struct io_kiocb *req; |
| 1080 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1081 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 1082 | io_kill_timeout(req); |
| 1083 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1084 | __io_commit_cqring(ctx); |
| 1085 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1086 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1087 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1088 | } |
| 1089 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1090 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1091 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1092 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1093 | unsigned tail; |
| 1094 | |
| 1095 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1096 | /* |
| 1097 | * writes to the cq entry need to come after reading head; the |
| 1098 | * control dependency is enough as we're using WRITE_ONCE to |
| 1099 | * fill the cq entry |
| 1100 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1101 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1102 | return NULL; |
| 1103 | |
| 1104 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1105 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1108 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1109 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1110 | if (!ctx->cq_ev_fd) |
| 1111 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1112 | if (!ctx->eventfd_async) |
| 1113 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1114 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1117 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1118 | { |
| 1119 | if (waitqueue_active(&ctx->wait)) |
| 1120 | wake_up(&ctx->wait); |
| 1121 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1122 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1123 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1124 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1125 | } |
| 1126 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1127 | /* Returns true if there are no backlogged entries after the flush */ |
| 1128 | 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] | 1129 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1130 | struct io_rings *rings = ctx->rings; |
| 1131 | struct io_uring_cqe *cqe; |
| 1132 | struct io_kiocb *req; |
| 1133 | unsigned long flags; |
| 1134 | LIST_HEAD(list); |
| 1135 | |
| 1136 | if (!force) { |
| 1137 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1138 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1139 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1140 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1141 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1145 | |
| 1146 | /* if force is set, the ring is going away. always drop after that */ |
| 1147 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1148 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1149 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1150 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1151 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1152 | cqe = io_get_cqring(ctx); |
| 1153 | if (!cqe && !force) |
| 1154 | break; |
| 1155 | |
| 1156 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1157 | list); |
| 1158 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1159 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1160 | if (cqe) { |
| 1161 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1162 | WRITE_ONCE(cqe->res, req->result); |
| 1163 | WRITE_ONCE(cqe->flags, 0); |
| 1164 | } else { |
| 1165 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1166 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1171 | if (cqe) { |
| 1172 | clear_bit(0, &ctx->sq_check_overflow); |
| 1173 | clear_bit(0, &ctx->cq_check_overflow); |
| 1174 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1175 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1176 | io_cqring_ev_posted(ctx); |
| 1177 | |
| 1178 | while (!list_empty(&list)) { |
| 1179 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1180 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1181 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1182 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1183 | |
| 1184 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1187 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1188 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1189 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1190 | struct io_uring_cqe *cqe; |
| 1191 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1192 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1193 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1194 | /* |
| 1195 | * If we can't get a cq entry, userspace overflowed the |
| 1196 | * submission (by quite a lot). Increment the overflow count in |
| 1197 | * the ring. |
| 1198 | */ |
| 1199 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1200 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1201 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1202 | WRITE_ONCE(cqe->res, res); |
| 1203 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1204 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1205 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1206 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1207 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1208 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1209 | set_bit(0, &ctx->sq_check_overflow); |
| 1210 | set_bit(0, &ctx->cq_check_overflow); |
| 1211 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1212 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1213 | refcount_inc(&req->refs); |
| 1214 | req->result = res; |
| 1215 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1219 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1220 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1221 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1222 | unsigned long flags; |
| 1223 | |
| 1224 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1225 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1226 | io_commit_cqring(ctx); |
| 1227 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1228 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1229 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1232 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1233 | { |
| 1234 | return req == (struct io_kiocb *) |
| 1235 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1236 | } |
| 1237 | |
| 1238 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1239 | { |
| 1240 | struct io_kiocb *req; |
| 1241 | |
| 1242 | req = ctx->fallback_req; |
| 1243 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1244 | return req; |
| 1245 | |
| 1246 | return NULL; |
| 1247 | } |
| 1248 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1249 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1250 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1251 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1252 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1253 | struct io_kiocb *req; |
| 1254 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1255 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1256 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1257 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1258 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1259 | } else if (!state->free_reqs) { |
| 1260 | size_t sz; |
| 1261 | int ret; |
| 1262 | |
| 1263 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1264 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1265 | |
| 1266 | /* |
| 1267 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1268 | * retry single alloc to be on the safe side. |
| 1269 | */ |
| 1270 | if (unlikely(ret <= 0)) { |
| 1271 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1272 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1273 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1274 | ret = 1; |
| 1275 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1276 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1277 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1278 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1279 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1280 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1283 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1284 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1285 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1286 | req->ctx = ctx; |
| 1287 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1288 | /* one is dropped after submission, the other at completion */ |
| 1289 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1290 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1291 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1292 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1293 | fallback: |
| 1294 | req = io_get_fallback_req(ctx); |
| 1295 | if (req) |
| 1296 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1297 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1298 | return NULL; |
| 1299 | } |
| 1300 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1301 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1302 | bool fixed) |
| 1303 | { |
| 1304 | if (fixed) |
| 1305 | percpu_ref_put(&req->ctx->file_data->refs); |
| 1306 | else |
| 1307 | fput(file); |
| 1308 | } |
| 1309 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1310 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1311 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1312 | if (likely(!io_is_fallback_req(req))) |
| 1313 | kmem_cache_free(req_cachep, req); |
| 1314 | else |
| 1315 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1316 | } |
| 1317 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1318 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1319 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1320 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1321 | io_cleanup_req(req); |
| 1322 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1323 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1324 | if (req->file) |
| 1325 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1326 | |
| 1327 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static void __io_free_req(struct io_kiocb *req) |
| 1331 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1332 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1333 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1334 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1335 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1336 | unsigned long flags; |
| 1337 | |
| 1338 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1339 | list_del(&req->inflight_entry); |
| 1340 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1341 | wake_up(&ctx->inflight_wait); |
| 1342 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1343 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1344 | |
| 1345 | percpu_ref_put(&req->ctx->refs); |
| 1346 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1347 | } |
| 1348 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1349 | struct req_batch { |
| 1350 | void *reqs[IO_IOPOLL_BATCH]; |
| 1351 | int to_free; |
| 1352 | int need_iter; |
| 1353 | }; |
| 1354 | |
| 1355 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1356 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1357 | int fixed_refs = rb->to_free; |
| 1358 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1359 | if (!rb->to_free) |
| 1360 | return; |
| 1361 | if (rb->need_iter) { |
| 1362 | int i, inflight = 0; |
| 1363 | unsigned long flags; |
| 1364 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1365 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1366 | for (i = 0; i < rb->to_free; i++) { |
| 1367 | struct io_kiocb *req = rb->reqs[i]; |
| 1368 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1369 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1370 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1371 | fixed_refs++; |
| 1372 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1373 | if (req->flags & REQ_F_INFLIGHT) |
| 1374 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1375 | __io_req_aux_free(req); |
| 1376 | } |
| 1377 | if (!inflight) |
| 1378 | goto do_free; |
| 1379 | |
| 1380 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1381 | for (i = 0; i < rb->to_free; i++) { |
| 1382 | struct io_kiocb *req = rb->reqs[i]; |
| 1383 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1384 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1385 | list_del(&req->inflight_entry); |
| 1386 | if (!--inflight) |
| 1387 | break; |
| 1388 | } |
| 1389 | } |
| 1390 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1391 | |
| 1392 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1393 | wake_up(&ctx->inflight_wait); |
| 1394 | } |
| 1395 | do_free: |
| 1396 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1397 | if (fixed_refs) |
| 1398 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1399 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1400 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1401 | } |
| 1402 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1403 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1404 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1405 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1406 | int ret; |
| 1407 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1408 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1409 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1410 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1411 | io_commit_cqring(ctx); |
| 1412 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1413 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1414 | return true; |
| 1415 | } |
| 1416 | |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1420 | 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] | 1421 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1422 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1423 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1424 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1425 | /* Already got next link */ |
| 1426 | if (req->flags & REQ_F_LINK_NEXT) |
| 1427 | return; |
| 1428 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1429 | /* |
| 1430 | * The list should never be empty when we are called here. But could |
| 1431 | * potentially happen if the chain is messed up, check to be on the |
| 1432 | * safe side. |
| 1433 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1434 | while (!list_empty(&req->link_list)) { |
| 1435 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1436 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1437 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1438 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1439 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1440 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1441 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1442 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1443 | continue; |
| 1444 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1445 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1446 | list_del_init(&req->link_list); |
| 1447 | if (!list_empty(&nxt->link_list)) |
| 1448 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1449 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1450 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1451 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1452 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1453 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1454 | if (wake_ev) |
| 1455 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1460 | */ |
| 1461 | static void io_fail_links(struct io_kiocb *req) |
| 1462 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1463 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1464 | unsigned long flags; |
| 1465 | |
| 1466 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1467 | |
| 1468 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1469 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1470 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1471 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1472 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1473 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1474 | |
| 1475 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1476 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1477 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1478 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1479 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1480 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1481 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1482 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1483 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1484 | |
| 1485 | io_commit_cqring(ctx); |
| 1486 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1487 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1488 | } |
| 1489 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1490 | 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] | 1491 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1492 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1493 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1494 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1495 | /* |
| 1496 | * If LINK is set, we have dependent requests in this chain. If we |
| 1497 | * didn't fail this request, queue the first one up, moving any other |
| 1498 | * dependencies to the next request. In case of failure, fail the rest |
| 1499 | * of the chain. |
| 1500 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1501 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1502 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1503 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1504 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1505 | struct io_ring_ctx *ctx = req->ctx; |
| 1506 | unsigned long flags; |
| 1507 | |
| 1508 | /* |
| 1509 | * If this is a timeout link, we could be racing with the |
| 1510 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1511 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1512 | */ |
| 1513 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1514 | io_req_link_next(req, nxt); |
| 1515 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1516 | } else { |
| 1517 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1518 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1519 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1520 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1521 | static void io_free_req(struct io_kiocb *req) |
| 1522 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1523 | struct io_kiocb *nxt = NULL; |
| 1524 | |
| 1525 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1526 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1527 | |
| 1528 | if (nxt) |
| 1529 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1532 | /* |
| 1533 | * Drop reference to request, return next in chain (if there is one) if this |
| 1534 | * was the last reference to this request. |
| 1535 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1536 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1537 | 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] | 1538 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1539 | if (refcount_dec_and_test(&req->refs)) { |
| 1540 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1541 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1542 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1545 | static void io_put_req(struct io_kiocb *req) |
| 1546 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1547 | if (refcount_dec_and_test(&req->refs)) |
| 1548 | io_free_req(req); |
| 1549 | } |
| 1550 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1551 | /* |
| 1552 | * Must only be used if we don't need to care about links, usually from |
| 1553 | * within the completion handling itself. |
| 1554 | */ |
| 1555 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1556 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1557 | /* drop both submit and complete references */ |
| 1558 | if (refcount_sub_and_test(2, &req->refs)) |
| 1559 | __io_free_req(req); |
| 1560 | } |
| 1561 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1562 | static void io_double_put_req(struct io_kiocb *req) |
| 1563 | { |
| 1564 | /* drop both submit and complete references */ |
| 1565 | if (refcount_sub_and_test(2, &req->refs)) |
| 1566 | io_free_req(req); |
| 1567 | } |
| 1568 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1569 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1570 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1571 | struct io_rings *rings = ctx->rings; |
| 1572 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1573 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1574 | /* |
| 1575 | * noflush == true is from the waitqueue handler, just ensure |
| 1576 | * we wake up the task, and the next invocation will flush the |
| 1577 | * entries. We cannot safely to it from here. |
| 1578 | */ |
| 1579 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1580 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1581 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1582 | io_cqring_overflow_flush(ctx, false); |
| 1583 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1584 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1585 | /* See comment at the top of this file */ |
| 1586 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1587 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1588 | } |
| 1589 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1590 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1591 | { |
| 1592 | struct io_rings *rings = ctx->rings; |
| 1593 | |
| 1594 | /* make sure SQ entry isn't read before tail */ |
| 1595 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1596 | } |
| 1597 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1598 | 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] | 1599 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1600 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1601 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1602 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1603 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1604 | rb->need_iter++; |
| 1605 | |
| 1606 | rb->reqs[rb->to_free++] = req; |
| 1607 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1608 | io_free_req_many(req->ctx, rb); |
| 1609 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1612 | /* |
| 1613 | * Find and free completed poll iocbs |
| 1614 | */ |
| 1615 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1616 | struct list_head *done) |
| 1617 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1618 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1619 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1620 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1621 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1622 | while (!list_empty(done)) { |
| 1623 | req = list_first_entry(done, struct io_kiocb, list); |
| 1624 | list_del(&req->list); |
| 1625 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1626 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1627 | (*nr_events)++; |
| 1628 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1629 | if (refcount_dec_and_test(&req->refs) && |
| 1630 | !io_req_multi_free(&rb, req)) |
| 1631 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1632 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1633 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1634 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1635 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1639 | long min) |
| 1640 | { |
| 1641 | struct io_kiocb *req, *tmp; |
| 1642 | LIST_HEAD(done); |
| 1643 | bool spin; |
| 1644 | int ret; |
| 1645 | |
| 1646 | /* |
| 1647 | * Only spin for completions if we don't have multiple devices hanging |
| 1648 | * off our complete list, and we're under the requested amount. |
| 1649 | */ |
| 1650 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1651 | |
| 1652 | ret = 0; |
| 1653 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1654 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1655 | |
| 1656 | /* |
| 1657 | * Move completed entries to our local list. If we find a |
| 1658 | * request that requires polling, break out and complete |
| 1659 | * the done list first, if we have entries there. |
| 1660 | */ |
| 1661 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1662 | list_move_tail(&req->list, &done); |
| 1663 | continue; |
| 1664 | } |
| 1665 | if (!list_empty(&done)) |
| 1666 | break; |
| 1667 | |
| 1668 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1669 | if (ret < 0) |
| 1670 | break; |
| 1671 | |
| 1672 | if (ret && spin) |
| 1673 | spin = false; |
| 1674 | ret = 0; |
| 1675 | } |
| 1676 | |
| 1677 | if (!list_empty(&done)) |
| 1678 | io_iopoll_complete(ctx, nr_events, &done); |
| 1679 | |
| 1680 | return ret; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1684 | * 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] | 1685 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1686 | * as a non-spinning completion check. |
| 1687 | */ |
| 1688 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1689 | long min) |
| 1690 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1691 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1692 | int ret; |
| 1693 | |
| 1694 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1695 | if (ret < 0) |
| 1696 | return ret; |
| 1697 | if (!min || *nr_events >= min) |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | return 1; |
| 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | * We can't just wait for polled events to come to us, we have to actively |
| 1706 | * find and complete them. |
| 1707 | */ |
| 1708 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1709 | { |
| 1710 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1711 | return; |
| 1712 | |
| 1713 | mutex_lock(&ctx->uring_lock); |
| 1714 | while (!list_empty(&ctx->poll_list)) { |
| 1715 | unsigned int nr_events = 0; |
| 1716 | |
| 1717 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1718 | |
| 1719 | /* |
| 1720 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1721 | * in this case we need to ensure that we reap all events. |
| 1722 | */ |
| 1723 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1724 | } |
| 1725 | mutex_unlock(&ctx->uring_lock); |
| 1726 | } |
| 1727 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1728 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1729 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1730 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1731 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1732 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1733 | /* |
| 1734 | * We disallow the app entering submit/complete with polling, but we |
| 1735 | * still need to lock the ring to prevent racing with polled issue |
| 1736 | * that got punted to a workqueue. |
| 1737 | */ |
| 1738 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1739 | do { |
| 1740 | int tmin = 0; |
| 1741 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1742 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1743 | * Don't enter poll loop if we already have events pending. |
| 1744 | * If we do, we can potentially be spinning for commands that |
| 1745 | * already triggered a CQE (eg in error). |
| 1746 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1747 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1748 | break; |
| 1749 | |
| 1750 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1751 | * If a submit got punted to a workqueue, we can have the |
| 1752 | * application entering polling for a command before it gets |
| 1753 | * issued. That app will hold the uring_lock for the duration |
| 1754 | * of the poll right here, so we need to take a breather every |
| 1755 | * now and then to ensure that the issue has a chance to add |
| 1756 | * the poll to the issued list. Otherwise we can spin here |
| 1757 | * forever, while the workqueue is stuck trying to acquire the |
| 1758 | * very same mutex. |
| 1759 | */ |
| 1760 | if (!(++iters & 7)) { |
| 1761 | mutex_unlock(&ctx->uring_lock); |
| 1762 | mutex_lock(&ctx->uring_lock); |
| 1763 | } |
| 1764 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1765 | if (*nr_events < min) |
| 1766 | tmin = min - *nr_events; |
| 1767 | |
| 1768 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1769 | if (ret <= 0) |
| 1770 | break; |
| 1771 | ret = 0; |
| 1772 | } while (min && !*nr_events && !need_resched()); |
| 1773 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1774 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1775 | return ret; |
| 1776 | } |
| 1777 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1778 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1779 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1780 | /* |
| 1781 | * Tell lockdep we inherited freeze protection from submission |
| 1782 | * thread. |
| 1783 | */ |
| 1784 | if (req->flags & REQ_F_ISREG) { |
| 1785 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1786 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1787 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1788 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1789 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1792 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1793 | { |
| 1794 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1795 | req->flags |= REQ_F_FAIL_LINK; |
| 1796 | } |
| 1797 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1798 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1799 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1800 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1801 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1802 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1803 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1804 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1805 | if (res != req->result) |
| 1806 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1807 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1811 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1812 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1813 | |
| 1814 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1815 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1818 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1819 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1820 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1821 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1822 | |
| 1823 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1824 | io_put_req_find_next(req, &nxt); |
| 1825 | |
| 1826 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1829 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1830 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1831 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1832 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1833 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1834 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1835 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1836 | if (res != req->result) |
| 1837 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1838 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1839 | if (res != -EAGAIN) |
| 1840 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1845 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1846 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1847 | * accessing the kiocb cookie. |
| 1848 | */ |
| 1849 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1850 | { |
| 1851 | struct io_ring_ctx *ctx = req->ctx; |
| 1852 | |
| 1853 | /* |
| 1854 | * Track whether we have multiple files in our lists. This will impact |
| 1855 | * how we do polling eventually, not spinning if we're on potentially |
| 1856 | * different devices. |
| 1857 | */ |
| 1858 | if (list_empty(&ctx->poll_list)) { |
| 1859 | ctx->poll_multi_file = false; |
| 1860 | } else if (!ctx->poll_multi_file) { |
| 1861 | struct io_kiocb *list_req; |
| 1862 | |
| 1863 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1864 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1865 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1866 | ctx->poll_multi_file = true; |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * For fast devices, IO may have already completed. If it has, add |
| 1871 | * it to the front so we find it first. |
| 1872 | */ |
| 1873 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1874 | list_add(&req->list, &ctx->poll_list); |
| 1875 | else |
| 1876 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 1877 | |
| 1878 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 1879 | wq_has_sleeper(&ctx->sqo_wait)) |
| 1880 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1883 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1884 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1885 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1886 | int diff = state->has_refs - state->used_refs; |
| 1887 | |
| 1888 | if (diff) |
| 1889 | fput_many(state->file, diff); |
| 1890 | state->file = NULL; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * Get as many references to a file as we have IOs left in this submission, |
| 1896 | * assuming most submissions are for one file, or at least that each file |
| 1897 | * has more than one submission. |
| 1898 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1899 | 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] | 1900 | { |
| 1901 | if (!state) |
| 1902 | return fget(fd); |
| 1903 | |
| 1904 | if (state->file) { |
| 1905 | if (state->fd == fd) { |
| 1906 | state->used_refs++; |
| 1907 | state->ios_left--; |
| 1908 | return state->file; |
| 1909 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1910 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1911 | } |
| 1912 | state->file = fget_many(fd, state->ios_left); |
| 1913 | if (!state->file) |
| 1914 | return NULL; |
| 1915 | |
| 1916 | state->fd = fd; |
| 1917 | state->has_refs = state->ios_left; |
| 1918 | state->used_refs = 1; |
| 1919 | state->ios_left--; |
| 1920 | return state->file; |
| 1921 | } |
| 1922 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1923 | /* |
| 1924 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1925 | * any file. For now, just ensure that anything potentially problematic is done |
| 1926 | * inline. |
| 1927 | */ |
| 1928 | static bool io_file_supports_async(struct file *file) |
| 1929 | { |
| 1930 | umode_t mode = file_inode(file)->i_mode; |
| 1931 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1932 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1933 | return true; |
| 1934 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1935 | return true; |
| 1936 | |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1940 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1941 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1942 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1943 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1944 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1945 | unsigned ioprio; |
| 1946 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1947 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1948 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1949 | req->flags |= REQ_F_ISREG; |
| 1950 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1951 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1952 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1953 | req->flags |= REQ_F_CUR_POS; |
| 1954 | kiocb->ki_pos = req->file->f_pos; |
| 1955 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1956 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 1957 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1958 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1959 | if (unlikely(ret)) |
| 1960 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1961 | |
| 1962 | ioprio = READ_ONCE(sqe->ioprio); |
| 1963 | if (ioprio) { |
| 1964 | ret = ioprio_check_cap(ioprio); |
| 1965 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1966 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1967 | |
| 1968 | kiocb->ki_ioprio = ioprio; |
| 1969 | } else |
| 1970 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1971 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1972 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1973 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1974 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1975 | req->flags |= REQ_F_NOWAIT; |
| 1976 | |
| 1977 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1978 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1979 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1980 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1981 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1982 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1983 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1984 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1985 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1986 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1987 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1988 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1989 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1990 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1991 | kiocb->ki_complete = io_complete_rw; |
| 1992 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1993 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1994 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1995 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1996 | /* we own ->private, reuse it for the buffer index */ |
| 1997 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1998 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1999 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2003 | { |
| 2004 | switch (ret) { |
| 2005 | case -EIOCBQUEUED: |
| 2006 | break; |
| 2007 | case -ERESTARTSYS: |
| 2008 | case -ERESTARTNOINTR: |
| 2009 | case -ERESTARTNOHAND: |
| 2010 | case -ERESTART_RESTARTBLOCK: |
| 2011 | /* |
| 2012 | * We can't just restart the syscall, since previously |
| 2013 | * submitted sqes may already be in progress. Just fail this |
| 2014 | * IO with EINTR. |
| 2015 | */ |
| 2016 | ret = -EINTR; |
| 2017 | /* fall through */ |
| 2018 | default: |
| 2019 | kiocb->ki_complete(kiocb, ret, 0); |
| 2020 | } |
| 2021 | } |
| 2022 | |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2023 | 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] | 2024 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2025 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2026 | |
| 2027 | if (req->flags & REQ_F_CUR_POS) |
| 2028 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2029 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2030 | *nxt = __io_complete_rw(kiocb, ret); |
| 2031 | else |
| 2032 | io_rw_done(kiocb, ret); |
| 2033 | } |
| 2034 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2035 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2036 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2037 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2038 | struct io_ring_ctx *ctx = req->ctx; |
| 2039 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2040 | struct io_mapped_ubuf *imu; |
| 2041 | unsigned index, buf_index; |
| 2042 | size_t offset; |
| 2043 | u64 buf_addr; |
| 2044 | |
| 2045 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2046 | if (unlikely(!ctx->user_bufs)) |
| 2047 | return -EFAULT; |
| 2048 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2049 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2050 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2051 | return -EFAULT; |
| 2052 | |
| 2053 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2054 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2055 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2056 | |
| 2057 | /* overflow */ |
| 2058 | if (buf_addr + len < buf_addr) |
| 2059 | return -EFAULT; |
| 2060 | /* not inside the mapped region */ |
| 2061 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2062 | return -EFAULT; |
| 2063 | |
| 2064 | /* |
| 2065 | * May not be a start of buffer, set size appropriately |
| 2066 | * and advance us to the beginning. |
| 2067 | */ |
| 2068 | offset = buf_addr - imu->ubuf; |
| 2069 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2070 | |
| 2071 | if (offset) { |
| 2072 | /* |
| 2073 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2074 | * using the latter parts of a big fixed buffer - it iterates |
| 2075 | * over each segment manually. We can cheat a bit here, because |
| 2076 | * we know that: |
| 2077 | * |
| 2078 | * 1) it's a BVEC iter, we set it up |
| 2079 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2080 | * first and last bvec |
| 2081 | * |
| 2082 | * So just find our index, and adjust the iterator afterwards. |
| 2083 | * If the offset is within the first bvec (or the whole first |
| 2084 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2085 | * since we can just skip the first segment, which may not |
| 2086 | * be PAGE_SIZE aligned. |
| 2087 | */ |
| 2088 | const struct bio_vec *bvec = imu->bvec; |
| 2089 | |
| 2090 | if (offset <= bvec->bv_len) { |
| 2091 | iov_iter_advance(iter, offset); |
| 2092 | } else { |
| 2093 | unsigned long seg_skip; |
| 2094 | |
| 2095 | /* skip first vec */ |
| 2096 | offset -= bvec->bv_len; |
| 2097 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2098 | |
| 2099 | iter->bvec = bvec + seg_skip; |
| 2100 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2101 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2102 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2103 | } |
| 2104 | } |
| 2105 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2106 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2109 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2110 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2111 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2112 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2113 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2114 | u8 opcode; |
| 2115 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2116 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2117 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2118 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2119 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2120 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2121 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2122 | /* buffer index only valid with fixed read/write */ |
| 2123 | if (req->rw.kiocb.private) |
| 2124 | return -EINVAL; |
| 2125 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2126 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2127 | ssize_t ret; |
| 2128 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2129 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2130 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2131 | } |
| 2132 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2133 | if (req->io) { |
| 2134 | struct io_async_rw *iorw = &req->io->rw; |
| 2135 | |
| 2136 | *iovec = iorw->iov; |
| 2137 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2138 | if (iorw->iov == iorw->fast_iov) |
| 2139 | *iovec = NULL; |
| 2140 | return iorw->size; |
| 2141 | } |
| 2142 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2143 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2144 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2145 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2146 | iovec, iter); |
| 2147 | #endif |
| 2148 | |
| 2149 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2150 | } |
| 2151 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2152 | /* |
| 2153 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2154 | * by looping over ->read() or ->write() manually. |
| 2155 | */ |
| 2156 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2157 | struct iov_iter *iter) |
| 2158 | { |
| 2159 | ssize_t ret = 0; |
| 2160 | |
| 2161 | /* |
| 2162 | * Don't support polled IO through this interface, and we can't |
| 2163 | * support non-blocking either. For the latter, this just causes |
| 2164 | * the kiocb to be handled from an async context. |
| 2165 | */ |
| 2166 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2167 | return -EOPNOTSUPP; |
| 2168 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2169 | return -EAGAIN; |
| 2170 | |
| 2171 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2172 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2173 | ssize_t nr; |
| 2174 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2175 | if (!iov_iter_is_bvec(iter)) { |
| 2176 | iovec = iov_iter_iovec(iter); |
| 2177 | } else { |
| 2178 | /* fixed buffers import bvec */ |
| 2179 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2180 | + iter->iov_offset; |
| 2181 | iovec.iov_len = min(iter->count, |
| 2182 | iter->bvec->bv_len - iter->iov_offset); |
| 2183 | } |
| 2184 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2185 | if (rw == READ) { |
| 2186 | nr = file->f_op->read(file, iovec.iov_base, |
| 2187 | iovec.iov_len, &kiocb->ki_pos); |
| 2188 | } else { |
| 2189 | nr = file->f_op->write(file, iovec.iov_base, |
| 2190 | iovec.iov_len, &kiocb->ki_pos); |
| 2191 | } |
| 2192 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2193 | if (iov_iter_is_bvec(iter)) |
| 2194 | kunmap(iter->bvec->bv_page); |
| 2195 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2196 | if (nr < 0) { |
| 2197 | if (!ret) |
| 2198 | ret = nr; |
| 2199 | break; |
| 2200 | } |
| 2201 | ret += nr; |
| 2202 | if (nr != iovec.iov_len) |
| 2203 | break; |
| 2204 | iov_iter_advance(iter, nr); |
| 2205 | } |
| 2206 | |
| 2207 | return ret; |
| 2208 | } |
| 2209 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2210 | 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] | 2211 | struct iovec *iovec, struct iovec *fast_iov, |
| 2212 | struct iov_iter *iter) |
| 2213 | { |
| 2214 | req->io->rw.nr_segs = iter->nr_segs; |
| 2215 | req->io->rw.size = io_size; |
| 2216 | req->io->rw.iov = iovec; |
| 2217 | if (!req->io->rw.iov) { |
| 2218 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2219 | memcpy(req->io->rw.iov, fast_iov, |
| 2220 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2221 | } else { |
| 2222 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2226 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2227 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2228 | if (!io_op_defs[req->opcode].async_ctx) |
| 2229 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2230 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2231 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2234 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2235 | struct iovec *iovec, struct iovec *fast_iov, |
| 2236 | struct iov_iter *iter) |
| 2237 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2238 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2239 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2240 | if (!req->io) { |
| 2241 | if (io_alloc_async_ctx(req)) |
| 2242 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2243 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2244 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2245 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2246 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2249 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2250 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2251 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2252 | struct io_async_ctx *io; |
| 2253 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2254 | ssize_t ret; |
| 2255 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2256 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2257 | if (ret) |
| 2258 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2259 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2260 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2261 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2262 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2263 | /* either don't need iovec imported or already have it */ |
| 2264 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2265 | return 0; |
| 2266 | |
| 2267 | io = req->io; |
| 2268 | io->rw.iov = io->rw.fast_iov; |
| 2269 | req->io = NULL; |
| 2270 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2271 | req->io = io; |
| 2272 | if (ret < 0) |
| 2273 | return ret; |
| 2274 | |
| 2275 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2276 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2279 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2280 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2281 | { |
| 2282 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2283 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2284 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2285 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2286 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2287 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2288 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2289 | if (ret < 0) |
| 2290 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2291 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2292 | /* Ensure we clear previously set non-block flag */ |
| 2293 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2294 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2295 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2296 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2297 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2298 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2299 | req->result = io_size; |
| 2300 | |
| 2301 | /* |
| 2302 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2303 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2304 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2305 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2306 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2307 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2308 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2309 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2310 | if (!ret) { |
| 2311 | ssize_t ret2; |
| 2312 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2313 | if (req->file->f_op->read_iter) |
| 2314 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2315 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2316 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2317 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2318 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2319 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2320 | kiocb_done(kiocb, ret2, nxt); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2321 | } else { |
| 2322 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2323 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2324 | inline_vecs, &iter); |
| 2325 | if (ret) |
| 2326 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2327 | /* any defer here is final, must blocking retry */ |
| 2328 | if (!(req->flags & REQ_F_NOWAIT)) |
| 2329 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2330 | return -EAGAIN; |
| 2331 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2332 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2333 | out_free: |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2334 | kfree(iovec); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2335 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2336 | return ret; |
| 2337 | } |
| 2338 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2339 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2340 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2341 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2342 | struct io_async_ctx *io; |
| 2343 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2344 | ssize_t ret; |
| 2345 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2346 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2347 | if (ret) |
| 2348 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2349 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2350 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2351 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2352 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2353 | /* either don't need iovec imported or already have it */ |
| 2354 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2355 | return 0; |
| 2356 | |
| 2357 | io = req->io; |
| 2358 | io->rw.iov = io->rw.fast_iov; |
| 2359 | req->io = NULL; |
| 2360 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2361 | req->io = io; |
| 2362 | if (ret < 0) |
| 2363 | return ret; |
| 2364 | |
| 2365 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2366 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2369 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2370 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2371 | { |
| 2372 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2373 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2374 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2375 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2376 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2377 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2378 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2379 | if (ret < 0) |
| 2380 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2381 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2382 | /* Ensure we clear previously set non-block flag */ |
| 2383 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2384 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2385 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2386 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2387 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2388 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2389 | req->result = io_size; |
| 2390 | |
| 2391 | /* |
| 2392 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2393 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2394 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2395 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2396 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2397 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2398 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2399 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2400 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2401 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2402 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2403 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2404 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2405 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2406 | ssize_t ret2; |
| 2407 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2408 | /* |
| 2409 | * Open-code file_start_write here to grab freeze protection, |
| 2410 | * which will be released by another thread in |
| 2411 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2412 | * released so that it doesn't complain about the held lock when |
| 2413 | * we return to userspace. |
| 2414 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2415 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2416 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2417 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2418 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2419 | SB_FREEZE_WRITE); |
| 2420 | } |
| 2421 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2422 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2423 | if (req->file->f_op->write_iter) |
| 2424 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2425 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2426 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 2427 | /* |
| 2428 | * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 2429 | * retry them without IOCB_NOWAIT. |
| 2430 | */ |
| 2431 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 2432 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2433 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2434 | kiocb_done(kiocb, ret2, nxt); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2435 | } else { |
| 2436 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2437 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2438 | inline_vecs, &iter); |
| 2439 | if (ret) |
| 2440 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2441 | /* any defer here is final, must blocking retry */ |
| 2442 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2443 | return -EAGAIN; |
| 2444 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2445 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2446 | out_free: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2447 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2448 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2449 | return ret; |
| 2450 | } |
| 2451 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2452 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2453 | { |
| 2454 | struct io_splice* sp = &req->splice; |
| 2455 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 2456 | int ret; |
| 2457 | |
| 2458 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2459 | return 0; |
| 2460 | |
| 2461 | sp->file_in = NULL; |
| 2462 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 2463 | sp->off_out = READ_ONCE(sqe->off); |
| 2464 | sp->len = READ_ONCE(sqe->len); |
| 2465 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 2466 | |
| 2467 | if (unlikely(sp->flags & ~valid_flags)) |
| 2468 | return -EINVAL; |
| 2469 | |
| 2470 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 2471 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2472 | if (ret) |
| 2473 | return ret; |
| 2474 | req->flags |= REQ_F_NEED_CLEANUP; |
| 2475 | |
| 2476 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) |
| 2477 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | static bool io_splice_punt(struct file *file) |
| 2483 | { |
| 2484 | if (get_pipe_info(file)) |
| 2485 | return false; |
| 2486 | if (!io_file_supports_async(file)) |
| 2487 | return true; |
| 2488 | return !(file->f_mode & O_NONBLOCK); |
| 2489 | } |
| 2490 | |
| 2491 | static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2492 | bool force_nonblock) |
| 2493 | { |
| 2494 | struct io_splice *sp = &req->splice; |
| 2495 | struct file *in = sp->file_in; |
| 2496 | struct file *out = sp->file_out; |
| 2497 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 2498 | loff_t *poff_in, *poff_out; |
| 2499 | long ret; |
| 2500 | |
| 2501 | if (force_nonblock) { |
| 2502 | if (io_splice_punt(in) || io_splice_punt(out)) |
| 2503 | return -EAGAIN; |
| 2504 | flags |= SPLICE_F_NONBLOCK; |
| 2505 | } |
| 2506 | |
| 2507 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 2508 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
| 2509 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
| 2510 | if (force_nonblock && ret == -EAGAIN) |
| 2511 | return -EAGAIN; |
| 2512 | |
| 2513 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2514 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 2515 | |
| 2516 | io_cqring_add_event(req, ret); |
| 2517 | if (ret != sp->len) |
| 2518 | req_set_fail_links(req); |
| 2519 | io_put_req_find_next(req, nxt); |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2523 | /* |
| 2524 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2525 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2526 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2527 | { |
| 2528 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2529 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2530 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2531 | return -EINVAL; |
| 2532 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2533 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2534 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2535 | return 0; |
| 2536 | } |
| 2537 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2538 | 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] | 2539 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2540 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2541 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2542 | if (!req->file) |
| 2543 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2544 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2545 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2546 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2547 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2548 | return -EINVAL; |
| 2549 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2550 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2551 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2552 | return -EINVAL; |
| 2553 | |
| 2554 | req->sync.off = READ_ONCE(sqe->off); |
| 2555 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2556 | return 0; |
| 2557 | } |
| 2558 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2559 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2560 | { |
| 2561 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2562 | req_set_fail_links(req); |
| 2563 | io_cqring_add_event(req, -ECANCELED); |
| 2564 | io_put_req(req); |
| 2565 | return true; |
| 2566 | } |
| 2567 | |
| 2568 | return false; |
| 2569 | } |
| 2570 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2571 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2572 | { |
| 2573 | struct io_wq_work *work = *workptr; |
| 2574 | struct io_kiocb *link = work->data; |
| 2575 | |
| 2576 | io_queue_linked_timeout(link); |
Pavel Begunkov | 5eae861 | 2020-02-28 10:36:38 +0300 | [diff] [blame] | 2577 | io_wq_submit_work(workptr); |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2581 | { |
| 2582 | struct io_kiocb *link; |
| 2583 | |
Pavel Begunkov | deb6dc0 | 2020-02-24 11:30:17 +0300 | [diff] [blame] | 2584 | io_prep_next_work(nxt, &link); |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2585 | *workptr = &nxt->work; |
| 2586 | if (link) { |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2587 | nxt->work.func = io_link_work_cb; |
| 2588 | nxt->work.data = link; |
| 2589 | } |
| 2590 | } |
| 2591 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2592 | static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2593 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2594 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2595 | int ret; |
| 2596 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2597 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2598 | end > 0 ? end : LLONG_MAX, |
| 2599 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2600 | if (ret < 0) |
| 2601 | req_set_fail_links(req); |
| 2602 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2603 | io_put_req_find_next(req, nxt); |
| 2604 | } |
| 2605 | |
| 2606 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2607 | { |
| 2608 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2609 | struct io_kiocb *nxt = NULL; |
| 2610 | |
| 2611 | if (io_req_cancelled(req)) |
| 2612 | return; |
| 2613 | __io_fsync(req, &nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2614 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2615 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2616 | } |
| 2617 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2618 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2619 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2620 | { |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2621 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2622 | if (force_nonblock) { |
| 2623 | io_put_req(req); |
| 2624 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2625 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2626 | } |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2627 | __io_fsync(req, nxt); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2628 | return 0; |
| 2629 | } |
| 2630 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2631 | static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2632 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2633 | int ret; |
| 2634 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 2635 | if (io_req_cancelled(req)) |
| 2636 | return; |
| 2637 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2638 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2639 | req->sync.len); |
| 2640 | if (ret < 0) |
| 2641 | req_set_fail_links(req); |
| 2642 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2643 | io_put_req_find_next(req, nxt); |
| 2644 | } |
| 2645 | |
| 2646 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2647 | { |
| 2648 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2649 | struct io_kiocb *nxt = NULL; |
| 2650 | |
| 2651 | __io_fallocate(req, &nxt); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2652 | if (nxt) |
| 2653 | io_wq_assign_next(workptr, nxt); |
| 2654 | } |
| 2655 | |
| 2656 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2657 | const struct io_uring_sqe *sqe) |
| 2658 | { |
| 2659 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2660 | return -EINVAL; |
| 2661 | |
| 2662 | req->sync.off = READ_ONCE(sqe->off); |
| 2663 | req->sync.len = READ_ONCE(sqe->addr); |
| 2664 | req->sync.mode = READ_ONCE(sqe->len); |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
| 2668 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2669 | bool force_nonblock) |
| 2670 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2671 | /* fallocate always requiring blocking context */ |
| 2672 | if (force_nonblock) { |
| 2673 | io_put_req(req); |
| 2674 | req->work.func = io_fallocate_finish; |
| 2675 | return -EAGAIN; |
| 2676 | } |
| 2677 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2678 | __io_fallocate(req, nxt); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2679 | return 0; |
| 2680 | } |
| 2681 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2682 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2683 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2684 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2685 | int ret; |
| 2686 | |
| 2687 | if (sqe->ioprio || sqe->buf_index) |
| 2688 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2689 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2690 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2691 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2692 | return 0; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2693 | |
| 2694 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2695 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2696 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2697 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2698 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2699 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2700 | if (IS_ERR(req->open.filename)) { |
| 2701 | ret = PTR_ERR(req->open.filename); |
| 2702 | req->open.filename = NULL; |
| 2703 | return ret; |
| 2704 | } |
| 2705 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2706 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2707 | return 0; |
| 2708 | } |
| 2709 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2710 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2711 | { |
| 2712 | struct open_how __user *how; |
| 2713 | const char __user *fname; |
| 2714 | size_t len; |
| 2715 | int ret; |
| 2716 | |
| 2717 | if (sqe->ioprio || sqe->buf_index) |
| 2718 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2719 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2720 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2721 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2722 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2723 | |
| 2724 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2725 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2726 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2727 | len = READ_ONCE(sqe->len); |
| 2728 | |
| 2729 | if (len < OPEN_HOW_SIZE_VER0) |
| 2730 | return -EINVAL; |
| 2731 | |
| 2732 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2733 | len); |
| 2734 | if (ret) |
| 2735 | return ret; |
| 2736 | |
| 2737 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2738 | req->open.how.flags |= O_LARGEFILE; |
| 2739 | |
| 2740 | req->open.filename = getname(fname); |
| 2741 | if (IS_ERR(req->open.filename)) { |
| 2742 | ret = PTR_ERR(req->open.filename); |
| 2743 | req->open.filename = NULL; |
| 2744 | return ret; |
| 2745 | } |
| 2746 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2747 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2748 | return 0; |
| 2749 | } |
| 2750 | |
| 2751 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2752 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2753 | { |
| 2754 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2755 | struct file *file; |
| 2756 | int ret; |
| 2757 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2758 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2759 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2760 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2761 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2762 | if (ret) |
| 2763 | goto err; |
| 2764 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2765 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2766 | if (ret < 0) |
| 2767 | goto err; |
| 2768 | |
| 2769 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2770 | if (IS_ERR(file)) { |
| 2771 | put_unused_fd(ret); |
| 2772 | ret = PTR_ERR(file); |
| 2773 | } else { |
| 2774 | fsnotify_open(file); |
| 2775 | fd_install(ret, file); |
| 2776 | } |
| 2777 | err: |
| 2778 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2779 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2780 | if (ret < 0) |
| 2781 | req_set_fail_links(req); |
| 2782 | io_cqring_add_event(req, ret); |
| 2783 | io_put_req_find_next(req, nxt); |
| 2784 | return 0; |
| 2785 | } |
| 2786 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2787 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2788 | bool force_nonblock) |
| 2789 | { |
| 2790 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2791 | return io_openat2(req, nxt, force_nonblock); |
| 2792 | } |
| 2793 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 2794 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 2795 | const struct io_uring_sqe *sqe) |
| 2796 | { |
| 2797 | #if defined(CONFIG_EPOLL) |
| 2798 | if (sqe->ioprio || sqe->buf_index) |
| 2799 | return -EINVAL; |
| 2800 | |
| 2801 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 2802 | req->epoll.op = READ_ONCE(sqe->len); |
| 2803 | req->epoll.fd = READ_ONCE(sqe->off); |
| 2804 | |
| 2805 | if (ep_op_has_event(req->epoll.op)) { |
| 2806 | struct epoll_event __user *ev; |
| 2807 | |
| 2808 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2809 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 2810 | return -EFAULT; |
| 2811 | } |
| 2812 | |
| 2813 | return 0; |
| 2814 | #else |
| 2815 | return -EOPNOTSUPP; |
| 2816 | #endif |
| 2817 | } |
| 2818 | |
| 2819 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2820 | bool force_nonblock) |
| 2821 | { |
| 2822 | #if defined(CONFIG_EPOLL) |
| 2823 | struct io_epoll *ie = &req->epoll; |
| 2824 | int ret; |
| 2825 | |
| 2826 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 2827 | if (force_nonblock && ret == -EAGAIN) |
| 2828 | return -EAGAIN; |
| 2829 | |
| 2830 | if (ret < 0) |
| 2831 | req_set_fail_links(req); |
| 2832 | io_cqring_add_event(req, ret); |
| 2833 | io_put_req_find_next(req, nxt); |
| 2834 | return 0; |
| 2835 | #else |
| 2836 | return -EOPNOTSUPP; |
| 2837 | #endif |
| 2838 | } |
| 2839 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2840 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2841 | { |
| 2842 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2843 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2844 | return -EINVAL; |
| 2845 | |
| 2846 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2847 | req->madvise.len = READ_ONCE(sqe->len); |
| 2848 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2849 | return 0; |
| 2850 | #else |
| 2851 | return -EOPNOTSUPP; |
| 2852 | #endif |
| 2853 | } |
| 2854 | |
| 2855 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2856 | bool force_nonblock) |
| 2857 | { |
| 2858 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2859 | struct io_madvise *ma = &req->madvise; |
| 2860 | int ret; |
| 2861 | |
| 2862 | if (force_nonblock) |
| 2863 | return -EAGAIN; |
| 2864 | |
| 2865 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2866 | if (ret < 0) |
| 2867 | req_set_fail_links(req); |
| 2868 | io_cqring_add_event(req, ret); |
| 2869 | io_put_req_find_next(req, nxt); |
| 2870 | return 0; |
| 2871 | #else |
| 2872 | return -EOPNOTSUPP; |
| 2873 | #endif |
| 2874 | } |
| 2875 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2876 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2877 | { |
| 2878 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2879 | return -EINVAL; |
| 2880 | |
| 2881 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2882 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2883 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2884 | return 0; |
| 2885 | } |
| 2886 | |
| 2887 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2888 | bool force_nonblock) |
| 2889 | { |
| 2890 | struct io_fadvise *fa = &req->fadvise; |
| 2891 | int ret; |
| 2892 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 2893 | if (force_nonblock) { |
| 2894 | switch (fa->advice) { |
| 2895 | case POSIX_FADV_NORMAL: |
| 2896 | case POSIX_FADV_RANDOM: |
| 2897 | case POSIX_FADV_SEQUENTIAL: |
| 2898 | break; |
| 2899 | default: |
| 2900 | return -EAGAIN; |
| 2901 | } |
| 2902 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2903 | |
| 2904 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2905 | if (ret < 0) |
| 2906 | req_set_fail_links(req); |
| 2907 | io_cqring_add_event(req, ret); |
| 2908 | io_put_req_find_next(req, nxt); |
| 2909 | return 0; |
| 2910 | } |
| 2911 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2912 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2913 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2914 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2915 | unsigned lookup_flags; |
| 2916 | int ret; |
| 2917 | |
| 2918 | if (sqe->ioprio || sqe->buf_index) |
| 2919 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2920 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2921 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2922 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2923 | return 0; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2924 | |
| 2925 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2926 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2927 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2928 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2929 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2930 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2931 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2932 | return -EINVAL; |
| 2933 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2934 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2935 | if (IS_ERR(req->open.filename)) { |
| 2936 | ret = PTR_ERR(req->open.filename); |
| 2937 | req->open.filename = NULL; |
| 2938 | return ret; |
| 2939 | } |
| 2940 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2941 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2942 | return 0; |
| 2943 | } |
| 2944 | |
| 2945 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2946 | bool force_nonblock) |
| 2947 | { |
| 2948 | struct io_open *ctx = &req->open; |
| 2949 | unsigned lookup_flags; |
| 2950 | struct path path; |
| 2951 | struct kstat stat; |
| 2952 | int ret; |
| 2953 | |
| 2954 | if (force_nonblock) |
| 2955 | return -EAGAIN; |
| 2956 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2957 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2958 | return -EINVAL; |
| 2959 | |
| 2960 | retry: |
| 2961 | /* filename_lookup() drops it, keep a reference */ |
| 2962 | ctx->filename->refcnt++; |
| 2963 | |
| 2964 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2965 | NULL); |
| 2966 | if (ret) |
| 2967 | goto err; |
| 2968 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2969 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2970 | path_put(&path); |
| 2971 | if (retry_estale(ret, lookup_flags)) { |
| 2972 | lookup_flags |= LOOKUP_REVAL; |
| 2973 | goto retry; |
| 2974 | } |
| 2975 | if (!ret) |
| 2976 | ret = cp_statx(&stat, ctx->buffer); |
| 2977 | err: |
| 2978 | putname(ctx->filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2979 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2980 | if (ret < 0) |
| 2981 | req_set_fail_links(req); |
| 2982 | io_cqring_add_event(req, ret); |
| 2983 | io_put_req_find_next(req, nxt); |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2987 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2988 | { |
| 2989 | /* |
| 2990 | * If we queue this for async, it must not be cancellable. That would |
| 2991 | * leave the 'file' in an undeterminate state. |
| 2992 | */ |
| 2993 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2994 | |
| 2995 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2996 | sqe->rw_flags || sqe->buf_index) |
| 2997 | return -EINVAL; |
| 2998 | if (sqe->flags & IOSQE_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2999 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3000 | |
| 3001 | req->close.fd = READ_ONCE(sqe->fd); |
| 3002 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 3003 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3004 | return -EBADF; |
| 3005 | |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3009 | /* only called when __close_fd_get_file() is done */ |
| 3010 | static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3011 | { |
| 3012 | int ret; |
| 3013 | |
| 3014 | ret = filp_close(req->close.put_file, req->work.files); |
| 3015 | if (ret < 0) |
| 3016 | req_set_fail_links(req); |
| 3017 | io_cqring_add_event(req, ret); |
| 3018 | fput(req->close.put_file); |
| 3019 | io_put_req_find_next(req, nxt); |
| 3020 | } |
| 3021 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3022 | static void io_close_finish(struct io_wq_work **workptr) |
| 3023 | { |
| 3024 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3025 | struct io_kiocb *nxt = NULL; |
| 3026 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 3027 | /* not cancellable, don't do io_req_cancelled() */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3028 | __io_close_finish(req, &nxt); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3029 | if (nxt) |
| 3030 | io_wq_assign_next(workptr, nxt); |
| 3031 | } |
| 3032 | |
| 3033 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3034 | bool force_nonblock) |
| 3035 | { |
| 3036 | int ret; |
| 3037 | |
| 3038 | req->close.put_file = NULL; |
| 3039 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 3040 | if (ret < 0) |
| 3041 | return ret; |
| 3042 | |
| 3043 | /* 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] | 3044 | 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] | 3045 | goto eagain; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3046 | |
| 3047 | /* |
| 3048 | * No ->flush(), safely close from here and just punt the |
| 3049 | * fput() to async context. |
| 3050 | */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3051 | __io_close_finish(req, nxt); |
| 3052 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3053 | eagain: |
| 3054 | req->work.func = io_close_finish; |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 3055 | /* |
| 3056 | * Do manual async queue here to avoid grabbing files - we don't |
| 3057 | * need the files, and it'll cause io_close_finish() to close |
| 3058 | * the file again and cause a double CQE entry for this request |
| 3059 | */ |
| 3060 | io_queue_async_work(req); |
| 3061 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3062 | } |
| 3063 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3064 | 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] | 3065 | { |
| 3066 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3067 | |
| 3068 | if (!req->file) |
| 3069 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3070 | |
| 3071 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3072 | return -EINVAL; |
| 3073 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3074 | return -EINVAL; |
| 3075 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3076 | req->sync.off = READ_ONCE(sqe->off); |
| 3077 | req->sync.len = READ_ONCE(sqe->len); |
| 3078 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3079 | return 0; |
| 3080 | } |
| 3081 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3082 | 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] | 3083 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3084 | int ret; |
| 3085 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3086 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3087 | req->sync.flags); |
| 3088 | if (ret < 0) |
| 3089 | req_set_fail_links(req); |
| 3090 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3091 | io_put_req_find_next(req, nxt); |
| 3092 | } |
| 3093 | |
| 3094 | |
| 3095 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 3096 | { |
| 3097 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3098 | struct io_kiocb *nxt = NULL; |
| 3099 | |
| 3100 | if (io_req_cancelled(req)) |
| 3101 | return; |
| 3102 | __io_sync_file_range(req, &nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3103 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3104 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3105 | } |
| 3106 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3107 | 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] | 3108 | bool force_nonblock) |
| 3109 | { |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3110 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3111 | if (force_nonblock) { |
| 3112 | io_put_req(req); |
| 3113 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3114 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3115 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3116 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3117 | __io_sync_file_range(req, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3118 | return 0; |
| 3119 | } |
| 3120 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3121 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3122 | struct io_async_msghdr *kmsg) |
| 3123 | { |
| 3124 | if (req->io) |
| 3125 | return -EAGAIN; |
| 3126 | if (io_alloc_async_ctx(req)) { |
| 3127 | if (kmsg->iov != kmsg->fast_iov) |
| 3128 | kfree(kmsg->iov); |
| 3129 | return -ENOMEM; |
| 3130 | } |
| 3131 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3132 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3133 | return -EAGAIN; |
| 3134 | } |
| 3135 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3136 | 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] | 3137 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3138 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3139 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3140 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3141 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3142 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3143 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3144 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3145 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3146 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3147 | #ifdef CONFIG_COMPAT |
| 3148 | if (req->ctx->compat) |
| 3149 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3150 | #endif |
| 3151 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3152 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3153 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3154 | /* iovec is already imported */ |
| 3155 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3156 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3157 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3158 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3159 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3160 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3161 | if (!ret) |
| 3162 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3163 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3164 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3165 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3166 | #endif |
| 3167 | } |
| 3168 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3169 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3170 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3171 | { |
| 3172 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3173 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3174 | struct socket *sock; |
| 3175 | int ret; |
| 3176 | |
| 3177 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3178 | return -EINVAL; |
| 3179 | |
| 3180 | sock = sock_from_file(req->file, &ret); |
| 3181 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3182 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3183 | unsigned flags; |
| 3184 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3185 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3186 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3187 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3188 | /* if iov is set, it's allocated already */ |
| 3189 | if (!kmsg->iov) |
| 3190 | kmsg->iov = kmsg->fast_iov; |
| 3191 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3192 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3193 | struct io_sr_msg *sr = &req->sr_msg; |
| 3194 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3195 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3196 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3197 | |
| 3198 | io.msg.iov = io.msg.fast_iov; |
| 3199 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3200 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3201 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3202 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3205 | flags = req->sr_msg.msg_flags; |
| 3206 | if (flags & MSG_DONTWAIT) |
| 3207 | req->flags |= REQ_F_NOWAIT; |
| 3208 | else if (force_nonblock) |
| 3209 | flags |= MSG_DONTWAIT; |
| 3210 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3211 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3212 | if (force_nonblock && ret == -EAGAIN) |
| 3213 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3214 | if (ret == -ERESTARTSYS) |
| 3215 | ret = -EINTR; |
| 3216 | } |
| 3217 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3218 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3219 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3220 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3221 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3222 | if (ret < 0) |
| 3223 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3224 | io_put_req_find_next(req, nxt); |
| 3225 | return 0; |
| 3226 | #else |
| 3227 | return -EOPNOTSUPP; |
| 3228 | #endif |
| 3229 | } |
| 3230 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3231 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3232 | bool force_nonblock) |
| 3233 | { |
| 3234 | #if defined(CONFIG_NET) |
| 3235 | struct socket *sock; |
| 3236 | int ret; |
| 3237 | |
| 3238 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3239 | return -EINVAL; |
| 3240 | |
| 3241 | sock = sock_from_file(req->file, &ret); |
| 3242 | if (sock) { |
| 3243 | struct io_sr_msg *sr = &req->sr_msg; |
| 3244 | struct msghdr msg; |
| 3245 | struct iovec iov; |
| 3246 | unsigned flags; |
| 3247 | |
| 3248 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3249 | &msg.msg_iter); |
| 3250 | if (ret) |
| 3251 | return ret; |
| 3252 | |
| 3253 | msg.msg_name = NULL; |
| 3254 | msg.msg_control = NULL; |
| 3255 | msg.msg_controllen = 0; |
| 3256 | msg.msg_namelen = 0; |
| 3257 | |
| 3258 | flags = req->sr_msg.msg_flags; |
| 3259 | if (flags & MSG_DONTWAIT) |
| 3260 | req->flags |= REQ_F_NOWAIT; |
| 3261 | else if (force_nonblock) |
| 3262 | flags |= MSG_DONTWAIT; |
| 3263 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3264 | msg.msg_flags = flags; |
| 3265 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3266 | if (force_nonblock && ret == -EAGAIN) |
| 3267 | return -EAGAIN; |
| 3268 | if (ret == -ERESTARTSYS) |
| 3269 | ret = -EINTR; |
| 3270 | } |
| 3271 | |
| 3272 | io_cqring_add_event(req, ret); |
| 3273 | if (ret < 0) |
| 3274 | req_set_fail_links(req); |
| 3275 | io_put_req_find_next(req, nxt); |
| 3276 | return 0; |
| 3277 | #else |
| 3278 | return -EOPNOTSUPP; |
| 3279 | #endif |
| 3280 | } |
| 3281 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3282 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3283 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3284 | { |
| 3285 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3286 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3287 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3288 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3289 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3290 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3291 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3292 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3293 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3294 | #ifdef CONFIG_COMPAT |
| 3295 | if (req->ctx->compat) |
| 3296 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3297 | #endif |
| 3298 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3299 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3300 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3301 | /* iovec is already imported */ |
| 3302 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3303 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3304 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3305 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3306 | ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3307 | &io->msg.uaddr, &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3308 | if (!ret) |
| 3309 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3310 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3311 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3312 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3313 | #endif |
| 3314 | } |
| 3315 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3316 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3317 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3318 | { |
| 3319 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3320 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3321 | struct socket *sock; |
| 3322 | int ret; |
| 3323 | |
| 3324 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3325 | return -EINVAL; |
| 3326 | |
| 3327 | sock = sock_from_file(req->file, &ret); |
| 3328 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3329 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3330 | unsigned flags; |
| 3331 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3332 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3333 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3334 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3335 | /* if iov is set, it's allocated already */ |
| 3336 | if (!kmsg->iov) |
| 3337 | kmsg->iov = kmsg->fast_iov; |
| 3338 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3339 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3340 | struct io_sr_msg *sr = &req->sr_msg; |
| 3341 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3342 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3343 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3344 | |
| 3345 | io.msg.iov = io.msg.fast_iov; |
| 3346 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3347 | sr->msg_flags, &io.msg.uaddr, |
| 3348 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3349 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3350 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3351 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3352 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3353 | flags = req->sr_msg.msg_flags; |
| 3354 | if (flags & MSG_DONTWAIT) |
| 3355 | req->flags |= REQ_F_NOWAIT; |
| 3356 | else if (force_nonblock) |
| 3357 | flags |= MSG_DONTWAIT; |
| 3358 | |
| 3359 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3360 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3361 | if (force_nonblock && ret == -EAGAIN) |
| 3362 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3363 | if (ret == -ERESTARTSYS) |
| 3364 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3365 | } |
| 3366 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3367 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3368 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3369 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3370 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3371 | if (ret < 0) |
| 3372 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3373 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3374 | return 0; |
| 3375 | #else |
| 3376 | return -EOPNOTSUPP; |
| 3377 | #endif |
| 3378 | } |
| 3379 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3380 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3381 | bool force_nonblock) |
| 3382 | { |
| 3383 | #if defined(CONFIG_NET) |
| 3384 | struct socket *sock; |
| 3385 | int ret; |
| 3386 | |
| 3387 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3388 | return -EINVAL; |
| 3389 | |
| 3390 | sock = sock_from_file(req->file, &ret); |
| 3391 | if (sock) { |
| 3392 | struct io_sr_msg *sr = &req->sr_msg; |
| 3393 | struct msghdr msg; |
| 3394 | struct iovec iov; |
| 3395 | unsigned flags; |
| 3396 | |
| 3397 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3398 | &msg.msg_iter); |
| 3399 | if (ret) |
| 3400 | return ret; |
| 3401 | |
| 3402 | msg.msg_name = NULL; |
| 3403 | msg.msg_control = NULL; |
| 3404 | msg.msg_controllen = 0; |
| 3405 | msg.msg_namelen = 0; |
| 3406 | msg.msg_iocb = NULL; |
| 3407 | msg.msg_flags = 0; |
| 3408 | |
| 3409 | flags = req->sr_msg.msg_flags; |
| 3410 | if (flags & MSG_DONTWAIT) |
| 3411 | req->flags |= REQ_F_NOWAIT; |
| 3412 | else if (force_nonblock) |
| 3413 | flags |= MSG_DONTWAIT; |
| 3414 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3415 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3416 | if (force_nonblock && ret == -EAGAIN) |
| 3417 | return -EAGAIN; |
| 3418 | if (ret == -ERESTARTSYS) |
| 3419 | ret = -EINTR; |
| 3420 | } |
| 3421 | |
| 3422 | io_cqring_add_event(req, ret); |
| 3423 | if (ret < 0) |
| 3424 | req_set_fail_links(req); |
| 3425 | io_put_req_find_next(req, nxt); |
| 3426 | return 0; |
| 3427 | #else |
| 3428 | return -EOPNOTSUPP; |
| 3429 | #endif |
| 3430 | } |
| 3431 | |
| 3432 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3433 | 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] | 3434 | { |
| 3435 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3436 | struct io_accept *accept = &req->accept; |
| 3437 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3438 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3439 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3440 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3441 | return -EINVAL; |
| 3442 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3443 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3444 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3445 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3446 | return 0; |
| 3447 | #else |
| 3448 | return -EOPNOTSUPP; |
| 3449 | #endif |
| 3450 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3451 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3452 | #if defined(CONFIG_NET) |
| 3453 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3454 | bool force_nonblock) |
| 3455 | { |
| 3456 | struct io_accept *accept = &req->accept; |
| 3457 | unsigned file_flags; |
| 3458 | int ret; |
| 3459 | |
| 3460 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3461 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3462 | accept->addr_len, accept->flags); |
| 3463 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3464 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3465 | if (ret == -ERESTARTSYS) |
| 3466 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3467 | if (ret < 0) |
| 3468 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3469 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3470 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3471 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3475 | { |
| 3476 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3477 | struct io_kiocb *nxt = NULL; |
| 3478 | |
Jens Axboe | e441d1c | 2020-02-20 09:59:02 -0700 | [diff] [blame] | 3479 | io_put_req(req); |
| 3480 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3481 | if (io_req_cancelled(req)) |
| 3482 | return; |
| 3483 | __io_accept(req, &nxt, false); |
| 3484 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3485 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3486 | } |
| 3487 | #endif |
| 3488 | |
| 3489 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3490 | bool force_nonblock) |
| 3491 | { |
| 3492 | #if defined(CONFIG_NET) |
| 3493 | int ret; |
| 3494 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3495 | ret = __io_accept(req, nxt, force_nonblock); |
| 3496 | if (ret == -EAGAIN && force_nonblock) { |
| 3497 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3498 | return -EAGAIN; |
| 3499 | } |
| 3500 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3501 | #else |
| 3502 | return -EOPNOTSUPP; |
| 3503 | #endif |
| 3504 | } |
| 3505 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3506 | 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] | 3507 | { |
| 3508 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3509 | struct io_connect *conn = &req->connect; |
| 3510 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3511 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3512 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3513 | return -EINVAL; |
| 3514 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3515 | return -EINVAL; |
| 3516 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3517 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3518 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3519 | |
| 3520 | if (!io) |
| 3521 | return 0; |
| 3522 | |
| 3523 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3524 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3525 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3526 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3527 | #endif |
| 3528 | } |
| 3529 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3530 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3531 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3532 | { |
| 3533 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3534 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3535 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3536 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3537 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3538 | if (req->io) { |
| 3539 | io = req->io; |
| 3540 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3541 | ret = move_addr_to_kernel(req->connect.addr, |
| 3542 | req->connect.addr_len, |
| 3543 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3544 | if (ret) |
| 3545 | goto out; |
| 3546 | io = &__io; |
| 3547 | } |
| 3548 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3549 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3550 | |
| 3551 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3552 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3553 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3554 | if (req->io) |
| 3555 | return -EAGAIN; |
| 3556 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3557 | ret = -ENOMEM; |
| 3558 | goto out; |
| 3559 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3560 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3561 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3562 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3563 | if (ret == -ERESTARTSYS) |
| 3564 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3565 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3566 | if (ret < 0) |
| 3567 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3568 | io_cqring_add_event(req, ret); |
| 3569 | io_put_req_find_next(req, nxt); |
| 3570 | return 0; |
| 3571 | #else |
| 3572 | return -EOPNOTSUPP; |
| 3573 | #endif |
| 3574 | } |
| 3575 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3576 | struct io_poll_table { |
| 3577 | struct poll_table_struct pt; |
| 3578 | struct io_kiocb *req; |
| 3579 | int error; |
| 3580 | }; |
| 3581 | |
| 3582 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
| 3583 | struct wait_queue_head *head) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3584 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3585 | if (unlikely(poll->head)) { |
| 3586 | pt->error = -EINVAL; |
| 3587 | return; |
| 3588 | } |
| 3589 | |
| 3590 | pt->error = 0; |
| 3591 | poll->head = head; |
| 3592 | add_wait_queue(head, &poll->wait); |
| 3593 | } |
| 3594 | |
| 3595 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3596 | struct poll_table_struct *p) |
| 3597 | { |
| 3598 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3599 | |
| 3600 | __io_queue_proc(&pt->req->apoll->poll, pt, head); |
| 3601 | } |
| 3602 | |
| 3603 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 3604 | __poll_t mask, task_work_func_t func) |
| 3605 | { |
| 3606 | struct task_struct *tsk; |
| 3607 | |
| 3608 | /* for instances that support it check for an event match first: */ |
| 3609 | if (mask && !(mask & poll->events)) |
| 3610 | return 0; |
| 3611 | |
| 3612 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 3613 | |
| 3614 | list_del_init(&poll->wait.entry); |
| 3615 | |
| 3616 | tsk = req->task; |
| 3617 | req->result = mask; |
| 3618 | init_task_work(&req->task_work, func); |
| 3619 | /* |
| 3620 | * If this fails, then the task is exiting. If that is the case, then |
| 3621 | * the exit check will ultimately cancel these work items. Hence we |
| 3622 | * don't need to check here and handle it specifically. |
| 3623 | */ |
| 3624 | task_work_add(tsk, &req->task_work, true); |
| 3625 | wake_up_process(tsk); |
| 3626 | return 1; |
| 3627 | } |
| 3628 | |
| 3629 | static void io_async_task_func(struct callback_head *cb) |
| 3630 | { |
| 3631 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 3632 | struct async_poll *apoll = req->apoll; |
| 3633 | struct io_ring_ctx *ctx = req->ctx; |
| 3634 | |
| 3635 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 3636 | |
| 3637 | WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry)); |
| 3638 | |
| 3639 | if (hash_hashed(&req->hash_node)) { |
| 3640 | spin_lock_irq(&ctx->completion_lock); |
| 3641 | hash_del(&req->hash_node); |
| 3642 | spin_unlock_irq(&ctx->completion_lock); |
| 3643 | } |
| 3644 | |
| 3645 | /* restore ->work in case we need to retry again */ |
| 3646 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
| 3647 | |
| 3648 | __set_current_state(TASK_RUNNING); |
| 3649 | mutex_lock(&ctx->uring_lock); |
| 3650 | __io_queue_sqe(req, NULL); |
| 3651 | mutex_unlock(&ctx->uring_lock); |
| 3652 | |
| 3653 | kfree(apoll); |
| 3654 | } |
| 3655 | |
| 3656 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3657 | void *key) |
| 3658 | { |
| 3659 | struct io_kiocb *req = wait->private; |
| 3660 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 3661 | |
| 3662 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 3663 | key_to_poll(key)); |
| 3664 | |
| 3665 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 3666 | } |
| 3667 | |
| 3668 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3669 | { |
| 3670 | struct io_ring_ctx *ctx = req->ctx; |
| 3671 | struct hlist_head *list; |
| 3672 | |
| 3673 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3674 | hlist_add_head(&req->hash_node, list); |
| 3675 | } |
| 3676 | |
| 3677 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 3678 | struct io_poll_iocb *poll, |
| 3679 | struct io_poll_table *ipt, __poll_t mask, |
| 3680 | wait_queue_func_t wake_func) |
| 3681 | __acquires(&ctx->completion_lock) |
| 3682 | { |
| 3683 | struct io_ring_ctx *ctx = req->ctx; |
| 3684 | bool cancel = false; |
| 3685 | |
| 3686 | poll->file = req->file; |
| 3687 | poll->head = NULL; |
| 3688 | poll->done = poll->canceled = false; |
| 3689 | poll->events = mask; |
| 3690 | |
| 3691 | ipt->pt._key = mask; |
| 3692 | ipt->req = req; |
| 3693 | ipt->error = -EINVAL; |
| 3694 | |
| 3695 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3696 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 3697 | poll->wait.private = req; |
| 3698 | |
| 3699 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 3700 | |
| 3701 | spin_lock_irq(&ctx->completion_lock); |
| 3702 | if (likely(poll->head)) { |
| 3703 | spin_lock(&poll->head->lock); |
| 3704 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 3705 | if (ipt->error) |
| 3706 | cancel = true; |
| 3707 | ipt->error = 0; |
| 3708 | mask = 0; |
| 3709 | } |
| 3710 | if (mask || ipt->error) |
| 3711 | list_del_init(&poll->wait.entry); |
| 3712 | else if (cancel) |
| 3713 | WRITE_ONCE(poll->canceled, true); |
| 3714 | else if (!poll->done) /* actually waiting for an event */ |
| 3715 | io_poll_req_insert(req); |
| 3716 | spin_unlock(&poll->head->lock); |
| 3717 | } |
| 3718 | |
| 3719 | return mask; |
| 3720 | } |
| 3721 | |
| 3722 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 3723 | { |
| 3724 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 3725 | struct io_ring_ctx *ctx = req->ctx; |
| 3726 | struct async_poll *apoll; |
| 3727 | struct io_poll_table ipt; |
| 3728 | __poll_t mask, ret; |
| 3729 | |
| 3730 | if (!req->file || !file_can_poll(req->file)) |
| 3731 | return false; |
| 3732 | if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED)) |
| 3733 | return false; |
| 3734 | if (!def->pollin && !def->pollout) |
| 3735 | return false; |
| 3736 | |
| 3737 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 3738 | if (unlikely(!apoll)) |
| 3739 | return false; |
| 3740 | |
| 3741 | req->flags |= REQ_F_POLLED; |
| 3742 | memcpy(&apoll->work, &req->work, sizeof(req->work)); |
| 3743 | |
| 3744 | /* |
| 3745 | * Don't need a reference here, as we're adding it to the task |
| 3746 | * task_works list. If the task exits, the list is pruned. |
| 3747 | */ |
| 3748 | req->task = current; |
| 3749 | req->apoll = apoll; |
| 3750 | INIT_HLIST_NODE(&req->hash_node); |
| 3751 | |
| 3752 | if (def->pollin) |
| 3753 | mask = POLLIN | POLLRDNORM; |
| 3754 | if (def->pollout) |
| 3755 | mask |= POLLOUT | POLLWRNORM; |
| 3756 | mask |= POLLERR | POLLPRI; |
| 3757 | |
| 3758 | ipt.pt._qproc = io_async_queue_proc; |
| 3759 | |
| 3760 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 3761 | io_async_wake); |
| 3762 | if (ret) { |
| 3763 | ipt.error = 0; |
| 3764 | apoll->poll.done = true; |
| 3765 | spin_unlock_irq(&ctx->completion_lock); |
| 3766 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
| 3767 | kfree(apoll); |
| 3768 | return false; |
| 3769 | } |
| 3770 | spin_unlock_irq(&ctx->completion_lock); |
| 3771 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 3772 | apoll->poll.events); |
| 3773 | return true; |
| 3774 | } |
| 3775 | |
| 3776 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 3777 | struct io_poll_iocb *poll) |
| 3778 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3779 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3780 | |
| 3781 | spin_lock(&poll->head->lock); |
| 3782 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3783 | if (!list_empty(&poll->wait.entry)) { |
| 3784 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3785 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3786 | } |
| 3787 | spin_unlock(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3788 | return do_complete; |
| 3789 | } |
| 3790 | |
| 3791 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 3792 | { |
| 3793 | bool do_complete; |
| 3794 | |
| 3795 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 3796 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 3797 | } else { |
| 3798 | /* non-poll requests have submit ref still */ |
| 3799 | do_complete = __io_poll_remove_one(req, &req->apoll->poll); |
| 3800 | if (do_complete) |
| 3801 | io_put_req(req); |
| 3802 | } |
| 3803 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3804 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3805 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3806 | if (do_complete) { |
| 3807 | io_cqring_fill_event(req, -ECANCELED); |
| 3808 | io_commit_cqring(req->ctx); |
| 3809 | req->flags |= REQ_F_COMP_LOCKED; |
| 3810 | io_put_req(req); |
| 3811 | } |
| 3812 | |
| 3813 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3817 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3818 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3819 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3820 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3821 | |
| 3822 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3823 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3824 | struct hlist_head *list; |
| 3825 | |
| 3826 | list = &ctx->cancel_hash[i]; |
| 3827 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3828 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3829 | } |
| 3830 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3831 | |
| 3832 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3833 | } |
| 3834 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3835 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3836 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3837 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3838 | struct io_kiocb *req; |
| 3839 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3840 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3841 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3842 | if (sqe_addr != req->user_data) |
| 3843 | continue; |
| 3844 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3845 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3846 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | return -ENOENT; |
| 3850 | } |
| 3851 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3852 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3853 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3854 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3855 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3856 | return -EINVAL; |
| 3857 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3858 | sqe->poll_events) |
| 3859 | return -EINVAL; |
| 3860 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3861 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3862 | return 0; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * Find a running poll command that matches one specified in sqe->addr, |
| 3867 | * and remove it if found. |
| 3868 | */ |
| 3869 | static int io_poll_remove(struct io_kiocb *req) |
| 3870 | { |
| 3871 | struct io_ring_ctx *ctx = req->ctx; |
| 3872 | u64 addr; |
| 3873 | int ret; |
| 3874 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3875 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3876 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3877 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3878 | spin_unlock_irq(&ctx->completion_lock); |
| 3879 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3880 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3881 | if (ret < 0) |
| 3882 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3883 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3884 | return 0; |
| 3885 | } |
| 3886 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3887 | 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] | 3888 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3889 | struct io_ring_ctx *ctx = req->ctx; |
| 3890 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3891 | req->poll.done = true; |
Pavel Begunkov | b0a2034 | 2020-02-28 10:36:35 +0300 | [diff] [blame] | 3892 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3893 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3896 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3897 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3898 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3899 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3900 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3901 | hash_del(&req->hash_node); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3902 | io_poll_complete(req, req->result, 0); |
| 3903 | req->flags |= REQ_F_COMP_LOCKED; |
| 3904 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3905 | spin_unlock_irq(&ctx->completion_lock); |
| 3906 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3907 | io_cqring_ev_posted(ctx); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3908 | } |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3909 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3910 | static void io_poll_task_func(struct callback_head *cb) |
| 3911 | { |
| 3912 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 3913 | struct io_kiocb *nxt = NULL; |
| 3914 | |
| 3915 | io_poll_task_handler(req, &nxt); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3916 | if (nxt) { |
| 3917 | struct io_ring_ctx *ctx = nxt->ctx; |
| 3918 | |
| 3919 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3920 | __io_queue_sqe(nxt, NULL); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3921 | mutex_unlock(&ctx->uring_lock); |
| 3922 | } |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3925 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3926 | void *key) |
| 3927 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 3928 | struct io_kiocb *req = wait->private; |
| 3929 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3930 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3931 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3932 | } |
| 3933 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3934 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3935 | struct poll_table_struct *p) |
| 3936 | { |
| 3937 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3938 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3939 | __io_queue_proc(&pt->req->poll, pt, head); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3940 | } |
| 3941 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3942 | 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] | 3943 | { |
| 3944 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3945 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3946 | |
| 3947 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3948 | return -EINVAL; |
| 3949 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3950 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3951 | if (!poll->file) |
| 3952 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3953 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3954 | events = READ_ONCE(sqe->poll_events); |
| 3955 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3956 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3957 | /* |
| 3958 | * Don't need a reference here, as we're adding it to the task |
| 3959 | * task_works list. If the task exits, the list is pruned. |
| 3960 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3961 | req->task = current; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3962 | return 0; |
| 3963 | } |
| 3964 | |
| 3965 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3966 | { |
| 3967 | struct io_poll_iocb *poll = &req->poll; |
| 3968 | struct io_ring_ctx *ctx = req->ctx; |
| 3969 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3970 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3971 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3972 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3973 | INIT_LIST_HEAD(&req->list); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3974 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3975 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3976 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 3977 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3978 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3979 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3980 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3981 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3982 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3983 | spin_unlock_irq(&ctx->completion_lock); |
| 3984 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3985 | if (mask) { |
| 3986 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3987 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3988 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3989 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3990 | } |
| 3991 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3992 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3993 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3994 | struct io_timeout_data *data = container_of(timer, |
| 3995 | struct io_timeout_data, timer); |
| 3996 | struct io_kiocb *req = data->req; |
| 3997 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3998 | unsigned long flags; |
| 3999 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4000 | atomic_inc(&ctx->cq_timeouts); |
| 4001 | |
| 4002 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4003 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4004 | * We could be racing with timeout deletion. If the list is empty, |
| 4005 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4006 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4007 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4008 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4009 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4010 | /* |
| 4011 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4012 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4013 | * pointer will be increased, otherwise other timeout reqs may |
| 4014 | * return in advance without waiting for enough wait_nr. |
| 4015 | */ |
| 4016 | prev = req; |
| 4017 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 4018 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4019 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4020 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4021 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4022 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4023 | io_commit_cqring(ctx); |
| 4024 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4025 | |
| 4026 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4027 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4028 | io_put_req(req); |
| 4029 | return HRTIMER_NORESTART; |
| 4030 | } |
| 4031 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4032 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 4033 | { |
| 4034 | struct io_kiocb *req; |
| 4035 | int ret = -ENOENT; |
| 4036 | |
| 4037 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 4038 | if (user_data == req->user_data) { |
| 4039 | list_del_init(&req->list); |
| 4040 | ret = 0; |
| 4041 | break; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | if (ret == -ENOENT) |
| 4046 | return ret; |
| 4047 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4048 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4049 | if (ret == -1) |
| 4050 | return -EALREADY; |
| 4051 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4052 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4053 | io_cqring_fill_event(req, -ECANCELED); |
| 4054 | io_put_req(req); |
| 4055 | return 0; |
| 4056 | } |
| 4057 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4058 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 4059 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4060 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4061 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4062 | return -EINVAL; |
| 4063 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 4064 | return -EINVAL; |
| 4065 | |
| 4066 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 4067 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 4068 | if (req->timeout.flags) |
| 4069 | return -EINVAL; |
| 4070 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4071 | return 0; |
| 4072 | } |
| 4073 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4074 | /* |
| 4075 | * Remove or update an existing timeout command |
| 4076 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4077 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4078 | { |
| 4079 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4080 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4081 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4082 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4083 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4084 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4085 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4086 | io_commit_cqring(ctx); |
| 4087 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4088 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4089 | if (ret < 0) |
| 4090 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4091 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4092 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4093 | } |
| 4094 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4095 | 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] | 4096 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4097 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4098 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4099 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4100 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4101 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4102 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4103 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4104 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4105 | if (sqe->off && is_timeout_link) |
| 4106 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4107 | flags = READ_ONCE(sqe->timeout_flags); |
| 4108 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4109 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 4110 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4111 | req->timeout.count = READ_ONCE(sqe->off); |
| 4112 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4113 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4114 | return -ENOMEM; |
| 4115 | |
| 4116 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4117 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4118 | req->flags |= REQ_F_TIMEOUT; |
| 4119 | |
| 4120 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4121 | return -EFAULT; |
| 4122 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4123 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4124 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4125 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4126 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4127 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4128 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 4129 | return 0; |
| 4130 | } |
| 4131 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4132 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4133 | { |
| 4134 | unsigned count; |
| 4135 | struct io_ring_ctx *ctx = req->ctx; |
| 4136 | struct io_timeout_data *data; |
| 4137 | struct list_head *entry; |
| 4138 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4139 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4140 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4141 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4142 | /* |
| 4143 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4144 | * timeout event to be satisfied. If it isn't set, then this is |
| 4145 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4146 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4147 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4148 | if (!count) { |
| 4149 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 4150 | spin_lock_irq(&ctx->completion_lock); |
| 4151 | entry = ctx->timeout_list.prev; |
| 4152 | goto add; |
| 4153 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4154 | |
| 4155 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4156 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4157 | |
| 4158 | /* |
| 4159 | * Insertion sort, ensuring the first entry in the list is always |
| 4160 | * the one we need first. |
| 4161 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4162 | spin_lock_irq(&ctx->completion_lock); |
| 4163 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 4164 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4165 | unsigned nxt_sq_head; |
| 4166 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4167 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4168 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4169 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 4170 | continue; |
| 4171 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4172 | /* |
| 4173 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 4174 | * long to store it. |
| 4175 | */ |
| 4176 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 4177 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 4178 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4179 | |
| 4180 | /* |
| 4181 | * cached_sq_head may overflow, and it will never overflow twice |
| 4182 | * once there is some timeout req still be valid. |
| 4183 | */ |
| 4184 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 4185 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4186 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4187 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4188 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4189 | |
| 4190 | /* |
| 4191 | * Sequence of reqs after the insert one and itself should |
| 4192 | * be adjusted because each timeout req consumes a slot. |
| 4193 | */ |
| 4194 | span++; |
| 4195 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4196 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4197 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4198 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4199 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4200 | data->timer.function = io_timeout_fn; |
| 4201 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4202 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4203 | return 0; |
| 4204 | } |
| 4205 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4206 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4207 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4208 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4209 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4210 | return req->user_data == (unsigned long) data; |
| 4211 | } |
| 4212 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4213 | 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] | 4214 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4215 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4216 | int ret = 0; |
| 4217 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4218 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 4219 | switch (cancel_ret) { |
| 4220 | case IO_WQ_CANCEL_OK: |
| 4221 | ret = 0; |
| 4222 | break; |
| 4223 | case IO_WQ_CANCEL_RUNNING: |
| 4224 | ret = -EALREADY; |
| 4225 | break; |
| 4226 | case IO_WQ_CANCEL_NOTFOUND: |
| 4227 | ret = -ENOENT; |
| 4228 | break; |
| 4229 | } |
| 4230 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4231 | return ret; |
| 4232 | } |
| 4233 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4234 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 4235 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4236 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4237 | { |
| 4238 | unsigned long flags; |
| 4239 | int ret; |
| 4240 | |
| 4241 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 4242 | if (ret != -ENOENT) { |
| 4243 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4244 | goto done; |
| 4245 | } |
| 4246 | |
| 4247 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4248 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4249 | if (ret != -ENOENT) |
| 4250 | goto done; |
| 4251 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4252 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4253 | if (!ret) |
| 4254 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4255 | io_cqring_fill_event(req, ret); |
| 4256 | io_commit_cqring(ctx); |
| 4257 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4258 | io_cqring_ev_posted(ctx); |
| 4259 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4260 | if (ret < 0) |
| 4261 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4262 | io_put_req_find_next(req, nxt); |
| 4263 | } |
| 4264 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4265 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4266 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4267 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4268 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4269 | return -EINVAL; |
| 4270 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4271 | sqe->cancel_flags) |
| 4272 | return -EINVAL; |
| 4273 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4274 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4275 | return 0; |
| 4276 | } |
| 4277 | |
| 4278 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4279 | { |
| 4280 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4281 | |
| 4282 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4283 | return 0; |
| 4284 | } |
| 4285 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4286 | static int io_files_update_prep(struct io_kiocb *req, |
| 4287 | const struct io_uring_sqe *sqe) |
| 4288 | { |
| 4289 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4290 | return -EINVAL; |
| 4291 | |
| 4292 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4293 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4294 | if (!req->files_update.nr_args) |
| 4295 | return -EINVAL; |
| 4296 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4297 | return 0; |
| 4298 | } |
| 4299 | |
| 4300 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4301 | { |
| 4302 | struct io_ring_ctx *ctx = req->ctx; |
| 4303 | struct io_uring_files_update up; |
| 4304 | int ret; |
| 4305 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4306 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4307 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4308 | |
| 4309 | up.offset = req->files_update.offset; |
| 4310 | up.fds = req->files_update.arg; |
| 4311 | |
| 4312 | mutex_lock(&ctx->uring_lock); |
| 4313 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4314 | mutex_unlock(&ctx->uring_lock); |
| 4315 | |
| 4316 | if (ret < 0) |
| 4317 | req_set_fail_links(req); |
| 4318 | io_cqring_add_event(req, ret); |
| 4319 | io_put_req(req); |
| 4320 | return 0; |
| 4321 | } |
| 4322 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4323 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4324 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4325 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4326 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4327 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4328 | if (io_op_defs[req->opcode].file_table) { |
| 4329 | ret = io_grab_files(req); |
| 4330 | if (unlikely(ret)) |
| 4331 | return ret; |
| 4332 | } |
| 4333 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4334 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4335 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4336 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4337 | case IORING_OP_NOP: |
| 4338 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4339 | case IORING_OP_READV: |
| 4340 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4341 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4342 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4343 | break; |
| 4344 | case IORING_OP_WRITEV: |
| 4345 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4346 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4347 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4348 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4349 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4350 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4351 | break; |
| 4352 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4353 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4354 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4355 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4356 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4357 | break; |
| 4358 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4359 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4360 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4361 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4362 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4363 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4364 | break; |
| 4365 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4366 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4367 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4368 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4369 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4370 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4371 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4372 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4373 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4374 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4375 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4376 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4377 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4378 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4379 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4380 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4381 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4382 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4383 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4384 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4385 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4386 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4387 | case IORING_OP_FALLOCATE: |
| 4388 | ret = io_fallocate_prep(req, sqe); |
| 4389 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4390 | case IORING_OP_OPENAT: |
| 4391 | ret = io_openat_prep(req, sqe); |
| 4392 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4393 | case IORING_OP_CLOSE: |
| 4394 | ret = io_close_prep(req, sqe); |
| 4395 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4396 | case IORING_OP_FILES_UPDATE: |
| 4397 | ret = io_files_update_prep(req, sqe); |
| 4398 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4399 | case IORING_OP_STATX: |
| 4400 | ret = io_statx_prep(req, sqe); |
| 4401 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4402 | case IORING_OP_FADVISE: |
| 4403 | ret = io_fadvise_prep(req, sqe); |
| 4404 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4405 | case IORING_OP_MADVISE: |
| 4406 | ret = io_madvise_prep(req, sqe); |
| 4407 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4408 | case IORING_OP_OPENAT2: |
| 4409 | ret = io_openat2_prep(req, sqe); |
| 4410 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4411 | case IORING_OP_EPOLL_CTL: |
| 4412 | ret = io_epoll_ctl_prep(req, sqe); |
| 4413 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4414 | case IORING_OP_SPLICE: |
| 4415 | ret = io_splice_prep(req, sqe); |
| 4416 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4417 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4418 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4419 | req->opcode); |
| 4420 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4421 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4422 | } |
| 4423 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4424 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4427 | 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] | 4428 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4429 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4430 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4431 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4432 | /* Still need defer if there is pending req in defer list. */ |
| 4433 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4434 | return 0; |
| 4435 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4436 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4437 | return -EAGAIN; |
| 4438 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4439 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4440 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4441 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4442 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4443 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4444 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4445 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4446 | return 0; |
| 4447 | } |
| 4448 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4449 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4450 | list_add_tail(&req->list, &ctx->defer_list); |
| 4451 | spin_unlock_irq(&ctx->completion_lock); |
| 4452 | return -EIOCBQUEUED; |
| 4453 | } |
| 4454 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4455 | static void io_cleanup_req(struct io_kiocb *req) |
| 4456 | { |
| 4457 | struct io_async_ctx *io = req->io; |
| 4458 | |
| 4459 | switch (req->opcode) { |
| 4460 | case IORING_OP_READV: |
| 4461 | case IORING_OP_READ_FIXED: |
| 4462 | case IORING_OP_READ: |
| 4463 | case IORING_OP_WRITEV: |
| 4464 | case IORING_OP_WRITE_FIXED: |
| 4465 | case IORING_OP_WRITE: |
| 4466 | if (io->rw.iov != io->rw.fast_iov) |
| 4467 | kfree(io->rw.iov); |
| 4468 | break; |
| 4469 | case IORING_OP_SENDMSG: |
| 4470 | case IORING_OP_RECVMSG: |
| 4471 | if (io->msg.iov != io->msg.fast_iov) |
| 4472 | kfree(io->msg.iov); |
| 4473 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4474 | case IORING_OP_OPENAT: |
| 4475 | case IORING_OP_OPENAT2: |
| 4476 | case IORING_OP_STATX: |
| 4477 | putname(req->open.filename); |
| 4478 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4479 | case IORING_OP_SPLICE: |
| 4480 | io_put_file(req, req->splice.file_in, |
| 4481 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 4482 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4486 | } |
| 4487 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4488 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4489 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4490 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4491 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4492 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4493 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4494 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4495 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4496 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4497 | break; |
| 4498 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4499 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4500 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4501 | if (sqe) { |
| 4502 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4503 | if (ret < 0) |
| 4504 | break; |
| 4505 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4506 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4507 | break; |
| 4508 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4509 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4510 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4511 | if (sqe) { |
| 4512 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4513 | if (ret < 0) |
| 4514 | break; |
| 4515 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4516 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4517 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4518 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4519 | if (sqe) { |
| 4520 | ret = io_prep_fsync(req, sqe); |
| 4521 | if (ret < 0) |
| 4522 | break; |
| 4523 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4524 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4525 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4526 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4527 | if (sqe) { |
| 4528 | ret = io_poll_add_prep(req, sqe); |
| 4529 | if (ret) |
| 4530 | break; |
| 4531 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4532 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4533 | break; |
| 4534 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4535 | if (sqe) { |
| 4536 | ret = io_poll_remove_prep(req, sqe); |
| 4537 | if (ret < 0) |
| 4538 | break; |
| 4539 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4540 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4541 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4542 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4543 | if (sqe) { |
| 4544 | ret = io_prep_sfr(req, sqe); |
| 4545 | if (ret < 0) |
| 4546 | break; |
| 4547 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4548 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4549 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4550 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4551 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4552 | if (sqe) { |
| 4553 | ret = io_sendmsg_prep(req, sqe); |
| 4554 | if (ret < 0) |
| 4555 | break; |
| 4556 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4557 | if (req->opcode == IORING_OP_SENDMSG) |
| 4558 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4559 | else |
| 4560 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4561 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4562 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4563 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4564 | if (sqe) { |
| 4565 | ret = io_recvmsg_prep(req, sqe); |
| 4566 | if (ret) |
| 4567 | break; |
| 4568 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4569 | if (req->opcode == IORING_OP_RECVMSG) |
| 4570 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4571 | else |
| 4572 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4573 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4574 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4575 | if (sqe) { |
| 4576 | ret = io_timeout_prep(req, sqe, false); |
| 4577 | if (ret) |
| 4578 | break; |
| 4579 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4580 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4581 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4582 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4583 | if (sqe) { |
| 4584 | ret = io_timeout_remove_prep(req, sqe); |
| 4585 | if (ret) |
| 4586 | break; |
| 4587 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4588 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4589 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4590 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4591 | if (sqe) { |
| 4592 | ret = io_accept_prep(req, sqe); |
| 4593 | if (ret) |
| 4594 | break; |
| 4595 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4596 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4597 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4598 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4599 | if (sqe) { |
| 4600 | ret = io_connect_prep(req, sqe); |
| 4601 | if (ret) |
| 4602 | break; |
| 4603 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4604 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4605 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4606 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4607 | if (sqe) { |
| 4608 | ret = io_async_cancel_prep(req, sqe); |
| 4609 | if (ret) |
| 4610 | break; |
| 4611 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4612 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4613 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4614 | case IORING_OP_FALLOCATE: |
| 4615 | if (sqe) { |
| 4616 | ret = io_fallocate_prep(req, sqe); |
| 4617 | if (ret) |
| 4618 | break; |
| 4619 | } |
| 4620 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4621 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4622 | case IORING_OP_OPENAT: |
| 4623 | if (sqe) { |
| 4624 | ret = io_openat_prep(req, sqe); |
| 4625 | if (ret) |
| 4626 | break; |
| 4627 | } |
| 4628 | ret = io_openat(req, nxt, force_nonblock); |
| 4629 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4630 | case IORING_OP_CLOSE: |
| 4631 | if (sqe) { |
| 4632 | ret = io_close_prep(req, sqe); |
| 4633 | if (ret) |
| 4634 | break; |
| 4635 | } |
| 4636 | ret = io_close(req, nxt, force_nonblock); |
| 4637 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4638 | case IORING_OP_FILES_UPDATE: |
| 4639 | if (sqe) { |
| 4640 | ret = io_files_update_prep(req, sqe); |
| 4641 | if (ret) |
| 4642 | break; |
| 4643 | } |
| 4644 | ret = io_files_update(req, force_nonblock); |
| 4645 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4646 | case IORING_OP_STATX: |
| 4647 | if (sqe) { |
| 4648 | ret = io_statx_prep(req, sqe); |
| 4649 | if (ret) |
| 4650 | break; |
| 4651 | } |
| 4652 | ret = io_statx(req, nxt, force_nonblock); |
| 4653 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4654 | case IORING_OP_FADVISE: |
| 4655 | if (sqe) { |
| 4656 | ret = io_fadvise_prep(req, sqe); |
| 4657 | if (ret) |
| 4658 | break; |
| 4659 | } |
| 4660 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4661 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4662 | case IORING_OP_MADVISE: |
| 4663 | if (sqe) { |
| 4664 | ret = io_madvise_prep(req, sqe); |
| 4665 | if (ret) |
| 4666 | break; |
| 4667 | } |
| 4668 | ret = io_madvise(req, nxt, force_nonblock); |
| 4669 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4670 | case IORING_OP_OPENAT2: |
| 4671 | if (sqe) { |
| 4672 | ret = io_openat2_prep(req, sqe); |
| 4673 | if (ret) |
| 4674 | break; |
| 4675 | } |
| 4676 | ret = io_openat2(req, nxt, force_nonblock); |
| 4677 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4678 | case IORING_OP_EPOLL_CTL: |
| 4679 | if (sqe) { |
| 4680 | ret = io_epoll_ctl_prep(req, sqe); |
| 4681 | if (ret) |
| 4682 | break; |
| 4683 | } |
| 4684 | ret = io_epoll_ctl(req, nxt, force_nonblock); |
| 4685 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4686 | case IORING_OP_SPLICE: |
| 4687 | if (sqe) { |
| 4688 | ret = io_splice_prep(req, sqe); |
| 4689 | if (ret < 0) |
| 4690 | break; |
| 4691 | } |
| 4692 | ret = io_splice(req, nxt, force_nonblock); |
| 4693 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4694 | default: |
| 4695 | ret = -EINVAL; |
| 4696 | break; |
| 4697 | } |
| 4698 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4699 | if (ret) |
| 4700 | return ret; |
| 4701 | |
| 4702 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4703 | const bool in_async = io_wq_current_is_worker(); |
| 4704 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4705 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4706 | return -EAGAIN; |
| 4707 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4708 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4709 | if (in_async) |
| 4710 | mutex_lock(&ctx->uring_lock); |
| 4711 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4712 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4713 | |
| 4714 | if (in_async) |
| 4715 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4721 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4722 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4723 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4724 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4725 | struct io_kiocb *nxt = NULL; |
| 4726 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4727 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4728 | /* if NO_CANCEL is set, we must still run the work */ |
| 4729 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4730 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4731 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4732 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4733 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4734 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4735 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4736 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4737 | /* |
| 4738 | * We can get EAGAIN for polled IO even though we're |
| 4739 | * forcing a sync submission from here, since we can't |
| 4740 | * wait for request slots on the block side. |
| 4741 | */ |
| 4742 | if (ret != -EAGAIN) |
| 4743 | break; |
| 4744 | cond_resched(); |
| 4745 | } while (1); |
| 4746 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4747 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4748 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4749 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4750 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4751 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4752 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4753 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4754 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4755 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4756 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4757 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4758 | if (!ret && nxt) |
| 4759 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4760 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4761 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4762 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4763 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4764 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4765 | return 0; |
Jens Axboe | 0b5faf6 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 4766 | 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] | 4767 | return 0; |
| 4768 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4769 | } |
| 4770 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4771 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4772 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4773 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4774 | struct fixed_file_table *table; |
| 4775 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4776 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4777 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4778 | } |
| 4779 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4780 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 4781 | int fd, struct file **out_file, bool fixed) |
| 4782 | { |
| 4783 | struct io_ring_ctx *ctx = req->ctx; |
| 4784 | struct file *file; |
| 4785 | |
| 4786 | if (fixed) { |
| 4787 | if (unlikely(!ctx->file_data || |
| 4788 | (unsigned) fd >= ctx->nr_user_files)) |
| 4789 | return -EBADF; |
| 4790 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 4791 | file = io_file_from_index(ctx, fd); |
| 4792 | if (!file) |
| 4793 | return -EBADF; |
| 4794 | percpu_ref_get(&ctx->file_data->refs); |
| 4795 | } else { |
| 4796 | trace_io_uring_file_get(ctx, fd); |
| 4797 | file = __io_file_get(state, fd); |
| 4798 | if (unlikely(!file)) |
| 4799 | return -EBADF; |
| 4800 | } |
| 4801 | |
| 4802 | *out_file = file; |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4806 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4807 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4808 | { |
| 4809 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4810 | int fd; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4811 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4812 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4813 | flags = READ_ONCE(sqe->flags); |
| 4814 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4815 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4816 | if (!io_req_needs_file(req, fd)) |
| 4817 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4818 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4819 | fixed = (flags & IOSQE_FIXED_FILE); |
| 4820 | if (unlikely(!fixed && req->needs_fixed_file)) |
| 4821 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4822 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4823 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4824 | } |
| 4825 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4826 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4827 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4828 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4829 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4830 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4831 | if (req->work.files) |
| 4832 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4833 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4834 | return -EBADF; |
| 4835 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4836 | rcu_read_lock(); |
| 4837 | spin_lock_irq(&ctx->inflight_lock); |
| 4838 | /* |
| 4839 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4840 | * out work accessing these files if the fd is closed. Check if |
| 4841 | * the fd has changed since we started down this path, and disallow |
| 4842 | * this operation if it has. |
| 4843 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4844 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4845 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4846 | req->flags |= REQ_F_INFLIGHT; |
| 4847 | req->work.files = current->files; |
| 4848 | ret = 0; |
| 4849 | } |
| 4850 | spin_unlock_irq(&ctx->inflight_lock); |
| 4851 | rcu_read_unlock(); |
| 4852 | |
| 4853 | return ret; |
| 4854 | } |
| 4855 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4856 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4857 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4858 | struct io_timeout_data *data = container_of(timer, |
| 4859 | struct io_timeout_data, timer); |
| 4860 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4861 | struct io_ring_ctx *ctx = req->ctx; |
| 4862 | struct io_kiocb *prev = NULL; |
| 4863 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4864 | |
| 4865 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4866 | |
| 4867 | /* |
| 4868 | * We don't expect the list to be empty, that will only happen if we |
| 4869 | * race with the completion of the linked work. |
| 4870 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4871 | if (!list_empty(&req->link_list)) { |
| 4872 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4873 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4874 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4875 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4876 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4877 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4878 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4882 | |
| 4883 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4884 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4885 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4886 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4887 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4888 | } else { |
| 4889 | io_cqring_add_event(req, -ETIME); |
| 4890 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4891 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4892 | return HRTIMER_NORESTART; |
| 4893 | } |
| 4894 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4895 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4896 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4897 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4898 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4899 | /* |
| 4900 | * If the list is now empty, then our linked request finished before |
| 4901 | * we got a chance to setup the timer |
| 4902 | */ |
| 4903 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4904 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4905 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4906 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4907 | data->timer.function = io_link_timeout_fn; |
| 4908 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4909 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4910 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4911 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4912 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4913 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4914 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4917 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4918 | { |
| 4919 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4920 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4921 | if (!(req->flags & REQ_F_LINK)) |
| 4922 | return NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4923 | /* for polled retry, if flag is set, we already went through here */ |
| 4924 | if (req->flags & REQ_F_POLLED) |
| 4925 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4926 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4927 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4928 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4929 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4930 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4931 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4932 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4933 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4936 | 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] | 4937 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4938 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame^] | 4939 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4940 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4941 | int ret; |
| 4942 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4943 | again: |
| 4944 | linked_timeout = io_prep_linked_timeout(req); |
| 4945 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4946 | if (req->work.creds && req->work.creds != current_cred()) { |
| 4947 | if (old_creds) |
| 4948 | revert_creds(old_creds); |
| 4949 | if (old_creds == req->work.creds) |
| 4950 | old_creds = NULL; /* restored original creds */ |
| 4951 | else |
| 4952 | old_creds = override_creds(req->work.creds); |
| 4953 | } |
| 4954 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4955 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4956 | |
| 4957 | /* |
| 4958 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4959 | * doesn't support non-blocking read/write attempts |
| 4960 | */ |
| 4961 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4962 | (req->flags & REQ_F_MUST_PUNT))) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4963 | if (io_arm_poll_handler(req)) { |
| 4964 | if (linked_timeout) |
| 4965 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame^] | 4966 | goto exit; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4967 | } |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4968 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4969 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4970 | ret = io_grab_files(req); |
| 4971 | if (ret) |
| 4972 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4973 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4974 | |
| 4975 | /* |
| 4976 | * Queued up for async execution, worker will release |
| 4977 | * submit reference when the iocb is actually submitted. |
| 4978 | */ |
| 4979 | io_queue_async_work(req); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame^] | 4980 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4981 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4982 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4983 | err: |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame^] | 4984 | nxt = NULL; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4985 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 4986 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4987 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4988 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4989 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4990 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4991 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4992 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4993 | } |
| 4994 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4995 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4996 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4997 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4998 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4999 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5000 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5001 | if (nxt) { |
| 5002 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5003 | |
| 5004 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 5005 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5006 | goto again; |
| 5007 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame^] | 5008 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5009 | if (old_creds) |
| 5010 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5013 | 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] | 5014 | { |
| 5015 | int ret; |
| 5016 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5017 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5018 | if (ret) { |
| 5019 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5020 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5021 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5022 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5023 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5024 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 5025 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5026 | ret = io_req_defer_prep(req, sqe); |
| 5027 | if (unlikely(ret < 0)) |
| 5028 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5029 | /* |
| 5030 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 5031 | * to async execution. |
| 5032 | */ |
| 5033 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 5034 | io_queue_async_work(req); |
| 5035 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5036 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5037 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5038 | } |
| 5039 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5040 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5041 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5042 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5043 | io_cqring_add_event(req, -ECANCELED); |
| 5044 | io_double_put_req(req); |
| 5045 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5046 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5047 | } |
| 5048 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5049 | #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] | 5050 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5051 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5052 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 5053 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5054 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5055 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5056 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5057 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5058 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5059 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5060 | |
| 5061 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5062 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5063 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5064 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5065 | } |
| 5066 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5067 | id = READ_ONCE(sqe->personality); |
| 5068 | if (id) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5069 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 5070 | if (unlikely(!req->work.creds)) { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5071 | ret = -EINVAL; |
| 5072 | goto err_req; |
| 5073 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5074 | get_cred(req->work.creds); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5075 | } |
| 5076 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 5077 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5078 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK | |
| 5079 | IOSQE_ASYNC | IOSQE_FIXED_FILE); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5080 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5081 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5082 | if (unlikely(ret)) { |
| 5083 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5084 | io_cqring_add_event(req, ret); |
| 5085 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5086 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5087 | } |
| 5088 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5089 | /* |
| 5090 | * If we already have a head request, queue this one for async |
| 5091 | * submittal once the head completes. If we don't have a head but |
| 5092 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 5093 | * submitted sync once the chain is complete. If none of those |
| 5094 | * conditions are true (normal request), then just queue it. |
| 5095 | */ |
| 5096 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5097 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5098 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 5099 | /* |
| 5100 | * Taking sequential execution of a link, draining both sides |
| 5101 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 5102 | * requests in the link. So, it drains the head and the |
| 5103 | * next after the link request. The last one is done via |
| 5104 | * drain_next flag to persist the effect across calls. |
| 5105 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5106 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 5107 | head->flags |= REQ_F_IO_DRAIN; |
| 5108 | ctx->drain_next = 1; |
| 5109 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5110 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5111 | ret = -EAGAIN; |
| 5112 | goto err_req; |
| 5113 | } |
| 5114 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5115 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5116 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5117 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5118 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5119 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5120 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5121 | trace_io_uring_link(ctx, req, head); |
| 5122 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5123 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5124 | /* last request of a link, enqueue the link */ |
| 5125 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 5126 | io_queue_link_head(head); |
| 5127 | *link = NULL; |
| 5128 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5129 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5130 | if (unlikely(ctx->drain_next)) { |
| 5131 | req->flags |= REQ_F_IO_DRAIN; |
| 5132 | req->ctx->drain_next = 0; |
| 5133 | } |
| 5134 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 5135 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5136 | INIT_LIST_HEAD(&req->link_list); |
| 5137 | ret = io_req_defer_prep(req, sqe); |
| 5138 | if (ret) |
| 5139 | req->flags |= REQ_F_FAIL_LINK; |
| 5140 | *link = req; |
| 5141 | } else { |
| 5142 | io_queue_sqe(req, sqe); |
| 5143 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5144 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5145 | |
| 5146 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5147 | } |
| 5148 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5149 | /* |
| 5150 | * Batched submission is done, ensure local IO is flushed out. |
| 5151 | */ |
| 5152 | static void io_submit_state_end(struct io_submit_state *state) |
| 5153 | { |
| 5154 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 5155 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5156 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 5157 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | /* |
| 5161 | * Start submission side cache. |
| 5162 | */ |
| 5163 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5164 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5165 | { |
| 5166 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5167 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5168 | state->file = NULL; |
| 5169 | state->ios_left = max_ios; |
| 5170 | } |
| 5171 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5172 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 5173 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5174 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5175 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 5176 | /* |
| 5177 | * Ensure any loads from the SQEs are done at this point, |
| 5178 | * since once we write the new head, the application could |
| 5179 | * write new data to them. |
| 5180 | */ |
| 5181 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5185 | * 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] | 5186 | * that is mapped by userspace. This means that care needs to be taken to |
| 5187 | * ensure that reads are stable, as we cannot rely on userspace always |
| 5188 | * being a good citizen. If members of the sqe are validated and then later |
| 5189 | * used, it's important that those reads are done through READ_ONCE() to |
| 5190 | * prevent a re-load down the line. |
| 5191 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5192 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 5193 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5194 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5195 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5196 | unsigned head; |
| 5197 | |
| 5198 | /* |
| 5199 | * The cached sq head (or cq tail) serves two purposes: |
| 5200 | * |
| 5201 | * 1) allows us to batch the cost of updating the user visible |
| 5202 | * head updates. |
| 5203 | * 2) allows the kernel side to track the head on its own, even |
| 5204 | * though the application is the one updating it. |
| 5205 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5206 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 5207 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5208 | /* |
| 5209 | * All io need record the previous position, if LINK vs DARIN, |
| 5210 | * it can be used to mark the position of the first IO in the |
| 5211 | * link list. |
| 5212 | */ |
| 5213 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5214 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 5215 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 5216 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5217 | ctx->cached_sq_head++; |
| 5218 | return true; |
| 5219 | } |
| 5220 | |
| 5221 | /* drop invalid entries */ |
| 5222 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 5223 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5224 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5225 | return false; |
| 5226 | } |
| 5227 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5228 | 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] | 5229 | struct file *ring_file, int ring_fd, |
| 5230 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5231 | { |
| 5232 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5233 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5234 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5235 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5236 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 5237 | /* 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] | 5238 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 5239 | if (!list_empty(&ctx->cq_overflow_list) && |
| 5240 | !io_cqring_overflow_flush(ctx, false)) |
| 5241 | return -EBUSY; |
| 5242 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5243 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5244 | /* make sure SQ entry isn't read before tail */ |
| 5245 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 5246 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5247 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 5248 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5249 | |
| 5250 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5251 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5252 | statep = &state; |
| 5253 | } |
| 5254 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5255 | ctx->ring_fd = ring_fd; |
| 5256 | ctx->ring_file = ring_file; |
| 5257 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5258 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5259 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5260 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5261 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5262 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5263 | req = io_get_req(ctx, statep); |
| 5264 | if (unlikely(!req)) { |
| 5265 | if (!submitted) |
| 5266 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5267 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5268 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5269 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5270 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5271 | break; |
| 5272 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5273 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5274 | /* will complete beyond this point, count as submitted */ |
| 5275 | submitted++; |
| 5276 | |
| 5277 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5278 | err = -EINVAL; |
| 5279 | fail_req: |
| 5280 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5281 | io_double_put_req(req); |
| 5282 | break; |
| 5283 | } |
| 5284 | |
| 5285 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5286 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5287 | if (unlikely(mm_fault)) { |
| 5288 | err = -EFAULT; |
| 5289 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5290 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5291 | use_mm(ctx->sqo_mm); |
| 5292 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5293 | } |
| 5294 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5295 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 5296 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 5297 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5298 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5299 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 5302 | if (unlikely(submitted != nr)) { |
| 5303 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 5304 | |
| 5305 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 5306 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5307 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5308 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5309 | if (statep) |
| 5310 | io_submit_state_end(&state); |
| 5311 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5312 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5313 | io_commit_sqring(ctx); |
| 5314 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5315 | return submitted; |
| 5316 | } |
| 5317 | |
| 5318 | static int io_sq_thread(void *data) |
| 5319 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5320 | struct io_ring_ctx *ctx = data; |
| 5321 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5322 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5323 | mm_segment_t old_fs; |
| 5324 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5325 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5326 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5327 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5328 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5329 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5330 | old_fs = get_fs(); |
| 5331 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5332 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5333 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5334 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5335 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5336 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5337 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5338 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5339 | unsigned nr_events = 0; |
| 5340 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5341 | mutex_lock(&ctx->uring_lock); |
| 5342 | if (!list_empty(&ctx->poll_list)) |
| 5343 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 5344 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5345 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5346 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5347 | } |
| 5348 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5349 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5350 | |
| 5351 | /* |
| 5352 | * If submit got -EBUSY, flag us as needing the application |
| 5353 | * to enter the kernel to reap and flush events. |
| 5354 | */ |
| 5355 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5356 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 5357 | * Drop cur_mm before scheduling, we can't hold it for |
| 5358 | * long periods (or over schedule()). Do this before |
| 5359 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5360 | * may sleep. |
| 5361 | */ |
| 5362 | if (cur_mm) { |
| 5363 | unuse_mm(cur_mm); |
| 5364 | mmput(cur_mm); |
| 5365 | cur_mm = NULL; |
| 5366 | } |
| 5367 | |
| 5368 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5369 | * We're polling. If we're within the defined idle |
| 5370 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5371 | * to sleep. The exception is if we got EBUSY doing |
| 5372 | * more IO, we should wait for the application to |
| 5373 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5374 | */ |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5375 | if (!list_empty(&ctx->poll_list) || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5376 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5377 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5378 | if (current->task_works) |
| 5379 | task_work_run(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5380 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5381 | continue; |
| 5382 | } |
| 5383 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5384 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5385 | TASK_INTERRUPTIBLE); |
| 5386 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5387 | /* |
| 5388 | * While doing polled IO, before going to sleep, we need |
| 5389 | * to check if there are new reqs added to poll_list, it |
| 5390 | * is because reqs may have been punted to io worker and |
| 5391 | * will be added to poll_list later, hence check the |
| 5392 | * poll_list again. |
| 5393 | */ |
| 5394 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 5395 | !list_empty_careful(&ctx->poll_list)) { |
| 5396 | finish_wait(&ctx->sqo_wait, &wait); |
| 5397 | continue; |
| 5398 | } |
| 5399 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5400 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5401 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5402 | /* make sure to read SQ tail after writing flags */ |
| 5403 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5404 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5405 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5406 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5407 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5408 | finish_wait(&ctx->sqo_wait, &wait); |
| 5409 | break; |
| 5410 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5411 | if (current->task_works) { |
| 5412 | task_work_run(); |
| 5413 | continue; |
| 5414 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5415 | if (signal_pending(current)) |
| 5416 | flush_signals(current); |
| 5417 | schedule(); |
| 5418 | finish_wait(&ctx->sqo_wait, &wait); |
| 5419 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5420 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5421 | continue; |
| 5422 | } |
| 5423 | finish_wait(&ctx->sqo_wait, &wait); |
| 5424 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5425 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5426 | } |
| 5427 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5428 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5429 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5430 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5431 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5432 | } |
| 5433 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5434 | if (current->task_works) |
| 5435 | task_work_run(); |
| 5436 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5437 | set_fs(old_fs); |
| 5438 | if (cur_mm) { |
| 5439 | unuse_mm(cur_mm); |
| 5440 | mmput(cur_mm); |
| 5441 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5442 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5443 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5444 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5445 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5446 | return 0; |
| 5447 | } |
| 5448 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5449 | struct io_wait_queue { |
| 5450 | struct wait_queue_entry wq; |
| 5451 | struct io_ring_ctx *ctx; |
| 5452 | unsigned to_wait; |
| 5453 | unsigned nr_timeouts; |
| 5454 | }; |
| 5455 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5456 | 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] | 5457 | { |
| 5458 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5459 | |
| 5460 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5461 | * 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] | 5462 | * started waiting. For timeouts, we always want to return to userspace, |
| 5463 | * regardless of event count. |
| 5464 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5465 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5466 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5467 | } |
| 5468 | |
| 5469 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5470 | int wake_flags, void *key) |
| 5471 | { |
| 5472 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5473 | wq); |
| 5474 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5475 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5476 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5477 | return -1; |
| 5478 | |
| 5479 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5480 | } |
| 5481 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5482 | /* |
| 5483 | * Wait until events become available, if we don't already have some. The |
| 5484 | * application must reap them itself, as they reside on the shared cq ring. |
| 5485 | */ |
| 5486 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5487 | const sigset_t __user *sig, size_t sigsz) |
| 5488 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5489 | struct io_wait_queue iowq = { |
| 5490 | .wq = { |
| 5491 | .private = current, |
| 5492 | .func = io_wake_function, |
| 5493 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5494 | }, |
| 5495 | .ctx = ctx, |
| 5496 | .to_wait = min_events, |
| 5497 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5498 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5499 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5500 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5501 | do { |
| 5502 | if (io_cqring_events(ctx, false) >= min_events) |
| 5503 | return 0; |
| 5504 | if (!current->task_works) |
| 5505 | break; |
| 5506 | task_work_run(); |
| 5507 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5508 | |
| 5509 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5510 | #ifdef CONFIG_COMPAT |
| 5511 | if (in_compat_syscall()) |
| 5512 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5513 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5514 | else |
| 5515 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5516 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5517 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5518 | if (ret) |
| 5519 | return ret; |
| 5520 | } |
| 5521 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5522 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5523 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5524 | do { |
| 5525 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5526 | TASK_INTERRUPTIBLE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5527 | if (current->task_works) |
| 5528 | task_work_run(); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5529 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5530 | break; |
| 5531 | schedule(); |
| 5532 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5533 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5534 | break; |
| 5535 | } |
| 5536 | } while (1); |
| 5537 | finish_wait(&ctx->wait, &iowq.wq); |
| 5538 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5539 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5540 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5541 | 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] | 5542 | } |
| 5543 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5544 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5545 | { |
| 5546 | #if defined(CONFIG_UNIX) |
| 5547 | if (ctx->ring_sock) { |
| 5548 | struct sock *sock = ctx->ring_sock->sk; |
| 5549 | struct sk_buff *skb; |
| 5550 | |
| 5551 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5552 | kfree_skb(skb); |
| 5553 | } |
| 5554 | #else |
| 5555 | int i; |
| 5556 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5557 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5558 | struct file *file; |
| 5559 | |
| 5560 | file = io_file_from_index(ctx, i); |
| 5561 | if (file) |
| 5562 | fput(file); |
| 5563 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5564 | #endif |
| 5565 | } |
| 5566 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5567 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5568 | { |
| 5569 | struct fixed_file_data *data; |
| 5570 | |
| 5571 | data = container_of(ref, struct fixed_file_data, refs); |
| 5572 | complete(&data->done); |
| 5573 | } |
| 5574 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5575 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5576 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5577 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5578 | unsigned nr_tables, i; |
| 5579 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5580 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5581 | return -ENXIO; |
| 5582 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5583 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5584 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5585 | wait_for_completion(&data->done); |
| 5586 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5587 | percpu_ref_exit(&data->refs); |
| 5588 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5589 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5590 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5591 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5592 | kfree(data->table[i].files); |
| 5593 | kfree(data->table); |
| 5594 | kfree(data); |
| 5595 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5596 | ctx->nr_user_files = 0; |
| 5597 | return 0; |
| 5598 | } |
| 5599 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5600 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5601 | { |
| 5602 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5603 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5604 | /* |
| 5605 | * The park is a bit of a work-around, without it we get |
| 5606 | * warning spews on shutdown with SQPOLL set and affinity |
| 5607 | * set to a single CPU. |
| 5608 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5609 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5610 | kthread_stop(ctx->sqo_thread); |
| 5611 | ctx->sqo_thread = NULL; |
| 5612 | } |
| 5613 | } |
| 5614 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5615 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5616 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5617 | io_sq_thread_stop(ctx); |
| 5618 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5619 | if (ctx->io_wq) { |
| 5620 | io_wq_destroy(ctx->io_wq); |
| 5621 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5622 | } |
| 5623 | } |
| 5624 | |
| 5625 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5626 | /* |
| 5627 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5628 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5629 | * loops in the file referencing. |
| 5630 | */ |
| 5631 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5632 | { |
| 5633 | struct sock *sk = ctx->ring_sock->sk; |
| 5634 | struct scm_fp_list *fpl; |
| 5635 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5636 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5637 | |
| 5638 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5639 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5640 | |
| 5641 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5642 | return -EMFILE; |
| 5643 | } |
| 5644 | |
| 5645 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5646 | if (!fpl) |
| 5647 | return -ENOMEM; |
| 5648 | |
| 5649 | skb = alloc_skb(0, GFP_KERNEL); |
| 5650 | if (!skb) { |
| 5651 | kfree(fpl); |
| 5652 | return -ENOMEM; |
| 5653 | } |
| 5654 | |
| 5655 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5656 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5657 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5658 | fpl->user = get_uid(ctx->user); |
| 5659 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5660 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5661 | |
| 5662 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5663 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5664 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5665 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5666 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5667 | } |
| 5668 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5669 | if (nr_files) { |
| 5670 | fpl->max = SCM_MAX_FD; |
| 5671 | fpl->count = nr_files; |
| 5672 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5673 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5674 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5675 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5676 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5677 | for (i = 0; i < nr_files; i++) |
| 5678 | fput(fpl->fp[i]); |
| 5679 | } else { |
| 5680 | kfree_skb(skb); |
| 5681 | kfree(fpl); |
| 5682 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5683 | |
| 5684 | return 0; |
| 5685 | } |
| 5686 | |
| 5687 | /* |
| 5688 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5689 | * causes regular reference counting to break down. We rely on the UNIX |
| 5690 | * garbage collection to take care of this problem for us. |
| 5691 | */ |
| 5692 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5693 | { |
| 5694 | unsigned left, total; |
| 5695 | int ret = 0; |
| 5696 | |
| 5697 | total = 0; |
| 5698 | left = ctx->nr_user_files; |
| 5699 | while (left) { |
| 5700 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5701 | |
| 5702 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5703 | if (ret) |
| 5704 | break; |
| 5705 | left -= this_files; |
| 5706 | total += this_files; |
| 5707 | } |
| 5708 | |
| 5709 | if (!ret) |
| 5710 | return 0; |
| 5711 | |
| 5712 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5713 | struct file *file = io_file_from_index(ctx, total); |
| 5714 | |
| 5715 | if (file) |
| 5716 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5717 | total++; |
| 5718 | } |
| 5719 | |
| 5720 | return ret; |
| 5721 | } |
| 5722 | #else |
| 5723 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5724 | { |
| 5725 | return 0; |
| 5726 | } |
| 5727 | #endif |
| 5728 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5729 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5730 | unsigned nr_files) |
| 5731 | { |
| 5732 | int i; |
| 5733 | |
| 5734 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5735 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5736 | unsigned this_files; |
| 5737 | |
| 5738 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5739 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5740 | GFP_KERNEL); |
| 5741 | if (!table->files) |
| 5742 | break; |
| 5743 | nr_files -= this_files; |
| 5744 | } |
| 5745 | |
| 5746 | if (i == nr_tables) |
| 5747 | return 0; |
| 5748 | |
| 5749 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5750 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5751 | kfree(table->files); |
| 5752 | } |
| 5753 | return 1; |
| 5754 | } |
| 5755 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5756 | 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] | 5757 | { |
| 5758 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5759 | struct sock *sock = ctx->ring_sock->sk; |
| 5760 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5761 | struct sk_buff *skb; |
| 5762 | int i; |
| 5763 | |
| 5764 | __skb_queue_head_init(&list); |
| 5765 | |
| 5766 | /* |
| 5767 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5768 | * remove this entry and rearrange the file array. |
| 5769 | */ |
| 5770 | skb = skb_dequeue(head); |
| 5771 | while (skb) { |
| 5772 | struct scm_fp_list *fp; |
| 5773 | |
| 5774 | fp = UNIXCB(skb).fp; |
| 5775 | for (i = 0; i < fp->count; i++) { |
| 5776 | int left; |
| 5777 | |
| 5778 | if (fp->fp[i] != file) |
| 5779 | continue; |
| 5780 | |
| 5781 | unix_notinflight(fp->user, fp->fp[i]); |
| 5782 | left = fp->count - 1 - i; |
| 5783 | if (left) { |
| 5784 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5785 | left * sizeof(struct file *)); |
| 5786 | } |
| 5787 | fp->count--; |
| 5788 | if (!fp->count) { |
| 5789 | kfree_skb(skb); |
| 5790 | skb = NULL; |
| 5791 | } else { |
| 5792 | __skb_queue_tail(&list, skb); |
| 5793 | } |
| 5794 | fput(file); |
| 5795 | file = NULL; |
| 5796 | break; |
| 5797 | } |
| 5798 | |
| 5799 | if (!file) |
| 5800 | break; |
| 5801 | |
| 5802 | __skb_queue_tail(&list, skb); |
| 5803 | |
| 5804 | skb = skb_dequeue(head); |
| 5805 | } |
| 5806 | |
| 5807 | if (skb_peek(&list)) { |
| 5808 | spin_lock_irq(&head->lock); |
| 5809 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5810 | __skb_queue_tail(head, skb); |
| 5811 | spin_unlock_irq(&head->lock); |
| 5812 | } |
| 5813 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5814 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5815 | #endif |
| 5816 | } |
| 5817 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5818 | struct io_file_put { |
| 5819 | struct llist_node llist; |
| 5820 | struct file *file; |
| 5821 | struct completion *done; |
| 5822 | }; |
| 5823 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5824 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5825 | { |
| 5826 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5827 | struct llist_node *node; |
| 5828 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5829 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5830 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5831 | io_ring_file_put(data->ctx, pfile->file); |
| 5832 | if (pfile->done) |
| 5833 | complete(pfile->done); |
| 5834 | else |
| 5835 | kfree(pfile); |
| 5836 | } |
| 5837 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5838 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5839 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5840 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5841 | { |
| 5842 | struct fixed_file_data *data; |
| 5843 | |
| 5844 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5845 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5846 | percpu_ref_switch_to_percpu(&data->refs); |
| 5847 | } |
| 5848 | |
| 5849 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5850 | { |
| 5851 | struct fixed_file_data *data; |
| 5852 | |
| 5853 | data = container_of(ref, struct fixed_file_data, refs); |
| 5854 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5855 | /* |
| 5856 | * We can't safely switch from inside this context, punt to wq. If |
| 5857 | * the table ref is going away, the table is being unregistered. |
| 5858 | * Don't queue up the async work for that case, the caller will |
| 5859 | * handle it. |
| 5860 | */ |
| 5861 | if (!percpu_ref_is_dying(&data->refs)) |
| 5862 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5866 | unsigned nr_args) |
| 5867 | { |
| 5868 | __s32 __user *fds = (__s32 __user *) arg; |
| 5869 | unsigned nr_tables; |
| 5870 | struct file *file; |
| 5871 | int fd, ret = 0; |
| 5872 | unsigned i; |
| 5873 | |
| 5874 | if (ctx->file_data) |
| 5875 | return -EBUSY; |
| 5876 | if (!nr_args) |
| 5877 | return -EINVAL; |
| 5878 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5879 | return -EMFILE; |
| 5880 | |
| 5881 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5882 | if (!ctx->file_data) |
| 5883 | return -ENOMEM; |
| 5884 | ctx->file_data->ctx = ctx; |
| 5885 | init_completion(&ctx->file_data->done); |
| 5886 | |
| 5887 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5888 | ctx->file_data->table = kcalloc(nr_tables, |
| 5889 | sizeof(struct fixed_file_table), |
| 5890 | GFP_KERNEL); |
| 5891 | if (!ctx->file_data->table) { |
| 5892 | kfree(ctx->file_data); |
| 5893 | ctx->file_data = NULL; |
| 5894 | return -ENOMEM; |
| 5895 | } |
| 5896 | |
| 5897 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5898 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5899 | kfree(ctx->file_data->table); |
| 5900 | kfree(ctx->file_data); |
| 5901 | ctx->file_data = NULL; |
| 5902 | return -ENOMEM; |
| 5903 | } |
| 5904 | ctx->file_data->put_llist.first = NULL; |
| 5905 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5906 | |
| 5907 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5908 | percpu_ref_exit(&ctx->file_data->refs); |
| 5909 | kfree(ctx->file_data->table); |
| 5910 | kfree(ctx->file_data); |
| 5911 | ctx->file_data = NULL; |
| 5912 | return -ENOMEM; |
| 5913 | } |
| 5914 | |
| 5915 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5916 | struct fixed_file_table *table; |
| 5917 | unsigned index; |
| 5918 | |
| 5919 | ret = -EFAULT; |
| 5920 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5921 | break; |
| 5922 | /* allow sparse sets */ |
| 5923 | if (fd == -1) { |
| 5924 | ret = 0; |
| 5925 | continue; |
| 5926 | } |
| 5927 | |
| 5928 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5929 | index = i & IORING_FILE_TABLE_MASK; |
| 5930 | file = fget(fd); |
| 5931 | |
| 5932 | ret = -EBADF; |
| 5933 | if (!file) |
| 5934 | break; |
| 5935 | |
| 5936 | /* |
| 5937 | * Don't allow io_uring instances to be registered. If UNIX |
| 5938 | * isn't enabled, then this causes a reference cycle and this |
| 5939 | * instance can never get freed. If UNIX is enabled we'll |
| 5940 | * handle it just fine, but there's still no point in allowing |
| 5941 | * a ring fd as it doesn't support regular read/write anyway. |
| 5942 | */ |
| 5943 | if (file->f_op == &io_uring_fops) { |
| 5944 | fput(file); |
| 5945 | break; |
| 5946 | } |
| 5947 | ret = 0; |
| 5948 | table->files[index] = file; |
| 5949 | } |
| 5950 | |
| 5951 | if (ret) { |
| 5952 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5953 | file = io_file_from_index(ctx, i); |
| 5954 | if (file) |
| 5955 | fput(file); |
| 5956 | } |
| 5957 | for (i = 0; i < nr_tables; i++) |
| 5958 | kfree(ctx->file_data->table[i].files); |
| 5959 | |
| 5960 | kfree(ctx->file_data->table); |
| 5961 | kfree(ctx->file_data); |
| 5962 | ctx->file_data = NULL; |
| 5963 | ctx->nr_user_files = 0; |
| 5964 | return ret; |
| 5965 | } |
| 5966 | |
| 5967 | ret = io_sqe_files_scm(ctx); |
| 5968 | if (ret) |
| 5969 | io_sqe_files_unregister(ctx); |
| 5970 | |
| 5971 | return ret; |
| 5972 | } |
| 5973 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5974 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5975 | int index) |
| 5976 | { |
| 5977 | #if defined(CONFIG_UNIX) |
| 5978 | struct sock *sock = ctx->ring_sock->sk; |
| 5979 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5980 | struct sk_buff *skb; |
| 5981 | |
| 5982 | /* |
| 5983 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5984 | * file set. If there's no room, fall back to allocating a new skb |
| 5985 | * and filling it in. |
| 5986 | */ |
| 5987 | spin_lock_irq(&head->lock); |
| 5988 | skb = skb_peek(head); |
| 5989 | if (skb) { |
| 5990 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5991 | |
| 5992 | if (fpl->count < SCM_MAX_FD) { |
| 5993 | __skb_unlink(skb, head); |
| 5994 | spin_unlock_irq(&head->lock); |
| 5995 | fpl->fp[fpl->count] = get_file(file); |
| 5996 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5997 | fpl->count++; |
| 5998 | spin_lock_irq(&head->lock); |
| 5999 | __skb_queue_head(head, skb); |
| 6000 | } else { |
| 6001 | skb = NULL; |
| 6002 | } |
| 6003 | } |
| 6004 | spin_unlock_irq(&head->lock); |
| 6005 | |
| 6006 | if (skb) { |
| 6007 | fput(file); |
| 6008 | return 0; |
| 6009 | } |
| 6010 | |
| 6011 | return __io_sqe_files_scm(ctx, 1, index); |
| 6012 | #else |
| 6013 | return 0; |
| 6014 | #endif |
| 6015 | } |
| 6016 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6017 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6018 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6019 | struct fixed_file_data *data; |
| 6020 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6021 | /* |
| 6022 | * Juggle reference to ensure we hit zero, if needed, so we can |
| 6023 | * switch back to percpu mode |
| 6024 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6025 | data = container_of(ref, struct fixed_file_data, refs); |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6026 | percpu_ref_put(&data->refs); |
| 6027 | percpu_ref_get(&data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 6031 | struct file *file) |
| 6032 | { |
| 6033 | struct io_file_put *pfile, pfile_stack; |
| 6034 | DECLARE_COMPLETION_ONSTACK(done); |
| 6035 | |
| 6036 | /* |
| 6037 | * If we fail allocating the struct we need for doing async reomval |
| 6038 | * of this file, just punt to sync and wait for it. |
| 6039 | */ |
| 6040 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 6041 | if (!pfile) { |
| 6042 | pfile = &pfile_stack; |
| 6043 | pfile->done = &done; |
| 6044 | } |
| 6045 | |
| 6046 | pfile->file = file; |
| 6047 | llist_add(&pfile->llist, &data->put_llist); |
| 6048 | |
| 6049 | if (pfile == &pfile_stack) { |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6050 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6051 | wait_for_completion(&done); |
| 6052 | flush_work(&data->ref_work); |
| 6053 | return false; |
| 6054 | } |
| 6055 | |
| 6056 | return true; |
| 6057 | } |
| 6058 | |
| 6059 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 6060 | struct io_uring_files_update *up, |
| 6061 | unsigned nr_args) |
| 6062 | { |
| 6063 | struct fixed_file_data *data = ctx->file_data; |
| 6064 | bool ref_switch = false; |
| 6065 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6066 | __s32 __user *fds; |
| 6067 | int fd, i, err; |
| 6068 | __u32 done; |
| 6069 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6070 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6071 | return -EOVERFLOW; |
| 6072 | if (done > ctx->nr_user_files) |
| 6073 | return -EINVAL; |
| 6074 | |
| 6075 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6076 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6077 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6078 | struct fixed_file_table *table; |
| 6079 | unsigned index; |
| 6080 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6081 | err = 0; |
| 6082 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 6083 | err = -EFAULT; |
| 6084 | break; |
| 6085 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6086 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 6087 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6088 | index = i & IORING_FILE_TABLE_MASK; |
| 6089 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6090 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6091 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6092 | if (io_queue_file_removal(data, file)) |
| 6093 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6094 | } |
| 6095 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6096 | file = fget(fd); |
| 6097 | if (!file) { |
| 6098 | err = -EBADF; |
| 6099 | break; |
| 6100 | } |
| 6101 | /* |
| 6102 | * Don't allow io_uring instances to be registered. If |
| 6103 | * UNIX isn't enabled, then this causes a reference |
| 6104 | * cycle and this instance can never get freed. If UNIX |
| 6105 | * is enabled we'll handle it just fine, but there's |
| 6106 | * still no point in allowing a ring fd as it doesn't |
| 6107 | * support regular read/write anyway. |
| 6108 | */ |
| 6109 | if (file->f_op == &io_uring_fops) { |
| 6110 | fput(file); |
| 6111 | err = -EBADF; |
| 6112 | break; |
| 6113 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6114 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6115 | err = io_sqe_file_register(ctx, file, i); |
| 6116 | if (err) |
| 6117 | break; |
| 6118 | } |
| 6119 | nr_args--; |
| 6120 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6121 | up->offset++; |
| 6122 | } |
| 6123 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6124 | if (ref_switch) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6125 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6126 | |
| 6127 | return done ? done : err; |
| 6128 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6129 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 6130 | unsigned nr_args) |
| 6131 | { |
| 6132 | struct io_uring_files_update up; |
| 6133 | |
| 6134 | if (!ctx->file_data) |
| 6135 | return -ENXIO; |
| 6136 | if (!nr_args) |
| 6137 | return -EINVAL; |
| 6138 | if (copy_from_user(&up, arg, sizeof(up))) |
| 6139 | return -EFAULT; |
| 6140 | if (up.resv) |
| 6141 | return -EINVAL; |
| 6142 | |
| 6143 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 6144 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6145 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 6146 | static void io_put_work(struct io_wq_work *work) |
| 6147 | { |
| 6148 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6149 | |
| 6150 | io_put_req(req); |
| 6151 | } |
| 6152 | |
| 6153 | static void io_get_work(struct io_wq_work *work) |
| 6154 | { |
| 6155 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6156 | |
| 6157 | refcount_inc(&req->refs); |
| 6158 | } |
| 6159 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6160 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 6161 | struct io_uring_params *p) |
| 6162 | { |
| 6163 | struct io_wq_data data; |
| 6164 | struct fd f; |
| 6165 | struct io_ring_ctx *ctx_attach; |
| 6166 | unsigned int concurrency; |
| 6167 | int ret = 0; |
| 6168 | |
| 6169 | data.user = ctx->user; |
| 6170 | data.get_work = io_get_work; |
| 6171 | data.put_work = io_put_work; |
| 6172 | |
| 6173 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 6174 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 6175 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 6176 | |
| 6177 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 6178 | if (IS_ERR(ctx->io_wq)) { |
| 6179 | ret = PTR_ERR(ctx->io_wq); |
| 6180 | ctx->io_wq = NULL; |
| 6181 | } |
| 6182 | return ret; |
| 6183 | } |
| 6184 | |
| 6185 | f = fdget(p->wq_fd); |
| 6186 | if (!f.file) |
| 6187 | return -EBADF; |
| 6188 | |
| 6189 | if (f.file->f_op != &io_uring_fops) { |
| 6190 | ret = -EINVAL; |
| 6191 | goto out_fput; |
| 6192 | } |
| 6193 | |
| 6194 | ctx_attach = f.file->private_data; |
| 6195 | /* @io_wq is protected by holding the fd */ |
| 6196 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 6197 | ret = -EINVAL; |
| 6198 | goto out_fput; |
| 6199 | } |
| 6200 | |
| 6201 | ctx->io_wq = ctx_attach->io_wq; |
| 6202 | out_fput: |
| 6203 | fdput(f); |
| 6204 | return ret; |
| 6205 | } |
| 6206 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6207 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 6208 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6209 | { |
| 6210 | int ret; |
| 6211 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6212 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6213 | mmgrab(current->mm); |
| 6214 | ctx->sqo_mm = current->mm; |
| 6215 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6216 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 6217 | ret = -EPERM; |
| 6218 | if (!capable(CAP_SYS_ADMIN)) |
| 6219 | goto err; |
| 6220 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6221 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 6222 | if (!ctx->sq_thread_idle) |
| 6223 | ctx->sq_thread_idle = HZ; |
| 6224 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6225 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6226 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6227 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6228 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6229 | if (cpu >= nr_cpu_ids) |
| 6230 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 6231 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6232 | goto err; |
| 6233 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6234 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 6235 | ctx, cpu, |
| 6236 | "io_uring-sq"); |
| 6237 | } else { |
| 6238 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 6239 | "io_uring-sq"); |
| 6240 | } |
| 6241 | if (IS_ERR(ctx->sqo_thread)) { |
| 6242 | ret = PTR_ERR(ctx->sqo_thread); |
| 6243 | ctx->sqo_thread = NULL; |
| 6244 | goto err; |
| 6245 | } |
| 6246 | wake_up_process(ctx->sqo_thread); |
| 6247 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 6248 | /* Can't have SQ_AFF without SQPOLL */ |
| 6249 | ret = -EINVAL; |
| 6250 | goto err; |
| 6251 | } |
| 6252 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6253 | ret = io_init_wq_offload(ctx, p); |
| 6254 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6255 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6256 | |
| 6257 | return 0; |
| 6258 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 6259 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6260 | mmdrop(ctx->sqo_mm); |
| 6261 | ctx->sqo_mm = NULL; |
| 6262 | return ret; |
| 6263 | } |
| 6264 | |
| 6265 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 6266 | { |
| 6267 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 6268 | } |
| 6269 | |
| 6270 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 6271 | { |
| 6272 | unsigned long page_limit, cur_pages, new_pages; |
| 6273 | |
| 6274 | /* Don't allow more pages than we can safely lock */ |
| 6275 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 6276 | |
| 6277 | do { |
| 6278 | cur_pages = atomic_long_read(&user->locked_vm); |
| 6279 | new_pages = cur_pages + nr_pages; |
| 6280 | if (new_pages > page_limit) |
| 6281 | return -ENOMEM; |
| 6282 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 6283 | new_pages) != cur_pages); |
| 6284 | |
| 6285 | return 0; |
| 6286 | } |
| 6287 | |
| 6288 | static void io_mem_free(void *ptr) |
| 6289 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6290 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6291 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6292 | if (!ptr) |
| 6293 | return; |
| 6294 | |
| 6295 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6296 | if (put_page_testzero(page)) |
| 6297 | free_compound_page(page); |
| 6298 | } |
| 6299 | |
| 6300 | static void *io_mem_alloc(size_t size) |
| 6301 | { |
| 6302 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 6303 | __GFP_NORETRY; |
| 6304 | |
| 6305 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 6306 | } |
| 6307 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6308 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 6309 | size_t *sq_offset) |
| 6310 | { |
| 6311 | struct io_rings *rings; |
| 6312 | size_t off, sq_array_size; |
| 6313 | |
| 6314 | off = struct_size(rings, cqes, cq_entries); |
| 6315 | if (off == SIZE_MAX) |
| 6316 | return SIZE_MAX; |
| 6317 | |
| 6318 | #ifdef CONFIG_SMP |
| 6319 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6320 | if (off == 0) |
| 6321 | return SIZE_MAX; |
| 6322 | #endif |
| 6323 | |
| 6324 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6325 | if (sq_array_size == SIZE_MAX) |
| 6326 | return SIZE_MAX; |
| 6327 | |
| 6328 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6329 | return SIZE_MAX; |
| 6330 | |
| 6331 | if (sq_offset) |
| 6332 | *sq_offset = off; |
| 6333 | |
| 6334 | return off; |
| 6335 | } |
| 6336 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6337 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6338 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6339 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6340 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6341 | pages = (size_t)1 << get_order( |
| 6342 | rings_size(sq_entries, cq_entries, NULL)); |
| 6343 | pages += (size_t)1 << get_order( |
| 6344 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6345 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6346 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6347 | } |
| 6348 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6349 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6350 | { |
| 6351 | int i, j; |
| 6352 | |
| 6353 | if (!ctx->user_bufs) |
| 6354 | return -ENXIO; |
| 6355 | |
| 6356 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6357 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6358 | |
| 6359 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6360 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6361 | |
| 6362 | if (ctx->account_mem) |
| 6363 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6364 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6365 | imu->nr_bvecs = 0; |
| 6366 | } |
| 6367 | |
| 6368 | kfree(ctx->user_bufs); |
| 6369 | ctx->user_bufs = NULL; |
| 6370 | ctx->nr_user_bufs = 0; |
| 6371 | return 0; |
| 6372 | } |
| 6373 | |
| 6374 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6375 | void __user *arg, unsigned index) |
| 6376 | { |
| 6377 | struct iovec __user *src; |
| 6378 | |
| 6379 | #ifdef CONFIG_COMPAT |
| 6380 | if (ctx->compat) { |
| 6381 | struct compat_iovec __user *ciovs; |
| 6382 | struct compat_iovec ciov; |
| 6383 | |
| 6384 | ciovs = (struct compat_iovec __user *) arg; |
| 6385 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6386 | return -EFAULT; |
| 6387 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6388 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6389 | dst->iov_len = ciov.iov_len; |
| 6390 | return 0; |
| 6391 | } |
| 6392 | #endif |
| 6393 | src = (struct iovec __user *) arg; |
| 6394 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6395 | return -EFAULT; |
| 6396 | return 0; |
| 6397 | } |
| 6398 | |
| 6399 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6400 | unsigned nr_args) |
| 6401 | { |
| 6402 | struct vm_area_struct **vmas = NULL; |
| 6403 | struct page **pages = NULL; |
| 6404 | int i, j, got_pages = 0; |
| 6405 | int ret = -EINVAL; |
| 6406 | |
| 6407 | if (ctx->user_bufs) |
| 6408 | return -EBUSY; |
| 6409 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6410 | return -EINVAL; |
| 6411 | |
| 6412 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6413 | GFP_KERNEL); |
| 6414 | if (!ctx->user_bufs) |
| 6415 | return -ENOMEM; |
| 6416 | |
| 6417 | for (i = 0; i < nr_args; i++) { |
| 6418 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6419 | unsigned long off, start, end, ubuf; |
| 6420 | int pret, nr_pages; |
| 6421 | struct iovec iov; |
| 6422 | size_t size; |
| 6423 | |
| 6424 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6425 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6426 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6427 | |
| 6428 | /* |
| 6429 | * Don't impose further limits on the size and buffer |
| 6430 | * constraints here, we'll -EINVAL later when IO is |
| 6431 | * submitted if they are wrong. |
| 6432 | */ |
| 6433 | ret = -EFAULT; |
| 6434 | if (!iov.iov_base || !iov.iov_len) |
| 6435 | goto err; |
| 6436 | |
| 6437 | /* arbitrary limit, but we need something */ |
| 6438 | if (iov.iov_len > SZ_1G) |
| 6439 | goto err; |
| 6440 | |
| 6441 | ubuf = (unsigned long) iov.iov_base; |
| 6442 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6443 | start = ubuf >> PAGE_SHIFT; |
| 6444 | nr_pages = end - start; |
| 6445 | |
| 6446 | if (ctx->account_mem) { |
| 6447 | ret = io_account_mem(ctx->user, nr_pages); |
| 6448 | if (ret) |
| 6449 | goto err; |
| 6450 | } |
| 6451 | |
| 6452 | ret = 0; |
| 6453 | if (!pages || nr_pages > got_pages) { |
| 6454 | kfree(vmas); |
| 6455 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6456 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6457 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6458 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6459 | sizeof(struct vm_area_struct *), |
| 6460 | GFP_KERNEL); |
| 6461 | if (!pages || !vmas) { |
| 6462 | ret = -ENOMEM; |
| 6463 | if (ctx->account_mem) |
| 6464 | io_unaccount_mem(ctx->user, nr_pages); |
| 6465 | goto err; |
| 6466 | } |
| 6467 | got_pages = nr_pages; |
| 6468 | } |
| 6469 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6470 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6471 | GFP_KERNEL); |
| 6472 | ret = -ENOMEM; |
| 6473 | if (!imu->bvec) { |
| 6474 | if (ctx->account_mem) |
| 6475 | io_unaccount_mem(ctx->user, nr_pages); |
| 6476 | goto err; |
| 6477 | } |
| 6478 | |
| 6479 | ret = 0; |
| 6480 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6481 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6482 | FOLL_WRITE | FOLL_LONGTERM, |
| 6483 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6484 | if (pret == nr_pages) { |
| 6485 | /* don't support file backed memory */ |
| 6486 | for (j = 0; j < nr_pages; j++) { |
| 6487 | struct vm_area_struct *vma = vmas[j]; |
| 6488 | |
| 6489 | if (vma->vm_file && |
| 6490 | !is_file_hugepages(vma->vm_file)) { |
| 6491 | ret = -EOPNOTSUPP; |
| 6492 | break; |
| 6493 | } |
| 6494 | } |
| 6495 | } else { |
| 6496 | ret = pret < 0 ? pret : -EFAULT; |
| 6497 | } |
| 6498 | up_read(¤t->mm->mmap_sem); |
| 6499 | if (ret) { |
| 6500 | /* |
| 6501 | * if we did partial map, or found file backed vmas, |
| 6502 | * release any pages we did get |
| 6503 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6504 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6505 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6506 | if (ctx->account_mem) |
| 6507 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6508 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6509 | goto err; |
| 6510 | } |
| 6511 | |
| 6512 | off = ubuf & ~PAGE_MASK; |
| 6513 | size = iov.iov_len; |
| 6514 | for (j = 0; j < nr_pages; j++) { |
| 6515 | size_t vec_len; |
| 6516 | |
| 6517 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6518 | imu->bvec[j].bv_page = pages[j]; |
| 6519 | imu->bvec[j].bv_len = vec_len; |
| 6520 | imu->bvec[j].bv_offset = off; |
| 6521 | off = 0; |
| 6522 | size -= vec_len; |
| 6523 | } |
| 6524 | /* store original address for later verification */ |
| 6525 | imu->ubuf = ubuf; |
| 6526 | imu->len = iov.iov_len; |
| 6527 | imu->nr_bvecs = nr_pages; |
| 6528 | |
| 6529 | ctx->nr_user_bufs++; |
| 6530 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6531 | kvfree(pages); |
| 6532 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6533 | return 0; |
| 6534 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6535 | kvfree(pages); |
| 6536 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6537 | io_sqe_buffer_unregister(ctx); |
| 6538 | return ret; |
| 6539 | } |
| 6540 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6541 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6542 | { |
| 6543 | __s32 __user *fds = arg; |
| 6544 | int fd; |
| 6545 | |
| 6546 | if (ctx->cq_ev_fd) |
| 6547 | return -EBUSY; |
| 6548 | |
| 6549 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6550 | return -EFAULT; |
| 6551 | |
| 6552 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6553 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6554 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6555 | ctx->cq_ev_fd = NULL; |
| 6556 | return ret; |
| 6557 | } |
| 6558 | |
| 6559 | return 0; |
| 6560 | } |
| 6561 | |
| 6562 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6563 | { |
| 6564 | if (ctx->cq_ev_fd) { |
| 6565 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6566 | ctx->cq_ev_fd = NULL; |
| 6567 | return 0; |
| 6568 | } |
| 6569 | |
| 6570 | return -ENXIO; |
| 6571 | } |
| 6572 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6573 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6574 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6575 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6576 | if (ctx->sqo_mm) |
| 6577 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6578 | |
| 6579 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6580 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6581 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6582 | io_eventfd_unregister(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 6583 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6584 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6585 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6586 | if (ctx->ring_sock) { |
| 6587 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6588 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6589 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6590 | #endif |
| 6591 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6592 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6593 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6594 | |
| 6595 | percpu_ref_exit(&ctx->refs); |
| 6596 | if (ctx->account_mem) |
| 6597 | io_unaccount_mem(ctx->user, |
| 6598 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6599 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6600 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6601 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6602 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6603 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6604 | kfree(ctx); |
| 6605 | } |
| 6606 | |
| 6607 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6608 | { |
| 6609 | struct io_ring_ctx *ctx = file->private_data; |
| 6610 | __poll_t mask = 0; |
| 6611 | |
| 6612 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6613 | /* |
| 6614 | * synchronizes with barrier from wq_has_sleeper call in |
| 6615 | * io_commit_cqring |
| 6616 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6617 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6618 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6619 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6620 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 6621 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6622 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6623 | |
| 6624 | return mask; |
| 6625 | } |
| 6626 | |
| 6627 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6628 | { |
| 6629 | struct io_ring_ctx *ctx = file->private_data; |
| 6630 | |
| 6631 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6632 | } |
| 6633 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6634 | static int io_remove_personalities(int id, void *p, void *data) |
| 6635 | { |
| 6636 | struct io_ring_ctx *ctx = data; |
| 6637 | const struct cred *cred; |
| 6638 | |
| 6639 | cred = idr_remove(&ctx->personality_idr, id); |
| 6640 | if (cred) |
| 6641 | put_cred(cred); |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6645 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6646 | { |
| 6647 | mutex_lock(&ctx->uring_lock); |
| 6648 | percpu_ref_kill(&ctx->refs); |
| 6649 | mutex_unlock(&ctx->uring_lock); |
| 6650 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6651 | /* |
| 6652 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 6653 | * work after we've killed the ctx ref above. This is important to do |
| 6654 | * before we cancel existing commands, as the thread could otherwise |
| 6655 | * be queueing new work post that. If that's work we need to cancel, |
| 6656 | * it could cause shutdown to hang. |
| 6657 | */ |
| 6658 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 6659 | cpu_relax(); |
| 6660 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6661 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6662 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6663 | |
| 6664 | if (ctx->io_wq) |
| 6665 | io_wq_cancel_all(ctx->io_wq); |
| 6666 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6667 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6668 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6669 | if (ctx->rings) |
| 6670 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6671 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6672 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6673 | io_ring_ctx_free(ctx); |
| 6674 | } |
| 6675 | |
| 6676 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6677 | { |
| 6678 | struct io_ring_ctx *ctx = file->private_data; |
| 6679 | |
| 6680 | file->private_data = NULL; |
| 6681 | io_ring_ctx_wait_and_kill(ctx); |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6685 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6686 | struct files_struct *files) |
| 6687 | { |
| 6688 | struct io_kiocb *req; |
| 6689 | DEFINE_WAIT(wait); |
| 6690 | |
| 6691 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6692 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6693 | |
| 6694 | spin_lock_irq(&ctx->inflight_lock); |
| 6695 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6696 | if (req->work.files != files) |
| 6697 | continue; |
| 6698 | /* req is being completed, ignore */ |
| 6699 | if (!refcount_inc_not_zero(&req->refs)) |
| 6700 | continue; |
| 6701 | cancel_req = req; |
| 6702 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6703 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6704 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6705 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6706 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6707 | spin_unlock_irq(&ctx->inflight_lock); |
| 6708 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6709 | /* We need to keep going until we don't find a matching req */ |
| 6710 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6711 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6712 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 6713 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 6714 | spin_lock_irq(&ctx->completion_lock); |
| 6715 | list_del(&cancel_req->list); |
| 6716 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 6717 | if (list_empty(&ctx->cq_overflow_list)) { |
| 6718 | clear_bit(0, &ctx->sq_check_overflow); |
| 6719 | clear_bit(0, &ctx->cq_check_overflow); |
| 6720 | } |
| 6721 | spin_unlock_irq(&ctx->completion_lock); |
| 6722 | |
| 6723 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 6724 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 6725 | |
| 6726 | /* |
| 6727 | * Put inflight ref and overflow ref. If that's |
| 6728 | * all we had, then we're done with this request. |
| 6729 | */ |
| 6730 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
| 6731 | io_put_req(cancel_req); |
| 6732 | continue; |
| 6733 | } |
| 6734 | } |
| 6735 | |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6736 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6737 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6738 | schedule(); |
| 6739 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6740 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6741 | } |
| 6742 | |
| 6743 | static int io_uring_flush(struct file *file, void *data) |
| 6744 | { |
| 6745 | struct io_ring_ctx *ctx = file->private_data; |
| 6746 | |
| 6747 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 6748 | |
| 6749 | /* |
| 6750 | * If the task is going away, cancel work it may have pending |
| 6751 | */ |
| 6752 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 6753 | io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current)); |
| 6754 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6755 | return 0; |
| 6756 | } |
| 6757 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6758 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6759 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6760 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6761 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6762 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6763 | struct page *page; |
| 6764 | void *ptr; |
| 6765 | |
| 6766 | switch (offset) { |
| 6767 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6768 | case IORING_OFF_CQ_RING: |
| 6769 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6770 | break; |
| 6771 | case IORING_OFF_SQES: |
| 6772 | ptr = ctx->sq_sqes; |
| 6773 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6774 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6775 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6776 | } |
| 6777 | |
| 6778 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6779 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6780 | return ERR_PTR(-EINVAL); |
| 6781 | |
| 6782 | return ptr; |
| 6783 | } |
| 6784 | |
| 6785 | #ifdef CONFIG_MMU |
| 6786 | |
| 6787 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6788 | { |
| 6789 | size_t sz = vma->vm_end - vma->vm_start; |
| 6790 | unsigned long pfn; |
| 6791 | void *ptr; |
| 6792 | |
| 6793 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6794 | if (IS_ERR(ptr)) |
| 6795 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6796 | |
| 6797 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6798 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6799 | } |
| 6800 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6801 | #else /* !CONFIG_MMU */ |
| 6802 | |
| 6803 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6804 | { |
| 6805 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6806 | } |
| 6807 | |
| 6808 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6809 | { |
| 6810 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6811 | } |
| 6812 | |
| 6813 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6814 | unsigned long addr, unsigned long len, |
| 6815 | unsigned long pgoff, unsigned long flags) |
| 6816 | { |
| 6817 | void *ptr; |
| 6818 | |
| 6819 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6820 | if (IS_ERR(ptr)) |
| 6821 | return PTR_ERR(ptr); |
| 6822 | |
| 6823 | return (unsigned long) ptr; |
| 6824 | } |
| 6825 | |
| 6826 | #endif /* !CONFIG_MMU */ |
| 6827 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6828 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6829 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6830 | size_t, sigsz) |
| 6831 | { |
| 6832 | struct io_ring_ctx *ctx; |
| 6833 | long ret = -EBADF; |
| 6834 | int submitted = 0; |
| 6835 | struct fd f; |
| 6836 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6837 | if (current->task_works) |
| 6838 | task_work_run(); |
| 6839 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6840 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6841 | return -EINVAL; |
| 6842 | |
| 6843 | f = fdget(fd); |
| 6844 | if (!f.file) |
| 6845 | return -EBADF; |
| 6846 | |
| 6847 | ret = -EOPNOTSUPP; |
| 6848 | if (f.file->f_op != &io_uring_fops) |
| 6849 | goto out_fput; |
| 6850 | |
| 6851 | ret = -ENXIO; |
| 6852 | ctx = f.file->private_data; |
| 6853 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6854 | goto out_fput; |
| 6855 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6856 | /* |
| 6857 | * For SQ polling, the thread will do all submissions and completions. |
| 6858 | * Just return the requested submit count, and wake the thread if |
| 6859 | * we were asked to. |
| 6860 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6861 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6862 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6863 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6864 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6865 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6866 | wake_up(&ctx->sqo_wait); |
| 6867 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6868 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6869 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6870 | |
| 6871 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6872 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6873 | cur_mm = ctx->sqo_mm; |
| 6874 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6875 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6876 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6877 | |
| 6878 | if (submitted != to_submit) |
| 6879 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6880 | } |
| 6881 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6882 | unsigned nr_events = 0; |
| 6883 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6884 | min_complete = min(min_complete, ctx->cq_entries); |
| 6885 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6886 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6887 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6888 | } else { |
| 6889 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6890 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6891 | } |
| 6892 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6893 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6894 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6895 | out_fput: |
| 6896 | fdput(f); |
| 6897 | return submitted ? submitted : ret; |
| 6898 | } |
| 6899 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6900 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6901 | static int io_uring_show_cred(int id, void *p, void *data) |
| 6902 | { |
| 6903 | const struct cred *cred = p; |
| 6904 | struct seq_file *m = data; |
| 6905 | struct user_namespace *uns = seq_user_ns(m); |
| 6906 | struct group_info *gi; |
| 6907 | kernel_cap_t cap; |
| 6908 | unsigned __capi; |
| 6909 | int g; |
| 6910 | |
| 6911 | seq_printf(m, "%5d\n", id); |
| 6912 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 6913 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 6914 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 6915 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 6916 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 6917 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 6918 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 6919 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 6920 | seq_puts(m, "\n\tGroups:\t"); |
| 6921 | gi = cred->group_info; |
| 6922 | for (g = 0; g < gi->ngroups; g++) { |
| 6923 | seq_put_decimal_ull(m, g ? " " : "", |
| 6924 | from_kgid_munged(uns, gi->gid[g])); |
| 6925 | } |
| 6926 | seq_puts(m, "\n\tCapEff:\t"); |
| 6927 | cap = cred->cap_effective; |
| 6928 | CAP_FOR_EACH_U32(__capi) |
| 6929 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 6930 | seq_putc(m, '\n'); |
| 6931 | return 0; |
| 6932 | } |
| 6933 | |
| 6934 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 6935 | { |
| 6936 | int i; |
| 6937 | |
| 6938 | mutex_lock(&ctx->uring_lock); |
| 6939 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 6940 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6941 | struct fixed_file_table *table; |
| 6942 | struct file *f; |
| 6943 | |
| 6944 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6945 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 6946 | if (f) |
| 6947 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 6948 | else |
| 6949 | seq_printf(m, "%5u: <none>\n", i); |
| 6950 | } |
| 6951 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 6952 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6953 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 6954 | |
| 6955 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 6956 | (unsigned int) buf->len); |
| 6957 | } |
| 6958 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 6959 | seq_printf(m, "Personalities:\n"); |
| 6960 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 6961 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6962 | seq_printf(m, "PollList:\n"); |
| 6963 | spin_lock_irq(&ctx->completion_lock); |
| 6964 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 6965 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 6966 | struct io_kiocb *req; |
| 6967 | |
| 6968 | hlist_for_each_entry(req, list, hash_node) |
| 6969 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 6970 | req->task->task_works != NULL); |
| 6971 | } |
| 6972 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6973 | mutex_unlock(&ctx->uring_lock); |
| 6974 | } |
| 6975 | |
| 6976 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 6977 | { |
| 6978 | struct io_ring_ctx *ctx = f->private_data; |
| 6979 | |
| 6980 | if (percpu_ref_tryget(&ctx->refs)) { |
| 6981 | __io_uring_show_fdinfo(ctx, m); |
| 6982 | percpu_ref_put(&ctx->refs); |
| 6983 | } |
| 6984 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6985 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6986 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6987 | static const struct file_operations io_uring_fops = { |
| 6988 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6989 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6990 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6991 | #ifndef CONFIG_MMU |
| 6992 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6993 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6994 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6995 | .poll = io_uring_poll, |
| 6996 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6997 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6998 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6999 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7000 | }; |
| 7001 | |
| 7002 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 7003 | struct io_uring_params *p) |
| 7004 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7005 | struct io_rings *rings; |
| 7006 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7007 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7008 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 7009 | if (size == SIZE_MAX) |
| 7010 | return -EOVERFLOW; |
| 7011 | |
| 7012 | rings = io_mem_alloc(size); |
| 7013 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7014 | return -ENOMEM; |
| 7015 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7016 | ctx->rings = rings; |
| 7017 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 7018 | rings->sq_ring_mask = p->sq_entries - 1; |
| 7019 | rings->cq_ring_mask = p->cq_entries - 1; |
| 7020 | rings->sq_ring_entries = p->sq_entries; |
| 7021 | rings->cq_ring_entries = p->cq_entries; |
| 7022 | ctx->sq_mask = rings->sq_ring_mask; |
| 7023 | ctx->cq_mask = rings->cq_ring_mask; |
| 7024 | ctx->sq_entries = rings->sq_ring_entries; |
| 7025 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7026 | |
| 7027 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7028 | if (size == SIZE_MAX) { |
| 7029 | io_mem_free(ctx->rings); |
| 7030 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7031 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7032 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7033 | |
| 7034 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7035 | if (!ctx->sq_sqes) { |
| 7036 | io_mem_free(ctx->rings); |
| 7037 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7038 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7039 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7040 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7041 | return 0; |
| 7042 | } |
| 7043 | |
| 7044 | /* |
| 7045 | * Allocate an anonymous fd, this is what constitutes the application |
| 7046 | * visible backing of an io_uring instance. The application mmaps this |
| 7047 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 7048 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 7049 | */ |
| 7050 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 7051 | { |
| 7052 | struct file *file; |
| 7053 | int ret; |
| 7054 | |
| 7055 | #if defined(CONFIG_UNIX) |
| 7056 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 7057 | &ctx->ring_sock); |
| 7058 | if (ret) |
| 7059 | return ret; |
| 7060 | #endif |
| 7061 | |
| 7062 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 7063 | if (ret < 0) |
| 7064 | goto err; |
| 7065 | |
| 7066 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 7067 | O_RDWR | O_CLOEXEC); |
| 7068 | if (IS_ERR(file)) { |
| 7069 | put_unused_fd(ret); |
| 7070 | ret = PTR_ERR(file); |
| 7071 | goto err; |
| 7072 | } |
| 7073 | |
| 7074 | #if defined(CONFIG_UNIX) |
| 7075 | ctx->ring_sock->file = file; |
| 7076 | #endif |
| 7077 | fd_install(ret, file); |
| 7078 | return ret; |
| 7079 | err: |
| 7080 | #if defined(CONFIG_UNIX) |
| 7081 | sock_release(ctx->ring_sock); |
| 7082 | ctx->ring_sock = NULL; |
| 7083 | #endif |
| 7084 | return ret; |
| 7085 | } |
| 7086 | |
| 7087 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 7088 | { |
| 7089 | struct user_struct *user = NULL; |
| 7090 | struct io_ring_ctx *ctx; |
| 7091 | bool account_mem; |
| 7092 | int ret; |
| 7093 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7094 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7095 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7096 | if (entries > IORING_MAX_ENTRIES) { |
| 7097 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 7098 | return -EINVAL; |
| 7099 | entries = IORING_MAX_ENTRIES; |
| 7100 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7101 | |
| 7102 | /* |
| 7103 | * Use twice as many entries for the CQ ring. It's possible for the |
| 7104 | * application to drive a higher depth than the size of the SQ ring, |
| 7105 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7106 | * some flexibility in overcommitting a bit. If the application has |
| 7107 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 7108 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7109 | */ |
| 7110 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7111 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 7112 | /* |
| 7113 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 7114 | * to a power-of-two, if it isn't already. We do NOT impose |
| 7115 | * any cq vs sq ring sizing. |
| 7116 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7117 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7118 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7119 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 7120 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 7121 | return -EINVAL; |
| 7122 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 7123 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7124 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 7125 | } else { |
| 7126 | p->cq_entries = 2 * p->sq_entries; |
| 7127 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7128 | |
| 7129 | user = get_uid(current_user()); |
| 7130 | account_mem = !capable(CAP_IPC_LOCK); |
| 7131 | |
| 7132 | if (account_mem) { |
| 7133 | ret = io_account_mem(user, |
| 7134 | ring_pages(p->sq_entries, p->cq_entries)); |
| 7135 | if (ret) { |
| 7136 | free_uid(user); |
| 7137 | return ret; |
| 7138 | } |
| 7139 | } |
| 7140 | |
| 7141 | ctx = io_ring_ctx_alloc(p); |
| 7142 | if (!ctx) { |
| 7143 | if (account_mem) |
| 7144 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 7145 | p->cq_entries)); |
| 7146 | free_uid(user); |
| 7147 | return -ENOMEM; |
| 7148 | } |
| 7149 | ctx->compat = in_compat_syscall(); |
| 7150 | ctx->account_mem = account_mem; |
| 7151 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 7152 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7153 | |
| 7154 | ret = io_allocate_scq_urings(ctx, p); |
| 7155 | if (ret) |
| 7156 | goto err; |
| 7157 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7158 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7159 | if (ret) |
| 7160 | goto err; |
| 7161 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7162 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7163 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 7164 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 7165 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 7166 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 7167 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 7168 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 7169 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7170 | |
| 7171 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7172 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 7173 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 7174 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 7175 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 7176 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 7177 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 7178 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 7179 | /* |
| 7180 | * Install ring fd as the very last thing, so we don't risk someone |
| 7181 | * having closed it before we finish setup |
| 7182 | */ |
| 7183 | ret = io_uring_get_fd(ctx); |
| 7184 | if (ret < 0) |
| 7185 | goto err; |
| 7186 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 7187 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 7188 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 7189 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7190 | 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] | 7191 | return ret; |
| 7192 | err: |
| 7193 | io_ring_ctx_wait_and_kill(ctx); |
| 7194 | return ret; |
| 7195 | } |
| 7196 | |
| 7197 | /* |
| 7198 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 7199 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 7200 | * params structure passed in. |
| 7201 | */ |
| 7202 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 7203 | { |
| 7204 | struct io_uring_params p; |
| 7205 | long ret; |
| 7206 | int i; |
| 7207 | |
| 7208 | if (copy_from_user(&p, params, sizeof(p))) |
| 7209 | return -EFAULT; |
| 7210 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 7211 | if (p.resv[i]) |
| 7212 | return -EINVAL; |
| 7213 | } |
| 7214 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7215 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7216 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7217 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7218 | return -EINVAL; |
| 7219 | |
| 7220 | ret = io_uring_create(entries, &p); |
| 7221 | if (ret < 0) |
| 7222 | return ret; |
| 7223 | |
| 7224 | if (copy_to_user(params, &p, sizeof(p))) |
| 7225 | return -EFAULT; |
| 7226 | |
| 7227 | return ret; |
| 7228 | } |
| 7229 | |
| 7230 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 7231 | struct io_uring_params __user *, params) |
| 7232 | { |
| 7233 | return io_uring_setup(entries, params); |
| 7234 | } |
| 7235 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7236 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 7237 | { |
| 7238 | struct io_uring_probe *p; |
| 7239 | size_t size; |
| 7240 | int i, ret; |
| 7241 | |
| 7242 | size = struct_size(p, ops, nr_args); |
| 7243 | if (size == SIZE_MAX) |
| 7244 | return -EOVERFLOW; |
| 7245 | p = kzalloc(size, GFP_KERNEL); |
| 7246 | if (!p) |
| 7247 | return -ENOMEM; |
| 7248 | |
| 7249 | ret = -EFAULT; |
| 7250 | if (copy_from_user(p, arg, size)) |
| 7251 | goto out; |
| 7252 | ret = -EINVAL; |
| 7253 | if (memchr_inv(p, 0, size)) |
| 7254 | goto out; |
| 7255 | |
| 7256 | p->last_op = IORING_OP_LAST - 1; |
| 7257 | if (nr_args > IORING_OP_LAST) |
| 7258 | nr_args = IORING_OP_LAST; |
| 7259 | |
| 7260 | for (i = 0; i < nr_args; i++) { |
| 7261 | p->ops[i].op = i; |
| 7262 | if (!io_op_defs[i].not_supported) |
| 7263 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 7264 | } |
| 7265 | p->ops_len = i; |
| 7266 | |
| 7267 | ret = 0; |
| 7268 | if (copy_to_user(arg, p, size)) |
| 7269 | ret = -EFAULT; |
| 7270 | out: |
| 7271 | kfree(p); |
| 7272 | return ret; |
| 7273 | } |
| 7274 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7275 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 7276 | { |
| 7277 | const struct cred *creds = get_current_cred(); |
| 7278 | int id; |
| 7279 | |
| 7280 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 7281 | USHRT_MAX, GFP_KERNEL); |
| 7282 | if (id < 0) |
| 7283 | put_cred(creds); |
| 7284 | return id; |
| 7285 | } |
| 7286 | |
| 7287 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 7288 | { |
| 7289 | const struct cred *old_creds; |
| 7290 | |
| 7291 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 7292 | if (old_creds) { |
| 7293 | put_cred(old_creds); |
| 7294 | return 0; |
| 7295 | } |
| 7296 | |
| 7297 | return -EINVAL; |
| 7298 | } |
| 7299 | |
| 7300 | static bool io_register_op_must_quiesce(int op) |
| 7301 | { |
| 7302 | switch (op) { |
| 7303 | case IORING_UNREGISTER_FILES: |
| 7304 | case IORING_REGISTER_FILES_UPDATE: |
| 7305 | case IORING_REGISTER_PROBE: |
| 7306 | case IORING_REGISTER_PERSONALITY: |
| 7307 | case IORING_UNREGISTER_PERSONALITY: |
| 7308 | return false; |
| 7309 | default: |
| 7310 | return true; |
| 7311 | } |
| 7312 | } |
| 7313 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7314 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 7315 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7316 | __releases(ctx->uring_lock) |
| 7317 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7318 | { |
| 7319 | int ret; |
| 7320 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 7321 | /* |
| 7322 | * We're inside the ring mutex, if the ref is already dying, then |
| 7323 | * someone else killed the ctx or is already going through |
| 7324 | * io_uring_register(). |
| 7325 | */ |
| 7326 | if (percpu_ref_is_dying(&ctx->refs)) |
| 7327 | return -ENXIO; |
| 7328 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7329 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7330 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7331 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7332 | /* |
| 7333 | * Drop uring mutex before waiting for references to exit. If |
| 7334 | * another thread is currently inside io_uring_enter() it might |
| 7335 | * need to grab the uring_lock to make progress. If we hold it |
| 7336 | * here across the drain wait, then we can deadlock. It's safe |
| 7337 | * to drop the mutex here, since no new references will come in |
| 7338 | * after we've killed the percpu ref. |
| 7339 | */ |
| 7340 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7341 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7342 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7343 | if (ret) { |
| 7344 | percpu_ref_resurrect(&ctx->refs); |
| 7345 | ret = -EINTR; |
| 7346 | goto out; |
| 7347 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7348 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7349 | |
| 7350 | switch (opcode) { |
| 7351 | case IORING_REGISTER_BUFFERS: |
| 7352 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 7353 | break; |
| 7354 | case IORING_UNREGISTER_BUFFERS: |
| 7355 | ret = -EINVAL; |
| 7356 | if (arg || nr_args) |
| 7357 | break; |
| 7358 | ret = io_sqe_buffer_unregister(ctx); |
| 7359 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7360 | case IORING_REGISTER_FILES: |
| 7361 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 7362 | break; |
| 7363 | case IORING_UNREGISTER_FILES: |
| 7364 | ret = -EINVAL; |
| 7365 | if (arg || nr_args) |
| 7366 | break; |
| 7367 | ret = io_sqe_files_unregister(ctx); |
| 7368 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7369 | case IORING_REGISTER_FILES_UPDATE: |
| 7370 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7371 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7372 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7373 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7374 | ret = -EINVAL; |
| 7375 | if (nr_args != 1) |
| 7376 | break; |
| 7377 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7378 | if (ret) |
| 7379 | break; |
| 7380 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7381 | ctx->eventfd_async = 1; |
| 7382 | else |
| 7383 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7384 | break; |
| 7385 | case IORING_UNREGISTER_EVENTFD: |
| 7386 | ret = -EINVAL; |
| 7387 | if (arg || nr_args) |
| 7388 | break; |
| 7389 | ret = io_eventfd_unregister(ctx); |
| 7390 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7391 | case IORING_REGISTER_PROBE: |
| 7392 | ret = -EINVAL; |
| 7393 | if (!arg || nr_args > 256) |
| 7394 | break; |
| 7395 | ret = io_probe(ctx, arg, nr_args); |
| 7396 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7397 | case IORING_REGISTER_PERSONALITY: |
| 7398 | ret = -EINVAL; |
| 7399 | if (arg || nr_args) |
| 7400 | break; |
| 7401 | ret = io_register_personality(ctx); |
| 7402 | break; |
| 7403 | case IORING_UNREGISTER_PERSONALITY: |
| 7404 | ret = -EINVAL; |
| 7405 | if (arg) |
| 7406 | break; |
| 7407 | ret = io_unregister_personality(ctx, nr_args); |
| 7408 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7409 | default: |
| 7410 | ret = -EINVAL; |
| 7411 | break; |
| 7412 | } |
| 7413 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7414 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7415 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7416 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7417 | out: |
| 7418 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7419 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7420 | return ret; |
| 7421 | } |
| 7422 | |
| 7423 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7424 | void __user *, arg, unsigned int, nr_args) |
| 7425 | { |
| 7426 | struct io_ring_ctx *ctx; |
| 7427 | long ret = -EBADF; |
| 7428 | struct fd f; |
| 7429 | |
| 7430 | f = fdget(fd); |
| 7431 | if (!f.file) |
| 7432 | return -EBADF; |
| 7433 | |
| 7434 | ret = -EOPNOTSUPP; |
| 7435 | if (f.file->f_op != &io_uring_fops) |
| 7436 | goto out_fput; |
| 7437 | |
| 7438 | ctx = f.file->private_data; |
| 7439 | |
| 7440 | mutex_lock(&ctx->uring_lock); |
| 7441 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7442 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7443 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7444 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7445 | out_fput: |
| 7446 | fdput(f); |
| 7447 | return ret; |
| 7448 | } |
| 7449 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7450 | static int __init io_uring_init(void) |
| 7451 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7452 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7453 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7454 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7455 | } while (0) |
| 7456 | |
| 7457 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7458 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7459 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7460 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7461 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7462 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7463 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7464 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7465 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7466 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7467 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7468 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7469 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7470 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7471 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7472 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7473 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7474 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7475 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7476 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7477 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7478 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7479 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7480 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7481 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7482 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7483 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7484 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7485 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7486 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7487 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7488 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7489 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 7490 | return 0; |
| 7491 | }; |
| 7492 | __initcall(io_uring_init); |