Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 49 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 50 | |
| 51 | #include <linux/sched/signal.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/file.h> |
| 54 | #include <linux/fdtable.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/mman.h> |
| 57 | #include <linux/mmu_context.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 78 | #include <linux/fs_struct.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 79 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 80 | #define CREATE_TRACE_POINTS |
| 81 | #include <trace/events/io_uring.h> |
| 82 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 83 | #include <uapi/linux/io_uring.h> |
| 84 | |
| 85 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 86 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 87 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 88 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 89 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 93 | */ |
| 94 | #define IORING_FILE_TABLE_SHIFT 9 |
| 95 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 96 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 97 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 98 | |
| 99 | struct io_uring { |
| 100 | u32 head ____cacheline_aligned_in_smp; |
| 101 | u32 tail ____cacheline_aligned_in_smp; |
| 102 | }; |
| 103 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 104 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 105 | * This data is shared with the application through the mmap at offsets |
| 106 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 107 | * |
| 108 | * The offsets to the member fields are published through struct |
| 109 | * io_sqring_offsets when calling io_uring_setup. |
| 110 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 111 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 112 | /* |
| 113 | * Head and tail offsets into the ring; the offsets need to be |
| 114 | * masked to get valid indices. |
| 115 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 116 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 117 | * and the application controls tail of the sq ring and the head of the |
| 118 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 119 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 120 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 123 | * ring_entries - 1) |
| 124 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 125 | u32 sq_ring_mask, cq_ring_mask; |
| 126 | /* Ring sizes (constant, power of 2) */ |
| 127 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 128 | /* |
| 129 | * Number of invalid entries dropped by the kernel due to |
| 130 | * invalid index stored in array |
| 131 | * |
| 132 | * Written by the kernel, shouldn't be modified by the |
| 133 | * application (i.e. get number of "new events" by comparing to |
| 134 | * cached value). |
| 135 | * |
| 136 | * After a new SQ head value was read by the application this |
| 137 | * counter includes all submissions that were dropped reaching |
| 138 | * the new SQ head (and possibly more). |
| 139 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 140 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 141 | /* |
| 142 | * Runtime flags |
| 143 | * |
| 144 | * Written by the kernel, shouldn't be modified by the |
| 145 | * application. |
| 146 | * |
| 147 | * The application needs a full memory barrier before checking |
| 148 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 149 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 150 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 151 | /* |
| 152 | * Number of completion events lost because the queue was full; |
| 153 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 154 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 155 | * the completion queue. |
| 156 | * |
| 157 | * Written by the kernel, shouldn't be modified by the |
| 158 | * application (i.e. get number of "new events" by comparing to |
| 159 | * cached value). |
| 160 | * |
| 161 | * As completion events come in out of order this counter is not |
| 162 | * ordered with any other data. |
| 163 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 164 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 165 | /* |
| 166 | * Ring buffer of completion events. |
| 167 | * |
| 168 | * The kernel writes completion events fresh every time they are |
| 169 | * produced, so the application is allowed to modify pending |
| 170 | * entries. |
| 171 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 172 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 175 | struct io_mapped_ubuf { |
| 176 | u64 ubuf; |
| 177 | size_t len; |
| 178 | struct bio_vec *bvec; |
| 179 | unsigned int nr_bvecs; |
| 180 | }; |
| 181 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 182 | struct fixed_file_table { |
| 183 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 186 | enum { |
| 187 | FFD_F_ATOMIC, |
| 188 | }; |
| 189 | |
| 190 | struct fixed_file_data { |
| 191 | struct fixed_file_table *table; |
| 192 | struct io_ring_ctx *ctx; |
| 193 | |
| 194 | struct percpu_ref refs; |
| 195 | struct llist_head put_llist; |
| 196 | unsigned long state; |
| 197 | struct work_struct ref_work; |
| 198 | struct completion done; |
| 199 | }; |
| 200 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 201 | struct io_ring_ctx { |
| 202 | struct { |
| 203 | struct percpu_ref refs; |
| 204 | } ____cacheline_aligned_in_smp; |
| 205 | |
| 206 | struct { |
| 207 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 208 | unsigned int compat: 1; |
| 209 | unsigned int account_mem: 1; |
| 210 | unsigned int cq_overflow_flushed: 1; |
| 211 | unsigned int drain_next: 1; |
| 212 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 213 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 214 | /* |
| 215 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 216 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 217 | * |
| 218 | * This indirection could e.g. be used to assign fixed |
| 219 | * io_uring_sqe entries to operations and only submit them to |
| 220 | * the queue when needed. |
| 221 | * |
| 222 | * The kernel modifies neither the indices array nor the entries |
| 223 | * array. |
| 224 | */ |
| 225 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 226 | unsigned cached_sq_head; |
| 227 | unsigned sq_entries; |
| 228 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 229 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 230 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 231 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 232 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 233 | |
| 234 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 235 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 236 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 237 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 238 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 239 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 240 | } ____cacheline_aligned_in_smp; |
| 241 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 242 | struct io_rings *rings; |
| 243 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 244 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 245 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 246 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 247 | struct mm_struct *sqo_mm; |
| 248 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 249 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 250 | /* |
| 251 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 252 | * readers must ensure that ->refs is alive as long as the file* is |
| 253 | * used. Only updated through io_uring_register(2). |
| 254 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 255 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 256 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 257 | int ring_fd; |
| 258 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 259 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 260 | /* if used, fixed mapped user buffers */ |
| 261 | unsigned nr_user_bufs; |
| 262 | struct io_mapped_ubuf *user_bufs; |
| 263 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 264 | struct user_struct *user; |
| 265 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 266 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 267 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 268 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 269 | struct completion *completions; |
| 270 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 271 | /* if all else fails... */ |
| 272 | struct io_kiocb *fallback_req; |
| 273 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 274 | #if defined(CONFIG_UNIX) |
| 275 | struct socket *ring_sock; |
| 276 | #endif |
| 277 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 278 | struct idr personality_idr; |
| 279 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 280 | struct { |
| 281 | unsigned cached_cq_tail; |
| 282 | unsigned cq_entries; |
| 283 | unsigned cq_mask; |
| 284 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 285 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 286 | struct wait_queue_head cq_wait; |
| 287 | struct fasync_struct *cq_fasync; |
| 288 | struct eventfd_ctx *cq_ev_fd; |
| 289 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 290 | |
| 291 | struct { |
| 292 | struct mutex uring_lock; |
| 293 | wait_queue_head_t wait; |
| 294 | } ____cacheline_aligned_in_smp; |
| 295 | |
| 296 | struct { |
| 297 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 298 | struct llist_head poll_llist; |
| 299 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 300 | /* |
| 301 | * ->poll_list is protected by the ctx->uring_lock for |
| 302 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 303 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 304 | * manipulate the list, hence no extra locking is needed there. |
| 305 | */ |
| 306 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 307 | struct hlist_head *cancel_hash; |
| 308 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 309 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 310 | |
| 311 | spinlock_t inflight_lock; |
| 312 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 313 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 316 | /* |
| 317 | * First field must be the file pointer in all the |
| 318 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 319 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 320 | struct io_poll_iocb { |
| 321 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 322 | union { |
| 323 | struct wait_queue_head *head; |
| 324 | u64 addr; |
| 325 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 326 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 327 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 328 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 329 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 332 | struct io_close { |
| 333 | struct file *file; |
| 334 | struct file *put_file; |
| 335 | int fd; |
| 336 | }; |
| 337 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 338 | struct io_timeout_data { |
| 339 | struct io_kiocb *req; |
| 340 | struct hrtimer timer; |
| 341 | struct timespec64 ts; |
| 342 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 343 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 344 | }; |
| 345 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 346 | struct io_accept { |
| 347 | struct file *file; |
| 348 | struct sockaddr __user *addr; |
| 349 | int __user *addr_len; |
| 350 | int flags; |
| 351 | }; |
| 352 | |
| 353 | struct io_sync { |
| 354 | struct file *file; |
| 355 | loff_t len; |
| 356 | loff_t off; |
| 357 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 358 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 361 | struct io_cancel { |
| 362 | struct file *file; |
| 363 | u64 addr; |
| 364 | }; |
| 365 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 366 | struct io_timeout { |
| 367 | struct file *file; |
| 368 | u64 addr; |
| 369 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 370 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 373 | struct io_rw { |
| 374 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 375 | struct kiocb kiocb; |
| 376 | u64 addr; |
| 377 | u64 len; |
| 378 | }; |
| 379 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 380 | struct io_connect { |
| 381 | struct file *file; |
| 382 | struct sockaddr __user *addr; |
| 383 | int addr_len; |
| 384 | }; |
| 385 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 386 | struct io_sr_msg { |
| 387 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 388 | union { |
| 389 | struct user_msghdr __user *msg; |
| 390 | void __user *buf; |
| 391 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 392 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 393 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 396 | struct io_open { |
| 397 | struct file *file; |
| 398 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 399 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 400 | unsigned mask; |
| 401 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 402 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 403 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 404 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 407 | struct io_files_update { |
| 408 | struct file *file; |
| 409 | u64 arg; |
| 410 | u32 nr_args; |
| 411 | u32 offset; |
| 412 | }; |
| 413 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 414 | struct io_fadvise { |
| 415 | struct file *file; |
| 416 | u64 offset; |
| 417 | u32 len; |
| 418 | u32 advice; |
| 419 | }; |
| 420 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 421 | struct io_madvise { |
| 422 | struct file *file; |
| 423 | u64 addr; |
| 424 | u32 len; |
| 425 | u32 advice; |
| 426 | }; |
| 427 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 428 | struct io_epoll { |
| 429 | struct file *file; |
| 430 | int epfd; |
| 431 | int op; |
| 432 | int fd; |
| 433 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 434 | }; |
| 435 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 436 | struct io_async_connect { |
| 437 | struct sockaddr_storage address; |
| 438 | }; |
| 439 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 440 | struct io_async_msghdr { |
| 441 | struct iovec fast_iov[UIO_FASTIOV]; |
| 442 | struct iovec *iov; |
| 443 | struct sockaddr __user *uaddr; |
| 444 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 445 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 448 | struct io_async_rw { |
| 449 | struct iovec fast_iov[UIO_FASTIOV]; |
| 450 | struct iovec *iov; |
| 451 | ssize_t nr_segs; |
| 452 | ssize_t size; |
| 453 | }; |
| 454 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 455 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 456 | union { |
| 457 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 458 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 459 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 460 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 461 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 464 | enum { |
| 465 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 466 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 467 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 468 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 469 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 470 | |
| 471 | REQ_F_LINK_NEXT_BIT, |
| 472 | REQ_F_FAIL_LINK_BIT, |
| 473 | REQ_F_INFLIGHT_BIT, |
| 474 | REQ_F_CUR_POS_BIT, |
| 475 | REQ_F_NOWAIT_BIT, |
| 476 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 477 | REQ_F_LINK_TIMEOUT_BIT, |
| 478 | REQ_F_TIMEOUT_BIT, |
| 479 | REQ_F_ISREG_BIT, |
| 480 | REQ_F_MUST_PUNT_BIT, |
| 481 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 482 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 483 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 484 | REQ_F_OVERFLOW_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | enum { |
| 488 | /* ctx owns file */ |
| 489 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 490 | /* drain existing IO first */ |
| 491 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 492 | /* linked sqes */ |
| 493 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 494 | /* doesn't sever on completion < 0 */ |
| 495 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 496 | /* IOSQE_ASYNC */ |
| 497 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 498 | |
| 499 | /* already grabbed next link */ |
| 500 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 501 | /* fail rest of links */ |
| 502 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 503 | /* on inflight list */ |
| 504 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 505 | /* read/write uses file position */ |
| 506 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 507 | /* must not punt to workers */ |
| 508 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 509 | /* polled IO has completed */ |
| 510 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 511 | /* has linked timeout */ |
| 512 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 513 | /* timeout request */ |
| 514 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 515 | /* regular file */ |
| 516 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 517 | /* must be punted even for NONBLOCK */ |
| 518 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 519 | /* no timeout sequence */ |
| 520 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 521 | /* completion under lock */ |
| 522 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 523 | /* needs cleanup */ |
| 524 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 525 | /* in overflow list */ |
| 526 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 527 | }; |
| 528 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 529 | /* |
| 530 | * NOTE! Each of the iocb union members has the file pointer |
| 531 | * as the first entry in their struct definition. So you can |
| 532 | * access the file pointer through any of the sub-structs, |
| 533 | * or directly as just 'ki_filp' in this struct. |
| 534 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 535 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 536 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 537 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 538 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 539 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 540 | struct io_accept accept; |
| 541 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 542 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 543 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 544 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 545 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 546 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 547 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 548 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 549 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 550 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 551 | struct io_epoll epoll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 552 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 553 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 554 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 555 | /* |
| 556 | * llist_node is only used for poll deferred completions |
| 557 | */ |
| 558 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 559 | bool in_async; |
| 560 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 561 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 562 | |
| 563 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 564 | union { |
| 565 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 566 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 567 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 568 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 569 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 570 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 571 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 572 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 573 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 574 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 575 | struct list_head inflight_entry; |
| 576 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 577 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 578 | }; |
| 579 | |
| 580 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 581 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 582 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 583 | struct io_submit_state { |
| 584 | struct blk_plug plug; |
| 585 | |
| 586 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 587 | * io_kiocb alloc cache |
| 588 | */ |
| 589 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 590 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 591 | |
| 592 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 593 | * File reference cache |
| 594 | */ |
| 595 | struct file *file; |
| 596 | unsigned int fd; |
| 597 | unsigned int has_refs; |
| 598 | unsigned int used_refs; |
| 599 | unsigned int ios_left; |
| 600 | }; |
| 601 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 602 | struct io_op_def { |
| 603 | /* needs req->io allocated for deferral/async */ |
| 604 | unsigned async_ctx : 1; |
| 605 | /* needs current->mm setup, does mm access */ |
| 606 | unsigned needs_mm : 1; |
| 607 | /* needs req->file assigned */ |
| 608 | unsigned needs_file : 1; |
| 609 | /* needs req->file assigned IFF fd is >= 0 */ |
| 610 | unsigned fd_non_neg : 1; |
| 611 | /* hash wq insertion if file is a regular file */ |
| 612 | unsigned hash_reg_file : 1; |
| 613 | /* unbound wq insertion if file is a non-regular file */ |
| 614 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 615 | /* opcode is not supported by this kernel */ |
| 616 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 617 | /* needs file table */ |
| 618 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 619 | /* needs ->fs */ |
| 620 | unsigned needs_fs : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 624 | [IORING_OP_NOP] = {}, |
| 625 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 626 | .async_ctx = 1, |
| 627 | .needs_mm = 1, |
| 628 | .needs_file = 1, |
| 629 | .unbound_nonreg_file = 1, |
| 630 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 631 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 632 | .async_ctx = 1, |
| 633 | .needs_mm = 1, |
| 634 | .needs_file = 1, |
| 635 | .hash_reg_file = 1, |
| 636 | .unbound_nonreg_file = 1, |
| 637 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 638 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 639 | .needs_file = 1, |
| 640 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 641 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 642 | .needs_file = 1, |
| 643 | .unbound_nonreg_file = 1, |
| 644 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 645 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 646 | .needs_file = 1, |
| 647 | .hash_reg_file = 1, |
| 648 | .unbound_nonreg_file = 1, |
| 649 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 650 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 651 | .needs_file = 1, |
| 652 | .unbound_nonreg_file = 1, |
| 653 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 654 | [IORING_OP_POLL_REMOVE] = {}, |
| 655 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 656 | .needs_file = 1, |
| 657 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 658 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 659 | .async_ctx = 1, |
| 660 | .needs_mm = 1, |
| 661 | .needs_file = 1, |
| 662 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 663 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 664 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 665 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 666 | .async_ctx = 1, |
| 667 | .needs_mm = 1, |
| 668 | .needs_file = 1, |
| 669 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 670 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 671 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 672 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 673 | .async_ctx = 1, |
| 674 | .needs_mm = 1, |
| 675 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 676 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 677 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 678 | .needs_mm = 1, |
| 679 | .needs_file = 1, |
| 680 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 681 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 682 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 683 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 684 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 685 | .async_ctx = 1, |
| 686 | .needs_mm = 1, |
| 687 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 688 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 689 | .async_ctx = 1, |
| 690 | .needs_mm = 1, |
| 691 | .needs_file = 1, |
| 692 | .unbound_nonreg_file = 1, |
| 693 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 694 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 695 | .needs_file = 1, |
| 696 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 697 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 698 | .needs_file = 1, |
| 699 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 700 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 701 | .needs_fs = 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_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 704 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 705 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 706 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 707 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 708 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 709 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 710 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 711 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 712 | .needs_mm = 1, |
| 713 | .needs_file = 1, |
| 714 | .fd_non_neg = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 715 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 716 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 717 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 718 | .needs_mm = 1, |
| 719 | .needs_file = 1, |
| 720 | .unbound_nonreg_file = 1, |
| 721 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 722 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 723 | .needs_mm = 1, |
| 724 | .needs_file = 1, |
| 725 | .unbound_nonreg_file = 1, |
| 726 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 727 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 728 | .needs_file = 1, |
| 729 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 730 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 731 | .needs_mm = 1, |
| 732 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 733 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 734 | .needs_mm = 1, |
| 735 | .needs_file = 1, |
| 736 | .unbound_nonreg_file = 1, |
| 737 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 738 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 739 | .needs_mm = 1, |
| 740 | .needs_file = 1, |
| 741 | .unbound_nonreg_file = 1, |
| 742 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 743 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 744 | .needs_file = 1, |
| 745 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 746 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 747 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 748 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 749 | [IORING_OP_EPOLL_CTL] = { |
| 750 | .unbound_nonreg_file = 1, |
| 751 | .file_table = 1, |
| 752 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 753 | }; |
| 754 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 755 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 756 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 757 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 758 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 759 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 760 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 761 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 762 | struct io_uring_files_update *ip, |
| 763 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 764 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 765 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 766 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 767 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 768 | static struct kmem_cache *req_cachep; |
| 769 | |
| 770 | static const struct file_operations io_uring_fops; |
| 771 | |
| 772 | struct sock *io_uring_get_socket(struct file *file) |
| 773 | { |
| 774 | #if defined(CONFIG_UNIX) |
| 775 | if (file->f_op == &io_uring_fops) { |
| 776 | struct io_ring_ctx *ctx = file->private_data; |
| 777 | |
| 778 | return ctx->ring_sock->sk; |
| 779 | } |
| 780 | #endif |
| 781 | return NULL; |
| 782 | } |
| 783 | EXPORT_SYMBOL(io_uring_get_socket); |
| 784 | |
| 785 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 786 | { |
| 787 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 788 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 789 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 793 | { |
| 794 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 795 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 796 | |
| 797 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 798 | if (!ctx) |
| 799 | return NULL; |
| 800 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 801 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 802 | if (!ctx->fallback_req) |
| 803 | goto err; |
| 804 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 805 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 806 | if (!ctx->completions) |
| 807 | goto err; |
| 808 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 809 | /* |
| 810 | * Use 5 bits less than the max cq entries, that should give us around |
| 811 | * 32 entries per hash list if totally full and uniformly spread. |
| 812 | */ |
| 813 | hash_bits = ilog2(p->cq_entries); |
| 814 | hash_bits -= 5; |
| 815 | if (hash_bits <= 0) |
| 816 | hash_bits = 1; |
| 817 | ctx->cancel_hash_bits = hash_bits; |
| 818 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 819 | GFP_KERNEL); |
| 820 | if (!ctx->cancel_hash) |
| 821 | goto err; |
| 822 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 823 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 824 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 825 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 826 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 827 | |
| 828 | ctx->flags = p->flags; |
| 829 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 830 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 831 | init_completion(&ctx->completions[0]); |
| 832 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 833 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 834 | mutex_init(&ctx->uring_lock); |
| 835 | init_waitqueue_head(&ctx->wait); |
| 836 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 837 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 838 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 839 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 840 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 841 | init_waitqueue_head(&ctx->inflight_wait); |
| 842 | spin_lock_init(&ctx->inflight_lock); |
| 843 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 844 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 845 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 846 | if (ctx->fallback_req) |
| 847 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 848 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 849 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 850 | kfree(ctx); |
| 851 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 854 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 855 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 856 | struct io_ring_ctx *ctx = req->ctx; |
| 857 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 858 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 859 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 860 | } |
| 861 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 862 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 863 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 864 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 865 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 866 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 867 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | 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] | 871 | { |
| 872 | struct io_kiocb *req; |
| 873 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 874 | 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] | 875 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 876 | list_del_init(&req->list); |
| 877 | return req; |
| 878 | } |
| 879 | |
| 880 | return NULL; |
| 881 | } |
| 882 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 883 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 884 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 885 | struct io_kiocb *req; |
| 886 | |
| 887 | 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] | 888 | if (req) { |
| 889 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 890 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 891 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 892 | list_del_init(&req->list); |
| 893 | return req; |
| 894 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 898 | } |
| 899 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 900 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 901 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 902 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 903 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 904 | /* order cqe stores with ring update */ |
| 905 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 906 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 907 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 908 | wake_up_interruptible(&ctx->cq_wait); |
| 909 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 913 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 914 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 915 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 916 | if (!req->work.mm && def->needs_mm) { |
| 917 | mmgrab(current->mm); |
| 918 | req->work.mm = current->mm; |
| 919 | } |
| 920 | if (!req->work.creds) |
| 921 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 922 | if (!req->work.fs && def->needs_fs) { |
| 923 | spin_lock(¤t->fs->lock); |
| 924 | if (!current->fs->in_exec) { |
| 925 | req->work.fs = current->fs; |
| 926 | req->work.fs->users++; |
| 927 | } else { |
| 928 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 929 | } |
| 930 | spin_unlock(¤t->fs->lock); |
| 931 | } |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 932 | if (!req->work.task_pid) |
| 933 | req->work.task_pid = task_pid_vnr(current); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 937 | { |
| 938 | if (req->work.mm) { |
| 939 | mmdrop(req->work.mm); |
| 940 | req->work.mm = NULL; |
| 941 | } |
| 942 | if (req->work.creds) { |
| 943 | put_cred(req->work.creds); |
| 944 | req->work.creds = NULL; |
| 945 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 946 | if (req->work.fs) { |
| 947 | struct fs_struct *fs = req->work.fs; |
| 948 | |
| 949 | spin_lock(&req->work.fs->lock); |
| 950 | if (--fs->users) |
| 951 | fs = NULL; |
| 952 | spin_unlock(&req->work.fs->lock); |
| 953 | if (fs) |
| 954 | free_fs_struct(fs); |
| 955 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 956 | } |
| 957 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 958 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 959 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 960 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 961 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 962 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 963 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 964 | if (req->flags & REQ_F_ISREG) { |
| 965 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 966 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 967 | } else { |
| 968 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 969 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 970 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 971 | |
| 972 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 973 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 974 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 975 | return do_hashed; |
| 976 | } |
| 977 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 978 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 979 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 980 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 981 | struct io_kiocb *link; |
| 982 | bool do_hashed; |
| 983 | |
| 984 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 985 | |
| 986 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 987 | req->flags); |
| 988 | if (!do_hashed) { |
| 989 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 990 | } else { |
| 991 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 992 | file_inode(req->file)); |
| 993 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 994 | |
| 995 | if (link) |
| 996 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 997 | } |
| 998 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 999 | static void io_kill_timeout(struct io_kiocb *req) |
| 1000 | { |
| 1001 | int ret; |
| 1002 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1003 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1004 | if (ret != -1) { |
| 1005 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1006 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1007 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1008 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1013 | { |
| 1014 | struct io_kiocb *req, *tmp; |
| 1015 | |
| 1016 | spin_lock_irq(&ctx->completion_lock); |
| 1017 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1018 | io_kill_timeout(req); |
| 1019 | spin_unlock_irq(&ctx->completion_lock); |
| 1020 | } |
| 1021 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1022 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1023 | { |
| 1024 | struct io_kiocb *req; |
| 1025 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1026 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 1027 | io_kill_timeout(req); |
| 1028 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1029 | __io_commit_cqring(ctx); |
| 1030 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1031 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1032 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1033 | } |
| 1034 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1035 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1036 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1037 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1038 | unsigned tail; |
| 1039 | |
| 1040 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1041 | /* |
| 1042 | * writes to the cq entry need to come after reading head; the |
| 1043 | * control dependency is enough as we're using WRITE_ONCE to |
| 1044 | * fill the cq entry |
| 1045 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1046 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1047 | return NULL; |
| 1048 | |
| 1049 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1050 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1053 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1054 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1055 | if (!ctx->cq_ev_fd) |
| 1056 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1057 | if (!ctx->eventfd_async) |
| 1058 | return true; |
| 1059 | return io_wq_current_is_worker() || in_interrupt(); |
| 1060 | } |
| 1061 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1062 | 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] | 1063 | { |
| 1064 | if (waitqueue_active(&ctx->wait)) |
| 1065 | wake_up(&ctx->wait); |
| 1066 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1067 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1068 | if (trigger_ev) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1069 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1070 | } |
| 1071 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1072 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1073 | { |
| 1074 | __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx)); |
| 1075 | } |
| 1076 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1077 | /* Returns true if there are no backlogged entries after the flush */ |
| 1078 | 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] | 1079 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1080 | struct io_rings *rings = ctx->rings; |
| 1081 | struct io_uring_cqe *cqe; |
| 1082 | struct io_kiocb *req; |
| 1083 | unsigned long flags; |
| 1084 | LIST_HEAD(list); |
| 1085 | |
| 1086 | if (!force) { |
| 1087 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1088 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1089 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1090 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1091 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1095 | |
| 1096 | /* if force is set, the ring is going away. always drop after that */ |
| 1097 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1098 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1099 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1100 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1101 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1102 | cqe = io_get_cqring(ctx); |
| 1103 | if (!cqe && !force) |
| 1104 | break; |
| 1105 | |
| 1106 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1107 | list); |
| 1108 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1109 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1110 | if (cqe) { |
| 1111 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1112 | WRITE_ONCE(cqe->res, req->result); |
| 1113 | WRITE_ONCE(cqe->flags, 0); |
| 1114 | } else { |
| 1115 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1116 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1121 | if (cqe) { |
| 1122 | clear_bit(0, &ctx->sq_check_overflow); |
| 1123 | clear_bit(0, &ctx->cq_check_overflow); |
| 1124 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1125 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1126 | io_cqring_ev_posted(ctx); |
| 1127 | |
| 1128 | while (!list_empty(&list)) { |
| 1129 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1130 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1131 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1132 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1133 | |
| 1134 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1137 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1138 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1139 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1140 | struct io_uring_cqe *cqe; |
| 1141 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1142 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1143 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1144 | /* |
| 1145 | * If we can't get a cq entry, userspace overflowed the |
| 1146 | * submission (by quite a lot). Increment the overflow count in |
| 1147 | * the ring. |
| 1148 | */ |
| 1149 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1150 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1151 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1152 | WRITE_ONCE(cqe->res, res); |
| 1153 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1154 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1155 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1156 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1157 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1158 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1159 | set_bit(0, &ctx->sq_check_overflow); |
| 1160 | set_bit(0, &ctx->cq_check_overflow); |
| 1161 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1162 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1163 | refcount_inc(&req->refs); |
| 1164 | req->result = res; |
| 1165 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1169 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1170 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1171 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1172 | unsigned long flags; |
| 1173 | |
| 1174 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1175 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1176 | io_commit_cqring(ctx); |
| 1177 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1178 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1179 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1182 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1183 | { |
| 1184 | return req == (struct io_kiocb *) |
| 1185 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1186 | } |
| 1187 | |
| 1188 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1189 | { |
| 1190 | struct io_kiocb *req; |
| 1191 | |
| 1192 | req = ctx->fallback_req; |
| 1193 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1194 | return req; |
| 1195 | |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1199 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1200 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1201 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1202 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1203 | struct io_kiocb *req; |
| 1204 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1205 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1206 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1207 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1208 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1209 | } else if (!state->free_reqs) { |
| 1210 | size_t sz; |
| 1211 | int ret; |
| 1212 | |
| 1213 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1214 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1215 | |
| 1216 | /* |
| 1217 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1218 | * retry single alloc to be on the safe side. |
| 1219 | */ |
| 1220 | if (unlikely(ret <= 0)) { |
| 1221 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1222 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1223 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1224 | ret = 1; |
| 1225 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1226 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1227 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1228 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1229 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1230 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1233 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1234 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1235 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1236 | req->ctx = ctx; |
| 1237 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1238 | /* one is dropped after submission, the other at completion */ |
| 1239 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1240 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1241 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1242 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1243 | fallback: |
| 1244 | req = io_get_fallback_req(ctx); |
| 1245 | if (req) |
| 1246 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1247 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1248 | return NULL; |
| 1249 | } |
| 1250 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1251 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1252 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1253 | if (likely(!io_is_fallback_req(req))) |
| 1254 | kmem_cache_free(req_cachep, req); |
| 1255 | else |
| 1256 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1257 | } |
| 1258 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1259 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1260 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1261 | struct io_ring_ctx *ctx = req->ctx; |
| 1262 | |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1263 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1264 | io_cleanup_req(req); |
| 1265 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1266 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1267 | if (req->file) { |
| 1268 | if (req->flags & REQ_F_FIXED_FILE) |
| 1269 | percpu_ref_put(&ctx->file_data->refs); |
| 1270 | else |
| 1271 | fput(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1272 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1273 | |
| 1274 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | static void __io_free_req(struct io_kiocb *req) |
| 1278 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1279 | __io_req_aux_free(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1280 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1281 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1282 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1283 | unsigned long flags; |
| 1284 | |
| 1285 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1286 | list_del(&req->inflight_entry); |
| 1287 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1288 | wake_up(&ctx->inflight_wait); |
| 1289 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1290 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1291 | |
| 1292 | percpu_ref_put(&req->ctx->refs); |
| 1293 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1294 | } |
| 1295 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1296 | struct req_batch { |
| 1297 | void *reqs[IO_IOPOLL_BATCH]; |
| 1298 | int to_free; |
| 1299 | int need_iter; |
| 1300 | }; |
| 1301 | |
| 1302 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1303 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1304 | int fixed_refs = rb->to_free; |
| 1305 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1306 | if (!rb->to_free) |
| 1307 | return; |
| 1308 | if (rb->need_iter) { |
| 1309 | int i, inflight = 0; |
| 1310 | unsigned long flags; |
| 1311 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1312 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1313 | for (i = 0; i < rb->to_free; i++) { |
| 1314 | struct io_kiocb *req = rb->reqs[i]; |
| 1315 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1316 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1317 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1318 | fixed_refs++; |
| 1319 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1320 | if (req->flags & REQ_F_INFLIGHT) |
| 1321 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1322 | __io_req_aux_free(req); |
| 1323 | } |
| 1324 | if (!inflight) |
| 1325 | goto do_free; |
| 1326 | |
| 1327 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1328 | for (i = 0; i < rb->to_free; i++) { |
| 1329 | struct io_kiocb *req = rb->reqs[i]; |
| 1330 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1331 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1332 | list_del(&req->inflight_entry); |
| 1333 | if (!--inflight) |
| 1334 | break; |
| 1335 | } |
| 1336 | } |
| 1337 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1338 | |
| 1339 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1340 | wake_up(&ctx->inflight_wait); |
| 1341 | } |
| 1342 | do_free: |
| 1343 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1344 | if (fixed_refs) |
| 1345 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1346 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1347 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1348 | } |
| 1349 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1350 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1351 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1352 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1353 | int ret; |
| 1354 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1355 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1356 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1357 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1358 | io_commit_cqring(ctx); |
| 1359 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1360 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1361 | return true; |
| 1362 | } |
| 1363 | |
| 1364 | return false; |
| 1365 | } |
| 1366 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1367 | 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] | 1368 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1369 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1370 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1371 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1372 | /* Already got next link */ |
| 1373 | if (req->flags & REQ_F_LINK_NEXT) |
| 1374 | return; |
| 1375 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1376 | /* |
| 1377 | * The list should never be empty when we are called here. But could |
| 1378 | * potentially happen if the chain is messed up, check to be on the |
| 1379 | * safe side. |
| 1380 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1381 | while (!list_empty(&req->link_list)) { |
| 1382 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1383 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1384 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1385 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1386 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1387 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1388 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1389 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1390 | continue; |
| 1391 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1392 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1393 | list_del_init(&req->link_list); |
| 1394 | if (!list_empty(&nxt->link_list)) |
| 1395 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1396 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1397 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1398 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1399 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1400 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1401 | if (wake_ev) |
| 1402 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1407 | */ |
| 1408 | static void io_fail_links(struct io_kiocb *req) |
| 1409 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1410 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1411 | unsigned long flags; |
| 1412 | |
| 1413 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1414 | |
| 1415 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1416 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1417 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1418 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1419 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1420 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1421 | |
| 1422 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1423 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1424 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1425 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1426 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1427 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1428 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1429 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1430 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1431 | |
| 1432 | io_commit_cqring(ctx); |
| 1433 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1434 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1435 | } |
| 1436 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1437 | 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] | 1438 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1439 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1440 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1441 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1442 | /* |
| 1443 | * If LINK is set, we have dependent requests in this chain. If we |
| 1444 | * didn't fail this request, queue the first one up, moving any other |
| 1445 | * dependencies to the next request. In case of failure, fail the rest |
| 1446 | * of the chain. |
| 1447 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1448 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1449 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1450 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1451 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1452 | struct io_ring_ctx *ctx = req->ctx; |
| 1453 | unsigned long flags; |
| 1454 | |
| 1455 | /* |
| 1456 | * If this is a timeout link, we could be racing with the |
| 1457 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1458 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1459 | */ |
| 1460 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1461 | io_req_link_next(req, nxt); |
| 1462 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1463 | } else { |
| 1464 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1465 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1466 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1467 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1468 | static void io_free_req(struct io_kiocb *req) |
| 1469 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1470 | struct io_kiocb *nxt = NULL; |
| 1471 | |
| 1472 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1473 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1474 | |
| 1475 | if (nxt) |
| 1476 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1477 | } |
| 1478 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1479 | /* |
| 1480 | * Drop reference to request, return next in chain (if there is one) if this |
| 1481 | * was the last reference to this request. |
| 1482 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1483 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1484 | 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] | 1485 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1486 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1487 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1488 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1489 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1492 | static void io_put_req(struct io_kiocb *req) |
| 1493 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1494 | if (refcount_dec_and_test(&req->refs)) |
| 1495 | io_free_req(req); |
| 1496 | } |
| 1497 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1498 | /* |
| 1499 | * Must only be used if we don't need to care about links, usually from |
| 1500 | * within the completion handling itself. |
| 1501 | */ |
| 1502 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1503 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1504 | /* drop both submit and complete references */ |
| 1505 | if (refcount_sub_and_test(2, &req->refs)) |
| 1506 | __io_free_req(req); |
| 1507 | } |
| 1508 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1509 | static void io_double_put_req(struct io_kiocb *req) |
| 1510 | { |
| 1511 | /* drop both submit and complete references */ |
| 1512 | if (refcount_sub_and_test(2, &req->refs)) |
| 1513 | io_free_req(req); |
| 1514 | } |
| 1515 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1516 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1517 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1518 | struct io_rings *rings = ctx->rings; |
| 1519 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1520 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1521 | /* |
| 1522 | * noflush == true is from the waitqueue handler, just ensure |
| 1523 | * we wake up the task, and the next invocation will flush the |
| 1524 | * entries. We cannot safely to it from here. |
| 1525 | */ |
| 1526 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1527 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1528 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1529 | io_cqring_overflow_flush(ctx, false); |
| 1530 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1531 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1532 | /* See comment at the top of this file */ |
| 1533 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1534 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1535 | } |
| 1536 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1537 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1538 | { |
| 1539 | struct io_rings *rings = ctx->rings; |
| 1540 | |
| 1541 | /* make sure SQ entry isn't read before tail */ |
| 1542 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1543 | } |
| 1544 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1545 | 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] | 1546 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1547 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1548 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1549 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1550 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1551 | rb->need_iter++; |
| 1552 | |
| 1553 | rb->reqs[rb->to_free++] = req; |
| 1554 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1555 | io_free_req_many(req->ctx, rb); |
| 1556 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1559 | /* |
| 1560 | * Find and free completed poll iocbs |
| 1561 | */ |
| 1562 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1563 | struct list_head *done) |
| 1564 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1565 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1566 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1567 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1568 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1569 | while (!list_empty(done)) { |
| 1570 | req = list_first_entry(done, struct io_kiocb, list); |
| 1571 | list_del(&req->list); |
| 1572 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1573 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1574 | (*nr_events)++; |
| 1575 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1576 | if (refcount_dec_and_test(&req->refs) && |
| 1577 | !io_req_multi_free(&rb, req)) |
| 1578 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1579 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1580 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1581 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1582 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1586 | long min) |
| 1587 | { |
| 1588 | struct io_kiocb *req, *tmp; |
| 1589 | LIST_HEAD(done); |
| 1590 | bool spin; |
| 1591 | int ret; |
| 1592 | |
| 1593 | /* |
| 1594 | * Only spin for completions if we don't have multiple devices hanging |
| 1595 | * off our complete list, and we're under the requested amount. |
| 1596 | */ |
| 1597 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1598 | |
| 1599 | ret = 0; |
| 1600 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1601 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1602 | |
| 1603 | /* |
| 1604 | * Move completed entries to our local list. If we find a |
| 1605 | * request that requires polling, break out and complete |
| 1606 | * the done list first, if we have entries there. |
| 1607 | */ |
| 1608 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1609 | list_move_tail(&req->list, &done); |
| 1610 | continue; |
| 1611 | } |
| 1612 | if (!list_empty(&done)) |
| 1613 | break; |
| 1614 | |
| 1615 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1616 | if (ret < 0) |
| 1617 | break; |
| 1618 | |
| 1619 | if (ret && spin) |
| 1620 | spin = false; |
| 1621 | ret = 0; |
| 1622 | } |
| 1623 | |
| 1624 | if (!list_empty(&done)) |
| 1625 | io_iopoll_complete(ctx, nr_events, &done); |
| 1626 | |
| 1627 | return ret; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1631 | * 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] | 1632 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1633 | * as a non-spinning completion check. |
| 1634 | */ |
| 1635 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1636 | long min) |
| 1637 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1638 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1639 | int ret; |
| 1640 | |
| 1641 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1642 | if (ret < 0) |
| 1643 | return ret; |
| 1644 | if (!min || *nr_events >= min) |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | return 1; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * We can't just wait for polled events to come to us, we have to actively |
| 1653 | * find and complete them. |
| 1654 | */ |
| 1655 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1656 | { |
| 1657 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1658 | return; |
| 1659 | |
| 1660 | mutex_lock(&ctx->uring_lock); |
| 1661 | while (!list_empty(&ctx->poll_list)) { |
| 1662 | unsigned int nr_events = 0; |
| 1663 | |
| 1664 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1665 | |
| 1666 | /* |
| 1667 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1668 | * in this case we need to ensure that we reap all events. |
| 1669 | */ |
| 1670 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1671 | } |
| 1672 | mutex_unlock(&ctx->uring_lock); |
| 1673 | } |
| 1674 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1675 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1676 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1677 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1678 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1679 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1680 | /* |
| 1681 | * We disallow the app entering submit/complete with polling, but we |
| 1682 | * still need to lock the ring to prevent racing with polled issue |
| 1683 | * that got punted to a workqueue. |
| 1684 | */ |
| 1685 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1686 | do { |
| 1687 | int tmin = 0; |
| 1688 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1689 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1690 | * Don't enter poll loop if we already have events pending. |
| 1691 | * If we do, we can potentially be spinning for commands that |
| 1692 | * already triggered a CQE (eg in error). |
| 1693 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1694 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1695 | break; |
| 1696 | |
| 1697 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1698 | * If a submit got punted to a workqueue, we can have the |
| 1699 | * application entering polling for a command before it gets |
| 1700 | * issued. That app will hold the uring_lock for the duration |
| 1701 | * of the poll right here, so we need to take a breather every |
| 1702 | * now and then to ensure that the issue has a chance to add |
| 1703 | * the poll to the issued list. Otherwise we can spin here |
| 1704 | * forever, while the workqueue is stuck trying to acquire the |
| 1705 | * very same mutex. |
| 1706 | */ |
| 1707 | if (!(++iters & 7)) { |
| 1708 | mutex_unlock(&ctx->uring_lock); |
| 1709 | mutex_lock(&ctx->uring_lock); |
| 1710 | } |
| 1711 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1712 | if (*nr_events < min) |
| 1713 | tmin = min - *nr_events; |
| 1714 | |
| 1715 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1716 | if (ret <= 0) |
| 1717 | break; |
| 1718 | ret = 0; |
| 1719 | } while (min && !*nr_events && !need_resched()); |
| 1720 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1721 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1722 | return ret; |
| 1723 | } |
| 1724 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1725 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1726 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1727 | /* |
| 1728 | * Tell lockdep we inherited freeze protection from submission |
| 1729 | * thread. |
| 1730 | */ |
| 1731 | if (req->flags & REQ_F_ISREG) { |
| 1732 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1733 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1734 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1735 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1736 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1739 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1740 | { |
| 1741 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1742 | req->flags |= REQ_F_FAIL_LINK; |
| 1743 | } |
| 1744 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1745 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1746 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1747 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1748 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1749 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1750 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1751 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1752 | if (res != req->result) |
| 1753 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1754 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1758 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1759 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1760 | |
| 1761 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1762 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1765 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1766 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1767 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1768 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1769 | |
| 1770 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1771 | io_put_req_find_next(req, &nxt); |
| 1772 | |
| 1773 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1776 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1777 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1778 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1779 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1780 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1781 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1782 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1783 | if (res != req->result) |
| 1784 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1785 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1786 | if (res != -EAGAIN) |
| 1787 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1788 | } |
| 1789 | |
| 1790 | /* |
| 1791 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1792 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1793 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1794 | * accessing the kiocb cookie. |
| 1795 | */ |
| 1796 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1797 | { |
| 1798 | struct io_ring_ctx *ctx = req->ctx; |
| 1799 | |
| 1800 | /* |
| 1801 | * Track whether we have multiple files in our lists. This will impact |
| 1802 | * how we do polling eventually, not spinning if we're on potentially |
| 1803 | * different devices. |
| 1804 | */ |
| 1805 | if (list_empty(&ctx->poll_list)) { |
| 1806 | ctx->poll_multi_file = false; |
| 1807 | } else if (!ctx->poll_multi_file) { |
| 1808 | struct io_kiocb *list_req; |
| 1809 | |
| 1810 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1811 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1812 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1813 | ctx->poll_multi_file = true; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * For fast devices, IO may have already completed. If it has, add |
| 1818 | * it to the front so we find it first. |
| 1819 | */ |
| 1820 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1821 | list_add(&req->list, &ctx->poll_list); |
| 1822 | else |
| 1823 | list_add_tail(&req->list, &ctx->poll_list); |
| 1824 | } |
| 1825 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1826 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1827 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1828 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1829 | int diff = state->has_refs - state->used_refs; |
| 1830 | |
| 1831 | if (diff) |
| 1832 | fput_many(state->file, diff); |
| 1833 | state->file = NULL; |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | /* |
| 1838 | * Get as many references to a file as we have IOs left in this submission, |
| 1839 | * assuming most submissions are for one file, or at least that each file |
| 1840 | * has more than one submission. |
| 1841 | */ |
| 1842 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1843 | { |
| 1844 | if (!state) |
| 1845 | return fget(fd); |
| 1846 | |
| 1847 | if (state->file) { |
| 1848 | if (state->fd == fd) { |
| 1849 | state->used_refs++; |
| 1850 | state->ios_left--; |
| 1851 | return state->file; |
| 1852 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1853 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1854 | } |
| 1855 | state->file = fget_many(fd, state->ios_left); |
| 1856 | if (!state->file) |
| 1857 | return NULL; |
| 1858 | |
| 1859 | state->fd = fd; |
| 1860 | state->has_refs = state->ios_left; |
| 1861 | state->used_refs = 1; |
| 1862 | state->ios_left--; |
| 1863 | return state->file; |
| 1864 | } |
| 1865 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1866 | /* |
| 1867 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1868 | * any file. For now, just ensure that anything potentially problematic is done |
| 1869 | * inline. |
| 1870 | */ |
| 1871 | static bool io_file_supports_async(struct file *file) |
| 1872 | { |
| 1873 | umode_t mode = file_inode(file)->i_mode; |
| 1874 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1875 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1876 | return true; |
| 1877 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1878 | return true; |
| 1879 | |
| 1880 | return false; |
| 1881 | } |
| 1882 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1883 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1884 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1885 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1886 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1887 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1888 | unsigned ioprio; |
| 1889 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1890 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1891 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1892 | req->flags |= REQ_F_ISREG; |
| 1893 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1894 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1895 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1896 | req->flags |= REQ_F_CUR_POS; |
| 1897 | kiocb->ki_pos = req->file->f_pos; |
| 1898 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1899 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 1900 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1901 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1902 | if (unlikely(ret)) |
| 1903 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1904 | |
| 1905 | ioprio = READ_ONCE(sqe->ioprio); |
| 1906 | if (ioprio) { |
| 1907 | ret = ioprio_check_cap(ioprio); |
| 1908 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1909 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1910 | |
| 1911 | kiocb->ki_ioprio = ioprio; |
| 1912 | } else |
| 1913 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1914 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1915 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1916 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1917 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1918 | req->flags |= REQ_F_NOWAIT; |
| 1919 | |
| 1920 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1921 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1922 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1923 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1924 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1925 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1926 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1927 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1928 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1929 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1930 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1931 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1932 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1933 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1934 | kiocb->ki_complete = io_complete_rw; |
| 1935 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1936 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1937 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1938 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1939 | /* we own ->private, reuse it for the buffer index */ |
| 1940 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1941 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1942 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1946 | { |
| 1947 | switch (ret) { |
| 1948 | case -EIOCBQUEUED: |
| 1949 | break; |
| 1950 | case -ERESTARTSYS: |
| 1951 | case -ERESTARTNOINTR: |
| 1952 | case -ERESTARTNOHAND: |
| 1953 | case -ERESTART_RESTARTBLOCK: |
| 1954 | /* |
| 1955 | * We can't just restart the syscall, since previously |
| 1956 | * submitted sqes may already be in progress. Just fail this |
| 1957 | * IO with EINTR. |
| 1958 | */ |
| 1959 | ret = -EINTR; |
| 1960 | /* fall through */ |
| 1961 | default: |
| 1962 | kiocb->ki_complete(kiocb, ret, 0); |
| 1963 | } |
| 1964 | } |
| 1965 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1966 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1967 | bool in_async) |
| 1968 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1969 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1970 | |
| 1971 | if (req->flags & REQ_F_CUR_POS) |
| 1972 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1973 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1974 | *nxt = __io_complete_rw(kiocb, ret); |
| 1975 | else |
| 1976 | io_rw_done(kiocb, ret); |
| 1977 | } |
| 1978 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1979 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1980 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1981 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1982 | struct io_ring_ctx *ctx = req->ctx; |
| 1983 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1984 | struct io_mapped_ubuf *imu; |
| 1985 | unsigned index, buf_index; |
| 1986 | size_t offset; |
| 1987 | u64 buf_addr; |
| 1988 | |
| 1989 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1990 | if (unlikely(!ctx->user_bufs)) |
| 1991 | return -EFAULT; |
| 1992 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1993 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1994 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1995 | return -EFAULT; |
| 1996 | |
| 1997 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1998 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1999 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2000 | |
| 2001 | /* overflow */ |
| 2002 | if (buf_addr + len < buf_addr) |
| 2003 | return -EFAULT; |
| 2004 | /* not inside the mapped region */ |
| 2005 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2006 | return -EFAULT; |
| 2007 | |
| 2008 | /* |
| 2009 | * May not be a start of buffer, set size appropriately |
| 2010 | * and advance us to the beginning. |
| 2011 | */ |
| 2012 | offset = buf_addr - imu->ubuf; |
| 2013 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2014 | |
| 2015 | if (offset) { |
| 2016 | /* |
| 2017 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2018 | * using the latter parts of a big fixed buffer - it iterates |
| 2019 | * over each segment manually. We can cheat a bit here, because |
| 2020 | * we know that: |
| 2021 | * |
| 2022 | * 1) it's a BVEC iter, we set it up |
| 2023 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2024 | * first and last bvec |
| 2025 | * |
| 2026 | * So just find our index, and adjust the iterator afterwards. |
| 2027 | * If the offset is within the first bvec (or the whole first |
| 2028 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2029 | * since we can just skip the first segment, which may not |
| 2030 | * be PAGE_SIZE aligned. |
| 2031 | */ |
| 2032 | const struct bio_vec *bvec = imu->bvec; |
| 2033 | |
| 2034 | if (offset <= bvec->bv_len) { |
| 2035 | iov_iter_advance(iter, offset); |
| 2036 | } else { |
| 2037 | unsigned long seg_skip; |
| 2038 | |
| 2039 | /* skip first vec */ |
| 2040 | offset -= bvec->bv_len; |
| 2041 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2042 | |
| 2043 | iter->bvec = bvec + seg_skip; |
| 2044 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2045 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2046 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2050 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2053 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2054 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2055 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2056 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2057 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2058 | u8 opcode; |
| 2059 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2060 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2061 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2062 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2063 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2064 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2065 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2066 | /* buffer index only valid with fixed read/write */ |
| 2067 | if (req->rw.kiocb.private) |
| 2068 | return -EINVAL; |
| 2069 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2070 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2071 | ssize_t ret; |
| 2072 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2073 | *iovec = NULL; |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2077 | if (req->io) { |
| 2078 | struct io_async_rw *iorw = &req->io->rw; |
| 2079 | |
| 2080 | *iovec = iorw->iov; |
| 2081 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2082 | if (iorw->iov == iorw->fast_iov) |
| 2083 | *iovec = NULL; |
| 2084 | return iorw->size; |
| 2085 | } |
| 2086 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2087 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2088 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2089 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2090 | iovec, iter); |
| 2091 | #endif |
| 2092 | |
| 2093 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2094 | } |
| 2095 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2096 | /* |
| 2097 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2098 | * by looping over ->read() or ->write() manually. |
| 2099 | */ |
| 2100 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2101 | struct iov_iter *iter) |
| 2102 | { |
| 2103 | ssize_t ret = 0; |
| 2104 | |
| 2105 | /* |
| 2106 | * Don't support polled IO through this interface, and we can't |
| 2107 | * support non-blocking either. For the latter, this just causes |
| 2108 | * the kiocb to be handled from an async context. |
| 2109 | */ |
| 2110 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2111 | return -EOPNOTSUPP; |
| 2112 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2113 | return -EAGAIN; |
| 2114 | |
| 2115 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2116 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2117 | ssize_t nr; |
| 2118 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2119 | if (!iov_iter_is_bvec(iter)) { |
| 2120 | iovec = iov_iter_iovec(iter); |
| 2121 | } else { |
| 2122 | /* fixed buffers import bvec */ |
| 2123 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2124 | + iter->iov_offset; |
| 2125 | iovec.iov_len = min(iter->count, |
| 2126 | iter->bvec->bv_len - iter->iov_offset); |
| 2127 | } |
| 2128 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2129 | if (rw == READ) { |
| 2130 | nr = file->f_op->read(file, iovec.iov_base, |
| 2131 | iovec.iov_len, &kiocb->ki_pos); |
| 2132 | } else { |
| 2133 | nr = file->f_op->write(file, iovec.iov_base, |
| 2134 | iovec.iov_len, &kiocb->ki_pos); |
| 2135 | } |
| 2136 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2137 | if (iov_iter_is_bvec(iter)) |
| 2138 | kunmap(iter->bvec->bv_page); |
| 2139 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2140 | if (nr < 0) { |
| 2141 | if (!ret) |
| 2142 | ret = nr; |
| 2143 | break; |
| 2144 | } |
| 2145 | ret += nr; |
| 2146 | if (nr != iovec.iov_len) |
| 2147 | break; |
| 2148 | iov_iter_advance(iter, nr); |
| 2149 | } |
| 2150 | |
| 2151 | return ret; |
| 2152 | } |
| 2153 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2154 | 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] | 2155 | struct iovec *iovec, struct iovec *fast_iov, |
| 2156 | struct iov_iter *iter) |
| 2157 | { |
| 2158 | req->io->rw.nr_segs = iter->nr_segs; |
| 2159 | req->io->rw.size = io_size; |
| 2160 | req->io->rw.iov = iovec; |
| 2161 | if (!req->io->rw.iov) { |
| 2162 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2163 | memcpy(req->io->rw.iov, fast_iov, |
| 2164 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2165 | } else { |
| 2166 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2170 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2171 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2172 | if (!io_op_defs[req->opcode].async_ctx) |
| 2173 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2174 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2175 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2178 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2179 | struct iovec *iovec, struct iovec *fast_iov, |
| 2180 | struct iov_iter *iter) |
| 2181 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2182 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2183 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2184 | if (!req->io) { |
| 2185 | if (io_alloc_async_ctx(req)) |
| 2186 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2187 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2188 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2189 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2190 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2193 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2194 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2195 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2196 | struct io_async_ctx *io; |
| 2197 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2198 | ssize_t ret; |
| 2199 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2200 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2201 | if (ret) |
| 2202 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2203 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2204 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2205 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2206 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2207 | /* either don't need iovec imported or already have it */ |
| 2208 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2209 | return 0; |
| 2210 | |
| 2211 | io = req->io; |
| 2212 | io->rw.iov = io->rw.fast_iov; |
| 2213 | req->io = NULL; |
| 2214 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2215 | req->io = io; |
| 2216 | if (ret < 0) |
| 2217 | return ret; |
| 2218 | |
| 2219 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2220 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2223 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2224 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2225 | { |
| 2226 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2227 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2228 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2229 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2230 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2231 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2232 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2233 | if (ret < 0) |
| 2234 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2235 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2236 | /* Ensure we clear previously set non-block flag */ |
| 2237 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2238 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2239 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2240 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2241 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2242 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2243 | req->result = io_size; |
| 2244 | |
| 2245 | /* |
| 2246 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2247 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2248 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2249 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2250 | req->flags |= REQ_F_MUST_PUNT; |
| 2251 | goto copy_iov; |
| 2252 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2253 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2254 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2255 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2256 | if (!ret) { |
| 2257 | ssize_t ret2; |
| 2258 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2259 | if (req->file->f_op->read_iter) |
| 2260 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2261 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2262 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2263 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2264 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2265 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2266 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2267 | } else { |
| 2268 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2269 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2270 | inline_vecs, &iter); |
| 2271 | if (ret) |
| 2272 | goto out_free; |
| 2273 | return -EAGAIN; |
| 2274 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2275 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2276 | out_free: |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2277 | kfree(iovec); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2278 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2279 | return ret; |
| 2280 | } |
| 2281 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2282 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2283 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2284 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2285 | struct io_async_ctx *io; |
| 2286 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2287 | ssize_t ret; |
| 2288 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2289 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2290 | if (ret) |
| 2291 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2292 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2293 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2294 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2295 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2296 | /* either don't need iovec imported or already have it */ |
| 2297 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2298 | return 0; |
| 2299 | |
| 2300 | io = req->io; |
| 2301 | io->rw.iov = io->rw.fast_iov; |
| 2302 | req->io = NULL; |
| 2303 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2304 | req->io = io; |
| 2305 | if (ret < 0) |
| 2306 | return ret; |
| 2307 | |
| 2308 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2309 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2312 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2313 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2314 | { |
| 2315 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2316 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2317 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2318 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2319 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2320 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2321 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2322 | if (ret < 0) |
| 2323 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2324 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2325 | /* Ensure we clear previously set non-block flag */ |
| 2326 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2327 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2328 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2329 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2330 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2331 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2332 | req->result = io_size; |
| 2333 | |
| 2334 | /* |
| 2335 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2336 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2337 | */ |
| 2338 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2339 | req->flags |= REQ_F_MUST_PUNT; |
| 2340 | goto copy_iov; |
| 2341 | } |
| 2342 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2343 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2344 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2345 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2346 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2347 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2348 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2349 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2350 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2351 | ssize_t ret2; |
| 2352 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2353 | /* |
| 2354 | * Open-code file_start_write here to grab freeze protection, |
| 2355 | * which will be released by another thread in |
| 2356 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2357 | * released so that it doesn't complain about the held lock when |
| 2358 | * we return to userspace. |
| 2359 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2360 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2361 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2362 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2363 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2364 | SB_FREEZE_WRITE); |
| 2365 | } |
| 2366 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2367 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2368 | if (req->file->f_op->write_iter) |
| 2369 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2370 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2371 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 2372 | /* |
| 2373 | * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 2374 | * retry them without IOCB_NOWAIT. |
| 2375 | */ |
| 2376 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 2377 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2378 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2379 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2380 | } else { |
| 2381 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2382 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2383 | inline_vecs, &iter); |
| 2384 | if (ret) |
| 2385 | goto out_free; |
| 2386 | return -EAGAIN; |
| 2387 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2388 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2389 | out_free: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2390 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2391 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2392 | return ret; |
| 2393 | } |
| 2394 | |
| 2395 | /* |
| 2396 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2397 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2398 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2399 | { |
| 2400 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2401 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2402 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2403 | return -EINVAL; |
| 2404 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2405 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2406 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2407 | return 0; |
| 2408 | } |
| 2409 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2410 | 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] | 2411 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2412 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2413 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2414 | if (!req->file) |
| 2415 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2416 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2417 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2418 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2419 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2420 | return -EINVAL; |
| 2421 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2422 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2423 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2424 | return -EINVAL; |
| 2425 | |
| 2426 | req->sync.off = READ_ONCE(sqe->off); |
| 2427 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2428 | return 0; |
| 2429 | } |
| 2430 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2431 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2432 | { |
| 2433 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2434 | req_set_fail_links(req); |
| 2435 | io_cqring_add_event(req, -ECANCELED); |
| 2436 | io_put_req(req); |
| 2437 | return true; |
| 2438 | } |
| 2439 | |
| 2440 | return false; |
| 2441 | } |
| 2442 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2443 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2444 | { |
| 2445 | struct io_wq_work *work = *workptr; |
| 2446 | struct io_kiocb *link = work->data; |
| 2447 | |
| 2448 | io_queue_linked_timeout(link); |
| 2449 | work->func = io_wq_submit_work; |
| 2450 | } |
| 2451 | |
| 2452 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2453 | { |
| 2454 | struct io_kiocb *link; |
| 2455 | |
| 2456 | io_prep_async_work(nxt, &link); |
| 2457 | *workptr = &nxt->work; |
| 2458 | if (link) { |
| 2459 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2460 | nxt->work.func = io_link_work_cb; |
| 2461 | nxt->work.data = link; |
| 2462 | } |
| 2463 | } |
| 2464 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2465 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2466 | { |
| 2467 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2468 | loff_t end = req->sync.off + req->sync.len; |
| 2469 | struct io_kiocb *nxt = NULL; |
| 2470 | int ret; |
| 2471 | |
| 2472 | if (io_req_cancelled(req)) |
| 2473 | return; |
| 2474 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2475 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2476 | end > 0 ? end : LLONG_MAX, |
| 2477 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2478 | if (ret < 0) |
| 2479 | req_set_fail_links(req); |
| 2480 | io_cqring_add_event(req, ret); |
| 2481 | io_put_req_find_next(req, &nxt); |
| 2482 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2483 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2486 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2487 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2488 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2489 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2490 | |
| 2491 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2492 | if (force_nonblock) { |
| 2493 | io_put_req(req); |
| 2494 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2495 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2496 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2497 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2498 | work = old_work = &req->work; |
| 2499 | io_fsync_finish(&work); |
| 2500 | if (work && work != old_work) |
| 2501 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2502 | return 0; |
| 2503 | } |
| 2504 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2505 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2506 | { |
| 2507 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2508 | struct io_kiocb *nxt = NULL; |
| 2509 | int ret; |
| 2510 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 2511 | if (io_req_cancelled(req)) |
| 2512 | return; |
| 2513 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2514 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2515 | req->sync.len); |
| 2516 | if (ret < 0) |
| 2517 | req_set_fail_links(req); |
| 2518 | io_cqring_add_event(req, ret); |
| 2519 | io_put_req_find_next(req, &nxt); |
| 2520 | if (nxt) |
| 2521 | io_wq_assign_next(workptr, nxt); |
| 2522 | } |
| 2523 | |
| 2524 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2525 | const struct io_uring_sqe *sqe) |
| 2526 | { |
| 2527 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2528 | return -EINVAL; |
| 2529 | |
| 2530 | req->sync.off = READ_ONCE(sqe->off); |
| 2531 | req->sync.len = READ_ONCE(sqe->addr); |
| 2532 | req->sync.mode = READ_ONCE(sqe->len); |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2537 | bool force_nonblock) |
| 2538 | { |
| 2539 | struct io_wq_work *work, *old_work; |
| 2540 | |
| 2541 | /* fallocate always requiring blocking context */ |
| 2542 | if (force_nonblock) { |
| 2543 | io_put_req(req); |
| 2544 | req->work.func = io_fallocate_finish; |
| 2545 | return -EAGAIN; |
| 2546 | } |
| 2547 | |
| 2548 | work = old_work = &req->work; |
| 2549 | io_fallocate_finish(&work); |
| 2550 | if (work && work != old_work) |
| 2551 | *nxt = container_of(work, struct io_kiocb, work); |
| 2552 | |
| 2553 | return 0; |
| 2554 | } |
| 2555 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2556 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2557 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2558 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2559 | int ret; |
| 2560 | |
| 2561 | if (sqe->ioprio || sqe->buf_index) |
| 2562 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2563 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2564 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2565 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2566 | return 0; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2567 | |
| 2568 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2569 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2570 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2571 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2572 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2573 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2574 | if (IS_ERR(req->open.filename)) { |
| 2575 | ret = PTR_ERR(req->open.filename); |
| 2576 | req->open.filename = NULL; |
| 2577 | return ret; |
| 2578 | } |
| 2579 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2580 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2581 | return 0; |
| 2582 | } |
| 2583 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2584 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2585 | { |
| 2586 | struct open_how __user *how; |
| 2587 | const char __user *fname; |
| 2588 | size_t len; |
| 2589 | int ret; |
| 2590 | |
| 2591 | if (sqe->ioprio || sqe->buf_index) |
| 2592 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2593 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2594 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2595 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2596 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2597 | |
| 2598 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2599 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2600 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2601 | len = READ_ONCE(sqe->len); |
| 2602 | |
| 2603 | if (len < OPEN_HOW_SIZE_VER0) |
| 2604 | return -EINVAL; |
| 2605 | |
| 2606 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2607 | len); |
| 2608 | if (ret) |
| 2609 | return ret; |
| 2610 | |
| 2611 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2612 | req->open.how.flags |= O_LARGEFILE; |
| 2613 | |
| 2614 | req->open.filename = getname(fname); |
| 2615 | if (IS_ERR(req->open.filename)) { |
| 2616 | ret = PTR_ERR(req->open.filename); |
| 2617 | req->open.filename = NULL; |
| 2618 | return ret; |
| 2619 | } |
| 2620 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2621 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2622 | return 0; |
| 2623 | } |
| 2624 | |
| 2625 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2626 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2627 | { |
| 2628 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2629 | struct file *file; |
| 2630 | int ret; |
| 2631 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2632 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2633 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2634 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2635 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2636 | if (ret) |
| 2637 | goto err; |
| 2638 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2639 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2640 | if (ret < 0) |
| 2641 | goto err; |
| 2642 | |
| 2643 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2644 | if (IS_ERR(file)) { |
| 2645 | put_unused_fd(ret); |
| 2646 | ret = PTR_ERR(file); |
| 2647 | } else { |
| 2648 | fsnotify_open(file); |
| 2649 | fd_install(ret, file); |
| 2650 | } |
| 2651 | err: |
| 2652 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2653 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2654 | if (ret < 0) |
| 2655 | req_set_fail_links(req); |
| 2656 | io_cqring_add_event(req, ret); |
| 2657 | io_put_req_find_next(req, nxt); |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2661 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2662 | bool force_nonblock) |
| 2663 | { |
| 2664 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2665 | return io_openat2(req, nxt, force_nonblock); |
| 2666 | } |
| 2667 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 2668 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 2669 | const struct io_uring_sqe *sqe) |
| 2670 | { |
| 2671 | #if defined(CONFIG_EPOLL) |
| 2672 | if (sqe->ioprio || sqe->buf_index) |
| 2673 | return -EINVAL; |
| 2674 | |
| 2675 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 2676 | req->epoll.op = READ_ONCE(sqe->len); |
| 2677 | req->epoll.fd = READ_ONCE(sqe->off); |
| 2678 | |
| 2679 | if (ep_op_has_event(req->epoll.op)) { |
| 2680 | struct epoll_event __user *ev; |
| 2681 | |
| 2682 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2683 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 2684 | return -EFAULT; |
| 2685 | } |
| 2686 | |
| 2687 | return 0; |
| 2688 | #else |
| 2689 | return -EOPNOTSUPP; |
| 2690 | #endif |
| 2691 | } |
| 2692 | |
| 2693 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2694 | bool force_nonblock) |
| 2695 | { |
| 2696 | #if defined(CONFIG_EPOLL) |
| 2697 | struct io_epoll *ie = &req->epoll; |
| 2698 | int ret; |
| 2699 | |
| 2700 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 2701 | if (force_nonblock && ret == -EAGAIN) |
| 2702 | return -EAGAIN; |
| 2703 | |
| 2704 | if (ret < 0) |
| 2705 | req_set_fail_links(req); |
| 2706 | io_cqring_add_event(req, ret); |
| 2707 | io_put_req_find_next(req, nxt); |
| 2708 | return 0; |
| 2709 | #else |
| 2710 | return -EOPNOTSUPP; |
| 2711 | #endif |
| 2712 | } |
| 2713 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2714 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2715 | { |
| 2716 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2717 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2718 | return -EINVAL; |
| 2719 | |
| 2720 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2721 | req->madvise.len = READ_ONCE(sqe->len); |
| 2722 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2723 | return 0; |
| 2724 | #else |
| 2725 | return -EOPNOTSUPP; |
| 2726 | #endif |
| 2727 | } |
| 2728 | |
| 2729 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2730 | bool force_nonblock) |
| 2731 | { |
| 2732 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2733 | struct io_madvise *ma = &req->madvise; |
| 2734 | int ret; |
| 2735 | |
| 2736 | if (force_nonblock) |
| 2737 | return -EAGAIN; |
| 2738 | |
| 2739 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2740 | if (ret < 0) |
| 2741 | req_set_fail_links(req); |
| 2742 | io_cqring_add_event(req, ret); |
| 2743 | io_put_req_find_next(req, nxt); |
| 2744 | return 0; |
| 2745 | #else |
| 2746 | return -EOPNOTSUPP; |
| 2747 | #endif |
| 2748 | } |
| 2749 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2750 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2751 | { |
| 2752 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2753 | return -EINVAL; |
| 2754 | |
| 2755 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2756 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2757 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2758 | return 0; |
| 2759 | } |
| 2760 | |
| 2761 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2762 | bool force_nonblock) |
| 2763 | { |
| 2764 | struct io_fadvise *fa = &req->fadvise; |
| 2765 | int ret; |
| 2766 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 2767 | if (force_nonblock) { |
| 2768 | switch (fa->advice) { |
| 2769 | case POSIX_FADV_NORMAL: |
| 2770 | case POSIX_FADV_RANDOM: |
| 2771 | case POSIX_FADV_SEQUENTIAL: |
| 2772 | break; |
| 2773 | default: |
| 2774 | return -EAGAIN; |
| 2775 | } |
| 2776 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2777 | |
| 2778 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2779 | if (ret < 0) |
| 2780 | req_set_fail_links(req); |
| 2781 | io_cqring_add_event(req, ret); |
| 2782 | io_put_req_find_next(req, nxt); |
| 2783 | return 0; |
| 2784 | } |
| 2785 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2786 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2787 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2788 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2789 | unsigned lookup_flags; |
| 2790 | int ret; |
| 2791 | |
| 2792 | if (sqe->ioprio || sqe->buf_index) |
| 2793 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2794 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2795 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2796 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2797 | return 0; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2798 | |
| 2799 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2800 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2801 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2802 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2803 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2804 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2805 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2806 | return -EINVAL; |
| 2807 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2808 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2809 | if (IS_ERR(req->open.filename)) { |
| 2810 | ret = PTR_ERR(req->open.filename); |
| 2811 | req->open.filename = NULL; |
| 2812 | return ret; |
| 2813 | } |
| 2814 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2815 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2820 | bool force_nonblock) |
| 2821 | { |
| 2822 | struct io_open *ctx = &req->open; |
| 2823 | unsigned lookup_flags; |
| 2824 | struct path path; |
| 2825 | struct kstat stat; |
| 2826 | int ret; |
| 2827 | |
| 2828 | if (force_nonblock) |
| 2829 | return -EAGAIN; |
| 2830 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2831 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2832 | return -EINVAL; |
| 2833 | |
| 2834 | retry: |
| 2835 | /* filename_lookup() drops it, keep a reference */ |
| 2836 | ctx->filename->refcnt++; |
| 2837 | |
| 2838 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2839 | NULL); |
| 2840 | if (ret) |
| 2841 | goto err; |
| 2842 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2843 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2844 | path_put(&path); |
| 2845 | if (retry_estale(ret, lookup_flags)) { |
| 2846 | lookup_flags |= LOOKUP_REVAL; |
| 2847 | goto retry; |
| 2848 | } |
| 2849 | if (!ret) |
| 2850 | ret = cp_statx(&stat, ctx->buffer); |
| 2851 | err: |
| 2852 | putname(ctx->filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2853 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2854 | if (ret < 0) |
| 2855 | req_set_fail_links(req); |
| 2856 | io_cqring_add_event(req, ret); |
| 2857 | io_put_req_find_next(req, nxt); |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2861 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2862 | { |
| 2863 | /* |
| 2864 | * If we queue this for async, it must not be cancellable. That would |
| 2865 | * leave the 'file' in an undeterminate state. |
| 2866 | */ |
| 2867 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2868 | |
| 2869 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2870 | sqe->rw_flags || sqe->buf_index) |
| 2871 | return -EINVAL; |
| 2872 | if (sqe->flags & IOSQE_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2873 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2874 | |
| 2875 | req->close.fd = READ_ONCE(sqe->fd); |
| 2876 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2877 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2878 | return -EBADF; |
| 2879 | |
| 2880 | return 0; |
| 2881 | } |
| 2882 | |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 2883 | /* only called when __close_fd_get_file() is done */ |
| 2884 | static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt) |
| 2885 | { |
| 2886 | int ret; |
| 2887 | |
| 2888 | ret = filp_close(req->close.put_file, req->work.files); |
| 2889 | if (ret < 0) |
| 2890 | req_set_fail_links(req); |
| 2891 | io_cqring_add_event(req, ret); |
| 2892 | fput(req->close.put_file); |
| 2893 | io_put_req_find_next(req, nxt); |
| 2894 | } |
| 2895 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2896 | static void io_close_finish(struct io_wq_work **workptr) |
| 2897 | { |
| 2898 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2899 | struct io_kiocb *nxt = NULL; |
| 2900 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 2901 | /* not cancellable, don't do io_req_cancelled() */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 2902 | __io_close_finish(req, &nxt); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2903 | if (nxt) |
| 2904 | io_wq_assign_next(workptr, nxt); |
| 2905 | } |
| 2906 | |
| 2907 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2908 | bool force_nonblock) |
| 2909 | { |
| 2910 | int ret; |
| 2911 | |
| 2912 | req->close.put_file = NULL; |
| 2913 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2914 | if (ret < 0) |
| 2915 | return ret; |
| 2916 | |
| 2917 | /* 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] | 2918 | 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] | 2919 | goto eagain; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2920 | |
| 2921 | /* |
| 2922 | * No ->flush(), safely close from here and just punt the |
| 2923 | * fput() to async context. |
| 2924 | */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 2925 | __io_close_finish(req, nxt); |
| 2926 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2927 | eagain: |
| 2928 | req->work.func = io_close_finish; |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 2929 | /* |
| 2930 | * Do manual async queue here to avoid grabbing files - we don't |
| 2931 | * need the files, and it'll cause io_close_finish() to close |
| 2932 | * the file again and cause a double CQE entry for this request |
| 2933 | */ |
| 2934 | io_queue_async_work(req); |
| 2935 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2938 | 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] | 2939 | { |
| 2940 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2941 | |
| 2942 | if (!req->file) |
| 2943 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2944 | |
| 2945 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2946 | return -EINVAL; |
| 2947 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2948 | return -EINVAL; |
| 2949 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2950 | req->sync.off = READ_ONCE(sqe->off); |
| 2951 | req->sync.len = READ_ONCE(sqe->len); |
| 2952 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2953 | return 0; |
| 2954 | } |
| 2955 | |
| 2956 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2957 | { |
| 2958 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2959 | struct io_kiocb *nxt = NULL; |
| 2960 | int ret; |
| 2961 | |
| 2962 | if (io_req_cancelled(req)) |
| 2963 | return; |
| 2964 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2965 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2966 | req->sync.flags); |
| 2967 | if (ret < 0) |
| 2968 | req_set_fail_links(req); |
| 2969 | io_cqring_add_event(req, ret); |
| 2970 | io_put_req_find_next(req, &nxt); |
| 2971 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2972 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2973 | } |
| 2974 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2975 | 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] | 2976 | bool force_nonblock) |
| 2977 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2978 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2979 | |
| 2980 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2981 | if (force_nonblock) { |
| 2982 | io_put_req(req); |
| 2983 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2984 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2985 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2986 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2987 | work = old_work = &req->work; |
| 2988 | io_sync_file_range_finish(&work); |
| 2989 | if (work && work != old_work) |
| 2990 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2991 | return 0; |
| 2992 | } |
| 2993 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2994 | 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] | 2995 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2996 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2997 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2998 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2999 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3000 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3001 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3002 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3003 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3004 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3005 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3006 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3007 | /* iovec is already imported */ |
| 3008 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3009 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3010 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3011 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3012 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3013 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3014 | if (!ret) |
| 3015 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3016 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3017 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3018 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3019 | #endif |
| 3020 | } |
| 3021 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3022 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3023 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3024 | { |
| 3025 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3026 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3027 | struct socket *sock; |
| 3028 | int ret; |
| 3029 | |
| 3030 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3031 | return -EINVAL; |
| 3032 | |
| 3033 | sock = sock_from_file(req->file, &ret); |
| 3034 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3035 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3036 | unsigned flags; |
| 3037 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3038 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3039 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3040 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3041 | /* if iov is set, it's allocated already */ |
| 3042 | if (!kmsg->iov) |
| 3043 | kmsg->iov = kmsg->fast_iov; |
| 3044 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3045 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3046 | struct io_sr_msg *sr = &req->sr_msg; |
| 3047 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3048 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3049 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3050 | |
| 3051 | io.msg.iov = io.msg.fast_iov; |
| 3052 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3053 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3054 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3055 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3056 | } |
| 3057 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3058 | flags = req->sr_msg.msg_flags; |
| 3059 | if (flags & MSG_DONTWAIT) |
| 3060 | req->flags |= REQ_F_NOWAIT; |
| 3061 | else if (force_nonblock) |
| 3062 | flags |= MSG_DONTWAIT; |
| 3063 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3064 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3065 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3066 | if (req->io) |
| 3067 | return -EAGAIN; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3068 | if (io_alloc_async_ctx(req)) { |
Dan Carpenter | 297a31e | 2020-02-17 17:39:45 +0300 | [diff] [blame] | 3069 | if (kmsg->iov != kmsg->fast_iov) |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3070 | kfree(kmsg->iov); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3071 | return -ENOMEM; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3072 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3073 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3074 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3075 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3076 | } |
| 3077 | if (ret == -ERESTARTSYS) |
| 3078 | ret = -EINTR; |
| 3079 | } |
| 3080 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3081 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3082 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3083 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3084 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3085 | if (ret < 0) |
| 3086 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3087 | io_put_req_find_next(req, nxt); |
| 3088 | return 0; |
| 3089 | #else |
| 3090 | return -EOPNOTSUPP; |
| 3091 | #endif |
| 3092 | } |
| 3093 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3094 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3095 | bool force_nonblock) |
| 3096 | { |
| 3097 | #if defined(CONFIG_NET) |
| 3098 | struct socket *sock; |
| 3099 | int ret; |
| 3100 | |
| 3101 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3102 | return -EINVAL; |
| 3103 | |
| 3104 | sock = sock_from_file(req->file, &ret); |
| 3105 | if (sock) { |
| 3106 | struct io_sr_msg *sr = &req->sr_msg; |
| 3107 | struct msghdr msg; |
| 3108 | struct iovec iov; |
| 3109 | unsigned flags; |
| 3110 | |
| 3111 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3112 | &msg.msg_iter); |
| 3113 | if (ret) |
| 3114 | return ret; |
| 3115 | |
| 3116 | msg.msg_name = NULL; |
| 3117 | msg.msg_control = NULL; |
| 3118 | msg.msg_controllen = 0; |
| 3119 | msg.msg_namelen = 0; |
| 3120 | |
| 3121 | flags = req->sr_msg.msg_flags; |
| 3122 | if (flags & MSG_DONTWAIT) |
| 3123 | req->flags |= REQ_F_NOWAIT; |
| 3124 | else if (force_nonblock) |
| 3125 | flags |= MSG_DONTWAIT; |
| 3126 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3127 | msg.msg_flags = flags; |
| 3128 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3129 | if (force_nonblock && ret == -EAGAIN) |
| 3130 | return -EAGAIN; |
| 3131 | if (ret == -ERESTARTSYS) |
| 3132 | ret = -EINTR; |
| 3133 | } |
| 3134 | |
| 3135 | io_cqring_add_event(req, ret); |
| 3136 | if (ret < 0) |
| 3137 | req_set_fail_links(req); |
| 3138 | io_put_req_find_next(req, nxt); |
| 3139 | return 0; |
| 3140 | #else |
| 3141 | return -EOPNOTSUPP; |
| 3142 | #endif |
| 3143 | } |
| 3144 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3145 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3146 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3147 | { |
| 3148 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3149 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3150 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3151 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3152 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3153 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3154 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3155 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3156 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3157 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3158 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3159 | /* iovec is already imported */ |
| 3160 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3161 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3162 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3163 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3164 | ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3165 | &io->msg.uaddr, &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3166 | if (!ret) |
| 3167 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3168 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3169 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3170 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3171 | #endif |
| 3172 | } |
| 3173 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3174 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3175 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3176 | { |
| 3177 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3178 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3179 | struct socket *sock; |
| 3180 | int ret; |
| 3181 | |
| 3182 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3183 | return -EINVAL; |
| 3184 | |
| 3185 | sock = sock_from_file(req->file, &ret); |
| 3186 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3187 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3188 | unsigned flags; |
| 3189 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3190 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3191 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3192 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3193 | /* if iov is set, it's allocated already */ |
| 3194 | if (!kmsg->iov) |
| 3195 | kmsg->iov = kmsg->fast_iov; |
| 3196 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3197 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3198 | struct io_sr_msg *sr = &req->sr_msg; |
| 3199 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3200 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3201 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3202 | |
| 3203 | io.msg.iov = io.msg.fast_iov; |
| 3204 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3205 | sr->msg_flags, &io.msg.uaddr, |
| 3206 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3207 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3208 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3209 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3210 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3211 | flags = req->sr_msg.msg_flags; |
| 3212 | if (flags & MSG_DONTWAIT) |
| 3213 | req->flags |= REQ_F_NOWAIT; |
| 3214 | else if (force_nonblock) |
| 3215 | flags |= MSG_DONTWAIT; |
| 3216 | |
| 3217 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3218 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3219 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3220 | if (req->io) |
| 3221 | return -EAGAIN; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3222 | if (io_alloc_async_ctx(req)) { |
Dan Carpenter | 297a31e | 2020-02-17 17:39:45 +0300 | [diff] [blame] | 3223 | if (kmsg->iov != kmsg->fast_iov) |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3224 | kfree(kmsg->iov); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3225 | return -ENOMEM; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3226 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3227 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3228 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3229 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3230 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3231 | if (ret == -ERESTARTSYS) |
| 3232 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3233 | } |
| 3234 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3235 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3236 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3237 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3238 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3239 | if (ret < 0) |
| 3240 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3241 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3242 | return 0; |
| 3243 | #else |
| 3244 | return -EOPNOTSUPP; |
| 3245 | #endif |
| 3246 | } |
| 3247 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3248 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3249 | bool force_nonblock) |
| 3250 | { |
| 3251 | #if defined(CONFIG_NET) |
| 3252 | struct socket *sock; |
| 3253 | int ret; |
| 3254 | |
| 3255 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3256 | return -EINVAL; |
| 3257 | |
| 3258 | sock = sock_from_file(req->file, &ret); |
| 3259 | if (sock) { |
| 3260 | struct io_sr_msg *sr = &req->sr_msg; |
| 3261 | struct msghdr msg; |
| 3262 | struct iovec iov; |
| 3263 | unsigned flags; |
| 3264 | |
| 3265 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3266 | &msg.msg_iter); |
| 3267 | if (ret) |
| 3268 | return ret; |
| 3269 | |
| 3270 | msg.msg_name = NULL; |
| 3271 | msg.msg_control = NULL; |
| 3272 | msg.msg_controllen = 0; |
| 3273 | msg.msg_namelen = 0; |
| 3274 | msg.msg_iocb = NULL; |
| 3275 | msg.msg_flags = 0; |
| 3276 | |
| 3277 | flags = req->sr_msg.msg_flags; |
| 3278 | if (flags & MSG_DONTWAIT) |
| 3279 | req->flags |= REQ_F_NOWAIT; |
| 3280 | else if (force_nonblock) |
| 3281 | flags |= MSG_DONTWAIT; |
| 3282 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3283 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3284 | if (force_nonblock && ret == -EAGAIN) |
| 3285 | return -EAGAIN; |
| 3286 | if (ret == -ERESTARTSYS) |
| 3287 | ret = -EINTR; |
| 3288 | } |
| 3289 | |
| 3290 | io_cqring_add_event(req, ret); |
| 3291 | if (ret < 0) |
| 3292 | req_set_fail_links(req); |
| 3293 | io_put_req_find_next(req, nxt); |
| 3294 | return 0; |
| 3295 | #else |
| 3296 | return -EOPNOTSUPP; |
| 3297 | #endif |
| 3298 | } |
| 3299 | |
| 3300 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3301 | 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] | 3302 | { |
| 3303 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3304 | struct io_accept *accept = &req->accept; |
| 3305 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3306 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3307 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3308 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3309 | return -EINVAL; |
| 3310 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3311 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3312 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3313 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3314 | return 0; |
| 3315 | #else |
| 3316 | return -EOPNOTSUPP; |
| 3317 | #endif |
| 3318 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3319 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3320 | #if defined(CONFIG_NET) |
| 3321 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3322 | bool force_nonblock) |
| 3323 | { |
| 3324 | struct io_accept *accept = &req->accept; |
| 3325 | unsigned file_flags; |
| 3326 | int ret; |
| 3327 | |
| 3328 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3329 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3330 | accept->addr_len, accept->flags); |
| 3331 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3332 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3333 | if (ret == -ERESTARTSYS) |
| 3334 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3335 | if (ret < 0) |
| 3336 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3337 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3338 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3339 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3343 | { |
| 3344 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3345 | struct io_kiocb *nxt = NULL; |
| 3346 | |
| 3347 | if (io_req_cancelled(req)) |
| 3348 | return; |
| 3349 | __io_accept(req, &nxt, false); |
| 3350 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3351 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3352 | } |
| 3353 | #endif |
| 3354 | |
| 3355 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3356 | bool force_nonblock) |
| 3357 | { |
| 3358 | #if defined(CONFIG_NET) |
| 3359 | int ret; |
| 3360 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3361 | ret = __io_accept(req, nxt, force_nonblock); |
| 3362 | if (ret == -EAGAIN && force_nonblock) { |
| 3363 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3364 | io_put_req(req); |
| 3365 | return -EAGAIN; |
| 3366 | } |
| 3367 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3368 | #else |
| 3369 | return -EOPNOTSUPP; |
| 3370 | #endif |
| 3371 | } |
| 3372 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3373 | 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] | 3374 | { |
| 3375 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3376 | struct io_connect *conn = &req->connect; |
| 3377 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3378 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3379 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3380 | return -EINVAL; |
| 3381 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3382 | return -EINVAL; |
| 3383 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3384 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3385 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3386 | |
| 3387 | if (!io) |
| 3388 | return 0; |
| 3389 | |
| 3390 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3391 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3392 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3393 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3394 | #endif |
| 3395 | } |
| 3396 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3397 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3398 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3399 | { |
| 3400 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3401 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3402 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3403 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3404 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3405 | if (req->io) { |
| 3406 | io = req->io; |
| 3407 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3408 | ret = move_addr_to_kernel(req->connect.addr, |
| 3409 | req->connect.addr_len, |
| 3410 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3411 | if (ret) |
| 3412 | goto out; |
| 3413 | io = &__io; |
| 3414 | } |
| 3415 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3416 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3417 | |
| 3418 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3419 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3420 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3421 | if (req->io) |
| 3422 | return -EAGAIN; |
| 3423 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3424 | ret = -ENOMEM; |
| 3425 | goto out; |
| 3426 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3427 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3428 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3429 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3430 | if (ret == -ERESTARTSYS) |
| 3431 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3432 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3433 | if (ret < 0) |
| 3434 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3435 | io_cqring_add_event(req, ret); |
| 3436 | io_put_req_find_next(req, nxt); |
| 3437 | return 0; |
| 3438 | #else |
| 3439 | return -EOPNOTSUPP; |
| 3440 | #endif |
| 3441 | } |
| 3442 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3443 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3444 | { |
| 3445 | struct io_poll_iocb *poll = &req->poll; |
| 3446 | |
| 3447 | spin_lock(&poll->head->lock); |
| 3448 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3449 | if (!list_empty(&poll->wait.entry)) { |
| 3450 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3451 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3452 | } |
| 3453 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3454 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3458 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3459 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3460 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3461 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3462 | |
| 3463 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3464 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3465 | struct hlist_head *list; |
| 3466 | |
| 3467 | list = &ctx->cancel_hash[i]; |
| 3468 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3469 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3470 | } |
| 3471 | spin_unlock_irq(&ctx->completion_lock); |
| 3472 | } |
| 3473 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3474 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3475 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3476 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3477 | struct io_kiocb *req; |
| 3478 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3479 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3480 | hlist_for_each_entry(req, list, hash_node) { |
| 3481 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3482 | io_poll_remove_one(req); |
| 3483 | return 0; |
| 3484 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | return -ENOENT; |
| 3488 | } |
| 3489 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3490 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3491 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3492 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3493 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3494 | return -EINVAL; |
| 3495 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3496 | sqe->poll_events) |
| 3497 | return -EINVAL; |
| 3498 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3499 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3500 | return 0; |
| 3501 | } |
| 3502 | |
| 3503 | /* |
| 3504 | * Find a running poll command that matches one specified in sqe->addr, |
| 3505 | * and remove it if found. |
| 3506 | */ |
| 3507 | static int io_poll_remove(struct io_kiocb *req) |
| 3508 | { |
| 3509 | struct io_ring_ctx *ctx = req->ctx; |
| 3510 | u64 addr; |
| 3511 | int ret; |
| 3512 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3513 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3514 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3515 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3516 | spin_unlock_irq(&ctx->completion_lock); |
| 3517 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3518 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3519 | if (ret < 0) |
| 3520 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3521 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3522 | return 0; |
| 3523 | } |
| 3524 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3525 | 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] | 3526 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3527 | struct io_ring_ctx *ctx = req->ctx; |
| 3528 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3529 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3530 | if (error) |
| 3531 | io_cqring_fill_event(req, error); |
| 3532 | else |
| 3533 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3534 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3535 | } |
| 3536 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3537 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3538 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3539 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3540 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3541 | struct io_poll_iocb *poll = &req->poll; |
| 3542 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3543 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3544 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3545 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3546 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3547 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3548 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3549 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3550 | ret = -ECANCELED; |
| 3551 | } else if (READ_ONCE(poll->canceled)) { |
| 3552 | ret = -ECANCELED; |
| 3553 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3554 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3555 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3556 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3557 | |
| 3558 | /* |
| 3559 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3560 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3561 | * synchronize with them. In the cancellation case the list_del_init |
| 3562 | * itself is not actually needed, but harmless so we keep it in to |
| 3563 | * avoid further branches in the fast path. |
| 3564 | */ |
| 3565 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3566 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3567 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3568 | spin_unlock_irq(&ctx->completion_lock); |
| 3569 | return; |
| 3570 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3571 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3572 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3573 | spin_unlock_irq(&ctx->completion_lock); |
| 3574 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3575 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3576 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3577 | if (ret < 0) |
| 3578 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3579 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3580 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3581 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3584 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3585 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3586 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3587 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3588 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3589 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3590 | spin_lock_irq(&ctx->completion_lock); |
| 3591 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3592 | hash_del(&req->hash_node); |
| 3593 | io_poll_complete(req, req->result, 0); |
| 3594 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3595 | if (refcount_dec_and_test(&req->refs) && |
| 3596 | !io_req_multi_free(&rb, req)) { |
| 3597 | req->flags |= REQ_F_COMP_LOCKED; |
| 3598 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | spin_unlock_irq(&ctx->completion_lock); |
| 3602 | |
| 3603 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3604 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3608 | { |
| 3609 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3610 | struct llist_node *nodes; |
| 3611 | |
| 3612 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3613 | if (nodes) |
| 3614 | __io_poll_flush(req->ctx, nodes); |
| 3615 | } |
| 3616 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3617 | static void io_poll_trigger_evfd(struct io_wq_work **workptr) |
| 3618 | { |
| 3619 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3620 | |
| 3621 | eventfd_signal(req->ctx->cq_ev_fd, 1); |
| 3622 | io_put_req(req); |
| 3623 | } |
| 3624 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3625 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3626 | void *key) |
| 3627 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3628 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3629 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3630 | struct io_ring_ctx *ctx = req->ctx; |
| 3631 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3632 | |
| 3633 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3634 | if (mask && !(mask & poll->events)) |
| 3635 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3636 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3637 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3638 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3639 | /* |
| 3640 | * Run completion inline if we can. We're using trylock here because |
| 3641 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3642 | * If we have a link timeout we're going to need the completion_lock |
| 3643 | * for finalizing the request, mark us as having grabbed that already. |
| 3644 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3645 | if (mask) { |
| 3646 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3647 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3648 | if (llist_empty(&ctx->poll_llist) && |
| 3649 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3650 | bool trigger_ev; |
| 3651 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3652 | hash_del(&req->hash_node); |
| 3653 | io_poll_complete(req, mask, 0); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3654 | |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3655 | trigger_ev = io_should_trigger_evfd(ctx); |
| 3656 | if (trigger_ev && eventfd_signal_count()) { |
| 3657 | trigger_ev = false; |
| 3658 | req->work.func = io_poll_trigger_evfd; |
| 3659 | } else { |
| 3660 | req->flags |= REQ_F_COMP_LOCKED; |
| 3661 | io_put_req(req); |
| 3662 | req = NULL; |
| 3663 | } |
| 3664 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3665 | __io_cqring_ev_posted(ctx, trigger_ev); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3666 | } else { |
| 3667 | req->result = mask; |
| 3668 | req->llist_node.next = NULL; |
| 3669 | /* if the list wasn't empty, we're done */ |
| 3670 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3671 | req = NULL; |
| 3672 | else |
| 3673 | req->work.func = io_poll_flush; |
| 3674 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3675 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3676 | if (req) |
| 3677 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3678 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3679 | return 1; |
| 3680 | } |
| 3681 | |
| 3682 | struct io_poll_table { |
| 3683 | struct poll_table_struct pt; |
| 3684 | struct io_kiocb *req; |
| 3685 | int error; |
| 3686 | }; |
| 3687 | |
| 3688 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3689 | struct poll_table_struct *p) |
| 3690 | { |
| 3691 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3692 | |
| 3693 | if (unlikely(pt->req->poll.head)) { |
| 3694 | pt->error = -EINVAL; |
| 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | pt->error = 0; |
| 3699 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3700 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3703 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3704 | { |
| 3705 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3706 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3707 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3708 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3709 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3710 | } |
| 3711 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3712 | 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] | 3713 | { |
| 3714 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3715 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3716 | |
| 3717 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3718 | return -EINVAL; |
| 3719 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3720 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3721 | if (!poll->file) |
| 3722 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3723 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3724 | events = READ_ONCE(sqe->poll_events); |
| 3725 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3726 | return 0; |
| 3727 | } |
| 3728 | |
| 3729 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3730 | { |
| 3731 | struct io_poll_iocb *poll = &req->poll; |
| 3732 | struct io_ring_ctx *ctx = req->ctx; |
| 3733 | struct io_poll_table ipt; |
| 3734 | bool cancel = false; |
| 3735 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3736 | |
| 3737 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3738 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3739 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3740 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3741 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3742 | poll->canceled = false; |
| 3743 | |
| 3744 | ipt.pt._qproc = io_poll_queue_proc; |
| 3745 | ipt.pt._key = poll->events; |
| 3746 | ipt.req = req; |
| 3747 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3748 | |
| 3749 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3750 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3751 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3752 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3753 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3754 | INIT_LIST_HEAD(&req->list); |
| 3755 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3756 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3757 | |
| 3758 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3759 | if (likely(poll->head)) { |
| 3760 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3761 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3762 | if (ipt.error) |
| 3763 | cancel = true; |
| 3764 | ipt.error = 0; |
| 3765 | mask = 0; |
| 3766 | } |
| 3767 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3768 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3769 | else if (cancel) |
| 3770 | WRITE_ONCE(poll->canceled, true); |
| 3771 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3772 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3773 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3774 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3775 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3776 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3777 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3778 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3779 | spin_unlock_irq(&ctx->completion_lock); |
| 3780 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3781 | if (mask) { |
| 3782 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3783 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3784 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3785 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3788 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3789 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3790 | struct io_timeout_data *data = container_of(timer, |
| 3791 | struct io_timeout_data, timer); |
| 3792 | struct io_kiocb *req = data->req; |
| 3793 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3794 | unsigned long flags; |
| 3795 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3796 | atomic_inc(&ctx->cq_timeouts); |
| 3797 | |
| 3798 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3799 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3800 | * We could be racing with timeout deletion. If the list is empty, |
| 3801 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3802 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3803 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3804 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3805 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3806 | /* |
| 3807 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3808 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3809 | * pointer will be increased, otherwise other timeout reqs may |
| 3810 | * return in advance without waiting for enough wait_nr. |
| 3811 | */ |
| 3812 | prev = req; |
| 3813 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3814 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3815 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3816 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3817 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3818 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3819 | io_commit_cqring(ctx); |
| 3820 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3821 | |
| 3822 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3823 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3824 | io_put_req(req); |
| 3825 | return HRTIMER_NORESTART; |
| 3826 | } |
| 3827 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3828 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3829 | { |
| 3830 | struct io_kiocb *req; |
| 3831 | int ret = -ENOENT; |
| 3832 | |
| 3833 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3834 | if (user_data == req->user_data) { |
| 3835 | list_del_init(&req->list); |
| 3836 | ret = 0; |
| 3837 | break; |
| 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | if (ret == -ENOENT) |
| 3842 | return ret; |
| 3843 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3844 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3845 | if (ret == -1) |
| 3846 | return -EALREADY; |
| 3847 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3848 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3849 | io_cqring_fill_event(req, -ECANCELED); |
| 3850 | io_put_req(req); |
| 3851 | return 0; |
| 3852 | } |
| 3853 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3854 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3855 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3856 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3857 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3858 | return -EINVAL; |
| 3859 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3860 | return -EINVAL; |
| 3861 | |
| 3862 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3863 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3864 | if (req->timeout.flags) |
| 3865 | return -EINVAL; |
| 3866 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
| 3869 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3870 | /* |
| 3871 | * Remove or update an existing timeout command |
| 3872 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3873 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3874 | { |
| 3875 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3876 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3877 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3878 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3879 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3880 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3881 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3882 | io_commit_cqring(ctx); |
| 3883 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3884 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3885 | if (ret < 0) |
| 3886 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3887 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3888 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3889 | } |
| 3890 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3891 | 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] | 3892 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3893 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3894 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3895 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3896 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3897 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3898 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3899 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3900 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3901 | if (sqe->off && is_timeout_link) |
| 3902 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3903 | flags = READ_ONCE(sqe->timeout_flags); |
| 3904 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3905 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3906 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3907 | req->timeout.count = READ_ONCE(sqe->off); |
| 3908 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3909 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3910 | return -ENOMEM; |
| 3911 | |
| 3912 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3913 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3914 | req->flags |= REQ_F_TIMEOUT; |
| 3915 | |
| 3916 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3917 | return -EFAULT; |
| 3918 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3919 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3920 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3921 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3922 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3923 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3924 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3925 | return 0; |
| 3926 | } |
| 3927 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3928 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3929 | { |
| 3930 | unsigned count; |
| 3931 | struct io_ring_ctx *ctx = req->ctx; |
| 3932 | struct io_timeout_data *data; |
| 3933 | struct list_head *entry; |
| 3934 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3935 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3936 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3937 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3938 | /* |
| 3939 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3940 | * timeout event to be satisfied. If it isn't set, then this is |
| 3941 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3942 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3943 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3944 | if (!count) { |
| 3945 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3946 | spin_lock_irq(&ctx->completion_lock); |
| 3947 | entry = ctx->timeout_list.prev; |
| 3948 | goto add; |
| 3949 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3950 | |
| 3951 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3952 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3953 | |
| 3954 | /* |
| 3955 | * Insertion sort, ensuring the first entry in the list is always |
| 3956 | * the one we need first. |
| 3957 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3958 | spin_lock_irq(&ctx->completion_lock); |
| 3959 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3960 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3961 | unsigned nxt_sq_head; |
| 3962 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3963 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3964 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3965 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3966 | continue; |
| 3967 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3968 | /* |
| 3969 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3970 | * long to store it. |
| 3971 | */ |
| 3972 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3973 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3974 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3975 | |
| 3976 | /* |
| 3977 | * cached_sq_head may overflow, and it will never overflow twice |
| 3978 | * once there is some timeout req still be valid. |
| 3979 | */ |
| 3980 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3981 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3982 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3983 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3984 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3985 | |
| 3986 | /* |
| 3987 | * Sequence of reqs after the insert one and itself should |
| 3988 | * be adjusted because each timeout req consumes a slot. |
| 3989 | */ |
| 3990 | span++; |
| 3991 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3992 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3993 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3994 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3995 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3996 | data->timer.function = io_timeout_fn; |
| 3997 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3998 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3999 | return 0; |
| 4000 | } |
| 4001 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4002 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4003 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4004 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4005 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4006 | return req->user_data == (unsigned long) data; |
| 4007 | } |
| 4008 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4009 | 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] | 4010 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4011 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4012 | int ret = 0; |
| 4013 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4014 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 4015 | switch (cancel_ret) { |
| 4016 | case IO_WQ_CANCEL_OK: |
| 4017 | ret = 0; |
| 4018 | break; |
| 4019 | case IO_WQ_CANCEL_RUNNING: |
| 4020 | ret = -EALREADY; |
| 4021 | break; |
| 4022 | case IO_WQ_CANCEL_NOTFOUND: |
| 4023 | ret = -ENOENT; |
| 4024 | break; |
| 4025 | } |
| 4026 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4027 | return ret; |
| 4028 | } |
| 4029 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4030 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 4031 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4032 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4033 | { |
| 4034 | unsigned long flags; |
| 4035 | int ret; |
| 4036 | |
| 4037 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 4038 | if (ret != -ENOENT) { |
| 4039 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4040 | goto done; |
| 4041 | } |
| 4042 | |
| 4043 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4044 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4045 | if (ret != -ENOENT) |
| 4046 | goto done; |
| 4047 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4048 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4049 | if (!ret) |
| 4050 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4051 | io_cqring_fill_event(req, ret); |
| 4052 | io_commit_cqring(ctx); |
| 4053 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4054 | io_cqring_ev_posted(ctx); |
| 4055 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4056 | if (ret < 0) |
| 4057 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4058 | io_put_req_find_next(req, nxt); |
| 4059 | } |
| 4060 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4061 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4062 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4063 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4064 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4065 | return -EINVAL; |
| 4066 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4067 | sqe->cancel_flags) |
| 4068 | return -EINVAL; |
| 4069 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4070 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
| 4074 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4075 | { |
| 4076 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4077 | |
| 4078 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4079 | return 0; |
| 4080 | } |
| 4081 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4082 | static int io_files_update_prep(struct io_kiocb *req, |
| 4083 | const struct io_uring_sqe *sqe) |
| 4084 | { |
| 4085 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4086 | return -EINVAL; |
| 4087 | |
| 4088 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4089 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4090 | if (!req->files_update.nr_args) |
| 4091 | return -EINVAL; |
| 4092 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4093 | return 0; |
| 4094 | } |
| 4095 | |
| 4096 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4097 | { |
| 4098 | struct io_ring_ctx *ctx = req->ctx; |
| 4099 | struct io_uring_files_update up; |
| 4100 | int ret; |
| 4101 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4102 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4103 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4104 | |
| 4105 | up.offset = req->files_update.offset; |
| 4106 | up.fds = req->files_update.arg; |
| 4107 | |
| 4108 | mutex_lock(&ctx->uring_lock); |
| 4109 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4110 | mutex_unlock(&ctx->uring_lock); |
| 4111 | |
| 4112 | if (ret < 0) |
| 4113 | req_set_fail_links(req); |
| 4114 | io_cqring_add_event(req, ret); |
| 4115 | io_put_req(req); |
| 4116 | return 0; |
| 4117 | } |
| 4118 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4119 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4120 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4121 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4122 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4123 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4124 | if (io_op_defs[req->opcode].file_table) { |
| 4125 | ret = io_grab_files(req); |
| 4126 | if (unlikely(ret)) |
| 4127 | return ret; |
| 4128 | } |
| 4129 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4130 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4131 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4132 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4133 | case IORING_OP_NOP: |
| 4134 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4135 | case IORING_OP_READV: |
| 4136 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4137 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4138 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4139 | break; |
| 4140 | case IORING_OP_WRITEV: |
| 4141 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4142 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4143 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4144 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4145 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4146 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4147 | break; |
| 4148 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4149 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4150 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4151 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4152 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4153 | break; |
| 4154 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4155 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4156 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4157 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4158 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4159 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4160 | break; |
| 4161 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4162 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4163 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4164 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4165 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4166 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4167 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4168 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4169 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4170 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4171 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4172 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4173 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4174 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4175 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4176 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4177 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4178 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4179 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4180 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4181 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4182 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4183 | case IORING_OP_FALLOCATE: |
| 4184 | ret = io_fallocate_prep(req, sqe); |
| 4185 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4186 | case IORING_OP_OPENAT: |
| 4187 | ret = io_openat_prep(req, sqe); |
| 4188 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4189 | case IORING_OP_CLOSE: |
| 4190 | ret = io_close_prep(req, sqe); |
| 4191 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4192 | case IORING_OP_FILES_UPDATE: |
| 4193 | ret = io_files_update_prep(req, sqe); |
| 4194 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4195 | case IORING_OP_STATX: |
| 4196 | ret = io_statx_prep(req, sqe); |
| 4197 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4198 | case IORING_OP_FADVISE: |
| 4199 | ret = io_fadvise_prep(req, sqe); |
| 4200 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4201 | case IORING_OP_MADVISE: |
| 4202 | ret = io_madvise_prep(req, sqe); |
| 4203 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4204 | case IORING_OP_OPENAT2: |
| 4205 | ret = io_openat2_prep(req, sqe); |
| 4206 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4207 | case IORING_OP_EPOLL_CTL: |
| 4208 | ret = io_epoll_ctl_prep(req, sqe); |
| 4209 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4210 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4211 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4212 | req->opcode); |
| 4213 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4214 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4217 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4218 | } |
| 4219 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4220 | 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] | 4221 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4222 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4223 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4224 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4225 | /* Still need defer if there is pending req in defer list. */ |
| 4226 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4227 | return 0; |
| 4228 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4229 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4230 | return -EAGAIN; |
| 4231 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4232 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4233 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4234 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4235 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4236 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4237 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4238 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4239 | return 0; |
| 4240 | } |
| 4241 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4242 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4243 | list_add_tail(&req->list, &ctx->defer_list); |
| 4244 | spin_unlock_irq(&ctx->completion_lock); |
| 4245 | return -EIOCBQUEUED; |
| 4246 | } |
| 4247 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4248 | static void io_cleanup_req(struct io_kiocb *req) |
| 4249 | { |
| 4250 | struct io_async_ctx *io = req->io; |
| 4251 | |
| 4252 | switch (req->opcode) { |
| 4253 | case IORING_OP_READV: |
| 4254 | case IORING_OP_READ_FIXED: |
| 4255 | case IORING_OP_READ: |
| 4256 | case IORING_OP_WRITEV: |
| 4257 | case IORING_OP_WRITE_FIXED: |
| 4258 | case IORING_OP_WRITE: |
| 4259 | if (io->rw.iov != io->rw.fast_iov) |
| 4260 | kfree(io->rw.iov); |
| 4261 | break; |
| 4262 | case IORING_OP_SENDMSG: |
| 4263 | case IORING_OP_RECVMSG: |
| 4264 | if (io->msg.iov != io->msg.fast_iov) |
| 4265 | kfree(io->msg.iov); |
| 4266 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4267 | case IORING_OP_OPENAT: |
| 4268 | case IORING_OP_OPENAT2: |
| 4269 | case IORING_OP_STATX: |
| 4270 | putname(req->open.filename); |
| 4271 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4275 | } |
| 4276 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4277 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4278 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4279 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4280 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4281 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4282 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4283 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4284 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4285 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4286 | break; |
| 4287 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4288 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4289 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4290 | if (sqe) { |
| 4291 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4292 | if (ret < 0) |
| 4293 | break; |
| 4294 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4295 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4296 | break; |
| 4297 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4298 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4299 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4300 | if (sqe) { |
| 4301 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4302 | if (ret < 0) |
| 4303 | break; |
| 4304 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4305 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4306 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4307 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4308 | if (sqe) { |
| 4309 | ret = io_prep_fsync(req, sqe); |
| 4310 | if (ret < 0) |
| 4311 | break; |
| 4312 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4313 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4314 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4315 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4316 | if (sqe) { |
| 4317 | ret = io_poll_add_prep(req, sqe); |
| 4318 | if (ret) |
| 4319 | break; |
| 4320 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4321 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4322 | break; |
| 4323 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4324 | if (sqe) { |
| 4325 | ret = io_poll_remove_prep(req, sqe); |
| 4326 | if (ret < 0) |
| 4327 | break; |
| 4328 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4329 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4330 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4331 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4332 | if (sqe) { |
| 4333 | ret = io_prep_sfr(req, sqe); |
| 4334 | if (ret < 0) |
| 4335 | break; |
| 4336 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4337 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4338 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4339 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4340 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4341 | if (sqe) { |
| 4342 | ret = io_sendmsg_prep(req, sqe); |
| 4343 | if (ret < 0) |
| 4344 | break; |
| 4345 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4346 | if (req->opcode == IORING_OP_SENDMSG) |
| 4347 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4348 | else |
| 4349 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4350 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4351 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4352 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4353 | if (sqe) { |
| 4354 | ret = io_recvmsg_prep(req, sqe); |
| 4355 | if (ret) |
| 4356 | break; |
| 4357 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4358 | if (req->opcode == IORING_OP_RECVMSG) |
| 4359 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4360 | else |
| 4361 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4362 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4363 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4364 | if (sqe) { |
| 4365 | ret = io_timeout_prep(req, sqe, false); |
| 4366 | if (ret) |
| 4367 | break; |
| 4368 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4369 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4370 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4371 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4372 | if (sqe) { |
| 4373 | ret = io_timeout_remove_prep(req, sqe); |
| 4374 | if (ret) |
| 4375 | break; |
| 4376 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4377 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4378 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4379 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4380 | if (sqe) { |
| 4381 | ret = io_accept_prep(req, sqe); |
| 4382 | if (ret) |
| 4383 | break; |
| 4384 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4385 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4386 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4387 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4388 | if (sqe) { |
| 4389 | ret = io_connect_prep(req, sqe); |
| 4390 | if (ret) |
| 4391 | break; |
| 4392 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4393 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4394 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4395 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4396 | if (sqe) { |
| 4397 | ret = io_async_cancel_prep(req, sqe); |
| 4398 | if (ret) |
| 4399 | break; |
| 4400 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4401 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4402 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4403 | case IORING_OP_FALLOCATE: |
| 4404 | if (sqe) { |
| 4405 | ret = io_fallocate_prep(req, sqe); |
| 4406 | if (ret) |
| 4407 | break; |
| 4408 | } |
| 4409 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4410 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4411 | case IORING_OP_OPENAT: |
| 4412 | if (sqe) { |
| 4413 | ret = io_openat_prep(req, sqe); |
| 4414 | if (ret) |
| 4415 | break; |
| 4416 | } |
| 4417 | ret = io_openat(req, nxt, force_nonblock); |
| 4418 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4419 | case IORING_OP_CLOSE: |
| 4420 | if (sqe) { |
| 4421 | ret = io_close_prep(req, sqe); |
| 4422 | if (ret) |
| 4423 | break; |
| 4424 | } |
| 4425 | ret = io_close(req, nxt, force_nonblock); |
| 4426 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4427 | case IORING_OP_FILES_UPDATE: |
| 4428 | if (sqe) { |
| 4429 | ret = io_files_update_prep(req, sqe); |
| 4430 | if (ret) |
| 4431 | break; |
| 4432 | } |
| 4433 | ret = io_files_update(req, force_nonblock); |
| 4434 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4435 | case IORING_OP_STATX: |
| 4436 | if (sqe) { |
| 4437 | ret = io_statx_prep(req, sqe); |
| 4438 | if (ret) |
| 4439 | break; |
| 4440 | } |
| 4441 | ret = io_statx(req, nxt, force_nonblock); |
| 4442 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4443 | case IORING_OP_FADVISE: |
| 4444 | if (sqe) { |
| 4445 | ret = io_fadvise_prep(req, sqe); |
| 4446 | if (ret) |
| 4447 | break; |
| 4448 | } |
| 4449 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4450 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4451 | case IORING_OP_MADVISE: |
| 4452 | if (sqe) { |
| 4453 | ret = io_madvise_prep(req, sqe); |
| 4454 | if (ret) |
| 4455 | break; |
| 4456 | } |
| 4457 | ret = io_madvise(req, nxt, force_nonblock); |
| 4458 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4459 | case IORING_OP_OPENAT2: |
| 4460 | if (sqe) { |
| 4461 | ret = io_openat2_prep(req, sqe); |
| 4462 | if (ret) |
| 4463 | break; |
| 4464 | } |
| 4465 | ret = io_openat2(req, nxt, force_nonblock); |
| 4466 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4467 | case IORING_OP_EPOLL_CTL: |
| 4468 | if (sqe) { |
| 4469 | ret = io_epoll_ctl_prep(req, sqe); |
| 4470 | if (ret) |
| 4471 | break; |
| 4472 | } |
| 4473 | ret = io_epoll_ctl(req, nxt, force_nonblock); |
| 4474 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4475 | default: |
| 4476 | ret = -EINVAL; |
| 4477 | break; |
| 4478 | } |
| 4479 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4480 | if (ret) |
| 4481 | return ret; |
| 4482 | |
| 4483 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4484 | const bool in_async = io_wq_current_is_worker(); |
| 4485 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4486 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4487 | return -EAGAIN; |
| 4488 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4489 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4490 | if (in_async) |
| 4491 | mutex_lock(&ctx->uring_lock); |
| 4492 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4493 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4494 | |
| 4495 | if (in_async) |
| 4496 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4502 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4503 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4504 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4505 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4506 | struct io_kiocb *nxt = NULL; |
| 4507 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4508 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4509 | /* if NO_CANCEL is set, we must still run the work */ |
| 4510 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4511 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4512 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4513 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4514 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4515 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4516 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4517 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4518 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4519 | /* |
| 4520 | * We can get EAGAIN for polled IO even though we're |
| 4521 | * forcing a sync submission from here, since we can't |
| 4522 | * wait for request slots on the block side. |
| 4523 | */ |
| 4524 | if (ret != -EAGAIN) |
| 4525 | break; |
| 4526 | cond_resched(); |
| 4527 | } while (1); |
| 4528 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4529 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4530 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4531 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4532 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4533 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4534 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4535 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4536 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4537 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4538 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4539 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4540 | if (!ret && nxt) |
| 4541 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4542 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4543 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4544 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4545 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4546 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4547 | return 0; |
Jens Axboe | 0b5faf6 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 4548 | if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4549 | return 0; |
| 4550 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4551 | } |
| 4552 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4553 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4554 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4555 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4556 | struct fixed_file_table *table; |
| 4557 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4558 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4559 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4560 | } |
| 4561 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4562 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4563 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4564 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4565 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4566 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4567 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4568 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4569 | flags = READ_ONCE(sqe->flags); |
| 4570 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4571 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4572 | if (!io_req_needs_file(req, fd)) |
| 4573 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4574 | |
| 4575 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4576 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4577 | (unsigned) fd >= ctx->nr_user_files)) |
| 4578 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4579 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4580 | req->file = io_file_from_index(ctx, fd); |
| 4581 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4582 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4583 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4584 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4585 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4586 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4587 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4588 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4589 | req->file = io_file_get(state, fd); |
| 4590 | if (unlikely(!req->file)) |
| 4591 | return -EBADF; |
| 4592 | } |
| 4593 | |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4597 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4598 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4599 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4600 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4601 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4602 | if (req->work.files) |
| 4603 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4604 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4605 | return -EBADF; |
| 4606 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4607 | rcu_read_lock(); |
| 4608 | spin_lock_irq(&ctx->inflight_lock); |
| 4609 | /* |
| 4610 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4611 | * out work accessing these files if the fd is closed. Check if |
| 4612 | * the fd has changed since we started down this path, and disallow |
| 4613 | * this operation if it has. |
| 4614 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4615 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4616 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4617 | req->flags |= REQ_F_INFLIGHT; |
| 4618 | req->work.files = current->files; |
| 4619 | ret = 0; |
| 4620 | } |
| 4621 | spin_unlock_irq(&ctx->inflight_lock); |
| 4622 | rcu_read_unlock(); |
| 4623 | |
| 4624 | return ret; |
| 4625 | } |
| 4626 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4627 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4628 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4629 | struct io_timeout_data *data = container_of(timer, |
| 4630 | struct io_timeout_data, timer); |
| 4631 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4632 | struct io_ring_ctx *ctx = req->ctx; |
| 4633 | struct io_kiocb *prev = NULL; |
| 4634 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4635 | |
| 4636 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4637 | |
| 4638 | /* |
| 4639 | * We don't expect the list to be empty, that will only happen if we |
| 4640 | * race with the completion of the linked work. |
| 4641 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4642 | if (!list_empty(&req->link_list)) { |
| 4643 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4644 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4645 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4646 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4647 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4648 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4649 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4653 | |
| 4654 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4655 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4656 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4657 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4658 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4659 | } else { |
| 4660 | io_cqring_add_event(req, -ETIME); |
| 4661 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4662 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4663 | return HRTIMER_NORESTART; |
| 4664 | } |
| 4665 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4666 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4667 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4668 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4669 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4670 | /* |
| 4671 | * If the list is now empty, then our linked request finished before |
| 4672 | * we got a chance to setup the timer |
| 4673 | */ |
| 4674 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4675 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4676 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4677 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4678 | data->timer.function = io_link_timeout_fn; |
| 4679 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4680 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4681 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4682 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4683 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4684 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4685 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4688 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4689 | { |
| 4690 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4691 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4692 | if (!(req->flags & REQ_F_LINK)) |
| 4693 | return NULL; |
| 4694 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4695 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4696 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4697 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4698 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4699 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4700 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4701 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4702 | } |
| 4703 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4704 | 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] | 4705 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4706 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4707 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame^] | 4708 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4709 | int ret; |
| 4710 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4711 | again: |
| 4712 | linked_timeout = io_prep_linked_timeout(req); |
| 4713 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame^] | 4714 | if (req->work.creds && req->work.creds != current_cred()) { |
| 4715 | if (old_creds) |
| 4716 | revert_creds(old_creds); |
| 4717 | if (old_creds == req->work.creds) |
| 4718 | old_creds = NULL; /* restored original creds */ |
| 4719 | else |
| 4720 | old_creds = override_creds(req->work.creds); |
| 4721 | } |
| 4722 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4723 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4724 | |
| 4725 | /* |
| 4726 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4727 | * doesn't support non-blocking read/write attempts |
| 4728 | */ |
| 4729 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4730 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4731 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4732 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4733 | ret = io_grab_files(req); |
| 4734 | if (ret) |
| 4735 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4736 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4737 | |
| 4738 | /* |
| 4739 | * Queued up for async execution, worker will release |
| 4740 | * submit reference when the iocb is actually submitted. |
| 4741 | */ |
| 4742 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4743 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4744 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4745 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4746 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4747 | /* drop submission reference */ |
| 4748 | io_put_req(req); |
| 4749 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4750 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4751 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4752 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4753 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4754 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4755 | } |
| 4756 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4757 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4758 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4759 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4760 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4761 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4762 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4763 | done_req: |
| 4764 | if (nxt) { |
| 4765 | req = nxt; |
| 4766 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4767 | |
| 4768 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4769 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4770 | goto again; |
| 4771 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame^] | 4772 | if (old_creds) |
| 4773 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4776 | 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] | 4777 | { |
| 4778 | int ret; |
| 4779 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4780 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4781 | if (ret) { |
| 4782 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4783 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4784 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4785 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4786 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4787 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4788 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4789 | ret = io_req_defer_prep(req, sqe); |
| 4790 | if (unlikely(ret < 0)) |
| 4791 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4792 | /* |
| 4793 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4794 | * to async execution. |
| 4795 | */ |
| 4796 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4797 | io_queue_async_work(req); |
| 4798 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4799 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4800 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4801 | } |
| 4802 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4803 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4804 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4805 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4806 | io_cqring_add_event(req, -ECANCELED); |
| 4807 | io_double_put_req(req); |
| 4808 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4809 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4810 | } |
| 4811 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4812 | #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] | 4813 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4814 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4815 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4816 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4817 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4818 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4819 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4820 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4821 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4822 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4823 | |
| 4824 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4825 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4826 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4827 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4828 | } |
| 4829 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4830 | id = READ_ONCE(sqe->personality); |
| 4831 | if (id) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame^] | 4832 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 4833 | if (unlikely(!req->work.creds)) { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4834 | ret = -EINVAL; |
| 4835 | goto err_req; |
| 4836 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame^] | 4837 | get_cred(req->work.creds); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4840 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 4841 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| |
| 4842 | IOSQE_ASYNC); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4843 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4844 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4845 | if (unlikely(ret)) { |
| 4846 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4847 | io_cqring_add_event(req, ret); |
| 4848 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4849 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4850 | } |
| 4851 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4852 | /* |
| 4853 | * If we already have a head request, queue this one for async |
| 4854 | * submittal once the head completes. If we don't have a head but |
| 4855 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4856 | * submitted sync once the chain is complete. If none of those |
| 4857 | * conditions are true (normal request), then just queue it. |
| 4858 | */ |
| 4859 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4860 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4861 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 4862 | /* |
| 4863 | * Taking sequential execution of a link, draining both sides |
| 4864 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 4865 | * requests in the link. So, it drains the head and the |
| 4866 | * next after the link request. The last one is done via |
| 4867 | * drain_next flag to persist the effect across calls. |
| 4868 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4869 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4870 | head->flags |= REQ_F_IO_DRAIN; |
| 4871 | ctx->drain_next = 1; |
| 4872 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4873 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4874 | ret = -EAGAIN; |
| 4875 | goto err_req; |
| 4876 | } |
| 4877 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4878 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4879 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4880 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4881 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4882 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4883 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4884 | trace_io_uring_link(ctx, req, head); |
| 4885 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4886 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4887 | /* last request of a link, enqueue the link */ |
| 4888 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4889 | io_queue_link_head(head); |
| 4890 | *link = NULL; |
| 4891 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4892 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4893 | if (unlikely(ctx->drain_next)) { |
| 4894 | req->flags |= REQ_F_IO_DRAIN; |
| 4895 | req->ctx->drain_next = 0; |
| 4896 | } |
| 4897 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4898 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4899 | INIT_LIST_HEAD(&req->link_list); |
| 4900 | ret = io_req_defer_prep(req, sqe); |
| 4901 | if (ret) |
| 4902 | req->flags |= REQ_F_FAIL_LINK; |
| 4903 | *link = req; |
| 4904 | } else { |
| 4905 | io_queue_sqe(req, sqe); |
| 4906 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4907 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4908 | |
| 4909 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4910 | } |
| 4911 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4912 | /* |
| 4913 | * Batched submission is done, ensure local IO is flushed out. |
| 4914 | */ |
| 4915 | static void io_submit_state_end(struct io_submit_state *state) |
| 4916 | { |
| 4917 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4918 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4919 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 4920 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | /* |
| 4924 | * Start submission side cache. |
| 4925 | */ |
| 4926 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4927 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4928 | { |
| 4929 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4930 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4931 | state->file = NULL; |
| 4932 | state->ios_left = max_ios; |
| 4933 | } |
| 4934 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4935 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4936 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4937 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4938 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4939 | /* |
| 4940 | * Ensure any loads from the SQEs are done at this point, |
| 4941 | * since once we write the new head, the application could |
| 4942 | * write new data to them. |
| 4943 | */ |
| 4944 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4948 | * 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] | 4949 | * that is mapped by userspace. This means that care needs to be taken to |
| 4950 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4951 | * being a good citizen. If members of the sqe are validated and then later |
| 4952 | * used, it's important that those reads are done through READ_ONCE() to |
| 4953 | * prevent a re-load down the line. |
| 4954 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4955 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4956 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4957 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4958 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4959 | unsigned head; |
| 4960 | |
| 4961 | /* |
| 4962 | * The cached sq head (or cq tail) serves two purposes: |
| 4963 | * |
| 4964 | * 1) allows us to batch the cost of updating the user visible |
| 4965 | * head updates. |
| 4966 | * 2) allows the kernel side to track the head on its own, even |
| 4967 | * though the application is the one updating it. |
| 4968 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4969 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4970 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4971 | /* |
| 4972 | * All io need record the previous position, if LINK vs DARIN, |
| 4973 | * it can be used to mark the position of the first IO in the |
| 4974 | * link list. |
| 4975 | */ |
| 4976 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4977 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4978 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4979 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4980 | ctx->cached_sq_head++; |
| 4981 | return true; |
| 4982 | } |
| 4983 | |
| 4984 | /* drop invalid entries */ |
| 4985 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4986 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4987 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4988 | return false; |
| 4989 | } |
| 4990 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4991 | 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] | 4992 | struct file *ring_file, int ring_fd, |
| 4993 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4994 | { |
| 4995 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4996 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4997 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4998 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4999 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 5000 | /* 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] | 5001 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 5002 | if (!list_empty(&ctx->cq_overflow_list) && |
| 5003 | !io_cqring_overflow_flush(ctx, false)) |
| 5004 | return -EBUSY; |
| 5005 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5006 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5007 | /* make sure SQ entry isn't read before tail */ |
| 5008 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 5009 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5010 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 5011 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5012 | |
| 5013 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5014 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5015 | statep = &state; |
| 5016 | } |
| 5017 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5018 | ctx->ring_fd = ring_fd; |
| 5019 | ctx->ring_file = ring_file; |
| 5020 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5021 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5022 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5023 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5024 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5025 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5026 | req = io_get_req(ctx, statep); |
| 5027 | if (unlikely(!req)) { |
| 5028 | if (!submitted) |
| 5029 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5030 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5031 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5032 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5033 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5034 | break; |
| 5035 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5036 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5037 | /* will complete beyond this point, count as submitted */ |
| 5038 | submitted++; |
| 5039 | |
| 5040 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5041 | err = -EINVAL; |
| 5042 | fail_req: |
| 5043 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5044 | io_double_put_req(req); |
| 5045 | break; |
| 5046 | } |
| 5047 | |
| 5048 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5049 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5050 | if (unlikely(mm_fault)) { |
| 5051 | err = -EFAULT; |
| 5052 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5053 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5054 | use_mm(ctx->sqo_mm); |
| 5055 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5056 | } |
| 5057 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5058 | req->in_async = async; |
| 5059 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 5060 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 5061 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5062 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5063 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5064 | } |
| 5065 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 5066 | if (unlikely(submitted != nr)) { |
| 5067 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 5068 | |
| 5069 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 5070 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5071 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5072 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5073 | if (statep) |
| 5074 | io_submit_state_end(&state); |
| 5075 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5076 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5077 | io_commit_sqring(ctx); |
| 5078 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5079 | return submitted; |
| 5080 | } |
| 5081 | |
| 5082 | static int io_sq_thread(void *data) |
| 5083 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5084 | struct io_ring_ctx *ctx = data; |
| 5085 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5086 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5087 | mm_segment_t old_fs; |
| 5088 | DEFINE_WAIT(wait); |
| 5089 | unsigned inflight; |
| 5090 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5091 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5092 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5093 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5094 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5095 | old_fs = get_fs(); |
| 5096 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5097 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5098 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5099 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5100 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5101 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5102 | |
| 5103 | if (inflight) { |
| 5104 | unsigned nr_events = 0; |
| 5105 | |
| 5106 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 5107 | /* |
| 5108 | * inflight is the count of the maximum possible |
| 5109 | * entries we submitted, but it can be smaller |
| 5110 | * if we dropped some of them. If we don't have |
| 5111 | * poll entries available, then we know that we |
| 5112 | * have nothing left to poll for. Reset the |
| 5113 | * inflight count to zero in that case. |
| 5114 | */ |
| 5115 | mutex_lock(&ctx->uring_lock); |
| 5116 | if (!list_empty(&ctx->poll_list)) |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 5117 | io_iopoll_getevents(ctx, &nr_events, 0); |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 5118 | else |
| 5119 | inflight = 0; |
| 5120 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5121 | } else { |
| 5122 | /* |
| 5123 | * Normal IO, just pretend everything completed. |
| 5124 | * We don't have to poll completions for that. |
| 5125 | */ |
| 5126 | nr_events = inflight; |
| 5127 | } |
| 5128 | |
| 5129 | inflight -= nr_events; |
| 5130 | if (!inflight) |
| 5131 | timeout = jiffies + ctx->sq_thread_idle; |
| 5132 | } |
| 5133 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5134 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5135 | |
| 5136 | /* |
| 5137 | * If submit got -EBUSY, flag us as needing the application |
| 5138 | * to enter the kernel to reap and flush events. |
| 5139 | */ |
| 5140 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5141 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 5142 | * Drop cur_mm before scheduling, we can't hold it for |
| 5143 | * long periods (or over schedule()). Do this before |
| 5144 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5145 | * may sleep. |
| 5146 | */ |
| 5147 | if (cur_mm) { |
| 5148 | unuse_mm(cur_mm); |
| 5149 | mmput(cur_mm); |
| 5150 | cur_mm = NULL; |
| 5151 | } |
| 5152 | |
| 5153 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5154 | * We're polling. If we're within the defined idle |
| 5155 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5156 | * to sleep. The exception is if we got EBUSY doing |
| 5157 | * more IO, we should wait for the application to |
| 5158 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5159 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5160 | if (inflight || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5161 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5162 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5163 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5164 | continue; |
| 5165 | } |
| 5166 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5167 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5168 | TASK_INTERRUPTIBLE); |
| 5169 | |
| 5170 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5171 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5172 | /* make sure to read SQ tail after writing flags */ |
| 5173 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5174 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5175 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5176 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5177 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5178 | finish_wait(&ctx->sqo_wait, &wait); |
| 5179 | break; |
| 5180 | } |
| 5181 | if (signal_pending(current)) |
| 5182 | flush_signals(current); |
| 5183 | schedule(); |
| 5184 | finish_wait(&ctx->sqo_wait, &wait); |
| 5185 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5186 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5187 | continue; |
| 5188 | } |
| 5189 | finish_wait(&ctx->sqo_wait, &wait); |
| 5190 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5191 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5194 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5195 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5196 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5197 | if (ret > 0) |
| 5198 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | set_fs(old_fs); |
| 5202 | if (cur_mm) { |
| 5203 | unuse_mm(cur_mm); |
| 5204 | mmput(cur_mm); |
| 5205 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5206 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5207 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5208 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5209 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5210 | return 0; |
| 5211 | } |
| 5212 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5213 | struct io_wait_queue { |
| 5214 | struct wait_queue_entry wq; |
| 5215 | struct io_ring_ctx *ctx; |
| 5216 | unsigned to_wait; |
| 5217 | unsigned nr_timeouts; |
| 5218 | }; |
| 5219 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5220 | 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] | 5221 | { |
| 5222 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5223 | |
| 5224 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5225 | * 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] | 5226 | * started waiting. For timeouts, we always want to return to userspace, |
| 5227 | * regardless of event count. |
| 5228 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5229 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5230 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5231 | } |
| 5232 | |
| 5233 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5234 | int wake_flags, void *key) |
| 5235 | { |
| 5236 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5237 | wq); |
| 5238 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5239 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5240 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5241 | return -1; |
| 5242 | |
| 5243 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5244 | } |
| 5245 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5246 | /* |
| 5247 | * Wait until events become available, if we don't already have some. The |
| 5248 | * application must reap them itself, as they reside on the shared cq ring. |
| 5249 | */ |
| 5250 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5251 | const sigset_t __user *sig, size_t sigsz) |
| 5252 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5253 | struct io_wait_queue iowq = { |
| 5254 | .wq = { |
| 5255 | .private = current, |
| 5256 | .func = io_wake_function, |
| 5257 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5258 | }, |
| 5259 | .ctx = ctx, |
| 5260 | .to_wait = min_events, |
| 5261 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5262 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5263 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5264 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5265 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5266 | return 0; |
| 5267 | |
| 5268 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5269 | #ifdef CONFIG_COMPAT |
| 5270 | if (in_compat_syscall()) |
| 5271 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5272 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5273 | else |
| 5274 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5275 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5276 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5277 | if (ret) |
| 5278 | return ret; |
| 5279 | } |
| 5280 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5281 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5282 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5283 | do { |
| 5284 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5285 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5286 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5287 | break; |
| 5288 | schedule(); |
| 5289 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5290 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5291 | break; |
| 5292 | } |
| 5293 | } while (1); |
| 5294 | finish_wait(&ctx->wait, &iowq.wq); |
| 5295 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5296 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5297 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5298 | 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] | 5299 | } |
| 5300 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5301 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5302 | { |
| 5303 | #if defined(CONFIG_UNIX) |
| 5304 | if (ctx->ring_sock) { |
| 5305 | struct sock *sock = ctx->ring_sock->sk; |
| 5306 | struct sk_buff *skb; |
| 5307 | |
| 5308 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5309 | kfree_skb(skb); |
| 5310 | } |
| 5311 | #else |
| 5312 | int i; |
| 5313 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5314 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5315 | struct file *file; |
| 5316 | |
| 5317 | file = io_file_from_index(ctx, i); |
| 5318 | if (file) |
| 5319 | fput(file); |
| 5320 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5321 | #endif |
| 5322 | } |
| 5323 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5324 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5325 | { |
| 5326 | struct fixed_file_data *data; |
| 5327 | |
| 5328 | data = container_of(ref, struct fixed_file_data, refs); |
| 5329 | complete(&data->done); |
| 5330 | } |
| 5331 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5332 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5333 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5334 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5335 | unsigned nr_tables, i; |
| 5336 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5337 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5338 | return -ENXIO; |
| 5339 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5340 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5341 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5342 | wait_for_completion(&data->done); |
| 5343 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5344 | percpu_ref_exit(&data->refs); |
| 5345 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5346 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5347 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5348 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5349 | kfree(data->table[i].files); |
| 5350 | kfree(data->table); |
| 5351 | kfree(data); |
| 5352 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5353 | ctx->nr_user_files = 0; |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5357 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5358 | { |
| 5359 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5360 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5361 | /* |
| 5362 | * The park is a bit of a work-around, without it we get |
| 5363 | * warning spews on shutdown with SQPOLL set and affinity |
| 5364 | * set to a single CPU. |
| 5365 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5366 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5367 | kthread_stop(ctx->sqo_thread); |
| 5368 | ctx->sqo_thread = NULL; |
| 5369 | } |
| 5370 | } |
| 5371 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5372 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5373 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5374 | io_sq_thread_stop(ctx); |
| 5375 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5376 | if (ctx->io_wq) { |
| 5377 | io_wq_destroy(ctx->io_wq); |
| 5378 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5379 | } |
| 5380 | } |
| 5381 | |
| 5382 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5383 | /* |
| 5384 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5385 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5386 | * loops in the file referencing. |
| 5387 | */ |
| 5388 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5389 | { |
| 5390 | struct sock *sk = ctx->ring_sock->sk; |
| 5391 | struct scm_fp_list *fpl; |
| 5392 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5393 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5394 | |
| 5395 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5396 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5397 | |
| 5398 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5399 | return -EMFILE; |
| 5400 | } |
| 5401 | |
| 5402 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5403 | if (!fpl) |
| 5404 | return -ENOMEM; |
| 5405 | |
| 5406 | skb = alloc_skb(0, GFP_KERNEL); |
| 5407 | if (!skb) { |
| 5408 | kfree(fpl); |
| 5409 | return -ENOMEM; |
| 5410 | } |
| 5411 | |
| 5412 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5413 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5414 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5415 | fpl->user = get_uid(ctx->user); |
| 5416 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5417 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5418 | |
| 5419 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5420 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5421 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5422 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5423 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5424 | } |
| 5425 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5426 | if (nr_files) { |
| 5427 | fpl->max = SCM_MAX_FD; |
| 5428 | fpl->count = nr_files; |
| 5429 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5430 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5431 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5432 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5433 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5434 | for (i = 0; i < nr_files; i++) |
| 5435 | fput(fpl->fp[i]); |
| 5436 | } else { |
| 5437 | kfree_skb(skb); |
| 5438 | kfree(fpl); |
| 5439 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5440 | |
| 5441 | return 0; |
| 5442 | } |
| 5443 | |
| 5444 | /* |
| 5445 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5446 | * causes regular reference counting to break down. We rely on the UNIX |
| 5447 | * garbage collection to take care of this problem for us. |
| 5448 | */ |
| 5449 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5450 | { |
| 5451 | unsigned left, total; |
| 5452 | int ret = 0; |
| 5453 | |
| 5454 | total = 0; |
| 5455 | left = ctx->nr_user_files; |
| 5456 | while (left) { |
| 5457 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5458 | |
| 5459 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5460 | if (ret) |
| 5461 | break; |
| 5462 | left -= this_files; |
| 5463 | total += this_files; |
| 5464 | } |
| 5465 | |
| 5466 | if (!ret) |
| 5467 | return 0; |
| 5468 | |
| 5469 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5470 | struct file *file = io_file_from_index(ctx, total); |
| 5471 | |
| 5472 | if (file) |
| 5473 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5474 | total++; |
| 5475 | } |
| 5476 | |
| 5477 | return ret; |
| 5478 | } |
| 5479 | #else |
| 5480 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5481 | { |
| 5482 | return 0; |
| 5483 | } |
| 5484 | #endif |
| 5485 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5486 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5487 | unsigned nr_files) |
| 5488 | { |
| 5489 | int i; |
| 5490 | |
| 5491 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5492 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5493 | unsigned this_files; |
| 5494 | |
| 5495 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5496 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5497 | GFP_KERNEL); |
| 5498 | if (!table->files) |
| 5499 | break; |
| 5500 | nr_files -= this_files; |
| 5501 | } |
| 5502 | |
| 5503 | if (i == nr_tables) |
| 5504 | return 0; |
| 5505 | |
| 5506 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5507 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5508 | kfree(table->files); |
| 5509 | } |
| 5510 | return 1; |
| 5511 | } |
| 5512 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5513 | 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] | 5514 | { |
| 5515 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5516 | struct sock *sock = ctx->ring_sock->sk; |
| 5517 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5518 | struct sk_buff *skb; |
| 5519 | int i; |
| 5520 | |
| 5521 | __skb_queue_head_init(&list); |
| 5522 | |
| 5523 | /* |
| 5524 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5525 | * remove this entry and rearrange the file array. |
| 5526 | */ |
| 5527 | skb = skb_dequeue(head); |
| 5528 | while (skb) { |
| 5529 | struct scm_fp_list *fp; |
| 5530 | |
| 5531 | fp = UNIXCB(skb).fp; |
| 5532 | for (i = 0; i < fp->count; i++) { |
| 5533 | int left; |
| 5534 | |
| 5535 | if (fp->fp[i] != file) |
| 5536 | continue; |
| 5537 | |
| 5538 | unix_notinflight(fp->user, fp->fp[i]); |
| 5539 | left = fp->count - 1 - i; |
| 5540 | if (left) { |
| 5541 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5542 | left * sizeof(struct file *)); |
| 5543 | } |
| 5544 | fp->count--; |
| 5545 | if (!fp->count) { |
| 5546 | kfree_skb(skb); |
| 5547 | skb = NULL; |
| 5548 | } else { |
| 5549 | __skb_queue_tail(&list, skb); |
| 5550 | } |
| 5551 | fput(file); |
| 5552 | file = NULL; |
| 5553 | break; |
| 5554 | } |
| 5555 | |
| 5556 | if (!file) |
| 5557 | break; |
| 5558 | |
| 5559 | __skb_queue_tail(&list, skb); |
| 5560 | |
| 5561 | skb = skb_dequeue(head); |
| 5562 | } |
| 5563 | |
| 5564 | if (skb_peek(&list)) { |
| 5565 | spin_lock_irq(&head->lock); |
| 5566 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5567 | __skb_queue_tail(head, skb); |
| 5568 | spin_unlock_irq(&head->lock); |
| 5569 | } |
| 5570 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5571 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5572 | #endif |
| 5573 | } |
| 5574 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5575 | struct io_file_put { |
| 5576 | struct llist_node llist; |
| 5577 | struct file *file; |
| 5578 | struct completion *done; |
| 5579 | }; |
| 5580 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5581 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5582 | { |
| 5583 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5584 | struct llist_node *node; |
| 5585 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5586 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5587 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5588 | io_ring_file_put(data->ctx, pfile->file); |
| 5589 | if (pfile->done) |
| 5590 | complete(pfile->done); |
| 5591 | else |
| 5592 | kfree(pfile); |
| 5593 | } |
| 5594 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5595 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5596 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5597 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5598 | { |
| 5599 | struct fixed_file_data *data; |
| 5600 | |
| 5601 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5602 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5603 | percpu_ref_get(&data->refs); |
| 5604 | percpu_ref_switch_to_percpu(&data->refs); |
| 5605 | } |
| 5606 | |
| 5607 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5608 | { |
| 5609 | struct fixed_file_data *data; |
| 5610 | |
| 5611 | data = container_of(ref, struct fixed_file_data, refs); |
| 5612 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5613 | /* |
| 5614 | * We can't safely switch from inside this context, punt to wq. If |
| 5615 | * the table ref is going away, the table is being unregistered. |
| 5616 | * Don't queue up the async work for that case, the caller will |
| 5617 | * handle it. |
| 5618 | */ |
| 5619 | if (!percpu_ref_is_dying(&data->refs)) |
| 5620 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5624 | unsigned nr_args) |
| 5625 | { |
| 5626 | __s32 __user *fds = (__s32 __user *) arg; |
| 5627 | unsigned nr_tables; |
| 5628 | struct file *file; |
| 5629 | int fd, ret = 0; |
| 5630 | unsigned i; |
| 5631 | |
| 5632 | if (ctx->file_data) |
| 5633 | return -EBUSY; |
| 5634 | if (!nr_args) |
| 5635 | return -EINVAL; |
| 5636 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5637 | return -EMFILE; |
| 5638 | |
| 5639 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5640 | if (!ctx->file_data) |
| 5641 | return -ENOMEM; |
| 5642 | ctx->file_data->ctx = ctx; |
| 5643 | init_completion(&ctx->file_data->done); |
| 5644 | |
| 5645 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5646 | ctx->file_data->table = kcalloc(nr_tables, |
| 5647 | sizeof(struct fixed_file_table), |
| 5648 | GFP_KERNEL); |
| 5649 | if (!ctx->file_data->table) { |
| 5650 | kfree(ctx->file_data); |
| 5651 | ctx->file_data = NULL; |
| 5652 | return -ENOMEM; |
| 5653 | } |
| 5654 | |
| 5655 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5656 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5657 | kfree(ctx->file_data->table); |
| 5658 | kfree(ctx->file_data); |
| 5659 | ctx->file_data = NULL; |
| 5660 | return -ENOMEM; |
| 5661 | } |
| 5662 | ctx->file_data->put_llist.first = NULL; |
| 5663 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5664 | |
| 5665 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5666 | percpu_ref_exit(&ctx->file_data->refs); |
| 5667 | kfree(ctx->file_data->table); |
| 5668 | kfree(ctx->file_data); |
| 5669 | ctx->file_data = NULL; |
| 5670 | return -ENOMEM; |
| 5671 | } |
| 5672 | |
| 5673 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5674 | struct fixed_file_table *table; |
| 5675 | unsigned index; |
| 5676 | |
| 5677 | ret = -EFAULT; |
| 5678 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5679 | break; |
| 5680 | /* allow sparse sets */ |
| 5681 | if (fd == -1) { |
| 5682 | ret = 0; |
| 5683 | continue; |
| 5684 | } |
| 5685 | |
| 5686 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5687 | index = i & IORING_FILE_TABLE_MASK; |
| 5688 | file = fget(fd); |
| 5689 | |
| 5690 | ret = -EBADF; |
| 5691 | if (!file) |
| 5692 | break; |
| 5693 | |
| 5694 | /* |
| 5695 | * Don't allow io_uring instances to be registered. If UNIX |
| 5696 | * isn't enabled, then this causes a reference cycle and this |
| 5697 | * instance can never get freed. If UNIX is enabled we'll |
| 5698 | * handle it just fine, but there's still no point in allowing |
| 5699 | * a ring fd as it doesn't support regular read/write anyway. |
| 5700 | */ |
| 5701 | if (file->f_op == &io_uring_fops) { |
| 5702 | fput(file); |
| 5703 | break; |
| 5704 | } |
| 5705 | ret = 0; |
| 5706 | table->files[index] = file; |
| 5707 | } |
| 5708 | |
| 5709 | if (ret) { |
| 5710 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5711 | file = io_file_from_index(ctx, i); |
| 5712 | if (file) |
| 5713 | fput(file); |
| 5714 | } |
| 5715 | for (i = 0; i < nr_tables; i++) |
| 5716 | kfree(ctx->file_data->table[i].files); |
| 5717 | |
| 5718 | kfree(ctx->file_data->table); |
| 5719 | kfree(ctx->file_data); |
| 5720 | ctx->file_data = NULL; |
| 5721 | ctx->nr_user_files = 0; |
| 5722 | return ret; |
| 5723 | } |
| 5724 | |
| 5725 | ret = io_sqe_files_scm(ctx); |
| 5726 | if (ret) |
| 5727 | io_sqe_files_unregister(ctx); |
| 5728 | |
| 5729 | return ret; |
| 5730 | } |
| 5731 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5732 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5733 | int index) |
| 5734 | { |
| 5735 | #if defined(CONFIG_UNIX) |
| 5736 | struct sock *sock = ctx->ring_sock->sk; |
| 5737 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5738 | struct sk_buff *skb; |
| 5739 | |
| 5740 | /* |
| 5741 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5742 | * file set. If there's no room, fall back to allocating a new skb |
| 5743 | * and filling it in. |
| 5744 | */ |
| 5745 | spin_lock_irq(&head->lock); |
| 5746 | skb = skb_peek(head); |
| 5747 | if (skb) { |
| 5748 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5749 | |
| 5750 | if (fpl->count < SCM_MAX_FD) { |
| 5751 | __skb_unlink(skb, head); |
| 5752 | spin_unlock_irq(&head->lock); |
| 5753 | fpl->fp[fpl->count] = get_file(file); |
| 5754 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5755 | fpl->count++; |
| 5756 | spin_lock_irq(&head->lock); |
| 5757 | __skb_queue_head(head, skb); |
| 5758 | } else { |
| 5759 | skb = NULL; |
| 5760 | } |
| 5761 | } |
| 5762 | spin_unlock_irq(&head->lock); |
| 5763 | |
| 5764 | if (skb) { |
| 5765 | fput(file); |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
| 5769 | return __io_sqe_files_scm(ctx, 1, index); |
| 5770 | #else |
| 5771 | return 0; |
| 5772 | #endif |
| 5773 | } |
| 5774 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5775 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5776 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5777 | struct fixed_file_data *data; |
| 5778 | |
| 5779 | data = container_of(ref, struct fixed_file_data, refs); |
| 5780 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5781 | } |
| 5782 | |
| 5783 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5784 | struct file *file) |
| 5785 | { |
| 5786 | struct io_file_put *pfile, pfile_stack; |
| 5787 | DECLARE_COMPLETION_ONSTACK(done); |
| 5788 | |
| 5789 | /* |
| 5790 | * If we fail allocating the struct we need for doing async reomval |
| 5791 | * of this file, just punt to sync and wait for it. |
| 5792 | */ |
| 5793 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5794 | if (!pfile) { |
| 5795 | pfile = &pfile_stack; |
| 5796 | pfile->done = &done; |
| 5797 | } |
| 5798 | |
| 5799 | pfile->file = file; |
| 5800 | llist_add(&pfile->llist, &data->put_llist); |
| 5801 | |
| 5802 | if (pfile == &pfile_stack) { |
| 5803 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5804 | percpu_ref_put(&data->refs); |
| 5805 | percpu_ref_switch_to_atomic(&data->refs, |
| 5806 | io_atomic_switch); |
| 5807 | } |
| 5808 | wait_for_completion(&done); |
| 5809 | flush_work(&data->ref_work); |
| 5810 | return false; |
| 5811 | } |
| 5812 | |
| 5813 | return true; |
| 5814 | } |
| 5815 | |
| 5816 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5817 | struct io_uring_files_update *up, |
| 5818 | unsigned nr_args) |
| 5819 | { |
| 5820 | struct fixed_file_data *data = ctx->file_data; |
| 5821 | bool ref_switch = false; |
| 5822 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5823 | __s32 __user *fds; |
| 5824 | int fd, i, err; |
| 5825 | __u32 done; |
| 5826 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5827 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5828 | return -EOVERFLOW; |
| 5829 | if (done > ctx->nr_user_files) |
| 5830 | return -EINVAL; |
| 5831 | |
| 5832 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5833 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5834 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5835 | struct fixed_file_table *table; |
| 5836 | unsigned index; |
| 5837 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5838 | err = 0; |
| 5839 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5840 | err = -EFAULT; |
| 5841 | break; |
| 5842 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5843 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5844 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5845 | index = i & IORING_FILE_TABLE_MASK; |
| 5846 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5847 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5848 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5849 | if (io_queue_file_removal(data, file)) |
| 5850 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5851 | } |
| 5852 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5853 | file = fget(fd); |
| 5854 | if (!file) { |
| 5855 | err = -EBADF; |
| 5856 | break; |
| 5857 | } |
| 5858 | /* |
| 5859 | * Don't allow io_uring instances to be registered. If |
| 5860 | * UNIX isn't enabled, then this causes a reference |
| 5861 | * cycle and this instance can never get freed. If UNIX |
| 5862 | * is enabled we'll handle it just fine, but there's |
| 5863 | * still no point in allowing a ring fd as it doesn't |
| 5864 | * support regular read/write anyway. |
| 5865 | */ |
| 5866 | if (file->f_op == &io_uring_fops) { |
| 5867 | fput(file); |
| 5868 | err = -EBADF; |
| 5869 | break; |
| 5870 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5871 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5872 | err = io_sqe_file_register(ctx, file, i); |
| 5873 | if (err) |
| 5874 | break; |
| 5875 | } |
| 5876 | nr_args--; |
| 5877 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5878 | up->offset++; |
| 5879 | } |
| 5880 | |
| 5881 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5882 | percpu_ref_put(&data->refs); |
| 5883 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | return done ? done : err; |
| 5887 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5888 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5889 | unsigned nr_args) |
| 5890 | { |
| 5891 | struct io_uring_files_update up; |
| 5892 | |
| 5893 | if (!ctx->file_data) |
| 5894 | return -ENXIO; |
| 5895 | if (!nr_args) |
| 5896 | return -EINVAL; |
| 5897 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5898 | return -EFAULT; |
| 5899 | if (up.resv) |
| 5900 | return -EINVAL; |
| 5901 | |
| 5902 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5903 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5904 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5905 | static void io_put_work(struct io_wq_work *work) |
| 5906 | { |
| 5907 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5908 | |
| 5909 | io_put_req(req); |
| 5910 | } |
| 5911 | |
| 5912 | static void io_get_work(struct io_wq_work *work) |
| 5913 | { |
| 5914 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5915 | |
| 5916 | refcount_inc(&req->refs); |
| 5917 | } |
| 5918 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5919 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 5920 | struct io_uring_params *p) |
| 5921 | { |
| 5922 | struct io_wq_data data; |
| 5923 | struct fd f; |
| 5924 | struct io_ring_ctx *ctx_attach; |
| 5925 | unsigned int concurrency; |
| 5926 | int ret = 0; |
| 5927 | |
| 5928 | data.user = ctx->user; |
| 5929 | data.get_work = io_get_work; |
| 5930 | data.put_work = io_put_work; |
| 5931 | |
| 5932 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 5933 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5934 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 5935 | |
| 5936 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 5937 | if (IS_ERR(ctx->io_wq)) { |
| 5938 | ret = PTR_ERR(ctx->io_wq); |
| 5939 | ctx->io_wq = NULL; |
| 5940 | } |
| 5941 | return ret; |
| 5942 | } |
| 5943 | |
| 5944 | f = fdget(p->wq_fd); |
| 5945 | if (!f.file) |
| 5946 | return -EBADF; |
| 5947 | |
| 5948 | if (f.file->f_op != &io_uring_fops) { |
| 5949 | ret = -EINVAL; |
| 5950 | goto out_fput; |
| 5951 | } |
| 5952 | |
| 5953 | ctx_attach = f.file->private_data; |
| 5954 | /* @io_wq is protected by holding the fd */ |
| 5955 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 5956 | ret = -EINVAL; |
| 5957 | goto out_fput; |
| 5958 | } |
| 5959 | |
| 5960 | ctx->io_wq = ctx_attach->io_wq; |
| 5961 | out_fput: |
| 5962 | fdput(f); |
| 5963 | return ret; |
| 5964 | } |
| 5965 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5966 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5967 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5968 | { |
| 5969 | int ret; |
| 5970 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5971 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5972 | mmgrab(current->mm); |
| 5973 | ctx->sqo_mm = current->mm; |
| 5974 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5975 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5976 | ret = -EPERM; |
| 5977 | if (!capable(CAP_SYS_ADMIN)) |
| 5978 | goto err; |
| 5979 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5980 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5981 | if (!ctx->sq_thread_idle) |
| 5982 | ctx->sq_thread_idle = HZ; |
| 5983 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5984 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5985 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5986 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5987 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5988 | if (cpu >= nr_cpu_ids) |
| 5989 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5990 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5991 | goto err; |
| 5992 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5993 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5994 | ctx, cpu, |
| 5995 | "io_uring-sq"); |
| 5996 | } else { |
| 5997 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5998 | "io_uring-sq"); |
| 5999 | } |
| 6000 | if (IS_ERR(ctx->sqo_thread)) { |
| 6001 | ret = PTR_ERR(ctx->sqo_thread); |
| 6002 | ctx->sqo_thread = NULL; |
| 6003 | goto err; |
| 6004 | } |
| 6005 | wake_up_process(ctx->sqo_thread); |
| 6006 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 6007 | /* Can't have SQ_AFF without SQPOLL */ |
| 6008 | ret = -EINVAL; |
| 6009 | goto err; |
| 6010 | } |
| 6011 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6012 | ret = io_init_wq_offload(ctx, p); |
| 6013 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6014 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6015 | |
| 6016 | return 0; |
| 6017 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 6018 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6019 | mmdrop(ctx->sqo_mm); |
| 6020 | ctx->sqo_mm = NULL; |
| 6021 | return ret; |
| 6022 | } |
| 6023 | |
| 6024 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 6025 | { |
| 6026 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 6027 | } |
| 6028 | |
| 6029 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 6030 | { |
| 6031 | unsigned long page_limit, cur_pages, new_pages; |
| 6032 | |
| 6033 | /* Don't allow more pages than we can safely lock */ |
| 6034 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 6035 | |
| 6036 | do { |
| 6037 | cur_pages = atomic_long_read(&user->locked_vm); |
| 6038 | new_pages = cur_pages + nr_pages; |
| 6039 | if (new_pages > page_limit) |
| 6040 | return -ENOMEM; |
| 6041 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 6042 | new_pages) != cur_pages); |
| 6043 | |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
| 6047 | static void io_mem_free(void *ptr) |
| 6048 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6049 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6050 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6051 | if (!ptr) |
| 6052 | return; |
| 6053 | |
| 6054 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6055 | if (put_page_testzero(page)) |
| 6056 | free_compound_page(page); |
| 6057 | } |
| 6058 | |
| 6059 | static void *io_mem_alloc(size_t size) |
| 6060 | { |
| 6061 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 6062 | __GFP_NORETRY; |
| 6063 | |
| 6064 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 6065 | } |
| 6066 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6067 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 6068 | size_t *sq_offset) |
| 6069 | { |
| 6070 | struct io_rings *rings; |
| 6071 | size_t off, sq_array_size; |
| 6072 | |
| 6073 | off = struct_size(rings, cqes, cq_entries); |
| 6074 | if (off == SIZE_MAX) |
| 6075 | return SIZE_MAX; |
| 6076 | |
| 6077 | #ifdef CONFIG_SMP |
| 6078 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6079 | if (off == 0) |
| 6080 | return SIZE_MAX; |
| 6081 | #endif |
| 6082 | |
| 6083 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6084 | if (sq_array_size == SIZE_MAX) |
| 6085 | return SIZE_MAX; |
| 6086 | |
| 6087 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6088 | return SIZE_MAX; |
| 6089 | |
| 6090 | if (sq_offset) |
| 6091 | *sq_offset = off; |
| 6092 | |
| 6093 | return off; |
| 6094 | } |
| 6095 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6096 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6097 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6098 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6099 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6100 | pages = (size_t)1 << get_order( |
| 6101 | rings_size(sq_entries, cq_entries, NULL)); |
| 6102 | pages += (size_t)1 << get_order( |
| 6103 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6104 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6105 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6106 | } |
| 6107 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6108 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6109 | { |
| 6110 | int i, j; |
| 6111 | |
| 6112 | if (!ctx->user_bufs) |
| 6113 | return -ENXIO; |
| 6114 | |
| 6115 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6116 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6117 | |
| 6118 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6119 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6120 | |
| 6121 | if (ctx->account_mem) |
| 6122 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6123 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6124 | imu->nr_bvecs = 0; |
| 6125 | } |
| 6126 | |
| 6127 | kfree(ctx->user_bufs); |
| 6128 | ctx->user_bufs = NULL; |
| 6129 | ctx->nr_user_bufs = 0; |
| 6130 | return 0; |
| 6131 | } |
| 6132 | |
| 6133 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6134 | void __user *arg, unsigned index) |
| 6135 | { |
| 6136 | struct iovec __user *src; |
| 6137 | |
| 6138 | #ifdef CONFIG_COMPAT |
| 6139 | if (ctx->compat) { |
| 6140 | struct compat_iovec __user *ciovs; |
| 6141 | struct compat_iovec ciov; |
| 6142 | |
| 6143 | ciovs = (struct compat_iovec __user *) arg; |
| 6144 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6145 | return -EFAULT; |
| 6146 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6147 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6148 | dst->iov_len = ciov.iov_len; |
| 6149 | return 0; |
| 6150 | } |
| 6151 | #endif |
| 6152 | src = (struct iovec __user *) arg; |
| 6153 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6154 | return -EFAULT; |
| 6155 | return 0; |
| 6156 | } |
| 6157 | |
| 6158 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6159 | unsigned nr_args) |
| 6160 | { |
| 6161 | struct vm_area_struct **vmas = NULL; |
| 6162 | struct page **pages = NULL; |
| 6163 | int i, j, got_pages = 0; |
| 6164 | int ret = -EINVAL; |
| 6165 | |
| 6166 | if (ctx->user_bufs) |
| 6167 | return -EBUSY; |
| 6168 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6169 | return -EINVAL; |
| 6170 | |
| 6171 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6172 | GFP_KERNEL); |
| 6173 | if (!ctx->user_bufs) |
| 6174 | return -ENOMEM; |
| 6175 | |
| 6176 | for (i = 0; i < nr_args; i++) { |
| 6177 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6178 | unsigned long off, start, end, ubuf; |
| 6179 | int pret, nr_pages; |
| 6180 | struct iovec iov; |
| 6181 | size_t size; |
| 6182 | |
| 6183 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6184 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6185 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6186 | |
| 6187 | /* |
| 6188 | * Don't impose further limits on the size and buffer |
| 6189 | * constraints here, we'll -EINVAL later when IO is |
| 6190 | * submitted if they are wrong. |
| 6191 | */ |
| 6192 | ret = -EFAULT; |
| 6193 | if (!iov.iov_base || !iov.iov_len) |
| 6194 | goto err; |
| 6195 | |
| 6196 | /* arbitrary limit, but we need something */ |
| 6197 | if (iov.iov_len > SZ_1G) |
| 6198 | goto err; |
| 6199 | |
| 6200 | ubuf = (unsigned long) iov.iov_base; |
| 6201 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6202 | start = ubuf >> PAGE_SHIFT; |
| 6203 | nr_pages = end - start; |
| 6204 | |
| 6205 | if (ctx->account_mem) { |
| 6206 | ret = io_account_mem(ctx->user, nr_pages); |
| 6207 | if (ret) |
| 6208 | goto err; |
| 6209 | } |
| 6210 | |
| 6211 | ret = 0; |
| 6212 | if (!pages || nr_pages > got_pages) { |
| 6213 | kfree(vmas); |
| 6214 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6215 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6216 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6217 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6218 | sizeof(struct vm_area_struct *), |
| 6219 | GFP_KERNEL); |
| 6220 | if (!pages || !vmas) { |
| 6221 | ret = -ENOMEM; |
| 6222 | if (ctx->account_mem) |
| 6223 | io_unaccount_mem(ctx->user, nr_pages); |
| 6224 | goto err; |
| 6225 | } |
| 6226 | got_pages = nr_pages; |
| 6227 | } |
| 6228 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6229 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6230 | GFP_KERNEL); |
| 6231 | ret = -ENOMEM; |
| 6232 | if (!imu->bvec) { |
| 6233 | if (ctx->account_mem) |
| 6234 | io_unaccount_mem(ctx->user, nr_pages); |
| 6235 | goto err; |
| 6236 | } |
| 6237 | |
| 6238 | ret = 0; |
| 6239 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6240 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6241 | FOLL_WRITE | FOLL_LONGTERM, |
| 6242 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6243 | if (pret == nr_pages) { |
| 6244 | /* don't support file backed memory */ |
| 6245 | for (j = 0; j < nr_pages; j++) { |
| 6246 | struct vm_area_struct *vma = vmas[j]; |
| 6247 | |
| 6248 | if (vma->vm_file && |
| 6249 | !is_file_hugepages(vma->vm_file)) { |
| 6250 | ret = -EOPNOTSUPP; |
| 6251 | break; |
| 6252 | } |
| 6253 | } |
| 6254 | } else { |
| 6255 | ret = pret < 0 ? pret : -EFAULT; |
| 6256 | } |
| 6257 | up_read(¤t->mm->mmap_sem); |
| 6258 | if (ret) { |
| 6259 | /* |
| 6260 | * if we did partial map, or found file backed vmas, |
| 6261 | * release any pages we did get |
| 6262 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6263 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6264 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6265 | if (ctx->account_mem) |
| 6266 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6267 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6268 | goto err; |
| 6269 | } |
| 6270 | |
| 6271 | off = ubuf & ~PAGE_MASK; |
| 6272 | size = iov.iov_len; |
| 6273 | for (j = 0; j < nr_pages; j++) { |
| 6274 | size_t vec_len; |
| 6275 | |
| 6276 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6277 | imu->bvec[j].bv_page = pages[j]; |
| 6278 | imu->bvec[j].bv_len = vec_len; |
| 6279 | imu->bvec[j].bv_offset = off; |
| 6280 | off = 0; |
| 6281 | size -= vec_len; |
| 6282 | } |
| 6283 | /* store original address for later verification */ |
| 6284 | imu->ubuf = ubuf; |
| 6285 | imu->len = iov.iov_len; |
| 6286 | imu->nr_bvecs = nr_pages; |
| 6287 | |
| 6288 | ctx->nr_user_bufs++; |
| 6289 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6290 | kvfree(pages); |
| 6291 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6292 | return 0; |
| 6293 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6294 | kvfree(pages); |
| 6295 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6296 | io_sqe_buffer_unregister(ctx); |
| 6297 | return ret; |
| 6298 | } |
| 6299 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6300 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6301 | { |
| 6302 | __s32 __user *fds = arg; |
| 6303 | int fd; |
| 6304 | |
| 6305 | if (ctx->cq_ev_fd) |
| 6306 | return -EBUSY; |
| 6307 | |
| 6308 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6309 | return -EFAULT; |
| 6310 | |
| 6311 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6312 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6313 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6314 | ctx->cq_ev_fd = NULL; |
| 6315 | return ret; |
| 6316 | } |
| 6317 | |
| 6318 | return 0; |
| 6319 | } |
| 6320 | |
| 6321 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6322 | { |
| 6323 | if (ctx->cq_ev_fd) { |
| 6324 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6325 | ctx->cq_ev_fd = NULL; |
| 6326 | return 0; |
| 6327 | } |
| 6328 | |
| 6329 | return -ENXIO; |
| 6330 | } |
| 6331 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6332 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6333 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6334 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6335 | if (ctx->sqo_mm) |
| 6336 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6337 | |
| 6338 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6339 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6340 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6341 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6342 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6343 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6344 | if (ctx->ring_sock) { |
| 6345 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6346 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6347 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6348 | #endif |
| 6349 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6350 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6351 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6352 | |
| 6353 | percpu_ref_exit(&ctx->refs); |
| 6354 | if (ctx->account_mem) |
| 6355 | io_unaccount_mem(ctx->user, |
| 6356 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6357 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6358 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6359 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6360 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6361 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6362 | kfree(ctx); |
| 6363 | } |
| 6364 | |
| 6365 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6366 | { |
| 6367 | struct io_ring_ctx *ctx = file->private_data; |
| 6368 | __poll_t mask = 0; |
| 6369 | |
| 6370 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6371 | /* |
| 6372 | * synchronizes with barrier from wq_has_sleeper call in |
| 6373 | * io_commit_cqring |
| 6374 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6375 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6376 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6377 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6378 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 6379 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6380 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6381 | |
| 6382 | return mask; |
| 6383 | } |
| 6384 | |
| 6385 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6386 | { |
| 6387 | struct io_ring_ctx *ctx = file->private_data; |
| 6388 | |
| 6389 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6390 | } |
| 6391 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6392 | static int io_remove_personalities(int id, void *p, void *data) |
| 6393 | { |
| 6394 | struct io_ring_ctx *ctx = data; |
| 6395 | const struct cred *cred; |
| 6396 | |
| 6397 | cred = idr_remove(&ctx->personality_idr, id); |
| 6398 | if (cred) |
| 6399 | put_cred(cred); |
| 6400 | return 0; |
| 6401 | } |
| 6402 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6403 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6404 | { |
| 6405 | mutex_lock(&ctx->uring_lock); |
| 6406 | percpu_ref_kill(&ctx->refs); |
| 6407 | mutex_unlock(&ctx->uring_lock); |
| 6408 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6409 | /* |
| 6410 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 6411 | * work after we've killed the ctx ref above. This is important to do |
| 6412 | * before we cancel existing commands, as the thread could otherwise |
| 6413 | * be queueing new work post that. If that's work we need to cancel, |
| 6414 | * it could cause shutdown to hang. |
| 6415 | */ |
| 6416 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 6417 | cpu_relax(); |
| 6418 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6419 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6420 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6421 | |
| 6422 | if (ctx->io_wq) |
| 6423 | io_wq_cancel_all(ctx->io_wq); |
| 6424 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6425 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6426 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6427 | if (ctx->rings) |
| 6428 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6429 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6430 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6431 | io_ring_ctx_free(ctx); |
| 6432 | } |
| 6433 | |
| 6434 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6435 | { |
| 6436 | struct io_ring_ctx *ctx = file->private_data; |
| 6437 | |
| 6438 | file->private_data = NULL; |
| 6439 | io_ring_ctx_wait_and_kill(ctx); |
| 6440 | return 0; |
| 6441 | } |
| 6442 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6443 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6444 | struct files_struct *files) |
| 6445 | { |
| 6446 | struct io_kiocb *req; |
| 6447 | DEFINE_WAIT(wait); |
| 6448 | |
| 6449 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6450 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6451 | |
| 6452 | spin_lock_irq(&ctx->inflight_lock); |
| 6453 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6454 | if (req->work.files != files) |
| 6455 | continue; |
| 6456 | /* req is being completed, ignore */ |
| 6457 | if (!refcount_inc_not_zero(&req->refs)) |
| 6458 | continue; |
| 6459 | cancel_req = req; |
| 6460 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6461 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6462 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6463 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6464 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6465 | spin_unlock_irq(&ctx->inflight_lock); |
| 6466 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6467 | /* We need to keep going until we don't find a matching req */ |
| 6468 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6469 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6470 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 6471 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 6472 | spin_lock_irq(&ctx->completion_lock); |
| 6473 | list_del(&cancel_req->list); |
| 6474 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 6475 | if (list_empty(&ctx->cq_overflow_list)) { |
| 6476 | clear_bit(0, &ctx->sq_check_overflow); |
| 6477 | clear_bit(0, &ctx->cq_check_overflow); |
| 6478 | } |
| 6479 | spin_unlock_irq(&ctx->completion_lock); |
| 6480 | |
| 6481 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 6482 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 6483 | |
| 6484 | /* |
| 6485 | * Put inflight ref and overflow ref. If that's |
| 6486 | * all we had, then we're done with this request. |
| 6487 | */ |
| 6488 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
| 6489 | io_put_req(cancel_req); |
| 6490 | continue; |
| 6491 | } |
| 6492 | } |
| 6493 | |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6494 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6495 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6496 | schedule(); |
| 6497 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6498 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | static int io_uring_flush(struct file *file, void *data) |
| 6502 | { |
| 6503 | struct io_ring_ctx *ctx = file->private_data; |
| 6504 | |
| 6505 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 6506 | |
| 6507 | /* |
| 6508 | * If the task is going away, cancel work it may have pending |
| 6509 | */ |
| 6510 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 6511 | io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current)); |
| 6512 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6513 | return 0; |
| 6514 | } |
| 6515 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6516 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6517 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6518 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6519 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6520 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6521 | struct page *page; |
| 6522 | void *ptr; |
| 6523 | |
| 6524 | switch (offset) { |
| 6525 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6526 | case IORING_OFF_CQ_RING: |
| 6527 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6528 | break; |
| 6529 | case IORING_OFF_SQES: |
| 6530 | ptr = ctx->sq_sqes; |
| 6531 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6532 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6533 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6534 | } |
| 6535 | |
| 6536 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6537 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6538 | return ERR_PTR(-EINVAL); |
| 6539 | |
| 6540 | return ptr; |
| 6541 | } |
| 6542 | |
| 6543 | #ifdef CONFIG_MMU |
| 6544 | |
| 6545 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6546 | { |
| 6547 | size_t sz = vma->vm_end - vma->vm_start; |
| 6548 | unsigned long pfn; |
| 6549 | void *ptr; |
| 6550 | |
| 6551 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6552 | if (IS_ERR(ptr)) |
| 6553 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6554 | |
| 6555 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6556 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6557 | } |
| 6558 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6559 | #else /* !CONFIG_MMU */ |
| 6560 | |
| 6561 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6562 | { |
| 6563 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6564 | } |
| 6565 | |
| 6566 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6567 | { |
| 6568 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6569 | } |
| 6570 | |
| 6571 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6572 | unsigned long addr, unsigned long len, |
| 6573 | unsigned long pgoff, unsigned long flags) |
| 6574 | { |
| 6575 | void *ptr; |
| 6576 | |
| 6577 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6578 | if (IS_ERR(ptr)) |
| 6579 | return PTR_ERR(ptr); |
| 6580 | |
| 6581 | return (unsigned long) ptr; |
| 6582 | } |
| 6583 | |
| 6584 | #endif /* !CONFIG_MMU */ |
| 6585 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6586 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6587 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6588 | size_t, sigsz) |
| 6589 | { |
| 6590 | struct io_ring_ctx *ctx; |
| 6591 | long ret = -EBADF; |
| 6592 | int submitted = 0; |
| 6593 | struct fd f; |
| 6594 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6595 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6596 | return -EINVAL; |
| 6597 | |
| 6598 | f = fdget(fd); |
| 6599 | if (!f.file) |
| 6600 | return -EBADF; |
| 6601 | |
| 6602 | ret = -EOPNOTSUPP; |
| 6603 | if (f.file->f_op != &io_uring_fops) |
| 6604 | goto out_fput; |
| 6605 | |
| 6606 | ret = -ENXIO; |
| 6607 | ctx = f.file->private_data; |
| 6608 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6609 | goto out_fput; |
| 6610 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6611 | /* |
| 6612 | * For SQ polling, the thread will do all submissions and completions. |
| 6613 | * Just return the requested submit count, and wake the thread if |
| 6614 | * we were asked to. |
| 6615 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6616 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6617 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6618 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6619 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6620 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6621 | wake_up(&ctx->sqo_wait); |
| 6622 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6623 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6624 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6625 | |
| 6626 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6627 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6628 | cur_mm = ctx->sqo_mm; |
| 6629 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6630 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6631 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6632 | |
| 6633 | if (submitted != to_submit) |
| 6634 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6635 | } |
| 6636 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6637 | unsigned nr_events = 0; |
| 6638 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6639 | min_complete = min(min_complete, ctx->cq_entries); |
| 6640 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6641 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6642 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6643 | } else { |
| 6644 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6645 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6646 | } |
| 6647 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6648 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6649 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6650 | out_fput: |
| 6651 | fdput(f); |
| 6652 | return submitted ? submitted : ret; |
| 6653 | } |
| 6654 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6655 | static int io_uring_show_cred(int id, void *p, void *data) |
| 6656 | { |
| 6657 | const struct cred *cred = p; |
| 6658 | struct seq_file *m = data; |
| 6659 | struct user_namespace *uns = seq_user_ns(m); |
| 6660 | struct group_info *gi; |
| 6661 | kernel_cap_t cap; |
| 6662 | unsigned __capi; |
| 6663 | int g; |
| 6664 | |
| 6665 | seq_printf(m, "%5d\n", id); |
| 6666 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 6667 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 6668 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 6669 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 6670 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 6671 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 6672 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 6673 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 6674 | seq_puts(m, "\n\tGroups:\t"); |
| 6675 | gi = cred->group_info; |
| 6676 | for (g = 0; g < gi->ngroups; g++) { |
| 6677 | seq_put_decimal_ull(m, g ? " " : "", |
| 6678 | from_kgid_munged(uns, gi->gid[g])); |
| 6679 | } |
| 6680 | seq_puts(m, "\n\tCapEff:\t"); |
| 6681 | cap = cred->cap_effective; |
| 6682 | CAP_FOR_EACH_U32(__capi) |
| 6683 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 6684 | seq_putc(m, '\n'); |
| 6685 | return 0; |
| 6686 | } |
| 6687 | |
| 6688 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 6689 | { |
| 6690 | int i; |
| 6691 | |
| 6692 | mutex_lock(&ctx->uring_lock); |
| 6693 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 6694 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6695 | struct fixed_file_table *table; |
| 6696 | struct file *f; |
| 6697 | |
| 6698 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6699 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 6700 | if (f) |
| 6701 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 6702 | else |
| 6703 | seq_printf(m, "%5u: <none>\n", i); |
| 6704 | } |
| 6705 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 6706 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6707 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 6708 | |
| 6709 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 6710 | (unsigned int) buf->len); |
| 6711 | } |
| 6712 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 6713 | seq_printf(m, "Personalities:\n"); |
| 6714 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 6715 | } |
| 6716 | mutex_unlock(&ctx->uring_lock); |
| 6717 | } |
| 6718 | |
| 6719 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 6720 | { |
| 6721 | struct io_ring_ctx *ctx = f->private_data; |
| 6722 | |
| 6723 | if (percpu_ref_tryget(&ctx->refs)) { |
| 6724 | __io_uring_show_fdinfo(ctx, m); |
| 6725 | percpu_ref_put(&ctx->refs); |
| 6726 | } |
| 6727 | } |
| 6728 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6729 | static const struct file_operations io_uring_fops = { |
| 6730 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6731 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6732 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6733 | #ifndef CONFIG_MMU |
| 6734 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6735 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6736 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6737 | .poll = io_uring_poll, |
| 6738 | .fasync = io_uring_fasync, |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6739 | .show_fdinfo = io_uring_show_fdinfo, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6740 | }; |
| 6741 | |
| 6742 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6743 | struct io_uring_params *p) |
| 6744 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6745 | struct io_rings *rings; |
| 6746 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6747 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6748 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6749 | if (size == SIZE_MAX) |
| 6750 | return -EOVERFLOW; |
| 6751 | |
| 6752 | rings = io_mem_alloc(size); |
| 6753 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6754 | return -ENOMEM; |
| 6755 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6756 | ctx->rings = rings; |
| 6757 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6758 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6759 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6760 | rings->sq_ring_entries = p->sq_entries; |
| 6761 | rings->cq_ring_entries = p->cq_entries; |
| 6762 | ctx->sq_mask = rings->sq_ring_mask; |
| 6763 | ctx->cq_mask = rings->cq_ring_mask; |
| 6764 | ctx->sq_entries = rings->sq_ring_entries; |
| 6765 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6766 | |
| 6767 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6768 | if (size == SIZE_MAX) { |
| 6769 | io_mem_free(ctx->rings); |
| 6770 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6771 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6772 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6773 | |
| 6774 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6775 | if (!ctx->sq_sqes) { |
| 6776 | io_mem_free(ctx->rings); |
| 6777 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6778 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6779 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6780 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6781 | return 0; |
| 6782 | } |
| 6783 | |
| 6784 | /* |
| 6785 | * Allocate an anonymous fd, this is what constitutes the application |
| 6786 | * visible backing of an io_uring instance. The application mmaps this |
| 6787 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6788 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6789 | */ |
| 6790 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6791 | { |
| 6792 | struct file *file; |
| 6793 | int ret; |
| 6794 | |
| 6795 | #if defined(CONFIG_UNIX) |
| 6796 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6797 | &ctx->ring_sock); |
| 6798 | if (ret) |
| 6799 | return ret; |
| 6800 | #endif |
| 6801 | |
| 6802 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6803 | if (ret < 0) |
| 6804 | goto err; |
| 6805 | |
| 6806 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6807 | O_RDWR | O_CLOEXEC); |
| 6808 | if (IS_ERR(file)) { |
| 6809 | put_unused_fd(ret); |
| 6810 | ret = PTR_ERR(file); |
| 6811 | goto err; |
| 6812 | } |
| 6813 | |
| 6814 | #if defined(CONFIG_UNIX) |
| 6815 | ctx->ring_sock->file = file; |
| 6816 | #endif |
| 6817 | fd_install(ret, file); |
| 6818 | return ret; |
| 6819 | err: |
| 6820 | #if defined(CONFIG_UNIX) |
| 6821 | sock_release(ctx->ring_sock); |
| 6822 | ctx->ring_sock = NULL; |
| 6823 | #endif |
| 6824 | return ret; |
| 6825 | } |
| 6826 | |
| 6827 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6828 | { |
| 6829 | struct user_struct *user = NULL; |
| 6830 | struct io_ring_ctx *ctx; |
| 6831 | bool account_mem; |
| 6832 | int ret; |
| 6833 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6834 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6835 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6836 | if (entries > IORING_MAX_ENTRIES) { |
| 6837 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6838 | return -EINVAL; |
| 6839 | entries = IORING_MAX_ENTRIES; |
| 6840 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6841 | |
| 6842 | /* |
| 6843 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6844 | * application to drive a higher depth than the size of the SQ ring, |
| 6845 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6846 | * some flexibility in overcommitting a bit. If the application has |
| 6847 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6848 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6849 | */ |
| 6850 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6851 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6852 | /* |
| 6853 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6854 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6855 | * any cq vs sq ring sizing. |
| 6856 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6857 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6858 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6859 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6860 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6861 | return -EINVAL; |
| 6862 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6863 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6864 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6865 | } else { |
| 6866 | p->cq_entries = 2 * p->sq_entries; |
| 6867 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6868 | |
| 6869 | user = get_uid(current_user()); |
| 6870 | account_mem = !capable(CAP_IPC_LOCK); |
| 6871 | |
| 6872 | if (account_mem) { |
| 6873 | ret = io_account_mem(user, |
| 6874 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6875 | if (ret) { |
| 6876 | free_uid(user); |
| 6877 | return ret; |
| 6878 | } |
| 6879 | } |
| 6880 | |
| 6881 | ctx = io_ring_ctx_alloc(p); |
| 6882 | if (!ctx) { |
| 6883 | if (account_mem) |
| 6884 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6885 | p->cq_entries)); |
| 6886 | free_uid(user); |
| 6887 | return -ENOMEM; |
| 6888 | } |
| 6889 | ctx->compat = in_compat_syscall(); |
| 6890 | ctx->account_mem = account_mem; |
| 6891 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6892 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6893 | |
| 6894 | ret = io_allocate_scq_urings(ctx, p); |
| 6895 | if (ret) |
| 6896 | goto err; |
| 6897 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6898 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6899 | if (ret) |
| 6900 | goto err; |
| 6901 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6902 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6903 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6904 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6905 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6906 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6907 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6908 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6909 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6910 | |
| 6911 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6912 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6913 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6914 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6915 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6916 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6917 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6918 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6919 | /* |
| 6920 | * Install ring fd as the very last thing, so we don't risk someone |
| 6921 | * having closed it before we finish setup |
| 6922 | */ |
| 6923 | ret = io_uring_get_fd(ctx); |
| 6924 | if (ret < 0) |
| 6925 | goto err; |
| 6926 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6927 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 6928 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
| 6929 | IORING_FEAT_CUR_PERSONALITY; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6930 | 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] | 6931 | return ret; |
| 6932 | err: |
| 6933 | io_ring_ctx_wait_and_kill(ctx); |
| 6934 | return ret; |
| 6935 | } |
| 6936 | |
| 6937 | /* |
| 6938 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6939 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6940 | * params structure passed in. |
| 6941 | */ |
| 6942 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6943 | { |
| 6944 | struct io_uring_params p; |
| 6945 | long ret; |
| 6946 | int i; |
| 6947 | |
| 6948 | if (copy_from_user(&p, params, sizeof(p))) |
| 6949 | return -EFAULT; |
| 6950 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6951 | if (p.resv[i]) |
| 6952 | return -EINVAL; |
| 6953 | } |
| 6954 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6955 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6956 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6957 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6958 | return -EINVAL; |
| 6959 | |
| 6960 | ret = io_uring_create(entries, &p); |
| 6961 | if (ret < 0) |
| 6962 | return ret; |
| 6963 | |
| 6964 | if (copy_to_user(params, &p, sizeof(p))) |
| 6965 | return -EFAULT; |
| 6966 | |
| 6967 | return ret; |
| 6968 | } |
| 6969 | |
| 6970 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6971 | struct io_uring_params __user *, params) |
| 6972 | { |
| 6973 | return io_uring_setup(entries, params); |
| 6974 | } |
| 6975 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6976 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 6977 | { |
| 6978 | struct io_uring_probe *p; |
| 6979 | size_t size; |
| 6980 | int i, ret; |
| 6981 | |
| 6982 | size = struct_size(p, ops, nr_args); |
| 6983 | if (size == SIZE_MAX) |
| 6984 | return -EOVERFLOW; |
| 6985 | p = kzalloc(size, GFP_KERNEL); |
| 6986 | if (!p) |
| 6987 | return -ENOMEM; |
| 6988 | |
| 6989 | ret = -EFAULT; |
| 6990 | if (copy_from_user(p, arg, size)) |
| 6991 | goto out; |
| 6992 | ret = -EINVAL; |
| 6993 | if (memchr_inv(p, 0, size)) |
| 6994 | goto out; |
| 6995 | |
| 6996 | p->last_op = IORING_OP_LAST - 1; |
| 6997 | if (nr_args > IORING_OP_LAST) |
| 6998 | nr_args = IORING_OP_LAST; |
| 6999 | |
| 7000 | for (i = 0; i < nr_args; i++) { |
| 7001 | p->ops[i].op = i; |
| 7002 | if (!io_op_defs[i].not_supported) |
| 7003 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 7004 | } |
| 7005 | p->ops_len = i; |
| 7006 | |
| 7007 | ret = 0; |
| 7008 | if (copy_to_user(arg, p, size)) |
| 7009 | ret = -EFAULT; |
| 7010 | out: |
| 7011 | kfree(p); |
| 7012 | return ret; |
| 7013 | } |
| 7014 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7015 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 7016 | { |
| 7017 | const struct cred *creds = get_current_cred(); |
| 7018 | int id; |
| 7019 | |
| 7020 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 7021 | USHRT_MAX, GFP_KERNEL); |
| 7022 | if (id < 0) |
| 7023 | put_cred(creds); |
| 7024 | return id; |
| 7025 | } |
| 7026 | |
| 7027 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 7028 | { |
| 7029 | const struct cred *old_creds; |
| 7030 | |
| 7031 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 7032 | if (old_creds) { |
| 7033 | put_cred(old_creds); |
| 7034 | return 0; |
| 7035 | } |
| 7036 | |
| 7037 | return -EINVAL; |
| 7038 | } |
| 7039 | |
| 7040 | static bool io_register_op_must_quiesce(int op) |
| 7041 | { |
| 7042 | switch (op) { |
| 7043 | case IORING_UNREGISTER_FILES: |
| 7044 | case IORING_REGISTER_FILES_UPDATE: |
| 7045 | case IORING_REGISTER_PROBE: |
| 7046 | case IORING_REGISTER_PERSONALITY: |
| 7047 | case IORING_UNREGISTER_PERSONALITY: |
| 7048 | return false; |
| 7049 | default: |
| 7050 | return true; |
| 7051 | } |
| 7052 | } |
| 7053 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7054 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 7055 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7056 | __releases(ctx->uring_lock) |
| 7057 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7058 | { |
| 7059 | int ret; |
| 7060 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 7061 | /* |
| 7062 | * We're inside the ring mutex, if the ref is already dying, then |
| 7063 | * someone else killed the ctx or is already going through |
| 7064 | * io_uring_register(). |
| 7065 | */ |
| 7066 | if (percpu_ref_is_dying(&ctx->refs)) |
| 7067 | return -ENXIO; |
| 7068 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7069 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7070 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7071 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7072 | /* |
| 7073 | * Drop uring mutex before waiting for references to exit. If |
| 7074 | * another thread is currently inside io_uring_enter() it might |
| 7075 | * need to grab the uring_lock to make progress. If we hold it |
| 7076 | * here across the drain wait, then we can deadlock. It's safe |
| 7077 | * to drop the mutex here, since no new references will come in |
| 7078 | * after we've killed the percpu ref. |
| 7079 | */ |
| 7080 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7081 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7082 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7083 | if (ret) { |
| 7084 | percpu_ref_resurrect(&ctx->refs); |
| 7085 | ret = -EINTR; |
| 7086 | goto out; |
| 7087 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7088 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7089 | |
| 7090 | switch (opcode) { |
| 7091 | case IORING_REGISTER_BUFFERS: |
| 7092 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 7093 | break; |
| 7094 | case IORING_UNREGISTER_BUFFERS: |
| 7095 | ret = -EINVAL; |
| 7096 | if (arg || nr_args) |
| 7097 | break; |
| 7098 | ret = io_sqe_buffer_unregister(ctx); |
| 7099 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7100 | case IORING_REGISTER_FILES: |
| 7101 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 7102 | break; |
| 7103 | case IORING_UNREGISTER_FILES: |
| 7104 | ret = -EINVAL; |
| 7105 | if (arg || nr_args) |
| 7106 | break; |
| 7107 | ret = io_sqe_files_unregister(ctx); |
| 7108 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7109 | case IORING_REGISTER_FILES_UPDATE: |
| 7110 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7111 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7112 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7113 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7114 | ret = -EINVAL; |
| 7115 | if (nr_args != 1) |
| 7116 | break; |
| 7117 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7118 | if (ret) |
| 7119 | break; |
| 7120 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7121 | ctx->eventfd_async = 1; |
| 7122 | else |
| 7123 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7124 | break; |
| 7125 | case IORING_UNREGISTER_EVENTFD: |
| 7126 | ret = -EINVAL; |
| 7127 | if (arg || nr_args) |
| 7128 | break; |
| 7129 | ret = io_eventfd_unregister(ctx); |
| 7130 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7131 | case IORING_REGISTER_PROBE: |
| 7132 | ret = -EINVAL; |
| 7133 | if (!arg || nr_args > 256) |
| 7134 | break; |
| 7135 | ret = io_probe(ctx, arg, nr_args); |
| 7136 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7137 | case IORING_REGISTER_PERSONALITY: |
| 7138 | ret = -EINVAL; |
| 7139 | if (arg || nr_args) |
| 7140 | break; |
| 7141 | ret = io_register_personality(ctx); |
| 7142 | break; |
| 7143 | case IORING_UNREGISTER_PERSONALITY: |
| 7144 | ret = -EINVAL; |
| 7145 | if (arg) |
| 7146 | break; |
| 7147 | ret = io_unregister_personality(ctx, nr_args); |
| 7148 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7149 | default: |
| 7150 | ret = -EINVAL; |
| 7151 | break; |
| 7152 | } |
| 7153 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7154 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7155 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7156 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7157 | out: |
| 7158 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7159 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7160 | return ret; |
| 7161 | } |
| 7162 | |
| 7163 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7164 | void __user *, arg, unsigned int, nr_args) |
| 7165 | { |
| 7166 | struct io_ring_ctx *ctx; |
| 7167 | long ret = -EBADF; |
| 7168 | struct fd f; |
| 7169 | |
| 7170 | f = fdget(fd); |
| 7171 | if (!f.file) |
| 7172 | return -EBADF; |
| 7173 | |
| 7174 | ret = -EOPNOTSUPP; |
| 7175 | if (f.file->f_op != &io_uring_fops) |
| 7176 | goto out_fput; |
| 7177 | |
| 7178 | ctx = f.file->private_data; |
| 7179 | |
| 7180 | mutex_lock(&ctx->uring_lock); |
| 7181 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7182 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7183 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7184 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7185 | out_fput: |
| 7186 | fdput(f); |
| 7187 | return ret; |
| 7188 | } |
| 7189 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7190 | static int __init io_uring_init(void) |
| 7191 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7192 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7193 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7194 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7195 | } while (0) |
| 7196 | |
| 7197 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7198 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7199 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7200 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7201 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7202 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7203 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7204 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7205 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7206 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
| 7207 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7208 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7209 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7210 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7211 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7212 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7213 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7214 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7215 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7216 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7217 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7218 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7219 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7220 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
| 7221 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7222 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7223 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
| 7224 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7225 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7226 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 7227 | return 0; |
| 7228 | }; |
| 7229 | __initcall(io_uring_init); |