Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 49 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 50 | |
| 51 | #include <linux/sched/signal.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/file.h> |
| 54 | #include <linux/fdtable.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/mman.h> |
| 57 | #include <linux/mmu_context.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 78 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 79 | #define CREATE_TRACE_POINTS |
| 80 | #include <trace/events/io_uring.h> |
| 81 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 82 | #include <uapi/linux/io_uring.h> |
| 83 | |
| 84 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 85 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 86 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 87 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 88 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 92 | */ |
| 93 | #define IORING_FILE_TABLE_SHIFT 9 |
| 94 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 95 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 96 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 97 | |
| 98 | struct io_uring { |
| 99 | u32 head ____cacheline_aligned_in_smp; |
| 100 | u32 tail ____cacheline_aligned_in_smp; |
| 101 | }; |
| 102 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 103 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 104 | * This data is shared with the application through the mmap at offsets |
| 105 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | * |
| 107 | * The offsets to the member fields are published through struct |
| 108 | * io_sqring_offsets when calling io_uring_setup. |
| 109 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 110 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Head and tail offsets into the ring; the offsets need to be |
| 113 | * masked to get valid indices. |
| 114 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 115 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 116 | * and the application controls tail of the sq ring and the head of the |
| 117 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 118 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 120 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 121 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | * ring_entries - 1) |
| 123 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | u32 sq_ring_mask, cq_ring_mask; |
| 125 | /* Ring sizes (constant, power of 2) */ |
| 126 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Number of invalid entries dropped by the kernel due to |
| 129 | * invalid index stored in array |
| 130 | * |
| 131 | * Written by the kernel, shouldn't be modified by the |
| 132 | * application (i.e. get number of "new events" by comparing to |
| 133 | * cached value). |
| 134 | * |
| 135 | * After a new SQ head value was read by the application this |
| 136 | * counter includes all submissions that were dropped reaching |
| 137 | * the new SQ head (and possibly more). |
| 138 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 139 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Runtime flags |
| 142 | * |
| 143 | * Written by the kernel, shouldn't be modified by the |
| 144 | * application. |
| 145 | * |
| 146 | * The application needs a full memory barrier before checking |
| 147 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 148 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 149 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 150 | /* |
| 151 | * Number of completion events lost because the queue was full; |
| 152 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 153 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | * the completion queue. |
| 155 | * |
| 156 | * Written by the kernel, shouldn't be modified by the |
| 157 | * application (i.e. get number of "new events" by comparing to |
| 158 | * cached value). |
| 159 | * |
| 160 | * As completion events come in out of order this counter is not |
| 161 | * ordered with any other data. |
| 162 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 163 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 164 | /* |
| 165 | * Ring buffer of completion events. |
| 166 | * |
| 167 | * The kernel writes completion events fresh every time they are |
| 168 | * produced, so the application is allowed to modify pending |
| 169 | * entries. |
| 170 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 171 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 174 | struct io_mapped_ubuf { |
| 175 | u64 ubuf; |
| 176 | size_t len; |
| 177 | struct bio_vec *bvec; |
| 178 | unsigned int nr_bvecs; |
| 179 | }; |
| 180 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 181 | struct fixed_file_table { |
| 182 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 185 | enum { |
| 186 | FFD_F_ATOMIC, |
| 187 | }; |
| 188 | |
| 189 | struct fixed_file_data { |
| 190 | struct fixed_file_table *table; |
| 191 | struct io_ring_ctx *ctx; |
| 192 | |
| 193 | struct percpu_ref refs; |
| 194 | struct llist_head put_llist; |
| 195 | unsigned long state; |
| 196 | struct work_struct ref_work; |
| 197 | struct completion done; |
| 198 | }; |
| 199 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 200 | struct io_ring_ctx { |
| 201 | struct { |
| 202 | struct percpu_ref refs; |
| 203 | } ____cacheline_aligned_in_smp; |
| 204 | |
| 205 | struct { |
| 206 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame^] | 207 | unsigned int compat: 1; |
| 208 | unsigned int account_mem: 1; |
| 209 | unsigned int cq_overflow_flushed: 1; |
| 210 | unsigned int drain_next: 1; |
| 211 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 212 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 213 | /* |
| 214 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 215 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 216 | * |
| 217 | * This indirection could e.g. be used to assign fixed |
| 218 | * io_uring_sqe entries to operations and only submit them to |
| 219 | * the queue when needed. |
| 220 | * |
| 221 | * The kernel modifies neither the indices array nor the entries |
| 222 | * array. |
| 223 | */ |
| 224 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 225 | unsigned cached_sq_head; |
| 226 | unsigned sq_entries; |
| 227 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 228 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 229 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 230 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 231 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 232 | |
| 233 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 234 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 235 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 236 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 237 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 238 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 239 | } ____cacheline_aligned_in_smp; |
| 240 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 241 | struct io_rings *rings; |
| 242 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 243 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 244 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 245 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 246 | struct mm_struct *sqo_mm; |
| 247 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 248 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 249 | /* |
| 250 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 251 | * readers must ensure that ->refs is alive as long as the file* is |
| 252 | * used. Only updated through io_uring_register(2). |
| 253 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 254 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 255 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 256 | int ring_fd; |
| 257 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 258 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 259 | /* if used, fixed mapped user buffers */ |
| 260 | unsigned nr_user_bufs; |
| 261 | struct io_mapped_ubuf *user_bufs; |
| 262 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 263 | struct user_struct *user; |
| 264 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 265 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 266 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 267 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 268 | struct completion *completions; |
| 269 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 270 | /* if all else fails... */ |
| 271 | struct io_kiocb *fallback_req; |
| 272 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 273 | #if defined(CONFIG_UNIX) |
| 274 | struct socket *ring_sock; |
| 275 | #endif |
| 276 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 277 | struct idr personality_idr; |
| 278 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 279 | struct { |
| 280 | unsigned cached_cq_tail; |
| 281 | unsigned cq_entries; |
| 282 | unsigned cq_mask; |
| 283 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 284 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 285 | struct wait_queue_head cq_wait; |
| 286 | struct fasync_struct *cq_fasync; |
| 287 | struct eventfd_ctx *cq_ev_fd; |
| 288 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 289 | |
| 290 | struct { |
| 291 | struct mutex uring_lock; |
| 292 | wait_queue_head_t wait; |
| 293 | } ____cacheline_aligned_in_smp; |
| 294 | |
| 295 | struct { |
| 296 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 297 | struct llist_head poll_llist; |
| 298 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 299 | /* |
| 300 | * ->poll_list is protected by the ctx->uring_lock for |
| 301 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 302 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 303 | * manipulate the list, hence no extra locking is needed there. |
| 304 | */ |
| 305 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 306 | struct hlist_head *cancel_hash; |
| 307 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 308 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 309 | |
| 310 | spinlock_t inflight_lock; |
| 311 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 312 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 315 | /* |
| 316 | * First field must be the file pointer in all the |
| 317 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 318 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 319 | struct io_poll_iocb { |
| 320 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 321 | union { |
| 322 | struct wait_queue_head *head; |
| 323 | u64 addr; |
| 324 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 325 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 326 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 327 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 328 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 331 | struct io_close { |
| 332 | struct file *file; |
| 333 | struct file *put_file; |
| 334 | int fd; |
| 335 | }; |
| 336 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 337 | struct io_timeout_data { |
| 338 | struct io_kiocb *req; |
| 339 | struct hrtimer timer; |
| 340 | struct timespec64 ts; |
| 341 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 342 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 343 | }; |
| 344 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 345 | struct io_accept { |
| 346 | struct file *file; |
| 347 | struct sockaddr __user *addr; |
| 348 | int __user *addr_len; |
| 349 | int flags; |
| 350 | }; |
| 351 | |
| 352 | struct io_sync { |
| 353 | struct file *file; |
| 354 | loff_t len; |
| 355 | loff_t off; |
| 356 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 357 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 358 | }; |
| 359 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 360 | struct io_cancel { |
| 361 | struct file *file; |
| 362 | u64 addr; |
| 363 | }; |
| 364 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 365 | struct io_timeout { |
| 366 | struct file *file; |
| 367 | u64 addr; |
| 368 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 369 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 370 | }; |
| 371 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 372 | struct io_rw { |
| 373 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 374 | struct kiocb kiocb; |
| 375 | u64 addr; |
| 376 | u64 len; |
| 377 | }; |
| 378 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 379 | struct io_connect { |
| 380 | struct file *file; |
| 381 | struct sockaddr __user *addr; |
| 382 | int addr_len; |
| 383 | }; |
| 384 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 385 | struct io_sr_msg { |
| 386 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 387 | union { |
| 388 | struct user_msghdr __user *msg; |
| 389 | void __user *buf; |
| 390 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 391 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 392 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 393 | }; |
| 394 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 395 | struct io_open { |
| 396 | struct file *file; |
| 397 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 398 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 399 | unsigned mask; |
| 400 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 401 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 402 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 403 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 406 | struct io_files_update { |
| 407 | struct file *file; |
| 408 | u64 arg; |
| 409 | u32 nr_args; |
| 410 | u32 offset; |
| 411 | }; |
| 412 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 413 | struct io_fadvise { |
| 414 | struct file *file; |
| 415 | u64 offset; |
| 416 | u32 len; |
| 417 | u32 advice; |
| 418 | }; |
| 419 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 420 | struct io_madvise { |
| 421 | struct file *file; |
| 422 | u64 addr; |
| 423 | u32 len; |
| 424 | u32 advice; |
| 425 | }; |
| 426 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 427 | struct io_epoll { |
| 428 | struct file *file; |
| 429 | int epfd; |
| 430 | int op; |
| 431 | int fd; |
| 432 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 435 | struct io_async_connect { |
| 436 | struct sockaddr_storage address; |
| 437 | }; |
| 438 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 439 | struct io_async_msghdr { |
| 440 | struct iovec fast_iov[UIO_FASTIOV]; |
| 441 | struct iovec *iov; |
| 442 | struct sockaddr __user *uaddr; |
| 443 | struct msghdr msg; |
| 444 | }; |
| 445 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 446 | struct io_async_rw { |
| 447 | struct iovec fast_iov[UIO_FASTIOV]; |
| 448 | struct iovec *iov; |
| 449 | ssize_t nr_segs; |
| 450 | ssize_t size; |
| 451 | }; |
| 452 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 453 | struct io_async_open { |
| 454 | struct filename *filename; |
| 455 | }; |
| 456 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 457 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 458 | union { |
| 459 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 460 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 461 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 462 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 463 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 464 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 467 | enum { |
| 468 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 469 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 470 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 471 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 472 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 473 | |
| 474 | REQ_F_LINK_NEXT_BIT, |
| 475 | REQ_F_FAIL_LINK_BIT, |
| 476 | REQ_F_INFLIGHT_BIT, |
| 477 | REQ_F_CUR_POS_BIT, |
| 478 | REQ_F_NOWAIT_BIT, |
| 479 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 480 | REQ_F_LINK_TIMEOUT_BIT, |
| 481 | REQ_F_TIMEOUT_BIT, |
| 482 | REQ_F_ISREG_BIT, |
| 483 | REQ_F_MUST_PUNT_BIT, |
| 484 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 485 | REQ_F_COMP_LOCKED_BIT, |
| 486 | }; |
| 487 | |
| 488 | enum { |
| 489 | /* ctx owns file */ |
| 490 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 491 | /* drain existing IO first */ |
| 492 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 493 | /* linked sqes */ |
| 494 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 495 | /* doesn't sever on completion < 0 */ |
| 496 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 497 | /* IOSQE_ASYNC */ |
| 498 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 499 | |
| 500 | /* already grabbed next link */ |
| 501 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 502 | /* fail rest of links */ |
| 503 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 504 | /* on inflight list */ |
| 505 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 506 | /* read/write uses file position */ |
| 507 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 508 | /* must not punt to workers */ |
| 509 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 510 | /* polled IO has completed */ |
| 511 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 512 | /* has linked timeout */ |
| 513 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 514 | /* timeout request */ |
| 515 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 516 | /* regular file */ |
| 517 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 518 | /* must be punted even for NONBLOCK */ |
| 519 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 520 | /* no timeout sequence */ |
| 521 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 522 | /* completion under lock */ |
| 523 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
| 524 | }; |
| 525 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 526 | /* |
| 527 | * NOTE! Each of the iocb union members has the file pointer |
| 528 | * as the first entry in their struct definition. So you can |
| 529 | * access the file pointer through any of the sub-structs, |
| 530 | * or directly as just 'ki_filp' in this struct. |
| 531 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 532 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 533 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 534 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 535 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 536 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 537 | struct io_accept accept; |
| 538 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 539 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 540 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 541 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 542 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 543 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 544 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 545 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 546 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 547 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 548 | struct io_epoll epoll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 549 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 550 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 551 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 552 | /* |
| 553 | * llist_node is only used for poll deferred completions |
| 554 | */ |
| 555 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 556 | bool in_async; |
| 557 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 558 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 559 | |
| 560 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 561 | union { |
| 562 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 563 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 564 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 565 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 566 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 567 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 568 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 569 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 570 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 571 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 572 | struct list_head inflight_entry; |
| 573 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 574 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 578 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 579 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 580 | struct io_submit_state { |
| 581 | struct blk_plug plug; |
| 582 | |
| 583 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 584 | * io_kiocb alloc cache |
| 585 | */ |
| 586 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 587 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 588 | |
| 589 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 590 | * File reference cache |
| 591 | */ |
| 592 | struct file *file; |
| 593 | unsigned int fd; |
| 594 | unsigned int has_refs; |
| 595 | unsigned int used_refs; |
| 596 | unsigned int ios_left; |
| 597 | }; |
| 598 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 599 | struct io_op_def { |
| 600 | /* needs req->io allocated for deferral/async */ |
| 601 | unsigned async_ctx : 1; |
| 602 | /* needs current->mm setup, does mm access */ |
| 603 | unsigned needs_mm : 1; |
| 604 | /* needs req->file assigned */ |
| 605 | unsigned needs_file : 1; |
| 606 | /* needs req->file assigned IFF fd is >= 0 */ |
| 607 | unsigned fd_non_neg : 1; |
| 608 | /* hash wq insertion if file is a regular file */ |
| 609 | unsigned hash_reg_file : 1; |
| 610 | /* unbound wq insertion if file is a non-regular file */ |
| 611 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 612 | /* opcode is not supported by this kernel */ |
| 613 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 614 | /* needs file table */ |
| 615 | unsigned file_table : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 616 | }; |
| 617 | |
| 618 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 619 | [IORING_OP_NOP] = {}, |
| 620 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 621 | .async_ctx = 1, |
| 622 | .needs_mm = 1, |
| 623 | .needs_file = 1, |
| 624 | .unbound_nonreg_file = 1, |
| 625 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 626 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 627 | .async_ctx = 1, |
| 628 | .needs_mm = 1, |
| 629 | .needs_file = 1, |
| 630 | .hash_reg_file = 1, |
| 631 | .unbound_nonreg_file = 1, |
| 632 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 633 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 634 | .needs_file = 1, |
| 635 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 636 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 637 | .needs_file = 1, |
| 638 | .unbound_nonreg_file = 1, |
| 639 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 640 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 641 | .needs_file = 1, |
| 642 | .hash_reg_file = 1, |
| 643 | .unbound_nonreg_file = 1, |
| 644 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 645 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 646 | .needs_file = 1, |
| 647 | .unbound_nonreg_file = 1, |
| 648 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 649 | [IORING_OP_POLL_REMOVE] = {}, |
| 650 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 651 | .needs_file = 1, |
| 652 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 653 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 654 | .async_ctx = 1, |
| 655 | .needs_mm = 1, |
| 656 | .needs_file = 1, |
| 657 | .unbound_nonreg_file = 1, |
| 658 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 659 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 660 | .async_ctx = 1, |
| 661 | .needs_mm = 1, |
| 662 | .needs_file = 1, |
| 663 | .unbound_nonreg_file = 1, |
| 664 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 665 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 666 | .async_ctx = 1, |
| 667 | .needs_mm = 1, |
| 668 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 669 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 670 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 671 | .needs_mm = 1, |
| 672 | .needs_file = 1, |
| 673 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 674 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 675 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 676 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 677 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 678 | .async_ctx = 1, |
| 679 | .needs_mm = 1, |
| 680 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 681 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 682 | .async_ctx = 1, |
| 683 | .needs_mm = 1, |
| 684 | .needs_file = 1, |
| 685 | .unbound_nonreg_file = 1, |
| 686 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 687 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 688 | .needs_file = 1, |
| 689 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 690 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 691 | .needs_file = 1, |
| 692 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 693 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 694 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 695 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 696 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 697 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 698 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 699 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 700 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 701 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 702 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 703 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 704 | .needs_mm = 1, |
| 705 | .needs_file = 1, |
| 706 | .fd_non_neg = 1, |
| 707 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 708 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 709 | .needs_mm = 1, |
| 710 | .needs_file = 1, |
| 711 | .unbound_nonreg_file = 1, |
| 712 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 713 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 714 | .needs_mm = 1, |
| 715 | .needs_file = 1, |
| 716 | .unbound_nonreg_file = 1, |
| 717 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 718 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 719 | .needs_file = 1, |
| 720 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 721 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 722 | .needs_mm = 1, |
| 723 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 724 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 725 | .needs_mm = 1, |
| 726 | .needs_file = 1, |
| 727 | .unbound_nonreg_file = 1, |
| 728 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 729 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 730 | .needs_mm = 1, |
| 731 | .needs_file = 1, |
| 732 | .unbound_nonreg_file = 1, |
| 733 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 734 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 735 | .needs_file = 1, |
| 736 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 737 | .file_table = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 738 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 739 | [IORING_OP_EPOLL_CTL] = { |
| 740 | .unbound_nonreg_file = 1, |
| 741 | .file_table = 1, |
| 742 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 743 | }; |
| 744 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 745 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 746 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 747 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 748 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 749 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 750 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 751 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 752 | struct io_uring_files_update *ip, |
| 753 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 754 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 755 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 756 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 757 | static struct kmem_cache *req_cachep; |
| 758 | |
| 759 | static const struct file_operations io_uring_fops; |
| 760 | |
| 761 | struct sock *io_uring_get_socket(struct file *file) |
| 762 | { |
| 763 | #if defined(CONFIG_UNIX) |
| 764 | if (file->f_op == &io_uring_fops) { |
| 765 | struct io_ring_ctx *ctx = file->private_data; |
| 766 | |
| 767 | return ctx->ring_sock->sk; |
| 768 | } |
| 769 | #endif |
| 770 | return NULL; |
| 771 | } |
| 772 | EXPORT_SYMBOL(io_uring_get_socket); |
| 773 | |
| 774 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 775 | { |
| 776 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 777 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 778 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 782 | { |
| 783 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 784 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 785 | |
| 786 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 787 | if (!ctx) |
| 788 | return NULL; |
| 789 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 790 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 791 | if (!ctx->fallback_req) |
| 792 | goto err; |
| 793 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 794 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 795 | if (!ctx->completions) |
| 796 | goto err; |
| 797 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 798 | /* |
| 799 | * Use 5 bits less than the max cq entries, that should give us around |
| 800 | * 32 entries per hash list if totally full and uniformly spread. |
| 801 | */ |
| 802 | hash_bits = ilog2(p->cq_entries); |
| 803 | hash_bits -= 5; |
| 804 | if (hash_bits <= 0) |
| 805 | hash_bits = 1; |
| 806 | ctx->cancel_hash_bits = hash_bits; |
| 807 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 808 | GFP_KERNEL); |
| 809 | if (!ctx->cancel_hash) |
| 810 | goto err; |
| 811 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 812 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 813 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 814 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 815 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 816 | |
| 817 | ctx->flags = p->flags; |
| 818 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 819 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 820 | init_completion(&ctx->completions[0]); |
| 821 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 822 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 823 | mutex_init(&ctx->uring_lock); |
| 824 | init_waitqueue_head(&ctx->wait); |
| 825 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 826 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 827 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 828 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 829 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 830 | init_waitqueue_head(&ctx->inflight_wait); |
| 831 | spin_lock_init(&ctx->inflight_lock); |
| 832 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 833 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 834 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 835 | if (ctx->fallback_req) |
| 836 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 837 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 838 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 839 | kfree(ctx); |
| 840 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 843 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 844 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 845 | struct io_ring_ctx *ctx = req->ctx; |
| 846 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 847 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 848 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 849 | } |
| 850 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 851 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 852 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 853 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 854 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 855 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 856 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | 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] | 860 | { |
| 861 | struct io_kiocb *req; |
| 862 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 863 | 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] | 864 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 865 | list_del_init(&req->list); |
| 866 | return req; |
| 867 | } |
| 868 | |
| 869 | return NULL; |
| 870 | } |
| 871 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 872 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 873 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 874 | struct io_kiocb *req; |
| 875 | |
| 876 | 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] | 877 | if (req) { |
| 878 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 879 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 880 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 881 | list_del_init(&req->list); |
| 882 | return req; |
| 883 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 887 | } |
| 888 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 889 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 890 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 891 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 892 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 893 | /* order cqe stores with ring update */ |
| 894 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 895 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 896 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 897 | wake_up_interruptible(&ctx->cq_wait); |
| 898 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 902 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 903 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 904 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 905 | if (!req->work.mm && def->needs_mm) { |
| 906 | mmgrab(current->mm); |
| 907 | req->work.mm = current->mm; |
| 908 | } |
| 909 | if (!req->work.creds) |
| 910 | req->work.creds = get_current_cred(); |
| 911 | } |
| 912 | |
| 913 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 914 | { |
| 915 | if (req->work.mm) { |
| 916 | mmdrop(req->work.mm); |
| 917 | req->work.mm = NULL; |
| 918 | } |
| 919 | if (req->work.creds) { |
| 920 | put_cred(req->work.creds); |
| 921 | req->work.creds = NULL; |
| 922 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 923 | } |
| 924 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 925 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 926 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 927 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 928 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 929 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 930 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 931 | if (req->flags & REQ_F_ISREG) { |
| 932 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 933 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 934 | } else { |
| 935 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 936 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 937 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 938 | |
| 939 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 940 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 941 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 942 | return do_hashed; |
| 943 | } |
| 944 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 945 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 946 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 947 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 948 | struct io_kiocb *link; |
| 949 | bool do_hashed; |
| 950 | |
| 951 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 952 | |
| 953 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 954 | req->flags); |
| 955 | if (!do_hashed) { |
| 956 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 957 | } else { |
| 958 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 959 | file_inode(req->file)); |
| 960 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 961 | |
| 962 | if (link) |
| 963 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 964 | } |
| 965 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 966 | static void io_kill_timeout(struct io_kiocb *req) |
| 967 | { |
| 968 | int ret; |
| 969 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 970 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 971 | if (ret != -1) { |
| 972 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 973 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 974 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 975 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 980 | { |
| 981 | struct io_kiocb *req, *tmp; |
| 982 | |
| 983 | spin_lock_irq(&ctx->completion_lock); |
| 984 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 985 | io_kill_timeout(req); |
| 986 | spin_unlock_irq(&ctx->completion_lock); |
| 987 | } |
| 988 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 989 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 990 | { |
| 991 | struct io_kiocb *req; |
| 992 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 993 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 994 | io_kill_timeout(req); |
| 995 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 996 | __io_commit_cqring(ctx); |
| 997 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 998 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 999 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1000 | } |
| 1001 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1002 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1003 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1004 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1005 | unsigned tail; |
| 1006 | |
| 1007 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1008 | /* |
| 1009 | * writes to the cq entry need to come after reading head; the |
| 1010 | * control dependency is enough as we're using WRITE_ONCE to |
| 1011 | * fill the cq entry |
| 1012 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1013 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1014 | return NULL; |
| 1015 | |
| 1016 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1017 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1020 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1021 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1022 | if (!ctx->cq_ev_fd) |
| 1023 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1024 | if (!ctx->eventfd_async) |
| 1025 | return true; |
| 1026 | return io_wq_current_is_worker() || in_interrupt(); |
| 1027 | } |
| 1028 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1029 | static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1030 | { |
| 1031 | if (waitqueue_active(&ctx->wait)) |
| 1032 | wake_up(&ctx->wait); |
| 1033 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1034 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1035 | if (trigger_ev) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1036 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1037 | } |
| 1038 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1039 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1040 | { |
| 1041 | __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx)); |
| 1042 | } |
| 1043 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1044 | /* Returns true if there are no backlogged entries after the flush */ |
| 1045 | 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] | 1046 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1047 | struct io_rings *rings = ctx->rings; |
| 1048 | struct io_uring_cqe *cqe; |
| 1049 | struct io_kiocb *req; |
| 1050 | unsigned long flags; |
| 1051 | LIST_HEAD(list); |
| 1052 | |
| 1053 | if (!force) { |
| 1054 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1055 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1056 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1057 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1058 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1062 | |
| 1063 | /* if force is set, the ring is going away. always drop after that */ |
| 1064 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1065 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1066 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1067 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1068 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1069 | cqe = io_get_cqring(ctx); |
| 1070 | if (!cqe && !force) |
| 1071 | break; |
| 1072 | |
| 1073 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1074 | list); |
| 1075 | list_move(&req->list, &list); |
| 1076 | if (cqe) { |
| 1077 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1078 | WRITE_ONCE(cqe->res, req->result); |
| 1079 | WRITE_ONCE(cqe->flags, 0); |
| 1080 | } else { |
| 1081 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1082 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1087 | if (cqe) { |
| 1088 | clear_bit(0, &ctx->sq_check_overflow); |
| 1089 | clear_bit(0, &ctx->cq_check_overflow); |
| 1090 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1091 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1092 | io_cqring_ev_posted(ctx); |
| 1093 | |
| 1094 | while (!list_empty(&list)) { |
| 1095 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1096 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1097 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1098 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1099 | |
| 1100 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1103 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1104 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1105 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1106 | struct io_uring_cqe *cqe; |
| 1107 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1108 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1109 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1110 | /* |
| 1111 | * If we can't get a cq entry, userspace overflowed the |
| 1112 | * submission (by quite a lot). Increment the overflow count in |
| 1113 | * the ring. |
| 1114 | */ |
| 1115 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1116 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1117 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1118 | WRITE_ONCE(cqe->res, res); |
| 1119 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1120 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1121 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1122 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1123 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1124 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1125 | set_bit(0, &ctx->sq_check_overflow); |
| 1126 | set_bit(0, &ctx->cq_check_overflow); |
| 1127 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1128 | refcount_inc(&req->refs); |
| 1129 | req->result = res; |
| 1130 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1134 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1135 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1136 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1137 | unsigned long flags; |
| 1138 | |
| 1139 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1140 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1141 | io_commit_cqring(ctx); |
| 1142 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1143 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1144 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1147 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1148 | { |
| 1149 | return req == (struct io_kiocb *) |
| 1150 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1151 | } |
| 1152 | |
| 1153 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1154 | { |
| 1155 | struct io_kiocb *req; |
| 1156 | |
| 1157 | req = ctx->fallback_req; |
| 1158 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1159 | return req; |
| 1160 | |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1164 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1165 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1166 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1167 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1168 | struct io_kiocb *req; |
| 1169 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1170 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1171 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1172 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1173 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1174 | } else if (!state->free_reqs) { |
| 1175 | size_t sz; |
| 1176 | int ret; |
| 1177 | |
| 1178 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1179 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1180 | |
| 1181 | /* |
| 1182 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1183 | * retry single alloc to be on the safe side. |
| 1184 | */ |
| 1185 | if (unlikely(ret <= 0)) { |
| 1186 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1187 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1188 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1189 | ret = 1; |
| 1190 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1191 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1192 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1193 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1194 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1195 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1198 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1199 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1200 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1201 | req->ctx = ctx; |
| 1202 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1203 | /* one is dropped after submission, the other at completion */ |
| 1204 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1205 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1206 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1207 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1208 | fallback: |
| 1209 | req = io_get_fallback_req(ctx); |
| 1210 | if (req) |
| 1211 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1212 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1213 | return NULL; |
| 1214 | } |
| 1215 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1216 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1217 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1218 | if (likely(!io_is_fallback_req(req))) |
| 1219 | kmem_cache_free(req_cachep, req); |
| 1220 | else |
| 1221 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1222 | } |
| 1223 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1224 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1225 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1226 | struct io_ring_ctx *ctx = req->ctx; |
| 1227 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1228 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1229 | if (req->file) { |
| 1230 | if (req->flags & REQ_F_FIXED_FILE) |
| 1231 | percpu_ref_put(&ctx->file_data->refs); |
| 1232 | else |
| 1233 | fput(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1234 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1235 | |
| 1236 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | static void __io_free_req(struct io_kiocb *req) |
| 1240 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1241 | __io_req_aux_free(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1242 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1243 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1244 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1245 | unsigned long flags; |
| 1246 | |
| 1247 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1248 | list_del(&req->inflight_entry); |
| 1249 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1250 | wake_up(&ctx->inflight_wait); |
| 1251 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1252 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1253 | |
| 1254 | percpu_ref_put(&req->ctx->refs); |
| 1255 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1256 | } |
| 1257 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1258 | struct req_batch { |
| 1259 | void *reqs[IO_IOPOLL_BATCH]; |
| 1260 | int to_free; |
| 1261 | int need_iter; |
| 1262 | }; |
| 1263 | |
| 1264 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1265 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1266 | int fixed_refs = rb->to_free; |
| 1267 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1268 | if (!rb->to_free) |
| 1269 | return; |
| 1270 | if (rb->need_iter) { |
| 1271 | int i, inflight = 0; |
| 1272 | unsigned long flags; |
| 1273 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1274 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1275 | for (i = 0; i < rb->to_free; i++) { |
| 1276 | struct io_kiocb *req = rb->reqs[i]; |
| 1277 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1278 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1279 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1280 | fixed_refs++; |
| 1281 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1282 | if (req->flags & REQ_F_INFLIGHT) |
| 1283 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1284 | __io_req_aux_free(req); |
| 1285 | } |
| 1286 | if (!inflight) |
| 1287 | goto do_free; |
| 1288 | |
| 1289 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1290 | for (i = 0; i < rb->to_free; i++) { |
| 1291 | struct io_kiocb *req = rb->reqs[i]; |
| 1292 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1293 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1294 | list_del(&req->inflight_entry); |
| 1295 | if (!--inflight) |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1300 | |
| 1301 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1302 | wake_up(&ctx->inflight_wait); |
| 1303 | } |
| 1304 | do_free: |
| 1305 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1306 | if (fixed_refs) |
| 1307 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1308 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1309 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1310 | } |
| 1311 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1312 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1313 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1314 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1315 | int ret; |
| 1316 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1317 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1318 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1319 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1320 | io_commit_cqring(ctx); |
| 1321 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1322 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1329 | 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] | 1330 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1331 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1332 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1333 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1334 | /* Already got next link */ |
| 1335 | if (req->flags & REQ_F_LINK_NEXT) |
| 1336 | return; |
| 1337 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1338 | /* |
| 1339 | * The list should never be empty when we are called here. But could |
| 1340 | * potentially happen if the chain is messed up, check to be on the |
| 1341 | * safe side. |
| 1342 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1343 | while (!list_empty(&req->link_list)) { |
| 1344 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1345 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1346 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1347 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1348 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1349 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1350 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1351 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1352 | continue; |
| 1353 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1354 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1355 | list_del_init(&req->link_list); |
| 1356 | if (!list_empty(&nxt->link_list)) |
| 1357 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1358 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1359 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1360 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1361 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1362 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1363 | if (wake_ev) |
| 1364 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1369 | */ |
| 1370 | static void io_fail_links(struct io_kiocb *req) |
| 1371 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1372 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1373 | unsigned long flags; |
| 1374 | |
| 1375 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1376 | |
| 1377 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1378 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1379 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1380 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1381 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1382 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1383 | |
| 1384 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1385 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1386 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1387 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1388 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1389 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1390 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1391 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1392 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1393 | |
| 1394 | io_commit_cqring(ctx); |
| 1395 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1396 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1397 | } |
| 1398 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1399 | 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] | 1400 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1401 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1402 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1403 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1404 | /* |
| 1405 | * If LINK is set, we have dependent requests in this chain. If we |
| 1406 | * didn't fail this request, queue the first one up, moving any other |
| 1407 | * dependencies to the next request. In case of failure, fail the rest |
| 1408 | * of the chain. |
| 1409 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1410 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1411 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1412 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1413 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1414 | struct io_ring_ctx *ctx = req->ctx; |
| 1415 | unsigned long flags; |
| 1416 | |
| 1417 | /* |
| 1418 | * If this is a timeout link, we could be racing with the |
| 1419 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1420 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1421 | */ |
| 1422 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1423 | io_req_link_next(req, nxt); |
| 1424 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1425 | } else { |
| 1426 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1427 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1428 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1429 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1430 | static void io_free_req(struct io_kiocb *req) |
| 1431 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1432 | struct io_kiocb *nxt = NULL; |
| 1433 | |
| 1434 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1435 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1436 | |
| 1437 | if (nxt) |
| 1438 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1441 | /* |
| 1442 | * Drop reference to request, return next in chain (if there is one) if this |
| 1443 | * was the last reference to this request. |
| 1444 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1445 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1446 | 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] | 1447 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1448 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1449 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1450 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1451 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1454 | static void io_put_req(struct io_kiocb *req) |
| 1455 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1456 | if (refcount_dec_and_test(&req->refs)) |
| 1457 | io_free_req(req); |
| 1458 | } |
| 1459 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1460 | /* |
| 1461 | * Must only be used if we don't need to care about links, usually from |
| 1462 | * within the completion handling itself. |
| 1463 | */ |
| 1464 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1465 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1466 | /* drop both submit and complete references */ |
| 1467 | if (refcount_sub_and_test(2, &req->refs)) |
| 1468 | __io_free_req(req); |
| 1469 | } |
| 1470 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1471 | static void io_double_put_req(struct io_kiocb *req) |
| 1472 | { |
| 1473 | /* drop both submit and complete references */ |
| 1474 | if (refcount_sub_and_test(2, &req->refs)) |
| 1475 | io_free_req(req); |
| 1476 | } |
| 1477 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1478 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1479 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1480 | struct io_rings *rings = ctx->rings; |
| 1481 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1482 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1483 | /* |
| 1484 | * noflush == true is from the waitqueue handler, just ensure |
| 1485 | * we wake up the task, and the next invocation will flush the |
| 1486 | * entries. We cannot safely to it from here. |
| 1487 | */ |
| 1488 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1489 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1490 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1491 | io_cqring_overflow_flush(ctx, false); |
| 1492 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1493 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1494 | /* See comment at the top of this file */ |
| 1495 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1496 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1497 | } |
| 1498 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1499 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1500 | { |
| 1501 | struct io_rings *rings = ctx->rings; |
| 1502 | |
| 1503 | /* make sure SQ entry isn't read before tail */ |
| 1504 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1505 | } |
| 1506 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1507 | 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] | 1508 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1509 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1510 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1511 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1512 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1513 | rb->need_iter++; |
| 1514 | |
| 1515 | rb->reqs[rb->to_free++] = req; |
| 1516 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1517 | io_free_req_many(req->ctx, rb); |
| 1518 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1521 | /* |
| 1522 | * Find and free completed poll iocbs |
| 1523 | */ |
| 1524 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1525 | struct list_head *done) |
| 1526 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1527 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1528 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1529 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1530 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1531 | while (!list_empty(done)) { |
| 1532 | req = list_first_entry(done, struct io_kiocb, list); |
| 1533 | list_del(&req->list); |
| 1534 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1535 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1536 | (*nr_events)++; |
| 1537 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1538 | if (refcount_dec_and_test(&req->refs) && |
| 1539 | !io_req_multi_free(&rb, req)) |
| 1540 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1541 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1542 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1543 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1544 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1548 | long min) |
| 1549 | { |
| 1550 | struct io_kiocb *req, *tmp; |
| 1551 | LIST_HEAD(done); |
| 1552 | bool spin; |
| 1553 | int ret; |
| 1554 | |
| 1555 | /* |
| 1556 | * Only spin for completions if we don't have multiple devices hanging |
| 1557 | * off our complete list, and we're under the requested amount. |
| 1558 | */ |
| 1559 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1560 | |
| 1561 | ret = 0; |
| 1562 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1563 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1564 | |
| 1565 | /* |
| 1566 | * Move completed entries to our local list. If we find a |
| 1567 | * request that requires polling, break out and complete |
| 1568 | * the done list first, if we have entries there. |
| 1569 | */ |
| 1570 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1571 | list_move_tail(&req->list, &done); |
| 1572 | continue; |
| 1573 | } |
| 1574 | if (!list_empty(&done)) |
| 1575 | break; |
| 1576 | |
| 1577 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1578 | if (ret < 0) |
| 1579 | break; |
| 1580 | |
| 1581 | if (ret && spin) |
| 1582 | spin = false; |
| 1583 | ret = 0; |
| 1584 | } |
| 1585 | |
| 1586 | if (!list_empty(&done)) |
| 1587 | io_iopoll_complete(ctx, nr_events, &done); |
| 1588 | |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1593 | * 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] | 1594 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1595 | * as a non-spinning completion check. |
| 1596 | */ |
| 1597 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1598 | long min) |
| 1599 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1600 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1601 | int ret; |
| 1602 | |
| 1603 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1604 | if (ret < 0) |
| 1605 | return ret; |
| 1606 | if (!min || *nr_events >= min) |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | return 1; |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * We can't just wait for polled events to come to us, we have to actively |
| 1615 | * find and complete them. |
| 1616 | */ |
| 1617 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1618 | { |
| 1619 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1620 | return; |
| 1621 | |
| 1622 | mutex_lock(&ctx->uring_lock); |
| 1623 | while (!list_empty(&ctx->poll_list)) { |
| 1624 | unsigned int nr_events = 0; |
| 1625 | |
| 1626 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1627 | |
| 1628 | /* |
| 1629 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1630 | * in this case we need to ensure that we reap all events. |
| 1631 | */ |
| 1632 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1633 | } |
| 1634 | mutex_unlock(&ctx->uring_lock); |
| 1635 | } |
| 1636 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1637 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1638 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1639 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1640 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1641 | |
| 1642 | do { |
| 1643 | int tmin = 0; |
| 1644 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1645 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1646 | * Don't enter poll loop if we already have events pending. |
| 1647 | * If we do, we can potentially be spinning for commands that |
| 1648 | * already triggered a CQE (eg in error). |
| 1649 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1650 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1651 | break; |
| 1652 | |
| 1653 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1654 | * If a submit got punted to a workqueue, we can have the |
| 1655 | * application entering polling for a command before it gets |
| 1656 | * issued. That app will hold the uring_lock for the duration |
| 1657 | * of the poll right here, so we need to take a breather every |
| 1658 | * now and then to ensure that the issue has a chance to add |
| 1659 | * the poll to the issued list. Otherwise we can spin here |
| 1660 | * forever, while the workqueue is stuck trying to acquire the |
| 1661 | * very same mutex. |
| 1662 | */ |
| 1663 | if (!(++iters & 7)) { |
| 1664 | mutex_unlock(&ctx->uring_lock); |
| 1665 | mutex_lock(&ctx->uring_lock); |
| 1666 | } |
| 1667 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1668 | if (*nr_events < min) |
| 1669 | tmin = min - *nr_events; |
| 1670 | |
| 1671 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1672 | if (ret <= 0) |
| 1673 | break; |
| 1674 | ret = 0; |
| 1675 | } while (min && !*nr_events && !need_resched()); |
| 1676 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1681 | long min) |
| 1682 | { |
| 1683 | int ret; |
| 1684 | |
| 1685 | /* |
| 1686 | * We disallow the app entering submit/complete with polling, but we |
| 1687 | * still need to lock the ring to prevent racing with polled issue |
| 1688 | * that got punted to a workqueue. |
| 1689 | */ |
| 1690 | mutex_lock(&ctx->uring_lock); |
| 1691 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1692 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1693 | return ret; |
| 1694 | } |
| 1695 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1696 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1697 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1698 | /* |
| 1699 | * Tell lockdep we inherited freeze protection from submission |
| 1700 | * thread. |
| 1701 | */ |
| 1702 | if (req->flags & REQ_F_ISREG) { |
| 1703 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1704 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1705 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1706 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1707 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1710 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1711 | { |
| 1712 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1713 | req->flags |= REQ_F_FAIL_LINK; |
| 1714 | } |
| 1715 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1716 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1717 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1718 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1719 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1720 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1721 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1722 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1723 | if (res != req->result) |
| 1724 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1725 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1729 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1730 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1731 | |
| 1732 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1733 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1736 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1737 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1738 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1739 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1740 | |
| 1741 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1742 | io_put_req_find_next(req, &nxt); |
| 1743 | |
| 1744 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1747 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1748 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1749 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1750 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1751 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1752 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1753 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1754 | if (res != req->result) |
| 1755 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1756 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1757 | if (res != -EAGAIN) |
| 1758 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1763 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1764 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1765 | * accessing the kiocb cookie. |
| 1766 | */ |
| 1767 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1768 | { |
| 1769 | struct io_ring_ctx *ctx = req->ctx; |
| 1770 | |
| 1771 | /* |
| 1772 | * Track whether we have multiple files in our lists. This will impact |
| 1773 | * how we do polling eventually, not spinning if we're on potentially |
| 1774 | * different devices. |
| 1775 | */ |
| 1776 | if (list_empty(&ctx->poll_list)) { |
| 1777 | ctx->poll_multi_file = false; |
| 1778 | } else if (!ctx->poll_multi_file) { |
| 1779 | struct io_kiocb *list_req; |
| 1780 | |
| 1781 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1782 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1783 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1784 | ctx->poll_multi_file = true; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * For fast devices, IO may have already completed. If it has, add |
| 1789 | * it to the front so we find it first. |
| 1790 | */ |
| 1791 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1792 | list_add(&req->list, &ctx->poll_list); |
| 1793 | else |
| 1794 | list_add_tail(&req->list, &ctx->poll_list); |
| 1795 | } |
| 1796 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1797 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1798 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1799 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1800 | int diff = state->has_refs - state->used_refs; |
| 1801 | |
| 1802 | if (diff) |
| 1803 | fput_many(state->file, diff); |
| 1804 | state->file = NULL; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | * Get as many references to a file as we have IOs left in this submission, |
| 1810 | * assuming most submissions are for one file, or at least that each file |
| 1811 | * has more than one submission. |
| 1812 | */ |
| 1813 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1814 | { |
| 1815 | if (!state) |
| 1816 | return fget(fd); |
| 1817 | |
| 1818 | if (state->file) { |
| 1819 | if (state->fd == fd) { |
| 1820 | state->used_refs++; |
| 1821 | state->ios_left--; |
| 1822 | return state->file; |
| 1823 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1824 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1825 | } |
| 1826 | state->file = fget_many(fd, state->ios_left); |
| 1827 | if (!state->file) |
| 1828 | return NULL; |
| 1829 | |
| 1830 | state->fd = fd; |
| 1831 | state->has_refs = state->ios_left; |
| 1832 | state->used_refs = 1; |
| 1833 | state->ios_left--; |
| 1834 | return state->file; |
| 1835 | } |
| 1836 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1837 | /* |
| 1838 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1839 | * any file. For now, just ensure that anything potentially problematic is done |
| 1840 | * inline. |
| 1841 | */ |
| 1842 | static bool io_file_supports_async(struct file *file) |
| 1843 | { |
| 1844 | umode_t mode = file_inode(file)->i_mode; |
| 1845 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1846 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1847 | return true; |
| 1848 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1849 | return true; |
| 1850 | |
| 1851 | return false; |
| 1852 | } |
| 1853 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1854 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1855 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1856 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1857 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1858 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1859 | unsigned ioprio; |
| 1860 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1861 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1862 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1863 | req->flags |= REQ_F_ISREG; |
| 1864 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1865 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1866 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1867 | req->flags |= REQ_F_CUR_POS; |
| 1868 | kiocb->ki_pos = req->file->f_pos; |
| 1869 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1870 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 1871 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1872 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1873 | if (unlikely(ret)) |
| 1874 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1875 | |
| 1876 | ioprio = READ_ONCE(sqe->ioprio); |
| 1877 | if (ioprio) { |
| 1878 | ret = ioprio_check_cap(ioprio); |
| 1879 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1880 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1881 | |
| 1882 | kiocb->ki_ioprio = ioprio; |
| 1883 | } else |
| 1884 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1885 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1886 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1887 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1888 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1889 | req->flags |= REQ_F_NOWAIT; |
| 1890 | |
| 1891 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1892 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1893 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1894 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1895 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1896 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1897 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1898 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1899 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1900 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1901 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1902 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1903 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1904 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1905 | kiocb->ki_complete = io_complete_rw; |
| 1906 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1907 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1908 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1909 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1910 | /* we own ->private, reuse it for the buffer index */ |
| 1911 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1912 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1913 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1917 | { |
| 1918 | switch (ret) { |
| 1919 | case -EIOCBQUEUED: |
| 1920 | break; |
| 1921 | case -ERESTARTSYS: |
| 1922 | case -ERESTARTNOINTR: |
| 1923 | case -ERESTARTNOHAND: |
| 1924 | case -ERESTART_RESTARTBLOCK: |
| 1925 | /* |
| 1926 | * We can't just restart the syscall, since previously |
| 1927 | * submitted sqes may already be in progress. Just fail this |
| 1928 | * IO with EINTR. |
| 1929 | */ |
| 1930 | ret = -EINTR; |
| 1931 | /* fall through */ |
| 1932 | default: |
| 1933 | kiocb->ki_complete(kiocb, ret, 0); |
| 1934 | } |
| 1935 | } |
| 1936 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1937 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1938 | bool in_async) |
| 1939 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1940 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1941 | |
| 1942 | if (req->flags & REQ_F_CUR_POS) |
| 1943 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1944 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1945 | *nxt = __io_complete_rw(kiocb, ret); |
| 1946 | else |
| 1947 | io_rw_done(kiocb, ret); |
| 1948 | } |
| 1949 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1950 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1951 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1952 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1953 | struct io_ring_ctx *ctx = req->ctx; |
| 1954 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1955 | struct io_mapped_ubuf *imu; |
| 1956 | unsigned index, buf_index; |
| 1957 | size_t offset; |
| 1958 | u64 buf_addr; |
| 1959 | |
| 1960 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1961 | if (unlikely(!ctx->user_bufs)) |
| 1962 | return -EFAULT; |
| 1963 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1964 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1965 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1966 | return -EFAULT; |
| 1967 | |
| 1968 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1969 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1970 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1971 | |
| 1972 | /* overflow */ |
| 1973 | if (buf_addr + len < buf_addr) |
| 1974 | return -EFAULT; |
| 1975 | /* not inside the mapped region */ |
| 1976 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1977 | return -EFAULT; |
| 1978 | |
| 1979 | /* |
| 1980 | * May not be a start of buffer, set size appropriately |
| 1981 | * and advance us to the beginning. |
| 1982 | */ |
| 1983 | offset = buf_addr - imu->ubuf; |
| 1984 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1985 | |
| 1986 | if (offset) { |
| 1987 | /* |
| 1988 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1989 | * using the latter parts of a big fixed buffer - it iterates |
| 1990 | * over each segment manually. We can cheat a bit here, because |
| 1991 | * we know that: |
| 1992 | * |
| 1993 | * 1) it's a BVEC iter, we set it up |
| 1994 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1995 | * first and last bvec |
| 1996 | * |
| 1997 | * So just find our index, and adjust the iterator afterwards. |
| 1998 | * If the offset is within the first bvec (or the whole first |
| 1999 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2000 | * since we can just skip the first segment, which may not |
| 2001 | * be PAGE_SIZE aligned. |
| 2002 | */ |
| 2003 | const struct bio_vec *bvec = imu->bvec; |
| 2004 | |
| 2005 | if (offset <= bvec->bv_len) { |
| 2006 | iov_iter_advance(iter, offset); |
| 2007 | } else { |
| 2008 | unsigned long seg_skip; |
| 2009 | |
| 2010 | /* skip first vec */ |
| 2011 | offset -= bvec->bv_len; |
| 2012 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2013 | |
| 2014 | iter->bvec = bvec + seg_skip; |
| 2015 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2016 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2017 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2021 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2024 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2025 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2026 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2027 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2028 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2029 | u8 opcode; |
| 2030 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2031 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2032 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2033 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2034 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2035 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2036 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2037 | /* buffer index only valid with fixed read/write */ |
| 2038 | if (req->rw.kiocb.private) |
| 2039 | return -EINVAL; |
| 2040 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2041 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2042 | ssize_t ret; |
| 2043 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2044 | *iovec = NULL; |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2048 | if (req->io) { |
| 2049 | struct io_async_rw *iorw = &req->io->rw; |
| 2050 | |
| 2051 | *iovec = iorw->iov; |
| 2052 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2053 | if (iorw->iov == iorw->fast_iov) |
| 2054 | *iovec = NULL; |
| 2055 | return iorw->size; |
| 2056 | } |
| 2057 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2058 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2059 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2060 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2061 | iovec, iter); |
| 2062 | #endif |
| 2063 | |
| 2064 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2065 | } |
| 2066 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2067 | /* |
| 2068 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2069 | * by looping over ->read() or ->write() manually. |
| 2070 | */ |
| 2071 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2072 | struct iov_iter *iter) |
| 2073 | { |
| 2074 | ssize_t ret = 0; |
| 2075 | |
| 2076 | /* |
| 2077 | * Don't support polled IO through this interface, and we can't |
| 2078 | * support non-blocking either. For the latter, this just causes |
| 2079 | * the kiocb to be handled from an async context. |
| 2080 | */ |
| 2081 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2082 | return -EOPNOTSUPP; |
| 2083 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2084 | return -EAGAIN; |
| 2085 | |
| 2086 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2087 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2088 | ssize_t nr; |
| 2089 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2090 | if (!iov_iter_is_bvec(iter)) { |
| 2091 | iovec = iov_iter_iovec(iter); |
| 2092 | } else { |
| 2093 | /* fixed buffers import bvec */ |
| 2094 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2095 | + iter->iov_offset; |
| 2096 | iovec.iov_len = min(iter->count, |
| 2097 | iter->bvec->bv_len - iter->iov_offset); |
| 2098 | } |
| 2099 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2100 | if (rw == READ) { |
| 2101 | nr = file->f_op->read(file, iovec.iov_base, |
| 2102 | iovec.iov_len, &kiocb->ki_pos); |
| 2103 | } else { |
| 2104 | nr = file->f_op->write(file, iovec.iov_base, |
| 2105 | iovec.iov_len, &kiocb->ki_pos); |
| 2106 | } |
| 2107 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2108 | if (iov_iter_is_bvec(iter)) |
| 2109 | kunmap(iter->bvec->bv_page); |
| 2110 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2111 | if (nr < 0) { |
| 2112 | if (!ret) |
| 2113 | ret = nr; |
| 2114 | break; |
| 2115 | } |
| 2116 | ret += nr; |
| 2117 | if (nr != iovec.iov_len) |
| 2118 | break; |
| 2119 | iov_iter_advance(iter, nr); |
| 2120 | } |
| 2121 | |
| 2122 | return ret; |
| 2123 | } |
| 2124 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2125 | 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] | 2126 | struct iovec *iovec, struct iovec *fast_iov, |
| 2127 | struct iov_iter *iter) |
| 2128 | { |
| 2129 | req->io->rw.nr_segs = iter->nr_segs; |
| 2130 | req->io->rw.size = io_size; |
| 2131 | req->io->rw.iov = iovec; |
| 2132 | if (!req->io->rw.iov) { |
| 2133 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2134 | memcpy(req->io->rw.iov, fast_iov, |
| 2135 | sizeof(struct iovec) * iter->nr_segs); |
| 2136 | } |
| 2137 | } |
| 2138 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2139 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2140 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2141 | if (!io_op_defs[req->opcode].async_ctx) |
| 2142 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2143 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2144 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static void io_rw_async(struct io_wq_work **workptr) |
| 2148 | { |
| 2149 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2150 | struct iovec *iov = NULL; |
| 2151 | |
| 2152 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2153 | iov = req->io->rw.iov; |
| 2154 | io_wq_submit_work(workptr); |
| 2155 | kfree(iov); |
| 2156 | } |
| 2157 | |
| 2158 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2159 | struct iovec *iovec, struct iovec *fast_iov, |
| 2160 | struct iov_iter *iter) |
| 2161 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2162 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2163 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2164 | if (!req->io) { |
| 2165 | if (io_alloc_async_ctx(req)) |
| 2166 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2167 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2168 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2169 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2170 | req->work.func = io_rw_async; |
| 2171 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2174 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2175 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2176 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2177 | struct io_async_ctx *io; |
| 2178 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2179 | ssize_t ret; |
| 2180 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2181 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2182 | if (ret) |
| 2183 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2184 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2185 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2186 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2187 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2188 | if (!req->io) |
| 2189 | return 0; |
| 2190 | |
| 2191 | io = req->io; |
| 2192 | io->rw.iov = io->rw.fast_iov; |
| 2193 | req->io = NULL; |
| 2194 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2195 | req->io = io; |
| 2196 | if (ret < 0) |
| 2197 | return ret; |
| 2198 | |
| 2199 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2200 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2203 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2204 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2205 | { |
| 2206 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2207 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2208 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2209 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2210 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2211 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2212 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2213 | if (ret < 0) |
| 2214 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2215 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2216 | /* Ensure we clear previously set non-block flag */ |
| 2217 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2218 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2219 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2220 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2221 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2222 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2223 | req->result = io_size; |
| 2224 | |
| 2225 | /* |
| 2226 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2227 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2228 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2229 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2230 | req->flags |= REQ_F_MUST_PUNT; |
| 2231 | goto copy_iov; |
| 2232 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2233 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2234 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2235 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2236 | if (!ret) { |
| 2237 | ssize_t ret2; |
| 2238 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2239 | if (req->file->f_op->read_iter) |
| 2240 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2241 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2242 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2243 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2244 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2245 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2246 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2247 | } else { |
| 2248 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2249 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2250 | inline_vecs, &iter); |
| 2251 | if (ret) |
| 2252 | goto out_free; |
| 2253 | return -EAGAIN; |
| 2254 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2255 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2256 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2257 | if (!io_wq_current_is_worker()) |
| 2258 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2259 | return ret; |
| 2260 | } |
| 2261 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2262 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2263 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2264 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2265 | struct io_async_ctx *io; |
| 2266 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2267 | ssize_t ret; |
| 2268 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2269 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2270 | if (ret) |
| 2271 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2272 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2273 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2274 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2275 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2276 | if (!req->io) |
| 2277 | return 0; |
| 2278 | |
| 2279 | io = req->io; |
| 2280 | io->rw.iov = io->rw.fast_iov; |
| 2281 | req->io = NULL; |
| 2282 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2283 | req->io = io; |
| 2284 | if (ret < 0) |
| 2285 | return ret; |
| 2286 | |
| 2287 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2288 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2291 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2292 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2293 | { |
| 2294 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2295 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2296 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2297 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2298 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2299 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2300 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2301 | if (ret < 0) |
| 2302 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2303 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2304 | /* Ensure we clear previously set non-block flag */ |
| 2305 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2306 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2307 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2308 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2309 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2310 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2311 | req->result = io_size; |
| 2312 | |
| 2313 | /* |
| 2314 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2315 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2316 | */ |
| 2317 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2318 | req->flags |= REQ_F_MUST_PUNT; |
| 2319 | goto copy_iov; |
| 2320 | } |
| 2321 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2322 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2323 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2324 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2325 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2326 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2327 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2328 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2329 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2330 | ssize_t ret2; |
| 2331 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2332 | /* |
| 2333 | * Open-code file_start_write here to grab freeze protection, |
| 2334 | * which will be released by another thread in |
| 2335 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2336 | * released so that it doesn't complain about the held lock when |
| 2337 | * we return to userspace. |
| 2338 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2339 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2340 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2341 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2342 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2343 | SB_FREEZE_WRITE); |
| 2344 | } |
| 2345 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2346 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2347 | if (req->file->f_op->write_iter) |
| 2348 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2349 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2350 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2351 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2352 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2353 | } else { |
| 2354 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2355 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2356 | inline_vecs, &iter); |
| 2357 | if (ret) |
| 2358 | goto out_free; |
| 2359 | return -EAGAIN; |
| 2360 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2361 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2362 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2363 | if (!io_wq_current_is_worker()) |
| 2364 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2365 | return ret; |
| 2366 | } |
| 2367 | |
| 2368 | /* |
| 2369 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2370 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2371 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2372 | { |
| 2373 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2374 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2375 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2376 | return -EINVAL; |
| 2377 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2378 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2379 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2380 | return 0; |
| 2381 | } |
| 2382 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2383 | 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] | 2384 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2385 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2386 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2387 | if (!req->file) |
| 2388 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2389 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2390 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2391 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2392 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2393 | return -EINVAL; |
| 2394 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2395 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2396 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2397 | return -EINVAL; |
| 2398 | |
| 2399 | req->sync.off = READ_ONCE(sqe->off); |
| 2400 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2401 | return 0; |
| 2402 | } |
| 2403 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2404 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2405 | { |
| 2406 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2407 | req_set_fail_links(req); |
| 2408 | io_cqring_add_event(req, -ECANCELED); |
| 2409 | io_put_req(req); |
| 2410 | return true; |
| 2411 | } |
| 2412 | |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2416 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2417 | { |
| 2418 | struct io_wq_work *work = *workptr; |
| 2419 | struct io_kiocb *link = work->data; |
| 2420 | |
| 2421 | io_queue_linked_timeout(link); |
| 2422 | work->func = io_wq_submit_work; |
| 2423 | } |
| 2424 | |
| 2425 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2426 | { |
| 2427 | struct io_kiocb *link; |
| 2428 | |
| 2429 | io_prep_async_work(nxt, &link); |
| 2430 | *workptr = &nxt->work; |
| 2431 | if (link) { |
| 2432 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2433 | nxt->work.func = io_link_work_cb; |
| 2434 | nxt->work.data = link; |
| 2435 | } |
| 2436 | } |
| 2437 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2438 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2439 | { |
| 2440 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2441 | loff_t end = req->sync.off + req->sync.len; |
| 2442 | struct io_kiocb *nxt = NULL; |
| 2443 | int ret; |
| 2444 | |
| 2445 | if (io_req_cancelled(req)) |
| 2446 | return; |
| 2447 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2448 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2449 | end > 0 ? end : LLONG_MAX, |
| 2450 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2451 | if (ret < 0) |
| 2452 | req_set_fail_links(req); |
| 2453 | io_cqring_add_event(req, ret); |
| 2454 | io_put_req_find_next(req, &nxt); |
| 2455 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2456 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2459 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2460 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2461 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2462 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2463 | |
| 2464 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2465 | if (force_nonblock) { |
| 2466 | io_put_req(req); |
| 2467 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2468 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2469 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2470 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2471 | work = old_work = &req->work; |
| 2472 | io_fsync_finish(&work); |
| 2473 | if (work && work != old_work) |
| 2474 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2475 | return 0; |
| 2476 | } |
| 2477 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2478 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2479 | { |
| 2480 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2481 | struct io_kiocb *nxt = NULL; |
| 2482 | int ret; |
| 2483 | |
| 2484 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2485 | req->sync.len); |
| 2486 | if (ret < 0) |
| 2487 | req_set_fail_links(req); |
| 2488 | io_cqring_add_event(req, ret); |
| 2489 | io_put_req_find_next(req, &nxt); |
| 2490 | if (nxt) |
| 2491 | io_wq_assign_next(workptr, nxt); |
| 2492 | } |
| 2493 | |
| 2494 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2495 | const struct io_uring_sqe *sqe) |
| 2496 | { |
| 2497 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2498 | return -EINVAL; |
| 2499 | |
| 2500 | req->sync.off = READ_ONCE(sqe->off); |
| 2501 | req->sync.len = READ_ONCE(sqe->addr); |
| 2502 | req->sync.mode = READ_ONCE(sqe->len); |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2507 | bool force_nonblock) |
| 2508 | { |
| 2509 | struct io_wq_work *work, *old_work; |
| 2510 | |
| 2511 | /* fallocate always requiring blocking context */ |
| 2512 | if (force_nonblock) { |
| 2513 | io_put_req(req); |
| 2514 | req->work.func = io_fallocate_finish; |
| 2515 | return -EAGAIN; |
| 2516 | } |
| 2517 | |
| 2518 | work = old_work = &req->work; |
| 2519 | io_fallocate_finish(&work); |
| 2520 | if (work && work != old_work) |
| 2521 | *nxt = container_of(work, struct io_kiocb, work); |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2526 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2527 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2528 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2529 | int ret; |
| 2530 | |
| 2531 | if (sqe->ioprio || sqe->buf_index) |
| 2532 | return -EINVAL; |
| 2533 | |
| 2534 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2535 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2536 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2537 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2538 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2539 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2540 | if (IS_ERR(req->open.filename)) { |
| 2541 | ret = PTR_ERR(req->open.filename); |
| 2542 | req->open.filename = NULL; |
| 2543 | return ret; |
| 2544 | } |
| 2545 | |
| 2546 | return 0; |
| 2547 | } |
| 2548 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2549 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2550 | { |
| 2551 | struct open_how __user *how; |
| 2552 | const char __user *fname; |
| 2553 | size_t len; |
| 2554 | int ret; |
| 2555 | |
| 2556 | if (sqe->ioprio || sqe->buf_index) |
| 2557 | return -EINVAL; |
| 2558 | |
| 2559 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2560 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2561 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2562 | len = READ_ONCE(sqe->len); |
| 2563 | |
| 2564 | if (len < OPEN_HOW_SIZE_VER0) |
| 2565 | return -EINVAL; |
| 2566 | |
| 2567 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2568 | len); |
| 2569 | if (ret) |
| 2570 | return ret; |
| 2571 | |
| 2572 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2573 | req->open.how.flags |= O_LARGEFILE; |
| 2574 | |
| 2575 | req->open.filename = getname(fname); |
| 2576 | if (IS_ERR(req->open.filename)) { |
| 2577 | ret = PTR_ERR(req->open.filename); |
| 2578 | req->open.filename = NULL; |
| 2579 | return ret; |
| 2580 | } |
| 2581 | |
| 2582 | return 0; |
| 2583 | } |
| 2584 | |
| 2585 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2586 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2587 | { |
| 2588 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2589 | struct file *file; |
| 2590 | int ret; |
| 2591 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2592 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2593 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2594 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2595 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2596 | if (ret) |
| 2597 | goto err; |
| 2598 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2599 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2600 | if (ret < 0) |
| 2601 | goto err; |
| 2602 | |
| 2603 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2604 | if (IS_ERR(file)) { |
| 2605 | put_unused_fd(ret); |
| 2606 | ret = PTR_ERR(file); |
| 2607 | } else { |
| 2608 | fsnotify_open(file); |
| 2609 | fd_install(ret, file); |
| 2610 | } |
| 2611 | err: |
| 2612 | putname(req->open.filename); |
| 2613 | if (ret < 0) |
| 2614 | req_set_fail_links(req); |
| 2615 | io_cqring_add_event(req, ret); |
| 2616 | io_put_req_find_next(req, nxt); |
| 2617 | return 0; |
| 2618 | } |
| 2619 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2620 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2621 | bool force_nonblock) |
| 2622 | { |
| 2623 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2624 | return io_openat2(req, nxt, force_nonblock); |
| 2625 | } |
| 2626 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 2627 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 2628 | const struct io_uring_sqe *sqe) |
| 2629 | { |
| 2630 | #if defined(CONFIG_EPOLL) |
| 2631 | if (sqe->ioprio || sqe->buf_index) |
| 2632 | return -EINVAL; |
| 2633 | |
| 2634 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 2635 | req->epoll.op = READ_ONCE(sqe->len); |
| 2636 | req->epoll.fd = READ_ONCE(sqe->off); |
| 2637 | |
| 2638 | if (ep_op_has_event(req->epoll.op)) { |
| 2639 | struct epoll_event __user *ev; |
| 2640 | |
| 2641 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2642 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 2643 | return -EFAULT; |
| 2644 | } |
| 2645 | |
| 2646 | return 0; |
| 2647 | #else |
| 2648 | return -EOPNOTSUPP; |
| 2649 | #endif |
| 2650 | } |
| 2651 | |
| 2652 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2653 | bool force_nonblock) |
| 2654 | { |
| 2655 | #if defined(CONFIG_EPOLL) |
| 2656 | struct io_epoll *ie = &req->epoll; |
| 2657 | int ret; |
| 2658 | |
| 2659 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 2660 | if (force_nonblock && ret == -EAGAIN) |
| 2661 | return -EAGAIN; |
| 2662 | |
| 2663 | if (ret < 0) |
| 2664 | req_set_fail_links(req); |
| 2665 | io_cqring_add_event(req, ret); |
| 2666 | io_put_req_find_next(req, nxt); |
| 2667 | return 0; |
| 2668 | #else |
| 2669 | return -EOPNOTSUPP; |
| 2670 | #endif |
| 2671 | } |
| 2672 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2673 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2674 | { |
| 2675 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2676 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2677 | return -EINVAL; |
| 2678 | |
| 2679 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2680 | req->madvise.len = READ_ONCE(sqe->len); |
| 2681 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2682 | return 0; |
| 2683 | #else |
| 2684 | return -EOPNOTSUPP; |
| 2685 | #endif |
| 2686 | } |
| 2687 | |
| 2688 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2689 | bool force_nonblock) |
| 2690 | { |
| 2691 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2692 | struct io_madvise *ma = &req->madvise; |
| 2693 | int ret; |
| 2694 | |
| 2695 | if (force_nonblock) |
| 2696 | return -EAGAIN; |
| 2697 | |
| 2698 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2699 | if (ret < 0) |
| 2700 | req_set_fail_links(req); |
| 2701 | io_cqring_add_event(req, ret); |
| 2702 | io_put_req_find_next(req, nxt); |
| 2703 | return 0; |
| 2704 | #else |
| 2705 | return -EOPNOTSUPP; |
| 2706 | #endif |
| 2707 | } |
| 2708 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2709 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2710 | { |
| 2711 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2712 | return -EINVAL; |
| 2713 | |
| 2714 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2715 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2716 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
| 2720 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2721 | bool force_nonblock) |
| 2722 | { |
| 2723 | struct io_fadvise *fa = &req->fadvise; |
| 2724 | int ret; |
| 2725 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 2726 | if (force_nonblock) { |
| 2727 | switch (fa->advice) { |
| 2728 | case POSIX_FADV_NORMAL: |
| 2729 | case POSIX_FADV_RANDOM: |
| 2730 | case POSIX_FADV_SEQUENTIAL: |
| 2731 | break; |
| 2732 | default: |
| 2733 | return -EAGAIN; |
| 2734 | } |
| 2735 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2736 | |
| 2737 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2738 | if (ret < 0) |
| 2739 | req_set_fail_links(req); |
| 2740 | io_cqring_add_event(req, ret); |
| 2741 | io_put_req_find_next(req, nxt); |
| 2742 | return 0; |
| 2743 | } |
| 2744 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2745 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2746 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2747 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2748 | unsigned lookup_flags; |
| 2749 | int ret; |
| 2750 | |
| 2751 | if (sqe->ioprio || sqe->buf_index) |
| 2752 | return -EINVAL; |
| 2753 | |
| 2754 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2755 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2756 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2757 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2758 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2759 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2760 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2761 | return -EINVAL; |
| 2762 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2763 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2764 | if (IS_ERR(req->open.filename)) { |
| 2765 | ret = PTR_ERR(req->open.filename); |
| 2766 | req->open.filename = NULL; |
| 2767 | return ret; |
| 2768 | } |
| 2769 | |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
| 2773 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2774 | bool force_nonblock) |
| 2775 | { |
| 2776 | struct io_open *ctx = &req->open; |
| 2777 | unsigned lookup_flags; |
| 2778 | struct path path; |
| 2779 | struct kstat stat; |
| 2780 | int ret; |
| 2781 | |
| 2782 | if (force_nonblock) |
| 2783 | return -EAGAIN; |
| 2784 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2785 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2786 | return -EINVAL; |
| 2787 | |
| 2788 | retry: |
| 2789 | /* filename_lookup() drops it, keep a reference */ |
| 2790 | ctx->filename->refcnt++; |
| 2791 | |
| 2792 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2793 | NULL); |
| 2794 | if (ret) |
| 2795 | goto err; |
| 2796 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2797 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2798 | path_put(&path); |
| 2799 | if (retry_estale(ret, lookup_flags)) { |
| 2800 | lookup_flags |= LOOKUP_REVAL; |
| 2801 | goto retry; |
| 2802 | } |
| 2803 | if (!ret) |
| 2804 | ret = cp_statx(&stat, ctx->buffer); |
| 2805 | err: |
| 2806 | putname(ctx->filename); |
| 2807 | if (ret < 0) |
| 2808 | req_set_fail_links(req); |
| 2809 | io_cqring_add_event(req, ret); |
| 2810 | io_put_req_find_next(req, nxt); |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2814 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2815 | { |
| 2816 | /* |
| 2817 | * If we queue this for async, it must not be cancellable. That would |
| 2818 | * leave the 'file' in an undeterminate state. |
| 2819 | */ |
| 2820 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2821 | |
| 2822 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2823 | sqe->rw_flags || sqe->buf_index) |
| 2824 | return -EINVAL; |
| 2825 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2826 | return -EINVAL; |
| 2827 | |
| 2828 | req->close.fd = READ_ONCE(sqe->fd); |
| 2829 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2830 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2831 | return -EBADF; |
| 2832 | |
| 2833 | return 0; |
| 2834 | } |
| 2835 | |
| 2836 | static void io_close_finish(struct io_wq_work **workptr) |
| 2837 | { |
| 2838 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2839 | struct io_kiocb *nxt = NULL; |
| 2840 | |
| 2841 | /* Invoked with files, we need to do the close */ |
| 2842 | if (req->work.files) { |
| 2843 | int ret; |
| 2844 | |
| 2845 | ret = filp_close(req->close.put_file, req->work.files); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 2846 | if (ret < 0) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2847 | req_set_fail_links(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2848 | io_cqring_add_event(req, ret); |
| 2849 | } |
| 2850 | |
| 2851 | fput(req->close.put_file); |
| 2852 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2853 | io_put_req_find_next(req, &nxt); |
| 2854 | if (nxt) |
| 2855 | io_wq_assign_next(workptr, nxt); |
| 2856 | } |
| 2857 | |
| 2858 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2859 | bool force_nonblock) |
| 2860 | { |
| 2861 | int ret; |
| 2862 | |
| 2863 | req->close.put_file = NULL; |
| 2864 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2865 | if (ret < 0) |
| 2866 | return ret; |
| 2867 | |
| 2868 | /* 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] | 2869 | 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] | 2870 | goto eagain; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2871 | |
| 2872 | /* |
| 2873 | * No ->flush(), safely close from here and just punt the |
| 2874 | * fput() to async context. |
| 2875 | */ |
| 2876 | ret = filp_close(req->close.put_file, current->files); |
| 2877 | |
| 2878 | if (ret < 0) |
| 2879 | req_set_fail_links(req); |
| 2880 | io_cqring_add_event(req, ret); |
| 2881 | |
| 2882 | if (io_wq_current_is_worker()) { |
| 2883 | struct io_wq_work *old_work, *work; |
| 2884 | |
| 2885 | old_work = work = &req->work; |
| 2886 | io_close_finish(&work); |
| 2887 | if (work && work != old_work) |
| 2888 | *nxt = container_of(work, struct io_kiocb, work); |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
| 2892 | eagain: |
| 2893 | req->work.func = io_close_finish; |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 2894 | /* |
| 2895 | * Do manual async queue here to avoid grabbing files - we don't |
| 2896 | * need the files, and it'll cause io_close_finish() to close |
| 2897 | * the file again and cause a double CQE entry for this request |
| 2898 | */ |
| 2899 | io_queue_async_work(req); |
| 2900 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2903 | 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] | 2904 | { |
| 2905 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2906 | |
| 2907 | if (!req->file) |
| 2908 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2909 | |
| 2910 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2911 | return -EINVAL; |
| 2912 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2913 | return -EINVAL; |
| 2914 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2915 | req->sync.off = READ_ONCE(sqe->off); |
| 2916 | req->sync.len = READ_ONCE(sqe->len); |
| 2917 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2918 | return 0; |
| 2919 | } |
| 2920 | |
| 2921 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2922 | { |
| 2923 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2924 | struct io_kiocb *nxt = NULL; |
| 2925 | int ret; |
| 2926 | |
| 2927 | if (io_req_cancelled(req)) |
| 2928 | return; |
| 2929 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2930 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2931 | req->sync.flags); |
| 2932 | if (ret < 0) |
| 2933 | req_set_fail_links(req); |
| 2934 | io_cqring_add_event(req, ret); |
| 2935 | io_put_req_find_next(req, &nxt); |
| 2936 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2937 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2938 | } |
| 2939 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2940 | 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] | 2941 | bool force_nonblock) |
| 2942 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2943 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2944 | |
| 2945 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2946 | if (force_nonblock) { |
| 2947 | io_put_req(req); |
| 2948 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2949 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2950 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2951 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2952 | work = old_work = &req->work; |
| 2953 | io_sync_file_range_finish(&work); |
| 2954 | if (work && work != old_work) |
| 2955 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2956 | return 0; |
| 2957 | } |
| 2958 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2959 | #if defined(CONFIG_NET) |
| 2960 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2961 | { |
| 2962 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2963 | struct iovec *iov = NULL; |
| 2964 | |
| 2965 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2966 | iov = req->io->msg.iov; |
| 2967 | io_wq_submit_work(workptr); |
| 2968 | kfree(iov); |
| 2969 | } |
| 2970 | #endif |
| 2971 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2972 | 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] | 2973 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2974 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2975 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2976 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2977 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2978 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2979 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2980 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2981 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2982 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2983 | return 0; |
| 2984 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2985 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2986 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2987 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2988 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2989 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2990 | #endif |
| 2991 | } |
| 2992 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2993 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2994 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2995 | { |
| 2996 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2997 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2998 | struct socket *sock; |
| 2999 | int ret; |
| 3000 | |
| 3001 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3002 | return -EINVAL; |
| 3003 | |
| 3004 | sock = sock_from_file(req->file, &ret); |
| 3005 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3006 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3007 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3008 | unsigned flags; |
| 3009 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3010 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3011 | kmsg = &req->io->msg; |
| 3012 | kmsg->msg.msg_name = &addr; |
| 3013 | /* if iov is set, it's allocated already */ |
| 3014 | if (!kmsg->iov) |
| 3015 | kmsg->iov = kmsg->fast_iov; |
| 3016 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3017 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3018 | struct io_sr_msg *sr = &req->sr_msg; |
| 3019 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3020 | kmsg = &io.msg; |
| 3021 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3022 | |
| 3023 | io.msg.iov = io.msg.fast_iov; |
| 3024 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3025 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3026 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3027 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3030 | flags = req->sr_msg.msg_flags; |
| 3031 | if (flags & MSG_DONTWAIT) |
| 3032 | req->flags |= REQ_F_NOWAIT; |
| 3033 | else if (force_nonblock) |
| 3034 | flags |= MSG_DONTWAIT; |
| 3035 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3036 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3037 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3038 | if (req->io) |
| 3039 | return -EAGAIN; |
| 3040 | if (io_alloc_async_ctx(req)) |
| 3041 | return -ENOMEM; |
| 3042 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3043 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3044 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3045 | } |
| 3046 | if (ret == -ERESTARTSYS) |
| 3047 | ret = -EINTR; |
| 3048 | } |
| 3049 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3050 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3051 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3052 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3053 | if (ret < 0) |
| 3054 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3055 | io_put_req_find_next(req, nxt); |
| 3056 | return 0; |
| 3057 | #else |
| 3058 | return -EOPNOTSUPP; |
| 3059 | #endif |
| 3060 | } |
| 3061 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3062 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3063 | bool force_nonblock) |
| 3064 | { |
| 3065 | #if defined(CONFIG_NET) |
| 3066 | struct socket *sock; |
| 3067 | int ret; |
| 3068 | |
| 3069 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3070 | return -EINVAL; |
| 3071 | |
| 3072 | sock = sock_from_file(req->file, &ret); |
| 3073 | if (sock) { |
| 3074 | struct io_sr_msg *sr = &req->sr_msg; |
| 3075 | struct msghdr msg; |
| 3076 | struct iovec iov; |
| 3077 | unsigned flags; |
| 3078 | |
| 3079 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3080 | &msg.msg_iter); |
| 3081 | if (ret) |
| 3082 | return ret; |
| 3083 | |
| 3084 | msg.msg_name = NULL; |
| 3085 | msg.msg_control = NULL; |
| 3086 | msg.msg_controllen = 0; |
| 3087 | msg.msg_namelen = 0; |
| 3088 | |
| 3089 | flags = req->sr_msg.msg_flags; |
| 3090 | if (flags & MSG_DONTWAIT) |
| 3091 | req->flags |= REQ_F_NOWAIT; |
| 3092 | else if (force_nonblock) |
| 3093 | flags |= MSG_DONTWAIT; |
| 3094 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3095 | msg.msg_flags = flags; |
| 3096 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3097 | if (force_nonblock && ret == -EAGAIN) |
| 3098 | return -EAGAIN; |
| 3099 | if (ret == -ERESTARTSYS) |
| 3100 | ret = -EINTR; |
| 3101 | } |
| 3102 | |
| 3103 | io_cqring_add_event(req, ret); |
| 3104 | if (ret < 0) |
| 3105 | req_set_fail_links(req); |
| 3106 | io_put_req_find_next(req, nxt); |
| 3107 | return 0; |
| 3108 | #else |
| 3109 | return -EOPNOTSUPP; |
| 3110 | #endif |
| 3111 | } |
| 3112 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3113 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3114 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3115 | { |
| 3116 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3117 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3118 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3119 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3120 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3121 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3122 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3123 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3124 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3125 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3126 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3127 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3128 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3129 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3130 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3131 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3132 | #endif |
| 3133 | } |
| 3134 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3135 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3136 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3137 | { |
| 3138 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3139 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3140 | struct socket *sock; |
| 3141 | int ret; |
| 3142 | |
| 3143 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3144 | return -EINVAL; |
| 3145 | |
| 3146 | sock = sock_from_file(req->file, &ret); |
| 3147 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3148 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3149 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3150 | unsigned flags; |
| 3151 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3152 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3153 | kmsg = &req->io->msg; |
| 3154 | kmsg->msg.msg_name = &addr; |
| 3155 | /* if iov is set, it's allocated already */ |
| 3156 | if (!kmsg->iov) |
| 3157 | kmsg->iov = kmsg->fast_iov; |
| 3158 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3159 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3160 | struct io_sr_msg *sr = &req->sr_msg; |
| 3161 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3162 | kmsg = &io.msg; |
| 3163 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3164 | |
| 3165 | io.msg.iov = io.msg.fast_iov; |
| 3166 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3167 | sr->msg_flags, &io.msg.uaddr, |
| 3168 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3169 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3170 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3171 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3172 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3173 | flags = req->sr_msg.msg_flags; |
| 3174 | if (flags & MSG_DONTWAIT) |
| 3175 | req->flags |= REQ_F_NOWAIT; |
| 3176 | else if (force_nonblock) |
| 3177 | flags |= MSG_DONTWAIT; |
| 3178 | |
| 3179 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3180 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3181 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3182 | if (req->io) |
| 3183 | return -EAGAIN; |
| 3184 | if (io_alloc_async_ctx(req)) |
| 3185 | return -ENOMEM; |
| 3186 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3187 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3188 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3189 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3190 | if (ret == -ERESTARTSYS) |
| 3191 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3192 | } |
| 3193 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3194 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3195 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3196 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3197 | if (ret < 0) |
| 3198 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3199 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3200 | return 0; |
| 3201 | #else |
| 3202 | return -EOPNOTSUPP; |
| 3203 | #endif |
| 3204 | } |
| 3205 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3206 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3207 | bool force_nonblock) |
| 3208 | { |
| 3209 | #if defined(CONFIG_NET) |
| 3210 | struct socket *sock; |
| 3211 | int ret; |
| 3212 | |
| 3213 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3214 | return -EINVAL; |
| 3215 | |
| 3216 | sock = sock_from_file(req->file, &ret); |
| 3217 | if (sock) { |
| 3218 | struct io_sr_msg *sr = &req->sr_msg; |
| 3219 | struct msghdr msg; |
| 3220 | struct iovec iov; |
| 3221 | unsigned flags; |
| 3222 | |
| 3223 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3224 | &msg.msg_iter); |
| 3225 | if (ret) |
| 3226 | return ret; |
| 3227 | |
| 3228 | msg.msg_name = NULL; |
| 3229 | msg.msg_control = NULL; |
| 3230 | msg.msg_controllen = 0; |
| 3231 | msg.msg_namelen = 0; |
| 3232 | msg.msg_iocb = NULL; |
| 3233 | msg.msg_flags = 0; |
| 3234 | |
| 3235 | flags = req->sr_msg.msg_flags; |
| 3236 | if (flags & MSG_DONTWAIT) |
| 3237 | req->flags |= REQ_F_NOWAIT; |
| 3238 | else if (force_nonblock) |
| 3239 | flags |= MSG_DONTWAIT; |
| 3240 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3241 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3242 | if (force_nonblock && ret == -EAGAIN) |
| 3243 | return -EAGAIN; |
| 3244 | if (ret == -ERESTARTSYS) |
| 3245 | ret = -EINTR; |
| 3246 | } |
| 3247 | |
| 3248 | io_cqring_add_event(req, ret); |
| 3249 | if (ret < 0) |
| 3250 | req_set_fail_links(req); |
| 3251 | io_put_req_find_next(req, nxt); |
| 3252 | return 0; |
| 3253 | #else |
| 3254 | return -EOPNOTSUPP; |
| 3255 | #endif |
| 3256 | } |
| 3257 | |
| 3258 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3259 | 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] | 3260 | { |
| 3261 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3262 | struct io_accept *accept = &req->accept; |
| 3263 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3264 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3265 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3266 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3267 | return -EINVAL; |
| 3268 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3269 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3270 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3271 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3272 | return 0; |
| 3273 | #else |
| 3274 | return -EOPNOTSUPP; |
| 3275 | #endif |
| 3276 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3277 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3278 | #if defined(CONFIG_NET) |
| 3279 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3280 | bool force_nonblock) |
| 3281 | { |
| 3282 | struct io_accept *accept = &req->accept; |
| 3283 | unsigned file_flags; |
| 3284 | int ret; |
| 3285 | |
| 3286 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3287 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3288 | accept->addr_len, accept->flags); |
| 3289 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3290 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3291 | if (ret == -ERESTARTSYS) |
| 3292 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3293 | if (ret < 0) |
| 3294 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3295 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3296 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3297 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3301 | { |
| 3302 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3303 | struct io_kiocb *nxt = NULL; |
| 3304 | |
| 3305 | if (io_req_cancelled(req)) |
| 3306 | return; |
| 3307 | __io_accept(req, &nxt, false); |
| 3308 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3309 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3310 | } |
| 3311 | #endif |
| 3312 | |
| 3313 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3314 | bool force_nonblock) |
| 3315 | { |
| 3316 | #if defined(CONFIG_NET) |
| 3317 | int ret; |
| 3318 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3319 | ret = __io_accept(req, nxt, force_nonblock); |
| 3320 | if (ret == -EAGAIN && force_nonblock) { |
| 3321 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3322 | io_put_req(req); |
| 3323 | return -EAGAIN; |
| 3324 | } |
| 3325 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3326 | #else |
| 3327 | return -EOPNOTSUPP; |
| 3328 | #endif |
| 3329 | } |
| 3330 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3331 | 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] | 3332 | { |
| 3333 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3334 | struct io_connect *conn = &req->connect; |
| 3335 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3336 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3337 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3338 | return -EINVAL; |
| 3339 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3340 | return -EINVAL; |
| 3341 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3342 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3343 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3344 | |
| 3345 | if (!io) |
| 3346 | return 0; |
| 3347 | |
| 3348 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3349 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3350 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3351 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3352 | #endif |
| 3353 | } |
| 3354 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3355 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3356 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3357 | { |
| 3358 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3359 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3360 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3361 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3362 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3363 | if (req->io) { |
| 3364 | io = req->io; |
| 3365 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3366 | ret = move_addr_to_kernel(req->connect.addr, |
| 3367 | req->connect.addr_len, |
| 3368 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3369 | if (ret) |
| 3370 | goto out; |
| 3371 | io = &__io; |
| 3372 | } |
| 3373 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3374 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3375 | |
| 3376 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3377 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3378 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3379 | if (req->io) |
| 3380 | return -EAGAIN; |
| 3381 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3382 | ret = -ENOMEM; |
| 3383 | goto out; |
| 3384 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3385 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3386 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3387 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3388 | if (ret == -ERESTARTSYS) |
| 3389 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3390 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3391 | if (ret < 0) |
| 3392 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3393 | io_cqring_add_event(req, ret); |
| 3394 | io_put_req_find_next(req, nxt); |
| 3395 | return 0; |
| 3396 | #else |
| 3397 | return -EOPNOTSUPP; |
| 3398 | #endif |
| 3399 | } |
| 3400 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3401 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3402 | { |
| 3403 | struct io_poll_iocb *poll = &req->poll; |
| 3404 | |
| 3405 | spin_lock(&poll->head->lock); |
| 3406 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3407 | if (!list_empty(&poll->wait.entry)) { |
| 3408 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3409 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3410 | } |
| 3411 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3412 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3416 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3417 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3418 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3419 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3420 | |
| 3421 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3422 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3423 | struct hlist_head *list; |
| 3424 | |
| 3425 | list = &ctx->cancel_hash[i]; |
| 3426 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3427 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3428 | } |
| 3429 | spin_unlock_irq(&ctx->completion_lock); |
| 3430 | } |
| 3431 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3432 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3433 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3434 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3435 | struct io_kiocb *req; |
| 3436 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3437 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3438 | hlist_for_each_entry(req, list, hash_node) { |
| 3439 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3440 | io_poll_remove_one(req); |
| 3441 | return 0; |
| 3442 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | return -ENOENT; |
| 3446 | } |
| 3447 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3448 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3449 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3450 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3451 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3452 | return -EINVAL; |
| 3453 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3454 | sqe->poll_events) |
| 3455 | return -EINVAL; |
| 3456 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3457 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3458 | return 0; |
| 3459 | } |
| 3460 | |
| 3461 | /* |
| 3462 | * Find a running poll command that matches one specified in sqe->addr, |
| 3463 | * and remove it if found. |
| 3464 | */ |
| 3465 | static int io_poll_remove(struct io_kiocb *req) |
| 3466 | { |
| 3467 | struct io_ring_ctx *ctx = req->ctx; |
| 3468 | u64 addr; |
| 3469 | int ret; |
| 3470 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3471 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3472 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3473 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3474 | spin_unlock_irq(&ctx->completion_lock); |
| 3475 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3476 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3477 | if (ret < 0) |
| 3478 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3479 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3480 | return 0; |
| 3481 | } |
| 3482 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3483 | 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] | 3484 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3485 | struct io_ring_ctx *ctx = req->ctx; |
| 3486 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3487 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3488 | if (error) |
| 3489 | io_cqring_fill_event(req, error); |
| 3490 | else |
| 3491 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3492 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3495 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3496 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3497 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3498 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3499 | struct io_poll_iocb *poll = &req->poll; |
| 3500 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3501 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3502 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3503 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3504 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3505 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3506 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3507 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3508 | ret = -ECANCELED; |
| 3509 | } else if (READ_ONCE(poll->canceled)) { |
| 3510 | ret = -ECANCELED; |
| 3511 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3512 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3513 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3514 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3515 | |
| 3516 | /* |
| 3517 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3518 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3519 | * synchronize with them. In the cancellation case the list_del_init |
| 3520 | * itself is not actually needed, but harmless so we keep it in to |
| 3521 | * avoid further branches in the fast path. |
| 3522 | */ |
| 3523 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3524 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3525 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3526 | spin_unlock_irq(&ctx->completion_lock); |
| 3527 | return; |
| 3528 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3529 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3530 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3531 | spin_unlock_irq(&ctx->completion_lock); |
| 3532 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3533 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3534 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3535 | if (ret < 0) |
| 3536 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3537 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3538 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3539 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3542 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3543 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3544 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3545 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3546 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3547 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3548 | spin_lock_irq(&ctx->completion_lock); |
| 3549 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3550 | hash_del(&req->hash_node); |
| 3551 | io_poll_complete(req, req->result, 0); |
| 3552 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3553 | if (refcount_dec_and_test(&req->refs) && |
| 3554 | !io_req_multi_free(&rb, req)) { |
| 3555 | req->flags |= REQ_F_COMP_LOCKED; |
| 3556 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | spin_unlock_irq(&ctx->completion_lock); |
| 3560 | |
| 3561 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3562 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3566 | { |
| 3567 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3568 | struct llist_node *nodes; |
| 3569 | |
| 3570 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3571 | if (nodes) |
| 3572 | __io_poll_flush(req->ctx, nodes); |
| 3573 | } |
| 3574 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3575 | static void io_poll_trigger_evfd(struct io_wq_work **workptr) |
| 3576 | { |
| 3577 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3578 | |
| 3579 | eventfd_signal(req->ctx->cq_ev_fd, 1); |
| 3580 | io_put_req(req); |
| 3581 | } |
| 3582 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3583 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3584 | void *key) |
| 3585 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3586 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3587 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3588 | struct io_ring_ctx *ctx = req->ctx; |
| 3589 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3590 | |
| 3591 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3592 | if (mask && !(mask & poll->events)) |
| 3593 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3594 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3595 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3596 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3597 | /* |
| 3598 | * Run completion inline if we can. We're using trylock here because |
| 3599 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3600 | * If we have a link timeout we're going to need the completion_lock |
| 3601 | * for finalizing the request, mark us as having grabbed that already. |
| 3602 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3603 | if (mask) { |
| 3604 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3605 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3606 | if (llist_empty(&ctx->poll_llist) && |
| 3607 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3608 | bool trigger_ev; |
| 3609 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3610 | hash_del(&req->hash_node); |
| 3611 | io_poll_complete(req, mask, 0); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3612 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3613 | trigger_ev = io_should_trigger_evfd(ctx); |
| 3614 | if (trigger_ev && eventfd_signal_count()) { |
| 3615 | trigger_ev = false; |
| 3616 | req->work.func = io_poll_trigger_evfd; |
| 3617 | } else { |
| 3618 | req->flags |= REQ_F_COMP_LOCKED; |
| 3619 | io_put_req(req); |
| 3620 | req = NULL; |
| 3621 | } |
| 3622 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3623 | __io_cqring_ev_posted(ctx, trigger_ev); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3624 | } else { |
| 3625 | req->result = mask; |
| 3626 | req->llist_node.next = NULL; |
| 3627 | /* if the list wasn't empty, we're done */ |
| 3628 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3629 | req = NULL; |
| 3630 | else |
| 3631 | req->work.func = io_poll_flush; |
| 3632 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3633 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3634 | if (req) |
| 3635 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3636 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3637 | return 1; |
| 3638 | } |
| 3639 | |
| 3640 | struct io_poll_table { |
| 3641 | struct poll_table_struct pt; |
| 3642 | struct io_kiocb *req; |
| 3643 | int error; |
| 3644 | }; |
| 3645 | |
| 3646 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3647 | struct poll_table_struct *p) |
| 3648 | { |
| 3649 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3650 | |
| 3651 | if (unlikely(pt->req->poll.head)) { |
| 3652 | pt->error = -EINVAL; |
| 3653 | return; |
| 3654 | } |
| 3655 | |
| 3656 | pt->error = 0; |
| 3657 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3658 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3661 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3662 | { |
| 3663 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3664 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3665 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3666 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3667 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3670 | 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] | 3671 | { |
| 3672 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3673 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3674 | |
| 3675 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3676 | return -EINVAL; |
| 3677 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3678 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3679 | if (!poll->file) |
| 3680 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3681 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3682 | events = READ_ONCE(sqe->poll_events); |
| 3683 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3684 | return 0; |
| 3685 | } |
| 3686 | |
| 3687 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3688 | { |
| 3689 | struct io_poll_iocb *poll = &req->poll; |
| 3690 | struct io_ring_ctx *ctx = req->ctx; |
| 3691 | struct io_poll_table ipt; |
| 3692 | bool cancel = false; |
| 3693 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3694 | |
| 3695 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3696 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3697 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3698 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3699 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3700 | poll->canceled = false; |
| 3701 | |
| 3702 | ipt.pt._qproc = io_poll_queue_proc; |
| 3703 | ipt.pt._key = poll->events; |
| 3704 | ipt.req = req; |
| 3705 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3706 | |
| 3707 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3708 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3709 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3710 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3711 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3712 | INIT_LIST_HEAD(&req->list); |
| 3713 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3714 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3715 | |
| 3716 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3717 | if (likely(poll->head)) { |
| 3718 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3719 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3720 | if (ipt.error) |
| 3721 | cancel = true; |
| 3722 | ipt.error = 0; |
| 3723 | mask = 0; |
| 3724 | } |
| 3725 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3726 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3727 | else if (cancel) |
| 3728 | WRITE_ONCE(poll->canceled, true); |
| 3729 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3730 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3731 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3732 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3733 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3734 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3735 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3736 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3737 | spin_unlock_irq(&ctx->completion_lock); |
| 3738 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3739 | if (mask) { |
| 3740 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3741 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3742 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3743 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3746 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3747 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3748 | struct io_timeout_data *data = container_of(timer, |
| 3749 | struct io_timeout_data, timer); |
| 3750 | struct io_kiocb *req = data->req; |
| 3751 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3752 | unsigned long flags; |
| 3753 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3754 | atomic_inc(&ctx->cq_timeouts); |
| 3755 | |
| 3756 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3757 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3758 | * We could be racing with timeout deletion. If the list is empty, |
| 3759 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3760 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3761 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3762 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3763 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3764 | /* |
| 3765 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3766 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3767 | * pointer will be increased, otherwise other timeout reqs may |
| 3768 | * return in advance without waiting for enough wait_nr. |
| 3769 | */ |
| 3770 | prev = req; |
| 3771 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3772 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3773 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3774 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3775 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3776 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3777 | io_commit_cqring(ctx); |
| 3778 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3779 | |
| 3780 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3781 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3782 | io_put_req(req); |
| 3783 | return HRTIMER_NORESTART; |
| 3784 | } |
| 3785 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3786 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3787 | { |
| 3788 | struct io_kiocb *req; |
| 3789 | int ret = -ENOENT; |
| 3790 | |
| 3791 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3792 | if (user_data == req->user_data) { |
| 3793 | list_del_init(&req->list); |
| 3794 | ret = 0; |
| 3795 | break; |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | if (ret == -ENOENT) |
| 3800 | return ret; |
| 3801 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3802 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3803 | if (ret == -1) |
| 3804 | return -EALREADY; |
| 3805 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3806 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3807 | io_cqring_fill_event(req, -ECANCELED); |
| 3808 | io_put_req(req); |
| 3809 | return 0; |
| 3810 | } |
| 3811 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3812 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3813 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3814 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3815 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3816 | return -EINVAL; |
| 3817 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3818 | return -EINVAL; |
| 3819 | |
| 3820 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3821 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3822 | if (req->timeout.flags) |
| 3823 | return -EINVAL; |
| 3824 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3825 | return 0; |
| 3826 | } |
| 3827 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3828 | /* |
| 3829 | * Remove or update an existing timeout command |
| 3830 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3831 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3832 | { |
| 3833 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3834 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3835 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3836 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3837 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3838 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3839 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3840 | io_commit_cqring(ctx); |
| 3841 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3842 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3843 | if (ret < 0) |
| 3844 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3845 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3846 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3847 | } |
| 3848 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3849 | 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] | 3850 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3851 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3852 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3853 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3854 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3855 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3856 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3857 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3858 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3859 | if (sqe->off && is_timeout_link) |
| 3860 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3861 | flags = READ_ONCE(sqe->timeout_flags); |
| 3862 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3863 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3864 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3865 | req->timeout.count = READ_ONCE(sqe->off); |
| 3866 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3867 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3868 | return -ENOMEM; |
| 3869 | |
| 3870 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3871 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3872 | req->flags |= REQ_F_TIMEOUT; |
| 3873 | |
| 3874 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3875 | return -EFAULT; |
| 3876 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3877 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3878 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3879 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3880 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3881 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3882 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3883 | return 0; |
| 3884 | } |
| 3885 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3886 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3887 | { |
| 3888 | unsigned count; |
| 3889 | struct io_ring_ctx *ctx = req->ctx; |
| 3890 | struct io_timeout_data *data; |
| 3891 | struct list_head *entry; |
| 3892 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3893 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3894 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3895 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3896 | /* |
| 3897 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3898 | * timeout event to be satisfied. If it isn't set, then this is |
| 3899 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3900 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3901 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3902 | if (!count) { |
| 3903 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3904 | spin_lock_irq(&ctx->completion_lock); |
| 3905 | entry = ctx->timeout_list.prev; |
| 3906 | goto add; |
| 3907 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3908 | |
| 3909 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3910 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3911 | |
| 3912 | /* |
| 3913 | * Insertion sort, ensuring the first entry in the list is always |
| 3914 | * the one we need first. |
| 3915 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3916 | spin_lock_irq(&ctx->completion_lock); |
| 3917 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3918 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3919 | unsigned nxt_sq_head; |
| 3920 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3921 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3922 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3923 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3924 | continue; |
| 3925 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3926 | /* |
| 3927 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3928 | * long to store it. |
| 3929 | */ |
| 3930 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3931 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3932 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3933 | |
| 3934 | /* |
| 3935 | * cached_sq_head may overflow, and it will never overflow twice |
| 3936 | * once there is some timeout req still be valid. |
| 3937 | */ |
| 3938 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3939 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3940 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3941 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3942 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3943 | |
| 3944 | /* |
| 3945 | * Sequence of reqs after the insert one and itself should |
| 3946 | * be adjusted because each timeout req consumes a slot. |
| 3947 | */ |
| 3948 | span++; |
| 3949 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3950 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3951 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3952 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3953 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3954 | data->timer.function = io_timeout_fn; |
| 3955 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3956 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3957 | return 0; |
| 3958 | } |
| 3959 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3960 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3961 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3962 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3963 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3964 | return req->user_data == (unsigned long) data; |
| 3965 | } |
| 3966 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3967 | 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] | 3968 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3969 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3970 | int ret = 0; |
| 3971 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3972 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3973 | switch (cancel_ret) { |
| 3974 | case IO_WQ_CANCEL_OK: |
| 3975 | ret = 0; |
| 3976 | break; |
| 3977 | case IO_WQ_CANCEL_RUNNING: |
| 3978 | ret = -EALREADY; |
| 3979 | break; |
| 3980 | case IO_WQ_CANCEL_NOTFOUND: |
| 3981 | ret = -ENOENT; |
| 3982 | break; |
| 3983 | } |
| 3984 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3985 | return ret; |
| 3986 | } |
| 3987 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3988 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3989 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3990 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3991 | { |
| 3992 | unsigned long flags; |
| 3993 | int ret; |
| 3994 | |
| 3995 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3996 | if (ret != -ENOENT) { |
| 3997 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3998 | goto done; |
| 3999 | } |
| 4000 | |
| 4001 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4002 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4003 | if (ret != -ENOENT) |
| 4004 | goto done; |
| 4005 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4006 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4007 | if (!ret) |
| 4008 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4009 | io_cqring_fill_event(req, ret); |
| 4010 | io_commit_cqring(ctx); |
| 4011 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4012 | io_cqring_ev_posted(ctx); |
| 4013 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4014 | if (ret < 0) |
| 4015 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4016 | io_put_req_find_next(req, nxt); |
| 4017 | } |
| 4018 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4019 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4020 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4021 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4022 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4023 | return -EINVAL; |
| 4024 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4025 | sqe->cancel_flags) |
| 4026 | return -EINVAL; |
| 4027 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4028 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4029 | return 0; |
| 4030 | } |
| 4031 | |
| 4032 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4033 | { |
| 4034 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4035 | |
| 4036 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4037 | return 0; |
| 4038 | } |
| 4039 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4040 | static int io_files_update_prep(struct io_kiocb *req, |
| 4041 | const struct io_uring_sqe *sqe) |
| 4042 | { |
| 4043 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4044 | return -EINVAL; |
| 4045 | |
| 4046 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4047 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4048 | if (!req->files_update.nr_args) |
| 4049 | return -EINVAL; |
| 4050 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4051 | return 0; |
| 4052 | } |
| 4053 | |
| 4054 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4055 | { |
| 4056 | struct io_ring_ctx *ctx = req->ctx; |
| 4057 | struct io_uring_files_update up; |
| 4058 | int ret; |
| 4059 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4060 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4061 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4062 | |
| 4063 | up.offset = req->files_update.offset; |
| 4064 | up.fds = req->files_update.arg; |
| 4065 | |
| 4066 | mutex_lock(&ctx->uring_lock); |
| 4067 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4068 | mutex_unlock(&ctx->uring_lock); |
| 4069 | |
| 4070 | if (ret < 0) |
| 4071 | req_set_fail_links(req); |
| 4072 | io_cqring_add_event(req, ret); |
| 4073 | io_put_req(req); |
| 4074 | return 0; |
| 4075 | } |
| 4076 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4077 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4078 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4079 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4080 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4081 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4082 | if (io_op_defs[req->opcode].file_table) { |
| 4083 | ret = io_grab_files(req); |
| 4084 | if (unlikely(ret)) |
| 4085 | return ret; |
| 4086 | } |
| 4087 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4088 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4089 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4090 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4091 | case IORING_OP_NOP: |
| 4092 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4093 | case IORING_OP_READV: |
| 4094 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4095 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4096 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4097 | break; |
| 4098 | case IORING_OP_WRITEV: |
| 4099 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4100 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4101 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4102 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4103 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4104 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4105 | break; |
| 4106 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4107 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4108 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4109 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4110 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4111 | break; |
| 4112 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4113 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4114 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4115 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4116 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4117 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4118 | break; |
| 4119 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4120 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4121 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4122 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4123 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4124 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4125 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4126 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4127 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4128 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4129 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4130 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4131 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4132 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4133 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4134 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4135 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4136 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4137 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4138 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4139 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4140 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4141 | case IORING_OP_FALLOCATE: |
| 4142 | ret = io_fallocate_prep(req, sqe); |
| 4143 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4144 | case IORING_OP_OPENAT: |
| 4145 | ret = io_openat_prep(req, sqe); |
| 4146 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4147 | case IORING_OP_CLOSE: |
| 4148 | ret = io_close_prep(req, sqe); |
| 4149 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4150 | case IORING_OP_FILES_UPDATE: |
| 4151 | ret = io_files_update_prep(req, sqe); |
| 4152 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4153 | case IORING_OP_STATX: |
| 4154 | ret = io_statx_prep(req, sqe); |
| 4155 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4156 | case IORING_OP_FADVISE: |
| 4157 | ret = io_fadvise_prep(req, sqe); |
| 4158 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4159 | case IORING_OP_MADVISE: |
| 4160 | ret = io_madvise_prep(req, sqe); |
| 4161 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4162 | case IORING_OP_OPENAT2: |
| 4163 | ret = io_openat2_prep(req, sqe); |
| 4164 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4165 | case IORING_OP_EPOLL_CTL: |
| 4166 | ret = io_epoll_ctl_prep(req, sqe); |
| 4167 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4168 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4169 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4170 | req->opcode); |
| 4171 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4172 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4173 | } |
| 4174 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4175 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4178 | 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] | 4179 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4180 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4181 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4182 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4183 | /* Still need defer if there is pending req in defer list. */ |
| 4184 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4185 | return 0; |
| 4186 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4187 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4188 | return -EAGAIN; |
| 4189 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4190 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4191 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4192 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4193 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4194 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4195 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4196 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4197 | return 0; |
| 4198 | } |
| 4199 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4200 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4201 | list_add_tail(&req->list, &ctx->defer_list); |
| 4202 | spin_unlock_irq(&ctx->completion_lock); |
| 4203 | return -EIOCBQUEUED; |
| 4204 | } |
| 4205 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4206 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4207 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4208 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4209 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4210 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4211 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4212 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4213 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4214 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4215 | break; |
| 4216 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4217 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4218 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4219 | if (sqe) { |
| 4220 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4221 | if (ret < 0) |
| 4222 | break; |
| 4223 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4224 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4225 | break; |
| 4226 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4227 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4228 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4229 | if (sqe) { |
| 4230 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4231 | if (ret < 0) |
| 4232 | break; |
| 4233 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4234 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4235 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4236 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4237 | if (sqe) { |
| 4238 | ret = io_prep_fsync(req, sqe); |
| 4239 | if (ret < 0) |
| 4240 | break; |
| 4241 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4242 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4243 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4244 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4245 | if (sqe) { |
| 4246 | ret = io_poll_add_prep(req, sqe); |
| 4247 | if (ret) |
| 4248 | break; |
| 4249 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4250 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4251 | break; |
| 4252 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4253 | if (sqe) { |
| 4254 | ret = io_poll_remove_prep(req, sqe); |
| 4255 | if (ret < 0) |
| 4256 | break; |
| 4257 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4258 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4259 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4260 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4261 | if (sqe) { |
| 4262 | ret = io_prep_sfr(req, sqe); |
| 4263 | if (ret < 0) |
| 4264 | break; |
| 4265 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4266 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4267 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4268 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4269 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4270 | if (sqe) { |
| 4271 | ret = io_sendmsg_prep(req, sqe); |
| 4272 | if (ret < 0) |
| 4273 | break; |
| 4274 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4275 | if (req->opcode == IORING_OP_SENDMSG) |
| 4276 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4277 | else |
| 4278 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4279 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4280 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4281 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4282 | if (sqe) { |
| 4283 | ret = io_recvmsg_prep(req, sqe); |
| 4284 | if (ret) |
| 4285 | break; |
| 4286 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4287 | if (req->opcode == IORING_OP_RECVMSG) |
| 4288 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4289 | else |
| 4290 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4291 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4292 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4293 | if (sqe) { |
| 4294 | ret = io_timeout_prep(req, sqe, false); |
| 4295 | if (ret) |
| 4296 | break; |
| 4297 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4298 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4299 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4300 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4301 | if (sqe) { |
| 4302 | ret = io_timeout_remove_prep(req, sqe); |
| 4303 | if (ret) |
| 4304 | break; |
| 4305 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4306 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4307 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4308 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4309 | if (sqe) { |
| 4310 | ret = io_accept_prep(req, sqe); |
| 4311 | if (ret) |
| 4312 | break; |
| 4313 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4314 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4315 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4316 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4317 | if (sqe) { |
| 4318 | ret = io_connect_prep(req, sqe); |
| 4319 | if (ret) |
| 4320 | break; |
| 4321 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4322 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4323 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4324 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4325 | if (sqe) { |
| 4326 | ret = io_async_cancel_prep(req, sqe); |
| 4327 | if (ret) |
| 4328 | break; |
| 4329 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4330 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4331 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4332 | case IORING_OP_FALLOCATE: |
| 4333 | if (sqe) { |
| 4334 | ret = io_fallocate_prep(req, sqe); |
| 4335 | if (ret) |
| 4336 | break; |
| 4337 | } |
| 4338 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4339 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4340 | case IORING_OP_OPENAT: |
| 4341 | if (sqe) { |
| 4342 | ret = io_openat_prep(req, sqe); |
| 4343 | if (ret) |
| 4344 | break; |
| 4345 | } |
| 4346 | ret = io_openat(req, nxt, force_nonblock); |
| 4347 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4348 | case IORING_OP_CLOSE: |
| 4349 | if (sqe) { |
| 4350 | ret = io_close_prep(req, sqe); |
| 4351 | if (ret) |
| 4352 | break; |
| 4353 | } |
| 4354 | ret = io_close(req, nxt, force_nonblock); |
| 4355 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4356 | case IORING_OP_FILES_UPDATE: |
| 4357 | if (sqe) { |
| 4358 | ret = io_files_update_prep(req, sqe); |
| 4359 | if (ret) |
| 4360 | break; |
| 4361 | } |
| 4362 | ret = io_files_update(req, force_nonblock); |
| 4363 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4364 | case IORING_OP_STATX: |
| 4365 | if (sqe) { |
| 4366 | ret = io_statx_prep(req, sqe); |
| 4367 | if (ret) |
| 4368 | break; |
| 4369 | } |
| 4370 | ret = io_statx(req, nxt, force_nonblock); |
| 4371 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4372 | case IORING_OP_FADVISE: |
| 4373 | if (sqe) { |
| 4374 | ret = io_fadvise_prep(req, sqe); |
| 4375 | if (ret) |
| 4376 | break; |
| 4377 | } |
| 4378 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4379 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4380 | case IORING_OP_MADVISE: |
| 4381 | if (sqe) { |
| 4382 | ret = io_madvise_prep(req, sqe); |
| 4383 | if (ret) |
| 4384 | break; |
| 4385 | } |
| 4386 | ret = io_madvise(req, nxt, force_nonblock); |
| 4387 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4388 | case IORING_OP_OPENAT2: |
| 4389 | if (sqe) { |
| 4390 | ret = io_openat2_prep(req, sqe); |
| 4391 | if (ret) |
| 4392 | break; |
| 4393 | } |
| 4394 | ret = io_openat2(req, nxt, force_nonblock); |
| 4395 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4396 | case IORING_OP_EPOLL_CTL: |
| 4397 | if (sqe) { |
| 4398 | ret = io_epoll_ctl_prep(req, sqe); |
| 4399 | if (ret) |
| 4400 | break; |
| 4401 | } |
| 4402 | ret = io_epoll_ctl(req, nxt, force_nonblock); |
| 4403 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4404 | default: |
| 4405 | ret = -EINVAL; |
| 4406 | break; |
| 4407 | } |
| 4408 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4409 | if (ret) |
| 4410 | return ret; |
| 4411 | |
| 4412 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4413 | const bool in_async = io_wq_current_is_worker(); |
| 4414 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4415 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4416 | return -EAGAIN; |
| 4417 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4418 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4419 | if (in_async) |
| 4420 | mutex_lock(&ctx->uring_lock); |
| 4421 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4422 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4423 | |
| 4424 | if (in_async) |
| 4425 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4429 | } |
| 4430 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4431 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4432 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4433 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4434 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4435 | struct io_kiocb *nxt = NULL; |
| 4436 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4437 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4438 | /* if NO_CANCEL is set, we must still run the work */ |
| 4439 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4440 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4441 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4442 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4443 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4444 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4445 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4446 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4447 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4448 | /* |
| 4449 | * We can get EAGAIN for polled IO even though we're |
| 4450 | * forcing a sync submission from here, since we can't |
| 4451 | * wait for request slots on the block side. |
| 4452 | */ |
| 4453 | if (ret != -EAGAIN) |
| 4454 | break; |
| 4455 | cond_resched(); |
| 4456 | } while (1); |
| 4457 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4458 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4459 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4460 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4461 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4462 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4463 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4464 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4465 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4466 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4467 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4468 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4469 | if (!ret && nxt) |
| 4470 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4471 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4472 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4473 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4474 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4475 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4476 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4477 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4478 | return 0; |
| 4479 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4480 | } |
| 4481 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4482 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4483 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4484 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4485 | struct fixed_file_table *table; |
| 4486 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4487 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4488 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4489 | } |
| 4490 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4491 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4492 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4493 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4494 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4495 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4496 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4497 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4498 | flags = READ_ONCE(sqe->flags); |
| 4499 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4500 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4501 | if (!io_req_needs_file(req, fd)) |
| 4502 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4503 | |
| 4504 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4505 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4506 | (unsigned) fd >= ctx->nr_user_files)) |
| 4507 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4508 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4509 | req->file = io_file_from_index(ctx, fd); |
| 4510 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4511 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4512 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4513 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4514 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4515 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4516 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4517 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4518 | req->file = io_file_get(state, fd); |
| 4519 | if (unlikely(!req->file)) |
| 4520 | return -EBADF; |
| 4521 | } |
| 4522 | |
| 4523 | return 0; |
| 4524 | } |
| 4525 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4526 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4527 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4528 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4529 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4530 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4531 | if (req->work.files) |
| 4532 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4533 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4534 | return -EBADF; |
| 4535 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4536 | rcu_read_lock(); |
| 4537 | spin_lock_irq(&ctx->inflight_lock); |
| 4538 | /* |
| 4539 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4540 | * out work accessing these files if the fd is closed. Check if |
| 4541 | * the fd has changed since we started down this path, and disallow |
| 4542 | * this operation if it has. |
| 4543 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4544 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4545 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4546 | req->flags |= REQ_F_INFLIGHT; |
| 4547 | req->work.files = current->files; |
| 4548 | ret = 0; |
| 4549 | } |
| 4550 | spin_unlock_irq(&ctx->inflight_lock); |
| 4551 | rcu_read_unlock(); |
| 4552 | |
| 4553 | return ret; |
| 4554 | } |
| 4555 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4556 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4557 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4558 | struct io_timeout_data *data = container_of(timer, |
| 4559 | struct io_timeout_data, timer); |
| 4560 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4561 | struct io_ring_ctx *ctx = req->ctx; |
| 4562 | struct io_kiocb *prev = NULL; |
| 4563 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4564 | |
| 4565 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4566 | |
| 4567 | /* |
| 4568 | * We don't expect the list to be empty, that will only happen if we |
| 4569 | * race with the completion of the linked work. |
| 4570 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4571 | if (!list_empty(&req->link_list)) { |
| 4572 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4573 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4574 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4575 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4576 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4577 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4578 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4582 | |
| 4583 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4584 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4585 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4586 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4587 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4588 | } else { |
| 4589 | io_cqring_add_event(req, -ETIME); |
| 4590 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4591 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4592 | return HRTIMER_NORESTART; |
| 4593 | } |
| 4594 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4595 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4596 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4597 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4598 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4599 | /* |
| 4600 | * If the list is now empty, then our linked request finished before |
| 4601 | * we got a chance to setup the timer |
| 4602 | */ |
| 4603 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4604 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4605 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4606 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4607 | data->timer.function = io_link_timeout_fn; |
| 4608 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4609 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4610 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4611 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4612 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4613 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4614 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4615 | } |
| 4616 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4617 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4618 | { |
| 4619 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4620 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4621 | if (!(req->flags & REQ_F_LINK)) |
| 4622 | return NULL; |
| 4623 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4624 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4625 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4626 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4627 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4628 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4629 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4630 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4633 | 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] | 4634 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4635 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4636 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4637 | int ret; |
| 4638 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4639 | again: |
| 4640 | linked_timeout = io_prep_linked_timeout(req); |
| 4641 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4642 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4643 | |
| 4644 | /* |
| 4645 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4646 | * doesn't support non-blocking read/write attempts |
| 4647 | */ |
| 4648 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4649 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4650 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4651 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4652 | ret = io_grab_files(req); |
| 4653 | if (ret) |
| 4654 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4655 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4656 | |
| 4657 | /* |
| 4658 | * Queued up for async execution, worker will release |
| 4659 | * submit reference when the iocb is actually submitted. |
| 4660 | */ |
| 4661 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4662 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4663 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4664 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4665 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4666 | /* drop submission reference */ |
| 4667 | io_put_req(req); |
| 4668 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4669 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4670 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4671 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4672 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4673 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4674 | } |
| 4675 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4676 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4677 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4678 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4679 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4680 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4681 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4682 | done_req: |
| 4683 | if (nxt) { |
| 4684 | req = nxt; |
| 4685 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4686 | |
| 4687 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4688 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4689 | goto again; |
| 4690 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4691 | } |
| 4692 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4693 | 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] | 4694 | { |
| 4695 | int ret; |
| 4696 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4697 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4698 | if (ret) { |
| 4699 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4700 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4701 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4702 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4703 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4704 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4705 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4706 | ret = io_req_defer_prep(req, sqe); |
| 4707 | if (unlikely(ret < 0)) |
| 4708 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4709 | /* |
| 4710 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4711 | * to async execution. |
| 4712 | */ |
| 4713 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4714 | io_queue_async_work(req); |
| 4715 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4716 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4717 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4718 | } |
| 4719 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4720 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4721 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4722 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4723 | io_cqring_add_event(req, -ECANCELED); |
| 4724 | io_double_put_req(req); |
| 4725 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4726 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4727 | } |
| 4728 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4729 | #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] | 4730 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4731 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4732 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4733 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4734 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4735 | const struct cred *old_creds = NULL; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4736 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4737 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4738 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4739 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4740 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4741 | |
| 4742 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4743 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4744 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4745 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4746 | } |
| 4747 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4748 | id = READ_ONCE(sqe->personality); |
| 4749 | if (id) { |
| 4750 | const struct cred *personality_creds; |
| 4751 | |
| 4752 | personality_creds = idr_find(&ctx->personality_idr, id); |
| 4753 | if (unlikely(!personality_creds)) { |
| 4754 | ret = -EINVAL; |
| 4755 | goto err_req; |
| 4756 | } |
| 4757 | old_creds = override_creds(personality_creds); |
| 4758 | } |
| 4759 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4760 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 4761 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| |
| 4762 | IOSQE_ASYNC); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4763 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4764 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4765 | if (unlikely(ret)) { |
| 4766 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4767 | io_cqring_add_event(req, ret); |
| 4768 | io_double_put_req(req); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4769 | if (old_creds) |
| 4770 | revert_creds(old_creds); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4771 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4772 | } |
| 4773 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4774 | /* |
| 4775 | * If we already have a head request, queue this one for async |
| 4776 | * submittal once the head completes. If we don't have a head but |
| 4777 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4778 | * submitted sync once the chain is complete. If none of those |
| 4779 | * conditions are true (normal request), then just queue it. |
| 4780 | */ |
| 4781 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4782 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4783 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 4784 | /* |
| 4785 | * Taking sequential execution of a link, draining both sides |
| 4786 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 4787 | * requests in the link. So, it drains the head and the |
| 4788 | * next after the link request. The last one is done via |
| 4789 | * drain_next flag to persist the effect across calls. |
| 4790 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4791 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4792 | head->flags |= REQ_F_IO_DRAIN; |
| 4793 | ctx->drain_next = 1; |
| 4794 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4795 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4796 | ret = -EAGAIN; |
| 4797 | goto err_req; |
| 4798 | } |
| 4799 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4800 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4801 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4802 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4803 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4804 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4805 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4806 | trace_io_uring_link(ctx, req, head); |
| 4807 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4808 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4809 | /* last request of a link, enqueue the link */ |
| 4810 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4811 | io_queue_link_head(head); |
| 4812 | *link = NULL; |
| 4813 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4814 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4815 | if (unlikely(ctx->drain_next)) { |
| 4816 | req->flags |= REQ_F_IO_DRAIN; |
| 4817 | req->ctx->drain_next = 0; |
| 4818 | } |
| 4819 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4820 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4821 | INIT_LIST_HEAD(&req->link_list); |
| 4822 | ret = io_req_defer_prep(req, sqe); |
| 4823 | if (ret) |
| 4824 | req->flags |= REQ_F_FAIL_LINK; |
| 4825 | *link = req; |
| 4826 | } else { |
| 4827 | io_queue_sqe(req, sqe); |
| 4828 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4829 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4830 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4831 | if (old_creds) |
| 4832 | revert_creds(old_creds); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4833 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4834 | } |
| 4835 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4836 | /* |
| 4837 | * Batched submission is done, ensure local IO is flushed out. |
| 4838 | */ |
| 4839 | static void io_submit_state_end(struct io_submit_state *state) |
| 4840 | { |
| 4841 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4842 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4843 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 4844 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | /* |
| 4848 | * Start submission side cache. |
| 4849 | */ |
| 4850 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4851 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4852 | { |
| 4853 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4854 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4855 | state->file = NULL; |
| 4856 | state->ios_left = max_ios; |
| 4857 | } |
| 4858 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4859 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4860 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4861 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4862 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4863 | /* |
| 4864 | * Ensure any loads from the SQEs are done at this point, |
| 4865 | * since once we write the new head, the application could |
| 4866 | * write new data to them. |
| 4867 | */ |
| 4868 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4872 | * 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] | 4873 | * that is mapped by userspace. This means that care needs to be taken to |
| 4874 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4875 | * being a good citizen. If members of the sqe are validated and then later |
| 4876 | * used, it's important that those reads are done through READ_ONCE() to |
| 4877 | * prevent a re-load down the line. |
| 4878 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4879 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4880 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4881 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4882 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4883 | unsigned head; |
| 4884 | |
| 4885 | /* |
| 4886 | * The cached sq head (or cq tail) serves two purposes: |
| 4887 | * |
| 4888 | * 1) allows us to batch the cost of updating the user visible |
| 4889 | * head updates. |
| 4890 | * 2) allows the kernel side to track the head on its own, even |
| 4891 | * though the application is the one updating it. |
| 4892 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4893 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4894 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4895 | /* |
| 4896 | * All io need record the previous position, if LINK vs DARIN, |
| 4897 | * it can be used to mark the position of the first IO in the |
| 4898 | * link list. |
| 4899 | */ |
| 4900 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4901 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4902 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4903 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4904 | ctx->cached_sq_head++; |
| 4905 | return true; |
| 4906 | } |
| 4907 | |
| 4908 | /* drop invalid entries */ |
| 4909 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4910 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4911 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4912 | return false; |
| 4913 | } |
| 4914 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4915 | 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] | 4916 | struct file *ring_file, int ring_fd, |
| 4917 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4918 | { |
| 4919 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4920 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4921 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4922 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4923 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4924 | /* 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] | 4925 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4926 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4927 | !io_cqring_overflow_flush(ctx, false)) |
| 4928 | return -EBUSY; |
| 4929 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4930 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4931 | /* make sure SQ entry isn't read before tail */ |
| 4932 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4933 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4934 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4935 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4936 | |
| 4937 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4938 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4939 | statep = &state; |
| 4940 | } |
| 4941 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4942 | ctx->ring_fd = ring_fd; |
| 4943 | ctx->ring_file = ring_file; |
| 4944 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4945 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4946 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4947 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 4948 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4949 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4950 | req = io_get_req(ctx, statep); |
| 4951 | if (unlikely(!req)) { |
| 4952 | if (!submitted) |
| 4953 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4954 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4955 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4956 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4957 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4958 | break; |
| 4959 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4960 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4961 | /* will complete beyond this point, count as submitted */ |
| 4962 | submitted++; |
| 4963 | |
| 4964 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 4965 | err = -EINVAL; |
| 4966 | fail_req: |
| 4967 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4968 | io_double_put_req(req); |
| 4969 | break; |
| 4970 | } |
| 4971 | |
| 4972 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4973 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 4974 | if (unlikely(mm_fault)) { |
| 4975 | err = -EFAULT; |
| 4976 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4977 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 4978 | use_mm(ctx->sqo_mm); |
| 4979 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4980 | } |
| 4981 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4982 | req->in_async = async; |
| 4983 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 4984 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 4985 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4986 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4987 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 4990 | if (unlikely(submitted != nr)) { |
| 4991 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 4992 | |
| 4993 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 4994 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4995 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4996 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4997 | if (statep) |
| 4998 | io_submit_state_end(&state); |
| 4999 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5000 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5001 | io_commit_sqring(ctx); |
| 5002 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5003 | return submitted; |
| 5004 | } |
| 5005 | |
| 5006 | static int io_sq_thread(void *data) |
| 5007 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5008 | struct io_ring_ctx *ctx = data; |
| 5009 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5010 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5011 | mm_segment_t old_fs; |
| 5012 | DEFINE_WAIT(wait); |
| 5013 | unsigned inflight; |
| 5014 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5015 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5016 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5017 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5018 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5019 | old_fs = get_fs(); |
| 5020 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5021 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5022 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5023 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5024 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5025 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5026 | |
| 5027 | if (inflight) { |
| 5028 | unsigned nr_events = 0; |
| 5029 | |
| 5030 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 5031 | /* |
| 5032 | * inflight is the count of the maximum possible |
| 5033 | * entries we submitted, but it can be smaller |
| 5034 | * if we dropped some of them. If we don't have |
| 5035 | * poll entries available, then we know that we |
| 5036 | * have nothing left to poll for. Reset the |
| 5037 | * inflight count to zero in that case. |
| 5038 | */ |
| 5039 | mutex_lock(&ctx->uring_lock); |
| 5040 | if (!list_empty(&ctx->poll_list)) |
| 5041 | __io_iopoll_check(ctx, &nr_events, 0); |
| 5042 | else |
| 5043 | inflight = 0; |
| 5044 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5045 | } else { |
| 5046 | /* |
| 5047 | * Normal IO, just pretend everything completed. |
| 5048 | * We don't have to poll completions for that. |
| 5049 | */ |
| 5050 | nr_events = inflight; |
| 5051 | } |
| 5052 | |
| 5053 | inflight -= nr_events; |
| 5054 | if (!inflight) |
| 5055 | timeout = jiffies + ctx->sq_thread_idle; |
| 5056 | } |
| 5057 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5058 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5059 | |
| 5060 | /* |
| 5061 | * If submit got -EBUSY, flag us as needing the application |
| 5062 | * to enter the kernel to reap and flush events. |
| 5063 | */ |
| 5064 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5065 | /* |
| 5066 | * We're polling. If we're within the defined idle |
| 5067 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5068 | * to sleep. The exception is if we got EBUSY doing |
| 5069 | * more IO, we should wait for the application to |
| 5070 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5071 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5072 | if (inflight || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5073 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5074 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5075 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5076 | continue; |
| 5077 | } |
| 5078 | |
| 5079 | /* |
| 5080 | * Drop cur_mm before scheduling, we can't hold it for |
| 5081 | * long periods (or over schedule()). Do this before |
| 5082 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5083 | * may sleep. |
| 5084 | */ |
| 5085 | if (cur_mm) { |
| 5086 | unuse_mm(cur_mm); |
| 5087 | mmput(cur_mm); |
| 5088 | cur_mm = NULL; |
| 5089 | } |
| 5090 | |
| 5091 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5092 | TASK_INTERRUPTIBLE); |
| 5093 | |
| 5094 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5095 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5096 | /* make sure to read SQ tail after writing flags */ |
| 5097 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5098 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5099 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5100 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5101 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5102 | finish_wait(&ctx->sqo_wait, &wait); |
| 5103 | break; |
| 5104 | } |
| 5105 | if (signal_pending(current)) |
| 5106 | flush_signals(current); |
| 5107 | schedule(); |
| 5108 | finish_wait(&ctx->sqo_wait, &wait); |
| 5109 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5110 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5111 | continue; |
| 5112 | } |
| 5113 | finish_wait(&ctx->sqo_wait, &wait); |
| 5114 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5115 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5118 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5119 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5120 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5121 | if (ret > 0) |
| 5122 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | set_fs(old_fs); |
| 5126 | if (cur_mm) { |
| 5127 | unuse_mm(cur_mm); |
| 5128 | mmput(cur_mm); |
| 5129 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5130 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5131 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5132 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5133 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5134 | return 0; |
| 5135 | } |
| 5136 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5137 | struct io_wait_queue { |
| 5138 | struct wait_queue_entry wq; |
| 5139 | struct io_ring_ctx *ctx; |
| 5140 | unsigned to_wait; |
| 5141 | unsigned nr_timeouts; |
| 5142 | }; |
| 5143 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5144 | 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] | 5145 | { |
| 5146 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5147 | |
| 5148 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5149 | * 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] | 5150 | * started waiting. For timeouts, we always want to return to userspace, |
| 5151 | * regardless of event count. |
| 5152 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5153 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5154 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5155 | } |
| 5156 | |
| 5157 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5158 | int wake_flags, void *key) |
| 5159 | { |
| 5160 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5161 | wq); |
| 5162 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5163 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5164 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5165 | return -1; |
| 5166 | |
| 5167 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5168 | } |
| 5169 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5170 | /* |
| 5171 | * Wait until events become available, if we don't already have some. The |
| 5172 | * application must reap them itself, as they reside on the shared cq ring. |
| 5173 | */ |
| 5174 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5175 | const sigset_t __user *sig, size_t sigsz) |
| 5176 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5177 | struct io_wait_queue iowq = { |
| 5178 | .wq = { |
| 5179 | .private = current, |
| 5180 | .func = io_wake_function, |
| 5181 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5182 | }, |
| 5183 | .ctx = ctx, |
| 5184 | .to_wait = min_events, |
| 5185 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5186 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5187 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5188 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5189 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5190 | return 0; |
| 5191 | |
| 5192 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5193 | #ifdef CONFIG_COMPAT |
| 5194 | if (in_compat_syscall()) |
| 5195 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5196 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5197 | else |
| 5198 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5199 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5200 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5201 | if (ret) |
| 5202 | return ret; |
| 5203 | } |
| 5204 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5205 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5206 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5207 | do { |
| 5208 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5209 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5210 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5211 | break; |
| 5212 | schedule(); |
| 5213 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5214 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5215 | break; |
| 5216 | } |
| 5217 | } while (1); |
| 5218 | finish_wait(&ctx->wait, &iowq.wq); |
| 5219 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5220 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5221 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5222 | 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] | 5223 | } |
| 5224 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5225 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5226 | { |
| 5227 | #if defined(CONFIG_UNIX) |
| 5228 | if (ctx->ring_sock) { |
| 5229 | struct sock *sock = ctx->ring_sock->sk; |
| 5230 | struct sk_buff *skb; |
| 5231 | |
| 5232 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5233 | kfree_skb(skb); |
| 5234 | } |
| 5235 | #else |
| 5236 | int i; |
| 5237 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5238 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5239 | struct file *file; |
| 5240 | |
| 5241 | file = io_file_from_index(ctx, i); |
| 5242 | if (file) |
| 5243 | fput(file); |
| 5244 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5245 | #endif |
| 5246 | } |
| 5247 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5248 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5249 | { |
| 5250 | struct fixed_file_data *data; |
| 5251 | |
| 5252 | data = container_of(ref, struct fixed_file_data, refs); |
| 5253 | complete(&data->done); |
| 5254 | } |
| 5255 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5256 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5257 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5258 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5259 | unsigned nr_tables, i; |
| 5260 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5261 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5262 | return -ENXIO; |
| 5263 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5264 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5265 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5266 | wait_for_completion(&data->done); |
| 5267 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5268 | percpu_ref_exit(&data->refs); |
| 5269 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5270 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5271 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5272 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5273 | kfree(data->table[i].files); |
| 5274 | kfree(data->table); |
| 5275 | kfree(data); |
| 5276 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5277 | ctx->nr_user_files = 0; |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5281 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5282 | { |
| 5283 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5284 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5285 | /* |
| 5286 | * The park is a bit of a work-around, without it we get |
| 5287 | * warning spews on shutdown with SQPOLL set and affinity |
| 5288 | * set to a single CPU. |
| 5289 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5290 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5291 | kthread_stop(ctx->sqo_thread); |
| 5292 | ctx->sqo_thread = NULL; |
| 5293 | } |
| 5294 | } |
| 5295 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5296 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5297 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5298 | io_sq_thread_stop(ctx); |
| 5299 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5300 | if (ctx->io_wq) { |
| 5301 | io_wq_destroy(ctx->io_wq); |
| 5302 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5303 | } |
| 5304 | } |
| 5305 | |
| 5306 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5307 | /* |
| 5308 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5309 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5310 | * loops in the file referencing. |
| 5311 | */ |
| 5312 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5313 | { |
| 5314 | struct sock *sk = ctx->ring_sock->sk; |
| 5315 | struct scm_fp_list *fpl; |
| 5316 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5317 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5318 | |
| 5319 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5320 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5321 | |
| 5322 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5323 | return -EMFILE; |
| 5324 | } |
| 5325 | |
| 5326 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5327 | if (!fpl) |
| 5328 | return -ENOMEM; |
| 5329 | |
| 5330 | skb = alloc_skb(0, GFP_KERNEL); |
| 5331 | if (!skb) { |
| 5332 | kfree(fpl); |
| 5333 | return -ENOMEM; |
| 5334 | } |
| 5335 | |
| 5336 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5337 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5338 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5339 | fpl->user = get_uid(ctx->user); |
| 5340 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5341 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5342 | |
| 5343 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5344 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5345 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5346 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5347 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5348 | } |
| 5349 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5350 | if (nr_files) { |
| 5351 | fpl->max = SCM_MAX_FD; |
| 5352 | fpl->count = nr_files; |
| 5353 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5354 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5355 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5356 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5357 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5358 | for (i = 0; i < nr_files; i++) |
| 5359 | fput(fpl->fp[i]); |
| 5360 | } else { |
| 5361 | kfree_skb(skb); |
| 5362 | kfree(fpl); |
| 5363 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5364 | |
| 5365 | return 0; |
| 5366 | } |
| 5367 | |
| 5368 | /* |
| 5369 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5370 | * causes regular reference counting to break down. We rely on the UNIX |
| 5371 | * garbage collection to take care of this problem for us. |
| 5372 | */ |
| 5373 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5374 | { |
| 5375 | unsigned left, total; |
| 5376 | int ret = 0; |
| 5377 | |
| 5378 | total = 0; |
| 5379 | left = ctx->nr_user_files; |
| 5380 | while (left) { |
| 5381 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5382 | |
| 5383 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5384 | if (ret) |
| 5385 | break; |
| 5386 | left -= this_files; |
| 5387 | total += this_files; |
| 5388 | } |
| 5389 | |
| 5390 | if (!ret) |
| 5391 | return 0; |
| 5392 | |
| 5393 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5394 | struct file *file = io_file_from_index(ctx, total); |
| 5395 | |
| 5396 | if (file) |
| 5397 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5398 | total++; |
| 5399 | } |
| 5400 | |
| 5401 | return ret; |
| 5402 | } |
| 5403 | #else |
| 5404 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5405 | { |
| 5406 | return 0; |
| 5407 | } |
| 5408 | #endif |
| 5409 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5410 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5411 | unsigned nr_files) |
| 5412 | { |
| 5413 | int i; |
| 5414 | |
| 5415 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5416 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5417 | unsigned this_files; |
| 5418 | |
| 5419 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5420 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5421 | GFP_KERNEL); |
| 5422 | if (!table->files) |
| 5423 | break; |
| 5424 | nr_files -= this_files; |
| 5425 | } |
| 5426 | |
| 5427 | if (i == nr_tables) |
| 5428 | return 0; |
| 5429 | |
| 5430 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5431 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5432 | kfree(table->files); |
| 5433 | } |
| 5434 | return 1; |
| 5435 | } |
| 5436 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5437 | 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] | 5438 | { |
| 5439 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5440 | struct sock *sock = ctx->ring_sock->sk; |
| 5441 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5442 | struct sk_buff *skb; |
| 5443 | int i; |
| 5444 | |
| 5445 | __skb_queue_head_init(&list); |
| 5446 | |
| 5447 | /* |
| 5448 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5449 | * remove this entry and rearrange the file array. |
| 5450 | */ |
| 5451 | skb = skb_dequeue(head); |
| 5452 | while (skb) { |
| 5453 | struct scm_fp_list *fp; |
| 5454 | |
| 5455 | fp = UNIXCB(skb).fp; |
| 5456 | for (i = 0; i < fp->count; i++) { |
| 5457 | int left; |
| 5458 | |
| 5459 | if (fp->fp[i] != file) |
| 5460 | continue; |
| 5461 | |
| 5462 | unix_notinflight(fp->user, fp->fp[i]); |
| 5463 | left = fp->count - 1 - i; |
| 5464 | if (left) { |
| 5465 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5466 | left * sizeof(struct file *)); |
| 5467 | } |
| 5468 | fp->count--; |
| 5469 | if (!fp->count) { |
| 5470 | kfree_skb(skb); |
| 5471 | skb = NULL; |
| 5472 | } else { |
| 5473 | __skb_queue_tail(&list, skb); |
| 5474 | } |
| 5475 | fput(file); |
| 5476 | file = NULL; |
| 5477 | break; |
| 5478 | } |
| 5479 | |
| 5480 | if (!file) |
| 5481 | break; |
| 5482 | |
| 5483 | __skb_queue_tail(&list, skb); |
| 5484 | |
| 5485 | skb = skb_dequeue(head); |
| 5486 | } |
| 5487 | |
| 5488 | if (skb_peek(&list)) { |
| 5489 | spin_lock_irq(&head->lock); |
| 5490 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5491 | __skb_queue_tail(head, skb); |
| 5492 | spin_unlock_irq(&head->lock); |
| 5493 | } |
| 5494 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5495 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5496 | #endif |
| 5497 | } |
| 5498 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5499 | struct io_file_put { |
| 5500 | struct llist_node llist; |
| 5501 | struct file *file; |
| 5502 | struct completion *done; |
| 5503 | }; |
| 5504 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5505 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5506 | { |
| 5507 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5508 | struct llist_node *node; |
| 5509 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5510 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5511 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5512 | io_ring_file_put(data->ctx, pfile->file); |
| 5513 | if (pfile->done) |
| 5514 | complete(pfile->done); |
| 5515 | else |
| 5516 | kfree(pfile); |
| 5517 | } |
| 5518 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5519 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5520 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5521 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5522 | { |
| 5523 | struct fixed_file_data *data; |
| 5524 | |
| 5525 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5526 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5527 | percpu_ref_get(&data->refs); |
| 5528 | percpu_ref_switch_to_percpu(&data->refs); |
| 5529 | } |
| 5530 | |
| 5531 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5532 | { |
| 5533 | struct fixed_file_data *data; |
| 5534 | |
| 5535 | data = container_of(ref, struct fixed_file_data, refs); |
| 5536 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5537 | /* |
| 5538 | * We can't safely switch from inside this context, punt to wq. If |
| 5539 | * the table ref is going away, the table is being unregistered. |
| 5540 | * Don't queue up the async work for that case, the caller will |
| 5541 | * handle it. |
| 5542 | */ |
| 5543 | if (!percpu_ref_is_dying(&data->refs)) |
| 5544 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5548 | unsigned nr_args) |
| 5549 | { |
| 5550 | __s32 __user *fds = (__s32 __user *) arg; |
| 5551 | unsigned nr_tables; |
| 5552 | struct file *file; |
| 5553 | int fd, ret = 0; |
| 5554 | unsigned i; |
| 5555 | |
| 5556 | if (ctx->file_data) |
| 5557 | return -EBUSY; |
| 5558 | if (!nr_args) |
| 5559 | return -EINVAL; |
| 5560 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5561 | return -EMFILE; |
| 5562 | |
| 5563 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5564 | if (!ctx->file_data) |
| 5565 | return -ENOMEM; |
| 5566 | ctx->file_data->ctx = ctx; |
| 5567 | init_completion(&ctx->file_data->done); |
| 5568 | |
| 5569 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5570 | ctx->file_data->table = kcalloc(nr_tables, |
| 5571 | sizeof(struct fixed_file_table), |
| 5572 | GFP_KERNEL); |
| 5573 | if (!ctx->file_data->table) { |
| 5574 | kfree(ctx->file_data); |
| 5575 | ctx->file_data = NULL; |
| 5576 | return -ENOMEM; |
| 5577 | } |
| 5578 | |
| 5579 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5580 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5581 | kfree(ctx->file_data->table); |
| 5582 | kfree(ctx->file_data); |
| 5583 | ctx->file_data = NULL; |
| 5584 | return -ENOMEM; |
| 5585 | } |
| 5586 | ctx->file_data->put_llist.first = NULL; |
| 5587 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5588 | |
| 5589 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5590 | percpu_ref_exit(&ctx->file_data->refs); |
| 5591 | kfree(ctx->file_data->table); |
| 5592 | kfree(ctx->file_data); |
| 5593 | ctx->file_data = NULL; |
| 5594 | return -ENOMEM; |
| 5595 | } |
| 5596 | |
| 5597 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5598 | struct fixed_file_table *table; |
| 5599 | unsigned index; |
| 5600 | |
| 5601 | ret = -EFAULT; |
| 5602 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5603 | break; |
| 5604 | /* allow sparse sets */ |
| 5605 | if (fd == -1) { |
| 5606 | ret = 0; |
| 5607 | continue; |
| 5608 | } |
| 5609 | |
| 5610 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5611 | index = i & IORING_FILE_TABLE_MASK; |
| 5612 | file = fget(fd); |
| 5613 | |
| 5614 | ret = -EBADF; |
| 5615 | if (!file) |
| 5616 | break; |
| 5617 | |
| 5618 | /* |
| 5619 | * Don't allow io_uring instances to be registered. If UNIX |
| 5620 | * isn't enabled, then this causes a reference cycle and this |
| 5621 | * instance can never get freed. If UNIX is enabled we'll |
| 5622 | * handle it just fine, but there's still no point in allowing |
| 5623 | * a ring fd as it doesn't support regular read/write anyway. |
| 5624 | */ |
| 5625 | if (file->f_op == &io_uring_fops) { |
| 5626 | fput(file); |
| 5627 | break; |
| 5628 | } |
| 5629 | ret = 0; |
| 5630 | table->files[index] = file; |
| 5631 | } |
| 5632 | |
| 5633 | if (ret) { |
| 5634 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5635 | file = io_file_from_index(ctx, i); |
| 5636 | if (file) |
| 5637 | fput(file); |
| 5638 | } |
| 5639 | for (i = 0; i < nr_tables; i++) |
| 5640 | kfree(ctx->file_data->table[i].files); |
| 5641 | |
| 5642 | kfree(ctx->file_data->table); |
| 5643 | kfree(ctx->file_data); |
| 5644 | ctx->file_data = NULL; |
| 5645 | ctx->nr_user_files = 0; |
| 5646 | return ret; |
| 5647 | } |
| 5648 | |
| 5649 | ret = io_sqe_files_scm(ctx); |
| 5650 | if (ret) |
| 5651 | io_sqe_files_unregister(ctx); |
| 5652 | |
| 5653 | return ret; |
| 5654 | } |
| 5655 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5656 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5657 | int index) |
| 5658 | { |
| 5659 | #if defined(CONFIG_UNIX) |
| 5660 | struct sock *sock = ctx->ring_sock->sk; |
| 5661 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5662 | struct sk_buff *skb; |
| 5663 | |
| 5664 | /* |
| 5665 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5666 | * file set. If there's no room, fall back to allocating a new skb |
| 5667 | * and filling it in. |
| 5668 | */ |
| 5669 | spin_lock_irq(&head->lock); |
| 5670 | skb = skb_peek(head); |
| 5671 | if (skb) { |
| 5672 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5673 | |
| 5674 | if (fpl->count < SCM_MAX_FD) { |
| 5675 | __skb_unlink(skb, head); |
| 5676 | spin_unlock_irq(&head->lock); |
| 5677 | fpl->fp[fpl->count] = get_file(file); |
| 5678 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5679 | fpl->count++; |
| 5680 | spin_lock_irq(&head->lock); |
| 5681 | __skb_queue_head(head, skb); |
| 5682 | } else { |
| 5683 | skb = NULL; |
| 5684 | } |
| 5685 | } |
| 5686 | spin_unlock_irq(&head->lock); |
| 5687 | |
| 5688 | if (skb) { |
| 5689 | fput(file); |
| 5690 | return 0; |
| 5691 | } |
| 5692 | |
| 5693 | return __io_sqe_files_scm(ctx, 1, index); |
| 5694 | #else |
| 5695 | return 0; |
| 5696 | #endif |
| 5697 | } |
| 5698 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5699 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5700 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5701 | struct fixed_file_data *data; |
| 5702 | |
| 5703 | data = container_of(ref, struct fixed_file_data, refs); |
| 5704 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5705 | } |
| 5706 | |
| 5707 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5708 | struct file *file) |
| 5709 | { |
| 5710 | struct io_file_put *pfile, pfile_stack; |
| 5711 | DECLARE_COMPLETION_ONSTACK(done); |
| 5712 | |
| 5713 | /* |
| 5714 | * If we fail allocating the struct we need for doing async reomval |
| 5715 | * of this file, just punt to sync and wait for it. |
| 5716 | */ |
| 5717 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5718 | if (!pfile) { |
| 5719 | pfile = &pfile_stack; |
| 5720 | pfile->done = &done; |
| 5721 | } |
| 5722 | |
| 5723 | pfile->file = file; |
| 5724 | llist_add(&pfile->llist, &data->put_llist); |
| 5725 | |
| 5726 | if (pfile == &pfile_stack) { |
| 5727 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5728 | percpu_ref_put(&data->refs); |
| 5729 | percpu_ref_switch_to_atomic(&data->refs, |
| 5730 | io_atomic_switch); |
| 5731 | } |
| 5732 | wait_for_completion(&done); |
| 5733 | flush_work(&data->ref_work); |
| 5734 | return false; |
| 5735 | } |
| 5736 | |
| 5737 | return true; |
| 5738 | } |
| 5739 | |
| 5740 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5741 | struct io_uring_files_update *up, |
| 5742 | unsigned nr_args) |
| 5743 | { |
| 5744 | struct fixed_file_data *data = ctx->file_data; |
| 5745 | bool ref_switch = false; |
| 5746 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5747 | __s32 __user *fds; |
| 5748 | int fd, i, err; |
| 5749 | __u32 done; |
| 5750 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5751 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5752 | return -EOVERFLOW; |
| 5753 | if (done > ctx->nr_user_files) |
| 5754 | return -EINVAL; |
| 5755 | |
| 5756 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5757 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5758 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5759 | struct fixed_file_table *table; |
| 5760 | unsigned index; |
| 5761 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5762 | err = 0; |
| 5763 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5764 | err = -EFAULT; |
| 5765 | break; |
| 5766 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5767 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5768 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5769 | index = i & IORING_FILE_TABLE_MASK; |
| 5770 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5771 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5772 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5773 | if (io_queue_file_removal(data, file)) |
| 5774 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5775 | } |
| 5776 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5777 | file = fget(fd); |
| 5778 | if (!file) { |
| 5779 | err = -EBADF; |
| 5780 | break; |
| 5781 | } |
| 5782 | /* |
| 5783 | * Don't allow io_uring instances to be registered. If |
| 5784 | * UNIX isn't enabled, then this causes a reference |
| 5785 | * cycle and this instance can never get freed. If UNIX |
| 5786 | * is enabled we'll handle it just fine, but there's |
| 5787 | * still no point in allowing a ring fd as it doesn't |
| 5788 | * support regular read/write anyway. |
| 5789 | */ |
| 5790 | if (file->f_op == &io_uring_fops) { |
| 5791 | fput(file); |
| 5792 | err = -EBADF; |
| 5793 | break; |
| 5794 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5795 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5796 | err = io_sqe_file_register(ctx, file, i); |
| 5797 | if (err) |
| 5798 | break; |
| 5799 | } |
| 5800 | nr_args--; |
| 5801 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5802 | up->offset++; |
| 5803 | } |
| 5804 | |
| 5805 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5806 | percpu_ref_put(&data->refs); |
| 5807 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5808 | } |
| 5809 | |
| 5810 | return done ? done : err; |
| 5811 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5812 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5813 | unsigned nr_args) |
| 5814 | { |
| 5815 | struct io_uring_files_update up; |
| 5816 | |
| 5817 | if (!ctx->file_data) |
| 5818 | return -ENXIO; |
| 5819 | if (!nr_args) |
| 5820 | return -EINVAL; |
| 5821 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5822 | return -EFAULT; |
| 5823 | if (up.resv) |
| 5824 | return -EINVAL; |
| 5825 | |
| 5826 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5827 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5828 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5829 | static void io_put_work(struct io_wq_work *work) |
| 5830 | { |
| 5831 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5832 | |
| 5833 | io_put_req(req); |
| 5834 | } |
| 5835 | |
| 5836 | static void io_get_work(struct io_wq_work *work) |
| 5837 | { |
| 5838 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5839 | |
| 5840 | refcount_inc(&req->refs); |
| 5841 | } |
| 5842 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5843 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 5844 | struct io_uring_params *p) |
| 5845 | { |
| 5846 | struct io_wq_data data; |
| 5847 | struct fd f; |
| 5848 | struct io_ring_ctx *ctx_attach; |
| 5849 | unsigned int concurrency; |
| 5850 | int ret = 0; |
| 5851 | |
| 5852 | data.user = ctx->user; |
| 5853 | data.get_work = io_get_work; |
| 5854 | data.put_work = io_put_work; |
| 5855 | |
| 5856 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 5857 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5858 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 5859 | |
| 5860 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 5861 | if (IS_ERR(ctx->io_wq)) { |
| 5862 | ret = PTR_ERR(ctx->io_wq); |
| 5863 | ctx->io_wq = NULL; |
| 5864 | } |
| 5865 | return ret; |
| 5866 | } |
| 5867 | |
| 5868 | f = fdget(p->wq_fd); |
| 5869 | if (!f.file) |
| 5870 | return -EBADF; |
| 5871 | |
| 5872 | if (f.file->f_op != &io_uring_fops) { |
| 5873 | ret = -EINVAL; |
| 5874 | goto out_fput; |
| 5875 | } |
| 5876 | |
| 5877 | ctx_attach = f.file->private_data; |
| 5878 | /* @io_wq is protected by holding the fd */ |
| 5879 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 5880 | ret = -EINVAL; |
| 5881 | goto out_fput; |
| 5882 | } |
| 5883 | |
| 5884 | ctx->io_wq = ctx_attach->io_wq; |
| 5885 | out_fput: |
| 5886 | fdput(f); |
| 5887 | return ret; |
| 5888 | } |
| 5889 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5890 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5891 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5892 | { |
| 5893 | int ret; |
| 5894 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5895 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5896 | mmgrab(current->mm); |
| 5897 | ctx->sqo_mm = current->mm; |
| 5898 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5899 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5900 | ret = -EPERM; |
| 5901 | if (!capable(CAP_SYS_ADMIN)) |
| 5902 | goto err; |
| 5903 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5904 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5905 | if (!ctx->sq_thread_idle) |
| 5906 | ctx->sq_thread_idle = HZ; |
| 5907 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5908 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5909 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5910 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5911 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5912 | if (cpu >= nr_cpu_ids) |
| 5913 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5914 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5915 | goto err; |
| 5916 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5917 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5918 | ctx, cpu, |
| 5919 | "io_uring-sq"); |
| 5920 | } else { |
| 5921 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5922 | "io_uring-sq"); |
| 5923 | } |
| 5924 | if (IS_ERR(ctx->sqo_thread)) { |
| 5925 | ret = PTR_ERR(ctx->sqo_thread); |
| 5926 | ctx->sqo_thread = NULL; |
| 5927 | goto err; |
| 5928 | } |
| 5929 | wake_up_process(ctx->sqo_thread); |
| 5930 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5931 | /* Can't have SQ_AFF without SQPOLL */ |
| 5932 | ret = -EINVAL; |
| 5933 | goto err; |
| 5934 | } |
| 5935 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5936 | ret = io_init_wq_offload(ctx, p); |
| 5937 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5938 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5939 | |
| 5940 | return 0; |
| 5941 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5942 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5943 | mmdrop(ctx->sqo_mm); |
| 5944 | ctx->sqo_mm = NULL; |
| 5945 | return ret; |
| 5946 | } |
| 5947 | |
| 5948 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5949 | { |
| 5950 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5951 | } |
| 5952 | |
| 5953 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5954 | { |
| 5955 | unsigned long page_limit, cur_pages, new_pages; |
| 5956 | |
| 5957 | /* Don't allow more pages than we can safely lock */ |
| 5958 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5959 | |
| 5960 | do { |
| 5961 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5962 | new_pages = cur_pages + nr_pages; |
| 5963 | if (new_pages > page_limit) |
| 5964 | return -ENOMEM; |
| 5965 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5966 | new_pages) != cur_pages); |
| 5967 | |
| 5968 | return 0; |
| 5969 | } |
| 5970 | |
| 5971 | static void io_mem_free(void *ptr) |
| 5972 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5973 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5974 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5975 | if (!ptr) |
| 5976 | return; |
| 5977 | |
| 5978 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5979 | if (put_page_testzero(page)) |
| 5980 | free_compound_page(page); |
| 5981 | } |
| 5982 | |
| 5983 | static void *io_mem_alloc(size_t size) |
| 5984 | { |
| 5985 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5986 | __GFP_NORETRY; |
| 5987 | |
| 5988 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5989 | } |
| 5990 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5991 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5992 | size_t *sq_offset) |
| 5993 | { |
| 5994 | struct io_rings *rings; |
| 5995 | size_t off, sq_array_size; |
| 5996 | |
| 5997 | off = struct_size(rings, cqes, cq_entries); |
| 5998 | if (off == SIZE_MAX) |
| 5999 | return SIZE_MAX; |
| 6000 | |
| 6001 | #ifdef CONFIG_SMP |
| 6002 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6003 | if (off == 0) |
| 6004 | return SIZE_MAX; |
| 6005 | #endif |
| 6006 | |
| 6007 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6008 | if (sq_array_size == SIZE_MAX) |
| 6009 | return SIZE_MAX; |
| 6010 | |
| 6011 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6012 | return SIZE_MAX; |
| 6013 | |
| 6014 | if (sq_offset) |
| 6015 | *sq_offset = off; |
| 6016 | |
| 6017 | return off; |
| 6018 | } |
| 6019 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6020 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6021 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6022 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6023 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6024 | pages = (size_t)1 << get_order( |
| 6025 | rings_size(sq_entries, cq_entries, NULL)); |
| 6026 | pages += (size_t)1 << get_order( |
| 6027 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6028 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6029 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6030 | } |
| 6031 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6032 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6033 | { |
| 6034 | int i, j; |
| 6035 | |
| 6036 | if (!ctx->user_bufs) |
| 6037 | return -ENXIO; |
| 6038 | |
| 6039 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6040 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6041 | |
| 6042 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6043 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6044 | |
| 6045 | if (ctx->account_mem) |
| 6046 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6047 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6048 | imu->nr_bvecs = 0; |
| 6049 | } |
| 6050 | |
| 6051 | kfree(ctx->user_bufs); |
| 6052 | ctx->user_bufs = NULL; |
| 6053 | ctx->nr_user_bufs = 0; |
| 6054 | return 0; |
| 6055 | } |
| 6056 | |
| 6057 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6058 | void __user *arg, unsigned index) |
| 6059 | { |
| 6060 | struct iovec __user *src; |
| 6061 | |
| 6062 | #ifdef CONFIG_COMPAT |
| 6063 | if (ctx->compat) { |
| 6064 | struct compat_iovec __user *ciovs; |
| 6065 | struct compat_iovec ciov; |
| 6066 | |
| 6067 | ciovs = (struct compat_iovec __user *) arg; |
| 6068 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6069 | return -EFAULT; |
| 6070 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6071 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6072 | dst->iov_len = ciov.iov_len; |
| 6073 | return 0; |
| 6074 | } |
| 6075 | #endif |
| 6076 | src = (struct iovec __user *) arg; |
| 6077 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6078 | return -EFAULT; |
| 6079 | return 0; |
| 6080 | } |
| 6081 | |
| 6082 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6083 | unsigned nr_args) |
| 6084 | { |
| 6085 | struct vm_area_struct **vmas = NULL; |
| 6086 | struct page **pages = NULL; |
| 6087 | int i, j, got_pages = 0; |
| 6088 | int ret = -EINVAL; |
| 6089 | |
| 6090 | if (ctx->user_bufs) |
| 6091 | return -EBUSY; |
| 6092 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6093 | return -EINVAL; |
| 6094 | |
| 6095 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6096 | GFP_KERNEL); |
| 6097 | if (!ctx->user_bufs) |
| 6098 | return -ENOMEM; |
| 6099 | |
| 6100 | for (i = 0; i < nr_args; i++) { |
| 6101 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6102 | unsigned long off, start, end, ubuf; |
| 6103 | int pret, nr_pages; |
| 6104 | struct iovec iov; |
| 6105 | size_t size; |
| 6106 | |
| 6107 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6108 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6109 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6110 | |
| 6111 | /* |
| 6112 | * Don't impose further limits on the size and buffer |
| 6113 | * constraints here, we'll -EINVAL later when IO is |
| 6114 | * submitted if they are wrong. |
| 6115 | */ |
| 6116 | ret = -EFAULT; |
| 6117 | if (!iov.iov_base || !iov.iov_len) |
| 6118 | goto err; |
| 6119 | |
| 6120 | /* arbitrary limit, but we need something */ |
| 6121 | if (iov.iov_len > SZ_1G) |
| 6122 | goto err; |
| 6123 | |
| 6124 | ubuf = (unsigned long) iov.iov_base; |
| 6125 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6126 | start = ubuf >> PAGE_SHIFT; |
| 6127 | nr_pages = end - start; |
| 6128 | |
| 6129 | if (ctx->account_mem) { |
| 6130 | ret = io_account_mem(ctx->user, nr_pages); |
| 6131 | if (ret) |
| 6132 | goto err; |
| 6133 | } |
| 6134 | |
| 6135 | ret = 0; |
| 6136 | if (!pages || nr_pages > got_pages) { |
| 6137 | kfree(vmas); |
| 6138 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6139 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6140 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6141 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6142 | sizeof(struct vm_area_struct *), |
| 6143 | GFP_KERNEL); |
| 6144 | if (!pages || !vmas) { |
| 6145 | ret = -ENOMEM; |
| 6146 | if (ctx->account_mem) |
| 6147 | io_unaccount_mem(ctx->user, nr_pages); |
| 6148 | goto err; |
| 6149 | } |
| 6150 | got_pages = nr_pages; |
| 6151 | } |
| 6152 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6153 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6154 | GFP_KERNEL); |
| 6155 | ret = -ENOMEM; |
| 6156 | if (!imu->bvec) { |
| 6157 | if (ctx->account_mem) |
| 6158 | io_unaccount_mem(ctx->user, nr_pages); |
| 6159 | goto err; |
| 6160 | } |
| 6161 | |
| 6162 | ret = 0; |
| 6163 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6164 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6165 | FOLL_WRITE | FOLL_LONGTERM, |
| 6166 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6167 | if (pret == nr_pages) { |
| 6168 | /* don't support file backed memory */ |
| 6169 | for (j = 0; j < nr_pages; j++) { |
| 6170 | struct vm_area_struct *vma = vmas[j]; |
| 6171 | |
| 6172 | if (vma->vm_file && |
| 6173 | !is_file_hugepages(vma->vm_file)) { |
| 6174 | ret = -EOPNOTSUPP; |
| 6175 | break; |
| 6176 | } |
| 6177 | } |
| 6178 | } else { |
| 6179 | ret = pret < 0 ? pret : -EFAULT; |
| 6180 | } |
| 6181 | up_read(¤t->mm->mmap_sem); |
| 6182 | if (ret) { |
| 6183 | /* |
| 6184 | * if we did partial map, or found file backed vmas, |
| 6185 | * release any pages we did get |
| 6186 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6187 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6188 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6189 | if (ctx->account_mem) |
| 6190 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6191 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6192 | goto err; |
| 6193 | } |
| 6194 | |
| 6195 | off = ubuf & ~PAGE_MASK; |
| 6196 | size = iov.iov_len; |
| 6197 | for (j = 0; j < nr_pages; j++) { |
| 6198 | size_t vec_len; |
| 6199 | |
| 6200 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6201 | imu->bvec[j].bv_page = pages[j]; |
| 6202 | imu->bvec[j].bv_len = vec_len; |
| 6203 | imu->bvec[j].bv_offset = off; |
| 6204 | off = 0; |
| 6205 | size -= vec_len; |
| 6206 | } |
| 6207 | /* store original address for later verification */ |
| 6208 | imu->ubuf = ubuf; |
| 6209 | imu->len = iov.iov_len; |
| 6210 | imu->nr_bvecs = nr_pages; |
| 6211 | |
| 6212 | ctx->nr_user_bufs++; |
| 6213 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6214 | kvfree(pages); |
| 6215 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6216 | return 0; |
| 6217 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6218 | kvfree(pages); |
| 6219 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6220 | io_sqe_buffer_unregister(ctx); |
| 6221 | return ret; |
| 6222 | } |
| 6223 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6224 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6225 | { |
| 6226 | __s32 __user *fds = arg; |
| 6227 | int fd; |
| 6228 | |
| 6229 | if (ctx->cq_ev_fd) |
| 6230 | return -EBUSY; |
| 6231 | |
| 6232 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6233 | return -EFAULT; |
| 6234 | |
| 6235 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6236 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6237 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6238 | ctx->cq_ev_fd = NULL; |
| 6239 | return ret; |
| 6240 | } |
| 6241 | |
| 6242 | return 0; |
| 6243 | } |
| 6244 | |
| 6245 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6246 | { |
| 6247 | if (ctx->cq_ev_fd) { |
| 6248 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6249 | ctx->cq_ev_fd = NULL; |
| 6250 | return 0; |
| 6251 | } |
| 6252 | |
| 6253 | return -ENXIO; |
| 6254 | } |
| 6255 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6256 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6257 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6258 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6259 | if (ctx->sqo_mm) |
| 6260 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6261 | |
| 6262 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6263 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6264 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6265 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6266 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6267 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6268 | if (ctx->ring_sock) { |
| 6269 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6270 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6271 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6272 | #endif |
| 6273 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6274 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6275 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6276 | |
| 6277 | percpu_ref_exit(&ctx->refs); |
| 6278 | if (ctx->account_mem) |
| 6279 | io_unaccount_mem(ctx->user, |
| 6280 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6281 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6282 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6283 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6284 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6285 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6286 | kfree(ctx); |
| 6287 | } |
| 6288 | |
| 6289 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6290 | { |
| 6291 | struct io_ring_ctx *ctx = file->private_data; |
| 6292 | __poll_t mask = 0; |
| 6293 | |
| 6294 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6295 | /* |
| 6296 | * synchronizes with barrier from wq_has_sleeper call in |
| 6297 | * io_commit_cqring |
| 6298 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6299 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6300 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6301 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6302 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6303 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6304 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6305 | |
| 6306 | return mask; |
| 6307 | } |
| 6308 | |
| 6309 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6310 | { |
| 6311 | struct io_ring_ctx *ctx = file->private_data; |
| 6312 | |
| 6313 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6314 | } |
| 6315 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6316 | static int io_remove_personalities(int id, void *p, void *data) |
| 6317 | { |
| 6318 | struct io_ring_ctx *ctx = data; |
| 6319 | const struct cred *cred; |
| 6320 | |
| 6321 | cred = idr_remove(&ctx->personality_idr, id); |
| 6322 | if (cred) |
| 6323 | put_cred(cred); |
| 6324 | return 0; |
| 6325 | } |
| 6326 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6327 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6328 | { |
| 6329 | mutex_lock(&ctx->uring_lock); |
| 6330 | percpu_ref_kill(&ctx->refs); |
| 6331 | mutex_unlock(&ctx->uring_lock); |
| 6332 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6333 | /* |
| 6334 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 6335 | * work after we've killed the ctx ref above. This is important to do |
| 6336 | * before we cancel existing commands, as the thread could otherwise |
| 6337 | * be queueing new work post that. If that's work we need to cancel, |
| 6338 | * it could cause shutdown to hang. |
| 6339 | */ |
| 6340 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 6341 | cpu_relax(); |
| 6342 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6343 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6344 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6345 | |
| 6346 | if (ctx->io_wq) |
| 6347 | io_wq_cancel_all(ctx->io_wq); |
| 6348 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6349 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6350 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6351 | if (ctx->rings) |
| 6352 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6353 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6354 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6355 | io_ring_ctx_free(ctx); |
| 6356 | } |
| 6357 | |
| 6358 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6359 | { |
| 6360 | struct io_ring_ctx *ctx = file->private_data; |
| 6361 | |
| 6362 | file->private_data = NULL; |
| 6363 | io_ring_ctx_wait_and_kill(ctx); |
| 6364 | return 0; |
| 6365 | } |
| 6366 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6367 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6368 | struct files_struct *files) |
| 6369 | { |
| 6370 | struct io_kiocb *req; |
| 6371 | DEFINE_WAIT(wait); |
| 6372 | |
| 6373 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6374 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6375 | |
| 6376 | spin_lock_irq(&ctx->inflight_lock); |
| 6377 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6378 | if (req->work.files != files) |
| 6379 | continue; |
| 6380 | /* req is being completed, ignore */ |
| 6381 | if (!refcount_inc_not_zero(&req->refs)) |
| 6382 | continue; |
| 6383 | cancel_req = req; |
| 6384 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6385 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6386 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6387 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6388 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6389 | spin_unlock_irq(&ctx->inflight_lock); |
| 6390 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6391 | /* We need to keep going until we don't find a matching req */ |
| 6392 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6393 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6394 | |
| 6395 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6396 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6397 | schedule(); |
| 6398 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6399 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | static int io_uring_flush(struct file *file, void *data) |
| 6403 | { |
| 6404 | struct io_ring_ctx *ctx = file->private_data; |
| 6405 | |
| 6406 | io_uring_cancel_files(ctx, data); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6407 | return 0; |
| 6408 | } |
| 6409 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6410 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6411 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6412 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6413 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6414 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6415 | struct page *page; |
| 6416 | void *ptr; |
| 6417 | |
| 6418 | switch (offset) { |
| 6419 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6420 | case IORING_OFF_CQ_RING: |
| 6421 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6422 | break; |
| 6423 | case IORING_OFF_SQES: |
| 6424 | ptr = ctx->sq_sqes; |
| 6425 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6426 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6427 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6431 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6432 | return ERR_PTR(-EINVAL); |
| 6433 | |
| 6434 | return ptr; |
| 6435 | } |
| 6436 | |
| 6437 | #ifdef CONFIG_MMU |
| 6438 | |
| 6439 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6440 | { |
| 6441 | size_t sz = vma->vm_end - vma->vm_start; |
| 6442 | unsigned long pfn; |
| 6443 | void *ptr; |
| 6444 | |
| 6445 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6446 | if (IS_ERR(ptr)) |
| 6447 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6448 | |
| 6449 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6450 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6451 | } |
| 6452 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6453 | #else /* !CONFIG_MMU */ |
| 6454 | |
| 6455 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6456 | { |
| 6457 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6458 | } |
| 6459 | |
| 6460 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6461 | { |
| 6462 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6463 | } |
| 6464 | |
| 6465 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6466 | unsigned long addr, unsigned long len, |
| 6467 | unsigned long pgoff, unsigned long flags) |
| 6468 | { |
| 6469 | void *ptr; |
| 6470 | |
| 6471 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6472 | if (IS_ERR(ptr)) |
| 6473 | return PTR_ERR(ptr); |
| 6474 | |
| 6475 | return (unsigned long) ptr; |
| 6476 | } |
| 6477 | |
| 6478 | #endif /* !CONFIG_MMU */ |
| 6479 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6480 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6481 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6482 | size_t, sigsz) |
| 6483 | { |
| 6484 | struct io_ring_ctx *ctx; |
| 6485 | long ret = -EBADF; |
| 6486 | int submitted = 0; |
| 6487 | struct fd f; |
| 6488 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6489 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6490 | return -EINVAL; |
| 6491 | |
| 6492 | f = fdget(fd); |
| 6493 | if (!f.file) |
| 6494 | return -EBADF; |
| 6495 | |
| 6496 | ret = -EOPNOTSUPP; |
| 6497 | if (f.file->f_op != &io_uring_fops) |
| 6498 | goto out_fput; |
| 6499 | |
| 6500 | ret = -ENXIO; |
| 6501 | ctx = f.file->private_data; |
| 6502 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6503 | goto out_fput; |
| 6504 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6505 | /* |
| 6506 | * For SQ polling, the thread will do all submissions and completions. |
| 6507 | * Just return the requested submit count, and wake the thread if |
| 6508 | * we were asked to. |
| 6509 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6510 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6511 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6512 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6513 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6514 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6515 | wake_up(&ctx->sqo_wait); |
| 6516 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6517 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6518 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6519 | |
| 6520 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6521 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6522 | cur_mm = ctx->sqo_mm; |
| 6523 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6524 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6525 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6526 | |
| 6527 | if (submitted != to_submit) |
| 6528 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6529 | } |
| 6530 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6531 | unsigned nr_events = 0; |
| 6532 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6533 | min_complete = min(min_complete, ctx->cq_entries); |
| 6534 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6535 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6536 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6537 | } else { |
| 6538 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6539 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6540 | } |
| 6541 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6542 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6543 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6544 | out_fput: |
| 6545 | fdput(f); |
| 6546 | return submitted ? submitted : ret; |
| 6547 | } |
| 6548 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6549 | static int io_uring_show_cred(int id, void *p, void *data) |
| 6550 | { |
| 6551 | const struct cred *cred = p; |
| 6552 | struct seq_file *m = data; |
| 6553 | struct user_namespace *uns = seq_user_ns(m); |
| 6554 | struct group_info *gi; |
| 6555 | kernel_cap_t cap; |
| 6556 | unsigned __capi; |
| 6557 | int g; |
| 6558 | |
| 6559 | seq_printf(m, "%5d\n", id); |
| 6560 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 6561 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 6562 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 6563 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 6564 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 6565 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 6566 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 6567 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 6568 | seq_puts(m, "\n\tGroups:\t"); |
| 6569 | gi = cred->group_info; |
| 6570 | for (g = 0; g < gi->ngroups; g++) { |
| 6571 | seq_put_decimal_ull(m, g ? " " : "", |
| 6572 | from_kgid_munged(uns, gi->gid[g])); |
| 6573 | } |
| 6574 | seq_puts(m, "\n\tCapEff:\t"); |
| 6575 | cap = cred->cap_effective; |
| 6576 | CAP_FOR_EACH_U32(__capi) |
| 6577 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 6578 | seq_putc(m, '\n'); |
| 6579 | return 0; |
| 6580 | } |
| 6581 | |
| 6582 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 6583 | { |
| 6584 | int i; |
| 6585 | |
| 6586 | mutex_lock(&ctx->uring_lock); |
| 6587 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 6588 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6589 | struct fixed_file_table *table; |
| 6590 | struct file *f; |
| 6591 | |
| 6592 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6593 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 6594 | if (f) |
| 6595 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 6596 | else |
| 6597 | seq_printf(m, "%5u: <none>\n", i); |
| 6598 | } |
| 6599 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 6600 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6601 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 6602 | |
| 6603 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 6604 | (unsigned int) buf->len); |
| 6605 | } |
| 6606 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 6607 | seq_printf(m, "Personalities:\n"); |
| 6608 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 6609 | } |
| 6610 | mutex_unlock(&ctx->uring_lock); |
| 6611 | } |
| 6612 | |
| 6613 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 6614 | { |
| 6615 | struct io_ring_ctx *ctx = f->private_data; |
| 6616 | |
| 6617 | if (percpu_ref_tryget(&ctx->refs)) { |
| 6618 | __io_uring_show_fdinfo(ctx, m); |
| 6619 | percpu_ref_put(&ctx->refs); |
| 6620 | } |
| 6621 | } |
| 6622 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6623 | static const struct file_operations io_uring_fops = { |
| 6624 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6625 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6626 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6627 | #ifndef CONFIG_MMU |
| 6628 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6629 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6630 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6631 | .poll = io_uring_poll, |
| 6632 | .fasync = io_uring_fasync, |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6633 | .show_fdinfo = io_uring_show_fdinfo, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6634 | }; |
| 6635 | |
| 6636 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6637 | struct io_uring_params *p) |
| 6638 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6639 | struct io_rings *rings; |
| 6640 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6641 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6642 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6643 | if (size == SIZE_MAX) |
| 6644 | return -EOVERFLOW; |
| 6645 | |
| 6646 | rings = io_mem_alloc(size); |
| 6647 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6648 | return -ENOMEM; |
| 6649 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6650 | ctx->rings = rings; |
| 6651 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6652 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6653 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6654 | rings->sq_ring_entries = p->sq_entries; |
| 6655 | rings->cq_ring_entries = p->cq_entries; |
| 6656 | ctx->sq_mask = rings->sq_ring_mask; |
| 6657 | ctx->cq_mask = rings->cq_ring_mask; |
| 6658 | ctx->sq_entries = rings->sq_ring_entries; |
| 6659 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6660 | |
| 6661 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6662 | if (size == SIZE_MAX) { |
| 6663 | io_mem_free(ctx->rings); |
| 6664 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6665 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6666 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6667 | |
| 6668 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6669 | if (!ctx->sq_sqes) { |
| 6670 | io_mem_free(ctx->rings); |
| 6671 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6672 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6673 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6674 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6675 | return 0; |
| 6676 | } |
| 6677 | |
| 6678 | /* |
| 6679 | * Allocate an anonymous fd, this is what constitutes the application |
| 6680 | * visible backing of an io_uring instance. The application mmaps this |
| 6681 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6682 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6683 | */ |
| 6684 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6685 | { |
| 6686 | struct file *file; |
| 6687 | int ret; |
| 6688 | |
| 6689 | #if defined(CONFIG_UNIX) |
| 6690 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6691 | &ctx->ring_sock); |
| 6692 | if (ret) |
| 6693 | return ret; |
| 6694 | #endif |
| 6695 | |
| 6696 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6697 | if (ret < 0) |
| 6698 | goto err; |
| 6699 | |
| 6700 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6701 | O_RDWR | O_CLOEXEC); |
| 6702 | if (IS_ERR(file)) { |
| 6703 | put_unused_fd(ret); |
| 6704 | ret = PTR_ERR(file); |
| 6705 | goto err; |
| 6706 | } |
| 6707 | |
| 6708 | #if defined(CONFIG_UNIX) |
| 6709 | ctx->ring_sock->file = file; |
| 6710 | #endif |
| 6711 | fd_install(ret, file); |
| 6712 | return ret; |
| 6713 | err: |
| 6714 | #if defined(CONFIG_UNIX) |
| 6715 | sock_release(ctx->ring_sock); |
| 6716 | ctx->ring_sock = NULL; |
| 6717 | #endif |
| 6718 | return ret; |
| 6719 | } |
| 6720 | |
| 6721 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6722 | { |
| 6723 | struct user_struct *user = NULL; |
| 6724 | struct io_ring_ctx *ctx; |
| 6725 | bool account_mem; |
| 6726 | int ret; |
| 6727 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6728 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6729 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6730 | if (entries > IORING_MAX_ENTRIES) { |
| 6731 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6732 | return -EINVAL; |
| 6733 | entries = IORING_MAX_ENTRIES; |
| 6734 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6735 | |
| 6736 | /* |
| 6737 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6738 | * application to drive a higher depth than the size of the SQ ring, |
| 6739 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6740 | * some flexibility in overcommitting a bit. If the application has |
| 6741 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6742 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6743 | */ |
| 6744 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6745 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6746 | /* |
| 6747 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6748 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6749 | * any cq vs sq ring sizing. |
| 6750 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6751 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6752 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6753 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6754 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6755 | return -EINVAL; |
| 6756 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6757 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6758 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6759 | } else { |
| 6760 | p->cq_entries = 2 * p->sq_entries; |
| 6761 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6762 | |
| 6763 | user = get_uid(current_user()); |
| 6764 | account_mem = !capable(CAP_IPC_LOCK); |
| 6765 | |
| 6766 | if (account_mem) { |
| 6767 | ret = io_account_mem(user, |
| 6768 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6769 | if (ret) { |
| 6770 | free_uid(user); |
| 6771 | return ret; |
| 6772 | } |
| 6773 | } |
| 6774 | |
| 6775 | ctx = io_ring_ctx_alloc(p); |
| 6776 | if (!ctx) { |
| 6777 | if (account_mem) |
| 6778 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6779 | p->cq_entries)); |
| 6780 | free_uid(user); |
| 6781 | return -ENOMEM; |
| 6782 | } |
| 6783 | ctx->compat = in_compat_syscall(); |
| 6784 | ctx->account_mem = account_mem; |
| 6785 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6786 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6787 | |
| 6788 | ret = io_allocate_scq_urings(ctx, p); |
| 6789 | if (ret) |
| 6790 | goto err; |
| 6791 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6792 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6793 | if (ret) |
| 6794 | goto err; |
| 6795 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6796 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6797 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6798 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6799 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6800 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6801 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6802 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6803 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6804 | |
| 6805 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6806 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6807 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6808 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6809 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6810 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6811 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6812 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6813 | /* |
| 6814 | * Install ring fd as the very last thing, so we don't risk someone |
| 6815 | * having closed it before we finish setup |
| 6816 | */ |
| 6817 | ret = io_uring_get_fd(ctx); |
| 6818 | if (ret < 0) |
| 6819 | goto err; |
| 6820 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6821 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 6822 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
| 6823 | IORING_FEAT_CUR_PERSONALITY; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6824 | 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] | 6825 | return ret; |
| 6826 | err: |
| 6827 | io_ring_ctx_wait_and_kill(ctx); |
| 6828 | return ret; |
| 6829 | } |
| 6830 | |
| 6831 | /* |
| 6832 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6833 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6834 | * params structure passed in. |
| 6835 | */ |
| 6836 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6837 | { |
| 6838 | struct io_uring_params p; |
| 6839 | long ret; |
| 6840 | int i; |
| 6841 | |
| 6842 | if (copy_from_user(&p, params, sizeof(p))) |
| 6843 | return -EFAULT; |
| 6844 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6845 | if (p.resv[i]) |
| 6846 | return -EINVAL; |
| 6847 | } |
| 6848 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6849 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6850 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6851 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6852 | return -EINVAL; |
| 6853 | |
| 6854 | ret = io_uring_create(entries, &p); |
| 6855 | if (ret < 0) |
| 6856 | return ret; |
| 6857 | |
| 6858 | if (copy_to_user(params, &p, sizeof(p))) |
| 6859 | return -EFAULT; |
| 6860 | |
| 6861 | return ret; |
| 6862 | } |
| 6863 | |
| 6864 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6865 | struct io_uring_params __user *, params) |
| 6866 | { |
| 6867 | return io_uring_setup(entries, params); |
| 6868 | } |
| 6869 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6870 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 6871 | { |
| 6872 | struct io_uring_probe *p; |
| 6873 | size_t size; |
| 6874 | int i, ret; |
| 6875 | |
| 6876 | size = struct_size(p, ops, nr_args); |
| 6877 | if (size == SIZE_MAX) |
| 6878 | return -EOVERFLOW; |
| 6879 | p = kzalloc(size, GFP_KERNEL); |
| 6880 | if (!p) |
| 6881 | return -ENOMEM; |
| 6882 | |
| 6883 | ret = -EFAULT; |
| 6884 | if (copy_from_user(p, arg, size)) |
| 6885 | goto out; |
| 6886 | ret = -EINVAL; |
| 6887 | if (memchr_inv(p, 0, size)) |
| 6888 | goto out; |
| 6889 | |
| 6890 | p->last_op = IORING_OP_LAST - 1; |
| 6891 | if (nr_args > IORING_OP_LAST) |
| 6892 | nr_args = IORING_OP_LAST; |
| 6893 | |
| 6894 | for (i = 0; i < nr_args; i++) { |
| 6895 | p->ops[i].op = i; |
| 6896 | if (!io_op_defs[i].not_supported) |
| 6897 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 6898 | } |
| 6899 | p->ops_len = i; |
| 6900 | |
| 6901 | ret = 0; |
| 6902 | if (copy_to_user(arg, p, size)) |
| 6903 | ret = -EFAULT; |
| 6904 | out: |
| 6905 | kfree(p); |
| 6906 | return ret; |
| 6907 | } |
| 6908 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6909 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 6910 | { |
| 6911 | const struct cred *creds = get_current_cred(); |
| 6912 | int id; |
| 6913 | |
| 6914 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 6915 | USHRT_MAX, GFP_KERNEL); |
| 6916 | if (id < 0) |
| 6917 | put_cred(creds); |
| 6918 | return id; |
| 6919 | } |
| 6920 | |
| 6921 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 6922 | { |
| 6923 | const struct cred *old_creds; |
| 6924 | |
| 6925 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 6926 | if (old_creds) { |
| 6927 | put_cred(old_creds); |
| 6928 | return 0; |
| 6929 | } |
| 6930 | |
| 6931 | return -EINVAL; |
| 6932 | } |
| 6933 | |
| 6934 | static bool io_register_op_must_quiesce(int op) |
| 6935 | { |
| 6936 | switch (op) { |
| 6937 | case IORING_UNREGISTER_FILES: |
| 6938 | case IORING_REGISTER_FILES_UPDATE: |
| 6939 | case IORING_REGISTER_PROBE: |
| 6940 | case IORING_REGISTER_PERSONALITY: |
| 6941 | case IORING_UNREGISTER_PERSONALITY: |
| 6942 | return false; |
| 6943 | default: |
| 6944 | return true; |
| 6945 | } |
| 6946 | } |
| 6947 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6948 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6949 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6950 | __releases(ctx->uring_lock) |
| 6951 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6952 | { |
| 6953 | int ret; |
| 6954 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6955 | /* |
| 6956 | * We're inside the ring mutex, if the ref is already dying, then |
| 6957 | * someone else killed the ctx or is already going through |
| 6958 | * io_uring_register(). |
| 6959 | */ |
| 6960 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6961 | return -ENXIO; |
| 6962 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6963 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6964 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6965 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6966 | /* |
| 6967 | * Drop uring mutex before waiting for references to exit. If |
| 6968 | * another thread is currently inside io_uring_enter() it might |
| 6969 | * need to grab the uring_lock to make progress. If we hold it |
| 6970 | * here across the drain wait, then we can deadlock. It's safe |
| 6971 | * to drop the mutex here, since no new references will come in |
| 6972 | * after we've killed the percpu ref. |
| 6973 | */ |
| 6974 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6975 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6976 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6977 | if (ret) { |
| 6978 | percpu_ref_resurrect(&ctx->refs); |
| 6979 | ret = -EINTR; |
| 6980 | goto out; |
| 6981 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6982 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6983 | |
| 6984 | switch (opcode) { |
| 6985 | case IORING_REGISTER_BUFFERS: |
| 6986 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6987 | break; |
| 6988 | case IORING_UNREGISTER_BUFFERS: |
| 6989 | ret = -EINVAL; |
| 6990 | if (arg || nr_args) |
| 6991 | break; |
| 6992 | ret = io_sqe_buffer_unregister(ctx); |
| 6993 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6994 | case IORING_REGISTER_FILES: |
| 6995 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6996 | break; |
| 6997 | case IORING_UNREGISTER_FILES: |
| 6998 | ret = -EINVAL; |
| 6999 | if (arg || nr_args) |
| 7000 | break; |
| 7001 | ret = io_sqe_files_unregister(ctx); |
| 7002 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7003 | case IORING_REGISTER_FILES_UPDATE: |
| 7004 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7005 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7006 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7007 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7008 | ret = -EINVAL; |
| 7009 | if (nr_args != 1) |
| 7010 | break; |
| 7011 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7012 | if (ret) |
| 7013 | break; |
| 7014 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7015 | ctx->eventfd_async = 1; |
| 7016 | else |
| 7017 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7018 | break; |
| 7019 | case IORING_UNREGISTER_EVENTFD: |
| 7020 | ret = -EINVAL; |
| 7021 | if (arg || nr_args) |
| 7022 | break; |
| 7023 | ret = io_eventfd_unregister(ctx); |
| 7024 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7025 | case IORING_REGISTER_PROBE: |
| 7026 | ret = -EINVAL; |
| 7027 | if (!arg || nr_args > 256) |
| 7028 | break; |
| 7029 | ret = io_probe(ctx, arg, nr_args); |
| 7030 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7031 | case IORING_REGISTER_PERSONALITY: |
| 7032 | ret = -EINVAL; |
| 7033 | if (arg || nr_args) |
| 7034 | break; |
| 7035 | ret = io_register_personality(ctx); |
| 7036 | break; |
| 7037 | case IORING_UNREGISTER_PERSONALITY: |
| 7038 | ret = -EINVAL; |
| 7039 | if (arg) |
| 7040 | break; |
| 7041 | ret = io_unregister_personality(ctx, nr_args); |
| 7042 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7043 | default: |
| 7044 | ret = -EINVAL; |
| 7045 | break; |
| 7046 | } |
| 7047 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7048 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7049 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7050 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7051 | out: |
| 7052 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7053 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7054 | return ret; |
| 7055 | } |
| 7056 | |
| 7057 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7058 | void __user *, arg, unsigned int, nr_args) |
| 7059 | { |
| 7060 | struct io_ring_ctx *ctx; |
| 7061 | long ret = -EBADF; |
| 7062 | struct fd f; |
| 7063 | |
| 7064 | f = fdget(fd); |
| 7065 | if (!f.file) |
| 7066 | return -EBADF; |
| 7067 | |
| 7068 | ret = -EOPNOTSUPP; |
| 7069 | if (f.file->f_op != &io_uring_fops) |
| 7070 | goto out_fput; |
| 7071 | |
| 7072 | ctx = f.file->private_data; |
| 7073 | |
| 7074 | mutex_lock(&ctx->uring_lock); |
| 7075 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7076 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7077 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7078 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7079 | out_fput: |
| 7080 | fdput(f); |
| 7081 | return ret; |
| 7082 | } |
| 7083 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7084 | static int __init io_uring_init(void) |
| 7085 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7086 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7087 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7088 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7089 | } while (0) |
| 7090 | |
| 7091 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7092 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7093 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7094 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7095 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7096 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7097 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7098 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7099 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7100 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
| 7101 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7102 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7103 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7104 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7105 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7106 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7107 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7108 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7109 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7110 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7111 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7112 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7113 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7114 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
| 7115 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7116 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7117 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
| 7118 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7119 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7120 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 7121 | return 0; |
| 7122 | }; |
| 7123 | __initcall(io_uring_init); |