Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 49 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 50 | |
| 51 | #include <linux/sched/signal.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/file.h> |
| 54 | #include <linux/fdtable.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/mman.h> |
| 57 | #include <linux/mmu_context.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 78 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 79 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 80 | #include <linux/task_work.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 81 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 82 | #define CREATE_TRACE_POINTS |
| 83 | #include <trace/events/io_uring.h> |
| 84 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | #include <uapi/linux/io_uring.h> |
| 86 | |
| 87 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 88 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 89 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 90 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 91 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 95 | */ |
| 96 | #define IORING_FILE_TABLE_SHIFT 9 |
| 97 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 98 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 99 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 100 | |
| 101 | struct io_uring { |
| 102 | u32 head ____cacheline_aligned_in_smp; |
| 103 | u32 tail ____cacheline_aligned_in_smp; |
| 104 | }; |
| 105 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 107 | * This data is shared with the application through the mmap at offsets |
| 108 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 109 | * |
| 110 | * The offsets to the member fields are published through struct |
| 111 | * io_sqring_offsets when calling io_uring_setup. |
| 112 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 114 | /* |
| 115 | * Head and tail offsets into the ring; the offsets need to be |
| 116 | * masked to get valid indices. |
| 117 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 118 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 119 | * and the application controls tail of the sq ring and the head of the |
| 120 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 123 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | * ring_entries - 1) |
| 126 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 127 | u32 sq_ring_mask, cq_ring_mask; |
| 128 | /* Ring sizes (constant, power of 2) */ |
| 129 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 130 | /* |
| 131 | * Number of invalid entries dropped by the kernel due to |
| 132 | * invalid index stored in array |
| 133 | * |
| 134 | * Written by the kernel, shouldn't be modified by the |
| 135 | * application (i.e. get number of "new events" by comparing to |
| 136 | * cached value). |
| 137 | * |
| 138 | * After a new SQ head value was read by the application this |
| 139 | * counter includes all submissions that were dropped reaching |
| 140 | * the new SQ head (and possibly more). |
| 141 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 142 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 143 | /* |
| 144 | * Runtime flags |
| 145 | * |
| 146 | * Written by the kernel, shouldn't be modified by the |
| 147 | * application. |
| 148 | * |
| 149 | * The application needs a full memory barrier before checking |
| 150 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 151 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 152 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 153 | /* |
| 154 | * Number of completion events lost because the queue was full; |
| 155 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 156 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 157 | * the completion queue. |
| 158 | * |
| 159 | * Written by the kernel, shouldn't be modified by the |
| 160 | * application (i.e. get number of "new events" by comparing to |
| 161 | * cached value). |
| 162 | * |
| 163 | * As completion events come in out of order this counter is not |
| 164 | * ordered with any other data. |
| 165 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 166 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Ring buffer of completion events. |
| 169 | * |
| 170 | * The kernel writes completion events fresh every time they are |
| 171 | * produced, so the application is allowed to modify pending |
| 172 | * entries. |
| 173 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 174 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 177 | struct io_mapped_ubuf { |
| 178 | u64 ubuf; |
| 179 | size_t len; |
| 180 | struct bio_vec *bvec; |
| 181 | unsigned int nr_bvecs; |
| 182 | }; |
| 183 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 184 | struct fixed_file_table { |
| 185 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 188 | struct fixed_file_data { |
| 189 | struct fixed_file_table *table; |
| 190 | struct io_ring_ctx *ctx; |
| 191 | |
| 192 | struct percpu_ref refs; |
| 193 | struct llist_head put_llist; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 194 | struct work_struct ref_work; |
| 195 | struct completion done; |
| 196 | }; |
| 197 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 198 | struct io_ring_ctx { |
| 199 | struct { |
| 200 | struct percpu_ref refs; |
| 201 | } ____cacheline_aligned_in_smp; |
| 202 | |
| 203 | struct { |
| 204 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 205 | unsigned int compat: 1; |
| 206 | unsigned int account_mem: 1; |
| 207 | unsigned int cq_overflow_flushed: 1; |
| 208 | unsigned int drain_next: 1; |
| 209 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 210 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 213 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 214 | * |
| 215 | * This indirection could e.g. be used to assign fixed |
| 216 | * io_uring_sqe entries to operations and only submit them to |
| 217 | * the queue when needed. |
| 218 | * |
| 219 | * The kernel modifies neither the indices array nor the entries |
| 220 | * array. |
| 221 | */ |
| 222 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 223 | unsigned cached_sq_head; |
| 224 | unsigned sq_entries; |
| 225 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 226 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 227 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 228 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 229 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 230 | |
| 231 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 232 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 233 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 234 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 235 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 236 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 237 | } ____cacheline_aligned_in_smp; |
| 238 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 239 | struct io_rings *rings; |
| 240 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 241 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 242 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 243 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 244 | struct mm_struct *sqo_mm; |
| 245 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 246 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 247 | /* |
| 248 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 249 | * readers must ensure that ->refs is alive as long as the file* is |
| 250 | * used. Only updated through io_uring_register(2). |
| 251 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 252 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 253 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 254 | int ring_fd; |
| 255 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 256 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 257 | /* if used, fixed mapped user buffers */ |
| 258 | unsigned nr_user_bufs; |
| 259 | struct io_mapped_ubuf *user_bufs; |
| 260 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 261 | struct user_struct *user; |
| 262 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 263 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 264 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 265 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 266 | struct completion *completions; |
| 267 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 268 | /* if all else fails... */ |
| 269 | struct io_kiocb *fallback_req; |
| 270 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 271 | #if defined(CONFIG_UNIX) |
| 272 | struct socket *ring_sock; |
| 273 | #endif |
| 274 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 275 | struct idr personality_idr; |
| 276 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 277 | struct { |
| 278 | unsigned cached_cq_tail; |
| 279 | unsigned cq_entries; |
| 280 | unsigned cq_mask; |
| 281 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 282 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 283 | struct wait_queue_head cq_wait; |
| 284 | struct fasync_struct *cq_fasync; |
| 285 | struct eventfd_ctx *cq_ev_fd; |
| 286 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 287 | |
| 288 | struct { |
| 289 | struct mutex uring_lock; |
| 290 | wait_queue_head_t wait; |
| 291 | } ____cacheline_aligned_in_smp; |
| 292 | |
| 293 | struct { |
| 294 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 295 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 296 | /* |
| 297 | * ->poll_list is protected by the ctx->uring_lock for |
| 298 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 299 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 300 | * manipulate the list, hence no extra locking is needed there. |
| 301 | */ |
| 302 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 303 | struct hlist_head *cancel_hash; |
| 304 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 305 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 306 | |
| 307 | spinlock_t inflight_lock; |
| 308 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 309 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 312 | /* |
| 313 | * First field must be the file pointer in all the |
| 314 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 315 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 316 | struct io_poll_iocb { |
| 317 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 318 | union { |
| 319 | struct wait_queue_head *head; |
| 320 | u64 addr; |
| 321 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 322 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 323 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 324 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 325 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 328 | struct io_close { |
| 329 | struct file *file; |
| 330 | struct file *put_file; |
| 331 | int fd; |
| 332 | }; |
| 333 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 334 | struct io_timeout_data { |
| 335 | struct io_kiocb *req; |
| 336 | struct hrtimer timer; |
| 337 | struct timespec64 ts; |
| 338 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 339 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 342 | struct io_accept { |
| 343 | struct file *file; |
| 344 | struct sockaddr __user *addr; |
| 345 | int __user *addr_len; |
| 346 | int flags; |
| 347 | }; |
| 348 | |
| 349 | struct io_sync { |
| 350 | struct file *file; |
| 351 | loff_t len; |
| 352 | loff_t off; |
| 353 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 354 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 357 | struct io_cancel { |
| 358 | struct file *file; |
| 359 | u64 addr; |
| 360 | }; |
| 361 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 362 | struct io_timeout { |
| 363 | struct file *file; |
| 364 | u64 addr; |
| 365 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 366 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 367 | }; |
| 368 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 369 | struct io_rw { |
| 370 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 371 | struct kiocb kiocb; |
| 372 | u64 addr; |
| 373 | u64 len; |
| 374 | }; |
| 375 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 376 | struct io_connect { |
| 377 | struct file *file; |
| 378 | struct sockaddr __user *addr; |
| 379 | int addr_len; |
| 380 | }; |
| 381 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 382 | struct io_sr_msg { |
| 383 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 384 | union { |
| 385 | struct user_msghdr __user *msg; |
| 386 | void __user *buf; |
| 387 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 388 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 389 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 392 | struct io_open { |
| 393 | struct file *file; |
| 394 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 395 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 396 | unsigned mask; |
| 397 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 398 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 399 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 400 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 403 | struct io_files_update { |
| 404 | struct file *file; |
| 405 | u64 arg; |
| 406 | u32 nr_args; |
| 407 | u32 offset; |
| 408 | }; |
| 409 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 410 | struct io_fadvise { |
| 411 | struct file *file; |
| 412 | u64 offset; |
| 413 | u32 len; |
| 414 | u32 advice; |
| 415 | }; |
| 416 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 417 | struct io_madvise { |
| 418 | struct file *file; |
| 419 | u64 addr; |
| 420 | u32 len; |
| 421 | u32 advice; |
| 422 | }; |
| 423 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 424 | struct io_epoll { |
| 425 | struct file *file; |
| 426 | int epfd; |
| 427 | int op; |
| 428 | int fd; |
| 429 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 430 | }; |
| 431 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 432 | struct io_splice { |
| 433 | struct file *file_out; |
| 434 | struct file *file_in; |
| 435 | loff_t off_out; |
| 436 | loff_t off_in; |
| 437 | u64 len; |
| 438 | unsigned int flags; |
| 439 | }; |
| 440 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 441 | struct io_async_connect { |
| 442 | struct sockaddr_storage address; |
| 443 | }; |
| 444 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 445 | struct io_async_msghdr { |
| 446 | struct iovec fast_iov[UIO_FASTIOV]; |
| 447 | struct iovec *iov; |
| 448 | struct sockaddr __user *uaddr; |
| 449 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 450 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 451 | }; |
| 452 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 453 | struct io_async_rw { |
| 454 | struct iovec fast_iov[UIO_FASTIOV]; |
| 455 | struct iovec *iov; |
| 456 | ssize_t nr_segs; |
| 457 | ssize_t size; |
| 458 | }; |
| 459 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 460 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 461 | union { |
| 462 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 463 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 464 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 465 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 466 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 469 | enum { |
| 470 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 471 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 472 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 473 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 474 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 475 | |
| 476 | REQ_F_LINK_NEXT_BIT, |
| 477 | REQ_F_FAIL_LINK_BIT, |
| 478 | REQ_F_INFLIGHT_BIT, |
| 479 | REQ_F_CUR_POS_BIT, |
| 480 | REQ_F_NOWAIT_BIT, |
| 481 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 482 | REQ_F_LINK_TIMEOUT_BIT, |
| 483 | REQ_F_TIMEOUT_BIT, |
| 484 | REQ_F_ISREG_BIT, |
| 485 | REQ_F_MUST_PUNT_BIT, |
| 486 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 487 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 488 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 489 | REQ_F_OVERFLOW_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | enum { |
| 493 | /* ctx owns file */ |
| 494 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 495 | /* drain existing IO first */ |
| 496 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 497 | /* linked sqes */ |
| 498 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 499 | /* doesn't sever on completion < 0 */ |
| 500 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 501 | /* IOSQE_ASYNC */ |
| 502 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 503 | |
| 504 | /* already grabbed next link */ |
| 505 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 506 | /* fail rest of links */ |
| 507 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 508 | /* on inflight list */ |
| 509 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 510 | /* read/write uses file position */ |
| 511 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 512 | /* must not punt to workers */ |
| 513 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 514 | /* polled IO has completed */ |
| 515 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 516 | /* has linked timeout */ |
| 517 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 518 | /* timeout request */ |
| 519 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 520 | /* regular file */ |
| 521 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 522 | /* must be punted even for NONBLOCK */ |
| 523 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 524 | /* no timeout sequence */ |
| 525 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 526 | /* completion under lock */ |
| 527 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 528 | /* needs cleanup */ |
| 529 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 530 | /* in overflow list */ |
| 531 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 532 | }; |
| 533 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 534 | /* |
| 535 | * NOTE! Each of the iocb union members has the file pointer |
| 536 | * as the first entry in their struct definition. So you can |
| 537 | * access the file pointer through any of the sub-structs, |
| 538 | * or directly as just 'ki_filp' in this struct. |
| 539 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 540 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 541 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 542 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 543 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 544 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 545 | struct io_accept accept; |
| 546 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 547 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 548 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 549 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 550 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 551 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 552 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 553 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 554 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 555 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 556 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 557 | struct io_splice splice; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 558 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 559 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 560 | struct io_async_ctx *io; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 561 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 562 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 563 | |
| 564 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 565 | union { |
| 566 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 567 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 568 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 569 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 570 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 571 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 572 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 573 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 574 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 575 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 576 | struct list_head inflight_entry; |
| 577 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 578 | union { |
| 579 | /* |
| 580 | * Only commands that never go async can use the below fields, |
| 581 | * obviously. Right now only IORING_OP_POLL_ADD uses them. |
| 582 | */ |
| 583 | struct { |
| 584 | struct task_struct *task; |
| 585 | struct callback_head task_work; |
| 586 | }; |
| 587 | struct io_wq_work work; |
| 588 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 589 | }; |
| 590 | |
| 591 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 592 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 593 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 594 | struct io_submit_state { |
| 595 | struct blk_plug plug; |
| 596 | |
| 597 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 598 | * io_kiocb alloc cache |
| 599 | */ |
| 600 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 601 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 602 | |
| 603 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 604 | * File reference cache |
| 605 | */ |
| 606 | struct file *file; |
| 607 | unsigned int fd; |
| 608 | unsigned int has_refs; |
| 609 | unsigned int used_refs; |
| 610 | unsigned int ios_left; |
| 611 | }; |
| 612 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 613 | struct io_op_def { |
| 614 | /* needs req->io allocated for deferral/async */ |
| 615 | unsigned async_ctx : 1; |
| 616 | /* needs current->mm setup, does mm access */ |
| 617 | unsigned needs_mm : 1; |
| 618 | /* needs req->file assigned */ |
| 619 | unsigned needs_file : 1; |
| 620 | /* needs req->file assigned IFF fd is >= 0 */ |
| 621 | unsigned fd_non_neg : 1; |
| 622 | /* hash wq insertion if file is a regular file */ |
| 623 | unsigned hash_reg_file : 1; |
| 624 | /* unbound wq insertion if file is a non-regular file */ |
| 625 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 626 | /* opcode is not supported by this kernel */ |
| 627 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 628 | /* needs file table */ |
| 629 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 630 | /* needs ->fs */ |
| 631 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 632 | /* set if opcode supports polled "wait" */ |
| 633 | unsigned pollin : 1; |
| 634 | unsigned pollout : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 635 | }; |
| 636 | |
| 637 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 638 | [IORING_OP_NOP] = {}, |
| 639 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 640 | .async_ctx = 1, |
| 641 | .needs_mm = 1, |
| 642 | .needs_file = 1, |
| 643 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 644 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 645 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 646 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 647 | .async_ctx = 1, |
| 648 | .needs_mm = 1, |
| 649 | .needs_file = 1, |
| 650 | .hash_reg_file = 1, |
| 651 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 652 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 653 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 654 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 655 | .needs_file = 1, |
| 656 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 657 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 658 | .needs_file = 1, |
| 659 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 660 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 661 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 662 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 663 | .needs_file = 1, |
| 664 | .hash_reg_file = 1, |
| 665 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 666 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 667 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 668 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 669 | .needs_file = 1, |
| 670 | .unbound_nonreg_file = 1, |
| 671 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 672 | [IORING_OP_POLL_REMOVE] = {}, |
| 673 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 674 | .needs_file = 1, |
| 675 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 676 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 677 | .async_ctx = 1, |
| 678 | .needs_mm = 1, |
| 679 | .needs_file = 1, |
| 680 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 681 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 682 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 683 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 684 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 685 | .async_ctx = 1, |
| 686 | .needs_mm = 1, |
| 687 | .needs_file = 1, |
| 688 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 689 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 690 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 691 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 692 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 693 | .async_ctx = 1, |
| 694 | .needs_mm = 1, |
| 695 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 696 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 697 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 698 | .needs_mm = 1, |
| 699 | .needs_file = 1, |
| 700 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 701 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 702 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 703 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 704 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 705 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 706 | .async_ctx = 1, |
| 707 | .needs_mm = 1, |
| 708 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 709 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 710 | .async_ctx = 1, |
| 711 | .needs_mm = 1, |
| 712 | .needs_file = 1, |
| 713 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 714 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 715 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 716 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 717 | .needs_file = 1, |
| 718 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 719 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 720 | .needs_file = 1, |
| 721 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 722 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 723 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 724 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 725 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 726 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 727 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 728 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 729 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 730 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 731 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 732 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 733 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 734 | .needs_mm = 1, |
| 735 | .needs_file = 1, |
| 736 | .fd_non_neg = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 737 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 738 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 739 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 740 | .needs_mm = 1, |
| 741 | .needs_file = 1, |
| 742 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 743 | .pollin = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 744 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 745 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 746 | .needs_mm = 1, |
| 747 | .needs_file = 1, |
| 748 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 749 | .pollout = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 750 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 751 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 752 | .needs_file = 1, |
| 753 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 754 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 755 | .needs_mm = 1, |
| 756 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 757 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 758 | .needs_mm = 1, |
| 759 | .needs_file = 1, |
| 760 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 761 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 762 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 763 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 764 | .needs_mm = 1, |
| 765 | .needs_file = 1, |
| 766 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame^] | 767 | .pollin = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 768 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 769 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 770 | .needs_file = 1, |
| 771 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 772 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 773 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 774 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 775 | [IORING_OP_EPOLL_CTL] = { |
| 776 | .unbound_nonreg_file = 1, |
| 777 | .file_table = 1, |
| 778 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 779 | [IORING_OP_SPLICE] = { |
| 780 | .needs_file = 1, |
| 781 | .hash_reg_file = 1, |
| 782 | .unbound_nonreg_file = 1, |
| 783 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 784 | }; |
| 785 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 786 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 787 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 788 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 789 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 790 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 791 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 792 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 793 | struct io_uring_files_update *ip, |
| 794 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 795 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 796 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 797 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 798 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 799 | int fd, struct file **out_file, bool fixed); |
| 800 | static void __io_queue_sqe(struct io_kiocb *req, |
| 801 | const struct io_uring_sqe *sqe); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 802 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 803 | static struct kmem_cache *req_cachep; |
| 804 | |
| 805 | static const struct file_operations io_uring_fops; |
| 806 | |
| 807 | struct sock *io_uring_get_socket(struct file *file) |
| 808 | { |
| 809 | #if defined(CONFIG_UNIX) |
| 810 | if (file->f_op == &io_uring_fops) { |
| 811 | struct io_ring_ctx *ctx = file->private_data; |
| 812 | |
| 813 | return ctx->ring_sock->sk; |
| 814 | } |
| 815 | #endif |
| 816 | return NULL; |
| 817 | } |
| 818 | EXPORT_SYMBOL(io_uring_get_socket); |
| 819 | |
| 820 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 821 | { |
| 822 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 823 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 824 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 828 | { |
| 829 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 830 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 831 | |
| 832 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 833 | if (!ctx) |
| 834 | return NULL; |
| 835 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 836 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 837 | if (!ctx->fallback_req) |
| 838 | goto err; |
| 839 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 840 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 841 | if (!ctx->completions) |
| 842 | goto err; |
| 843 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 844 | /* |
| 845 | * Use 5 bits less than the max cq entries, that should give us around |
| 846 | * 32 entries per hash list if totally full and uniformly spread. |
| 847 | */ |
| 848 | hash_bits = ilog2(p->cq_entries); |
| 849 | hash_bits -= 5; |
| 850 | if (hash_bits <= 0) |
| 851 | hash_bits = 1; |
| 852 | ctx->cancel_hash_bits = hash_bits; |
| 853 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 854 | GFP_KERNEL); |
| 855 | if (!ctx->cancel_hash) |
| 856 | goto err; |
| 857 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 858 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 859 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 860 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 861 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 862 | |
| 863 | ctx->flags = p->flags; |
| 864 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 865 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 866 | init_completion(&ctx->completions[0]); |
| 867 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 868 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 869 | mutex_init(&ctx->uring_lock); |
| 870 | init_waitqueue_head(&ctx->wait); |
| 871 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 872 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 873 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 874 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 875 | init_waitqueue_head(&ctx->inflight_wait); |
| 876 | spin_lock_init(&ctx->inflight_lock); |
| 877 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 878 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 879 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 880 | if (ctx->fallback_req) |
| 881 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 882 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 883 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 884 | kfree(ctx); |
| 885 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 888 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 889 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 890 | struct io_ring_ctx *ctx = req->ctx; |
| 891 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 892 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 893 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 894 | } |
| 895 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 896 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 897 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 898 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 899 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 900 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 901 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | 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] | 905 | { |
| 906 | struct io_kiocb *req; |
| 907 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 908 | 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] | 909 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 910 | list_del_init(&req->list); |
| 911 | return req; |
| 912 | } |
| 913 | |
| 914 | return NULL; |
| 915 | } |
| 916 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 917 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 918 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 919 | struct io_kiocb *req; |
| 920 | |
| 921 | 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] | 922 | if (req) { |
| 923 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 924 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 925 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 926 | list_del_init(&req->list); |
| 927 | return req; |
| 928 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 932 | } |
| 933 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 934 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 935 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 936 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 937 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 938 | /* order cqe stores with ring update */ |
| 939 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 940 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 941 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 942 | wake_up_interruptible(&ctx->cq_wait); |
| 943 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 947 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 948 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 949 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 950 | if (!req->work.mm && def->needs_mm) { |
| 951 | mmgrab(current->mm); |
| 952 | req->work.mm = current->mm; |
| 953 | } |
| 954 | if (!req->work.creds) |
| 955 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 956 | if (!req->work.fs && def->needs_fs) { |
| 957 | spin_lock(¤t->fs->lock); |
| 958 | if (!current->fs->in_exec) { |
| 959 | req->work.fs = current->fs; |
| 960 | req->work.fs->users++; |
| 961 | } else { |
| 962 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 963 | } |
| 964 | spin_unlock(¤t->fs->lock); |
| 965 | } |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 966 | if (!req->work.task_pid) |
| 967 | req->work.task_pid = task_pid_vnr(current); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 971 | { |
| 972 | if (req->work.mm) { |
| 973 | mmdrop(req->work.mm); |
| 974 | req->work.mm = NULL; |
| 975 | } |
| 976 | if (req->work.creds) { |
| 977 | put_cred(req->work.creds); |
| 978 | req->work.creds = NULL; |
| 979 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 980 | if (req->work.fs) { |
| 981 | struct fs_struct *fs = req->work.fs; |
| 982 | |
| 983 | spin_lock(&req->work.fs->lock); |
| 984 | if (--fs->users) |
| 985 | fs = NULL; |
| 986 | spin_unlock(&req->work.fs->lock); |
| 987 | if (fs) |
| 988 | free_fs_struct(fs); |
| 989 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 990 | } |
| 991 | |
Pavel Begunkov | deb6dc0 | 2020-02-24 11:30:17 +0300 | [diff] [blame] | 992 | static inline void io_prep_next_work(struct io_kiocb *req, |
| 993 | struct io_kiocb **link) |
| 994 | { |
| 995 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 996 | |
| 997 | if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file) |
| 998 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 999 | |
| 1000 | *link = io_prep_linked_timeout(req); |
| 1001 | } |
| 1002 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1003 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 1004 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1005 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1006 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1007 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1008 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1009 | if (req->flags & REQ_F_ISREG) { |
| 1010 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1011 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1012 | } else { |
| 1013 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1014 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1015 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1016 | |
| 1017 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1018 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1019 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1020 | return do_hashed; |
| 1021 | } |
| 1022 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1023 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1024 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1025 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1026 | struct io_kiocb *link; |
| 1027 | bool do_hashed; |
| 1028 | |
| 1029 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1030 | |
| 1031 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 1032 | req->flags); |
| 1033 | if (!do_hashed) { |
| 1034 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 1035 | } else { |
| 1036 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 1037 | file_inode(req->file)); |
| 1038 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1039 | |
| 1040 | if (link) |
| 1041 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1042 | } |
| 1043 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1044 | static void io_kill_timeout(struct io_kiocb *req) |
| 1045 | { |
| 1046 | int ret; |
| 1047 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1048 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1049 | if (ret != -1) { |
| 1050 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1051 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1052 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1053 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1058 | { |
| 1059 | struct io_kiocb *req, *tmp; |
| 1060 | |
| 1061 | spin_lock_irq(&ctx->completion_lock); |
| 1062 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1063 | io_kill_timeout(req); |
| 1064 | spin_unlock_irq(&ctx->completion_lock); |
| 1065 | } |
| 1066 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1067 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1068 | { |
| 1069 | struct io_kiocb *req; |
| 1070 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1071 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 1072 | io_kill_timeout(req); |
| 1073 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1074 | __io_commit_cqring(ctx); |
| 1075 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1076 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1077 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1078 | } |
| 1079 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1080 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1081 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1082 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1083 | unsigned tail; |
| 1084 | |
| 1085 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1086 | /* |
| 1087 | * writes to the cq entry need to come after reading head; the |
| 1088 | * control dependency is enough as we're using WRITE_ONCE to |
| 1089 | * fill the cq entry |
| 1090 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1091 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1092 | return NULL; |
| 1093 | |
| 1094 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1095 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1098 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1099 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1100 | if (!ctx->cq_ev_fd) |
| 1101 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1102 | if (!ctx->eventfd_async) |
| 1103 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1104 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1107 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1108 | { |
| 1109 | if (waitqueue_active(&ctx->wait)) |
| 1110 | wake_up(&ctx->wait); |
| 1111 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1112 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1113 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1114 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1115 | } |
| 1116 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1117 | /* Returns true if there are no backlogged entries after the flush */ |
| 1118 | 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] | 1119 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1120 | struct io_rings *rings = ctx->rings; |
| 1121 | struct io_uring_cqe *cqe; |
| 1122 | struct io_kiocb *req; |
| 1123 | unsigned long flags; |
| 1124 | LIST_HEAD(list); |
| 1125 | |
| 1126 | if (!force) { |
| 1127 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1128 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1129 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1130 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1131 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1135 | |
| 1136 | /* if force is set, the ring is going away. always drop after that */ |
| 1137 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1138 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1139 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1140 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1141 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1142 | cqe = io_get_cqring(ctx); |
| 1143 | if (!cqe && !force) |
| 1144 | break; |
| 1145 | |
| 1146 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1147 | list); |
| 1148 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1149 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1150 | if (cqe) { |
| 1151 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1152 | WRITE_ONCE(cqe->res, req->result); |
| 1153 | WRITE_ONCE(cqe->flags, 0); |
| 1154 | } else { |
| 1155 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1156 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1161 | if (cqe) { |
| 1162 | clear_bit(0, &ctx->sq_check_overflow); |
| 1163 | clear_bit(0, &ctx->cq_check_overflow); |
| 1164 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1165 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1166 | io_cqring_ev_posted(ctx); |
| 1167 | |
| 1168 | while (!list_empty(&list)) { |
| 1169 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1170 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1171 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1172 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1173 | |
| 1174 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1177 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1178 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1179 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1180 | struct io_uring_cqe *cqe; |
| 1181 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1182 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1183 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1184 | /* |
| 1185 | * If we can't get a cq entry, userspace overflowed the |
| 1186 | * submission (by quite a lot). Increment the overflow count in |
| 1187 | * the ring. |
| 1188 | */ |
| 1189 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1190 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1191 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1192 | WRITE_ONCE(cqe->res, res); |
| 1193 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1194 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1195 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1196 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1197 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1198 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1199 | set_bit(0, &ctx->sq_check_overflow); |
| 1200 | set_bit(0, &ctx->cq_check_overflow); |
| 1201 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1202 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1203 | refcount_inc(&req->refs); |
| 1204 | req->result = res; |
| 1205 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1209 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1210 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1211 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1212 | unsigned long flags; |
| 1213 | |
| 1214 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1215 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1216 | io_commit_cqring(ctx); |
| 1217 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1218 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1219 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1222 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1223 | { |
| 1224 | return req == (struct io_kiocb *) |
| 1225 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1226 | } |
| 1227 | |
| 1228 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1229 | { |
| 1230 | struct io_kiocb *req; |
| 1231 | |
| 1232 | req = ctx->fallback_req; |
| 1233 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1234 | return req; |
| 1235 | |
| 1236 | return NULL; |
| 1237 | } |
| 1238 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1239 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1240 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1241 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1242 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1243 | struct io_kiocb *req; |
| 1244 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1245 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1246 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1247 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1248 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1249 | } else if (!state->free_reqs) { |
| 1250 | size_t sz; |
| 1251 | int ret; |
| 1252 | |
| 1253 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1254 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1255 | |
| 1256 | /* |
| 1257 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1258 | * retry single alloc to be on the safe side. |
| 1259 | */ |
| 1260 | if (unlikely(ret <= 0)) { |
| 1261 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1262 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1263 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1264 | ret = 1; |
| 1265 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1266 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1267 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1268 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1269 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1270 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1273 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1274 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1275 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1276 | req->ctx = ctx; |
| 1277 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1278 | /* one is dropped after submission, the other at completion */ |
| 1279 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1280 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1281 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1282 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1283 | fallback: |
| 1284 | req = io_get_fallback_req(ctx); |
| 1285 | if (req) |
| 1286 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1287 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1288 | return NULL; |
| 1289 | } |
| 1290 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1291 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1292 | bool fixed) |
| 1293 | { |
| 1294 | if (fixed) |
| 1295 | percpu_ref_put(&req->ctx->file_data->refs); |
| 1296 | else |
| 1297 | fput(file); |
| 1298 | } |
| 1299 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1300 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1301 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1302 | if (likely(!io_is_fallback_req(req))) |
| 1303 | kmem_cache_free(req_cachep, req); |
| 1304 | else |
| 1305 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1306 | } |
| 1307 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1308 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1309 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1310 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1311 | io_cleanup_req(req); |
| 1312 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1313 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1314 | if (req->file) |
| 1315 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1316 | |
| 1317 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static void __io_free_req(struct io_kiocb *req) |
| 1321 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1322 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1323 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1324 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1325 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1326 | unsigned long flags; |
| 1327 | |
| 1328 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1329 | list_del(&req->inflight_entry); |
| 1330 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1331 | wake_up(&ctx->inflight_wait); |
| 1332 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1333 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1334 | |
| 1335 | percpu_ref_put(&req->ctx->refs); |
| 1336 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1337 | } |
| 1338 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1339 | struct req_batch { |
| 1340 | void *reqs[IO_IOPOLL_BATCH]; |
| 1341 | int to_free; |
| 1342 | int need_iter; |
| 1343 | }; |
| 1344 | |
| 1345 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1346 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1347 | int fixed_refs = rb->to_free; |
| 1348 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1349 | if (!rb->to_free) |
| 1350 | return; |
| 1351 | if (rb->need_iter) { |
| 1352 | int i, inflight = 0; |
| 1353 | unsigned long flags; |
| 1354 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1355 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1356 | for (i = 0; i < rb->to_free; i++) { |
| 1357 | struct io_kiocb *req = rb->reqs[i]; |
| 1358 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1359 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1360 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1361 | fixed_refs++; |
| 1362 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1363 | if (req->flags & REQ_F_INFLIGHT) |
| 1364 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1365 | __io_req_aux_free(req); |
| 1366 | } |
| 1367 | if (!inflight) |
| 1368 | goto do_free; |
| 1369 | |
| 1370 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1371 | for (i = 0; i < rb->to_free; i++) { |
| 1372 | struct io_kiocb *req = rb->reqs[i]; |
| 1373 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1374 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1375 | list_del(&req->inflight_entry); |
| 1376 | if (!--inflight) |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
| 1380 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1381 | |
| 1382 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1383 | wake_up(&ctx->inflight_wait); |
| 1384 | } |
| 1385 | do_free: |
| 1386 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1387 | if (fixed_refs) |
| 1388 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1389 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1390 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1391 | } |
| 1392 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1393 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1394 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1395 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1396 | int ret; |
| 1397 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1398 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1399 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1400 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1401 | io_commit_cqring(ctx); |
| 1402 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1403 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1404 | return true; |
| 1405 | } |
| 1406 | |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1410 | 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] | 1411 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1412 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1413 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1414 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1415 | /* Already got next link */ |
| 1416 | if (req->flags & REQ_F_LINK_NEXT) |
| 1417 | return; |
| 1418 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1419 | /* |
| 1420 | * The list should never be empty when we are called here. But could |
| 1421 | * potentially happen if the chain is messed up, check to be on the |
| 1422 | * safe side. |
| 1423 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1424 | while (!list_empty(&req->link_list)) { |
| 1425 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1426 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1427 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1428 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1429 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1430 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1431 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1432 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1433 | continue; |
| 1434 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1435 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1436 | list_del_init(&req->link_list); |
| 1437 | if (!list_empty(&nxt->link_list)) |
| 1438 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1439 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1440 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1441 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1442 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1443 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1444 | if (wake_ev) |
| 1445 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1450 | */ |
| 1451 | static void io_fail_links(struct io_kiocb *req) |
| 1452 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1453 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1454 | unsigned long flags; |
| 1455 | |
| 1456 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1457 | |
| 1458 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1459 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1460 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1461 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1462 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1463 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1464 | |
| 1465 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1466 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1467 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1468 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1469 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1470 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1471 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1472 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1473 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1474 | |
| 1475 | io_commit_cqring(ctx); |
| 1476 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1477 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1478 | } |
| 1479 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1480 | 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] | 1481 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1482 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1483 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1484 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1485 | /* |
| 1486 | * If LINK is set, we have dependent requests in this chain. If we |
| 1487 | * didn't fail this request, queue the first one up, moving any other |
| 1488 | * dependencies to the next request. In case of failure, fail the rest |
| 1489 | * of the chain. |
| 1490 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1491 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1492 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1493 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1494 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1495 | struct io_ring_ctx *ctx = req->ctx; |
| 1496 | unsigned long flags; |
| 1497 | |
| 1498 | /* |
| 1499 | * If this is a timeout link, we could be racing with the |
| 1500 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1501 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1502 | */ |
| 1503 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1504 | io_req_link_next(req, nxt); |
| 1505 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1506 | } else { |
| 1507 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1508 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1509 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1510 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1511 | static void io_free_req(struct io_kiocb *req) |
| 1512 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1513 | struct io_kiocb *nxt = NULL; |
| 1514 | |
| 1515 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1516 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1517 | |
| 1518 | if (nxt) |
| 1519 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1520 | } |
| 1521 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1522 | /* |
| 1523 | * Drop reference to request, return next in chain (if there is one) if this |
| 1524 | * was the last reference to this request. |
| 1525 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1526 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1527 | 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] | 1528 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1529 | if (refcount_dec_and_test(&req->refs)) { |
| 1530 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1531 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1532 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1535 | static void io_put_req(struct io_kiocb *req) |
| 1536 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1537 | if (refcount_dec_and_test(&req->refs)) |
| 1538 | io_free_req(req); |
| 1539 | } |
| 1540 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1541 | /* |
| 1542 | * Must only be used if we don't need to care about links, usually from |
| 1543 | * within the completion handling itself. |
| 1544 | */ |
| 1545 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1546 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1547 | /* drop both submit and complete references */ |
| 1548 | if (refcount_sub_and_test(2, &req->refs)) |
| 1549 | __io_free_req(req); |
| 1550 | } |
| 1551 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1552 | static void io_double_put_req(struct io_kiocb *req) |
| 1553 | { |
| 1554 | /* drop both submit and complete references */ |
| 1555 | if (refcount_sub_and_test(2, &req->refs)) |
| 1556 | io_free_req(req); |
| 1557 | } |
| 1558 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1559 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1560 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1561 | struct io_rings *rings = ctx->rings; |
| 1562 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1563 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1564 | /* |
| 1565 | * noflush == true is from the waitqueue handler, just ensure |
| 1566 | * we wake up the task, and the next invocation will flush the |
| 1567 | * entries. We cannot safely to it from here. |
| 1568 | */ |
| 1569 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1570 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1571 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1572 | io_cqring_overflow_flush(ctx, false); |
| 1573 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1574 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1575 | /* See comment at the top of this file */ |
| 1576 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1577 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1578 | } |
| 1579 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1580 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1581 | { |
| 1582 | struct io_rings *rings = ctx->rings; |
| 1583 | |
| 1584 | /* make sure SQ entry isn't read before tail */ |
| 1585 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1586 | } |
| 1587 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1588 | 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] | 1589 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1590 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1591 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1592 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1593 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1594 | rb->need_iter++; |
| 1595 | |
| 1596 | rb->reqs[rb->to_free++] = req; |
| 1597 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1598 | io_free_req_many(req->ctx, rb); |
| 1599 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1602 | /* |
| 1603 | * Find and free completed poll iocbs |
| 1604 | */ |
| 1605 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1606 | struct list_head *done) |
| 1607 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1608 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1609 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1610 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1611 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1612 | while (!list_empty(done)) { |
| 1613 | req = list_first_entry(done, struct io_kiocb, list); |
| 1614 | list_del(&req->list); |
| 1615 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1616 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1617 | (*nr_events)++; |
| 1618 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1619 | if (refcount_dec_and_test(&req->refs) && |
| 1620 | !io_req_multi_free(&rb, req)) |
| 1621 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1622 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1623 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1624 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1625 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1629 | long min) |
| 1630 | { |
| 1631 | struct io_kiocb *req, *tmp; |
| 1632 | LIST_HEAD(done); |
| 1633 | bool spin; |
| 1634 | int ret; |
| 1635 | |
| 1636 | /* |
| 1637 | * Only spin for completions if we don't have multiple devices hanging |
| 1638 | * off our complete list, and we're under the requested amount. |
| 1639 | */ |
| 1640 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1641 | |
| 1642 | ret = 0; |
| 1643 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1644 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1645 | |
| 1646 | /* |
| 1647 | * Move completed entries to our local list. If we find a |
| 1648 | * request that requires polling, break out and complete |
| 1649 | * the done list first, if we have entries there. |
| 1650 | */ |
| 1651 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1652 | list_move_tail(&req->list, &done); |
| 1653 | continue; |
| 1654 | } |
| 1655 | if (!list_empty(&done)) |
| 1656 | break; |
| 1657 | |
| 1658 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1659 | if (ret < 0) |
| 1660 | break; |
| 1661 | |
| 1662 | if (ret && spin) |
| 1663 | spin = false; |
| 1664 | ret = 0; |
| 1665 | } |
| 1666 | |
| 1667 | if (!list_empty(&done)) |
| 1668 | io_iopoll_complete(ctx, nr_events, &done); |
| 1669 | |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1674 | * 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] | 1675 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1676 | * as a non-spinning completion check. |
| 1677 | */ |
| 1678 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1679 | long min) |
| 1680 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1681 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1682 | int ret; |
| 1683 | |
| 1684 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1685 | if (ret < 0) |
| 1686 | return ret; |
| 1687 | if (!min || *nr_events >= min) |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | return 1; |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * We can't just wait for polled events to come to us, we have to actively |
| 1696 | * find and complete them. |
| 1697 | */ |
| 1698 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1699 | { |
| 1700 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1701 | return; |
| 1702 | |
| 1703 | mutex_lock(&ctx->uring_lock); |
| 1704 | while (!list_empty(&ctx->poll_list)) { |
| 1705 | unsigned int nr_events = 0; |
| 1706 | |
| 1707 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1711 | * in this case we need to ensure that we reap all events. |
| 1712 | */ |
| 1713 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1714 | } |
| 1715 | mutex_unlock(&ctx->uring_lock); |
| 1716 | } |
| 1717 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1718 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1719 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1720 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1721 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1722 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1723 | /* |
| 1724 | * We disallow the app entering submit/complete with polling, but we |
| 1725 | * still need to lock the ring to prevent racing with polled issue |
| 1726 | * that got punted to a workqueue. |
| 1727 | */ |
| 1728 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1729 | do { |
| 1730 | int tmin = 0; |
| 1731 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1732 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1733 | * Don't enter poll loop if we already have events pending. |
| 1734 | * If we do, we can potentially be spinning for commands that |
| 1735 | * already triggered a CQE (eg in error). |
| 1736 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1737 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1738 | break; |
| 1739 | |
| 1740 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1741 | * If a submit got punted to a workqueue, we can have the |
| 1742 | * application entering polling for a command before it gets |
| 1743 | * issued. That app will hold the uring_lock for the duration |
| 1744 | * of the poll right here, so we need to take a breather every |
| 1745 | * now and then to ensure that the issue has a chance to add |
| 1746 | * the poll to the issued list. Otherwise we can spin here |
| 1747 | * forever, while the workqueue is stuck trying to acquire the |
| 1748 | * very same mutex. |
| 1749 | */ |
| 1750 | if (!(++iters & 7)) { |
| 1751 | mutex_unlock(&ctx->uring_lock); |
| 1752 | mutex_lock(&ctx->uring_lock); |
| 1753 | } |
| 1754 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1755 | if (*nr_events < min) |
| 1756 | tmin = min - *nr_events; |
| 1757 | |
| 1758 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1759 | if (ret <= 0) |
| 1760 | break; |
| 1761 | ret = 0; |
| 1762 | } while (min && !*nr_events && !need_resched()); |
| 1763 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1764 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1765 | return ret; |
| 1766 | } |
| 1767 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1768 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1769 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1770 | /* |
| 1771 | * Tell lockdep we inherited freeze protection from submission |
| 1772 | * thread. |
| 1773 | */ |
| 1774 | if (req->flags & REQ_F_ISREG) { |
| 1775 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1776 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1777 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1778 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1779 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1782 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1783 | { |
| 1784 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1785 | req->flags |= REQ_F_FAIL_LINK; |
| 1786 | } |
| 1787 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1788 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1789 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1790 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1791 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1792 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1793 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1794 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1795 | if (res != req->result) |
| 1796 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1797 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1801 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1802 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1803 | |
| 1804 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1805 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1808 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1809 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1810 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1811 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1812 | |
| 1813 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1814 | io_put_req_find_next(req, &nxt); |
| 1815 | |
| 1816 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1819 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1820 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1821 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1822 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1823 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1824 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1825 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1826 | if (res != req->result) |
| 1827 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1828 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1829 | if (res != -EAGAIN) |
| 1830 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1835 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1836 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1837 | * accessing the kiocb cookie. |
| 1838 | */ |
| 1839 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1840 | { |
| 1841 | struct io_ring_ctx *ctx = req->ctx; |
| 1842 | |
| 1843 | /* |
| 1844 | * Track whether we have multiple files in our lists. This will impact |
| 1845 | * how we do polling eventually, not spinning if we're on potentially |
| 1846 | * different devices. |
| 1847 | */ |
| 1848 | if (list_empty(&ctx->poll_list)) { |
| 1849 | ctx->poll_multi_file = false; |
| 1850 | } else if (!ctx->poll_multi_file) { |
| 1851 | struct io_kiocb *list_req; |
| 1852 | |
| 1853 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1854 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1855 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1856 | ctx->poll_multi_file = true; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * For fast devices, IO may have already completed. If it has, add |
| 1861 | * it to the front so we find it first. |
| 1862 | */ |
| 1863 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1864 | list_add(&req->list, &ctx->poll_list); |
| 1865 | else |
| 1866 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 1867 | |
| 1868 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 1869 | wq_has_sleeper(&ctx->sqo_wait)) |
| 1870 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1873 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1874 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1875 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1876 | int diff = state->has_refs - state->used_refs; |
| 1877 | |
| 1878 | if (diff) |
| 1879 | fput_many(state->file, diff); |
| 1880 | state->file = NULL; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * Get as many references to a file as we have IOs left in this submission, |
| 1886 | * assuming most submissions are for one file, or at least that each file |
| 1887 | * has more than one submission. |
| 1888 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1889 | static struct file *__io_file_get(struct io_submit_state *state, int fd) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1890 | { |
| 1891 | if (!state) |
| 1892 | return fget(fd); |
| 1893 | |
| 1894 | if (state->file) { |
| 1895 | if (state->fd == fd) { |
| 1896 | state->used_refs++; |
| 1897 | state->ios_left--; |
| 1898 | return state->file; |
| 1899 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1900 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1901 | } |
| 1902 | state->file = fget_many(fd, state->ios_left); |
| 1903 | if (!state->file) |
| 1904 | return NULL; |
| 1905 | |
| 1906 | state->fd = fd; |
| 1907 | state->has_refs = state->ios_left; |
| 1908 | state->used_refs = 1; |
| 1909 | state->ios_left--; |
| 1910 | return state->file; |
| 1911 | } |
| 1912 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1913 | /* |
| 1914 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1915 | * any file. For now, just ensure that anything potentially problematic is done |
| 1916 | * inline. |
| 1917 | */ |
| 1918 | static bool io_file_supports_async(struct file *file) |
| 1919 | { |
| 1920 | umode_t mode = file_inode(file)->i_mode; |
| 1921 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1922 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1923 | return true; |
| 1924 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1925 | return true; |
| 1926 | |
| 1927 | return false; |
| 1928 | } |
| 1929 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1930 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1931 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1932 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1933 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1934 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1935 | unsigned ioprio; |
| 1936 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1937 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1938 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1939 | req->flags |= REQ_F_ISREG; |
| 1940 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1941 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1942 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1943 | req->flags |= REQ_F_CUR_POS; |
| 1944 | kiocb->ki_pos = req->file->f_pos; |
| 1945 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1946 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 1947 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1948 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1949 | if (unlikely(ret)) |
| 1950 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1951 | |
| 1952 | ioprio = READ_ONCE(sqe->ioprio); |
| 1953 | if (ioprio) { |
| 1954 | ret = ioprio_check_cap(ioprio); |
| 1955 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1956 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1957 | |
| 1958 | kiocb->ki_ioprio = ioprio; |
| 1959 | } else |
| 1960 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1961 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1962 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1963 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1964 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1965 | req->flags |= REQ_F_NOWAIT; |
| 1966 | |
| 1967 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1968 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1969 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1970 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1971 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1972 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1973 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1974 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1975 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1976 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1977 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1978 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1979 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1980 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1981 | kiocb->ki_complete = io_complete_rw; |
| 1982 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1983 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1984 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1985 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1986 | /* we own ->private, reuse it for the buffer index */ |
| 1987 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1988 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1989 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1993 | { |
| 1994 | switch (ret) { |
| 1995 | case -EIOCBQUEUED: |
| 1996 | break; |
| 1997 | case -ERESTARTSYS: |
| 1998 | case -ERESTARTNOINTR: |
| 1999 | case -ERESTARTNOHAND: |
| 2000 | case -ERESTART_RESTARTBLOCK: |
| 2001 | /* |
| 2002 | * We can't just restart the syscall, since previously |
| 2003 | * submitted sqes may already be in progress. Just fail this |
| 2004 | * IO with EINTR. |
| 2005 | */ |
| 2006 | ret = -EINTR; |
| 2007 | /* fall through */ |
| 2008 | default: |
| 2009 | kiocb->ki_complete(kiocb, ret, 0); |
| 2010 | } |
| 2011 | } |
| 2012 | |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2013 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2014 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2015 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2016 | |
| 2017 | if (req->flags & REQ_F_CUR_POS) |
| 2018 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2019 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2020 | *nxt = __io_complete_rw(kiocb, ret); |
| 2021 | else |
| 2022 | io_rw_done(kiocb, ret); |
| 2023 | } |
| 2024 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2025 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2026 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2027 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2028 | struct io_ring_ctx *ctx = req->ctx; |
| 2029 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2030 | struct io_mapped_ubuf *imu; |
| 2031 | unsigned index, buf_index; |
| 2032 | size_t offset; |
| 2033 | u64 buf_addr; |
| 2034 | |
| 2035 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2036 | if (unlikely(!ctx->user_bufs)) |
| 2037 | return -EFAULT; |
| 2038 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2039 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2040 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2041 | return -EFAULT; |
| 2042 | |
| 2043 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2044 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2045 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2046 | |
| 2047 | /* overflow */ |
| 2048 | if (buf_addr + len < buf_addr) |
| 2049 | return -EFAULT; |
| 2050 | /* not inside the mapped region */ |
| 2051 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2052 | return -EFAULT; |
| 2053 | |
| 2054 | /* |
| 2055 | * May not be a start of buffer, set size appropriately |
| 2056 | * and advance us to the beginning. |
| 2057 | */ |
| 2058 | offset = buf_addr - imu->ubuf; |
| 2059 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2060 | |
| 2061 | if (offset) { |
| 2062 | /* |
| 2063 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2064 | * using the latter parts of a big fixed buffer - it iterates |
| 2065 | * over each segment manually. We can cheat a bit here, because |
| 2066 | * we know that: |
| 2067 | * |
| 2068 | * 1) it's a BVEC iter, we set it up |
| 2069 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2070 | * first and last bvec |
| 2071 | * |
| 2072 | * So just find our index, and adjust the iterator afterwards. |
| 2073 | * If the offset is within the first bvec (or the whole first |
| 2074 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2075 | * since we can just skip the first segment, which may not |
| 2076 | * be PAGE_SIZE aligned. |
| 2077 | */ |
| 2078 | const struct bio_vec *bvec = imu->bvec; |
| 2079 | |
| 2080 | if (offset <= bvec->bv_len) { |
| 2081 | iov_iter_advance(iter, offset); |
| 2082 | } else { |
| 2083 | unsigned long seg_skip; |
| 2084 | |
| 2085 | /* skip first vec */ |
| 2086 | offset -= bvec->bv_len; |
| 2087 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2088 | |
| 2089 | iter->bvec = bvec + seg_skip; |
| 2090 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2091 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2092 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2096 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2099 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2100 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2101 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2102 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2103 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2104 | u8 opcode; |
| 2105 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2106 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2107 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2108 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2109 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2110 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2111 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2112 | /* buffer index only valid with fixed read/write */ |
| 2113 | if (req->rw.kiocb.private) |
| 2114 | return -EINVAL; |
| 2115 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2116 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2117 | ssize_t ret; |
| 2118 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2119 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2120 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2123 | if (req->io) { |
| 2124 | struct io_async_rw *iorw = &req->io->rw; |
| 2125 | |
| 2126 | *iovec = iorw->iov; |
| 2127 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2128 | if (iorw->iov == iorw->fast_iov) |
| 2129 | *iovec = NULL; |
| 2130 | return iorw->size; |
| 2131 | } |
| 2132 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2133 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2134 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2135 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2136 | iovec, iter); |
| 2137 | #endif |
| 2138 | |
| 2139 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2140 | } |
| 2141 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2142 | /* |
| 2143 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2144 | * by looping over ->read() or ->write() manually. |
| 2145 | */ |
| 2146 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2147 | struct iov_iter *iter) |
| 2148 | { |
| 2149 | ssize_t ret = 0; |
| 2150 | |
| 2151 | /* |
| 2152 | * Don't support polled IO through this interface, and we can't |
| 2153 | * support non-blocking either. For the latter, this just causes |
| 2154 | * the kiocb to be handled from an async context. |
| 2155 | */ |
| 2156 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2157 | return -EOPNOTSUPP; |
| 2158 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2159 | return -EAGAIN; |
| 2160 | |
| 2161 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2162 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2163 | ssize_t nr; |
| 2164 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2165 | if (!iov_iter_is_bvec(iter)) { |
| 2166 | iovec = iov_iter_iovec(iter); |
| 2167 | } else { |
| 2168 | /* fixed buffers import bvec */ |
| 2169 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2170 | + iter->iov_offset; |
| 2171 | iovec.iov_len = min(iter->count, |
| 2172 | iter->bvec->bv_len - iter->iov_offset); |
| 2173 | } |
| 2174 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2175 | if (rw == READ) { |
| 2176 | nr = file->f_op->read(file, iovec.iov_base, |
| 2177 | iovec.iov_len, &kiocb->ki_pos); |
| 2178 | } else { |
| 2179 | nr = file->f_op->write(file, iovec.iov_base, |
| 2180 | iovec.iov_len, &kiocb->ki_pos); |
| 2181 | } |
| 2182 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2183 | if (iov_iter_is_bvec(iter)) |
| 2184 | kunmap(iter->bvec->bv_page); |
| 2185 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2186 | if (nr < 0) { |
| 2187 | if (!ret) |
| 2188 | ret = nr; |
| 2189 | break; |
| 2190 | } |
| 2191 | ret += nr; |
| 2192 | if (nr != iovec.iov_len) |
| 2193 | break; |
| 2194 | iov_iter_advance(iter, nr); |
| 2195 | } |
| 2196 | |
| 2197 | return ret; |
| 2198 | } |
| 2199 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2200 | 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] | 2201 | struct iovec *iovec, struct iovec *fast_iov, |
| 2202 | struct iov_iter *iter) |
| 2203 | { |
| 2204 | req->io->rw.nr_segs = iter->nr_segs; |
| 2205 | req->io->rw.size = io_size; |
| 2206 | req->io->rw.iov = iovec; |
| 2207 | if (!req->io->rw.iov) { |
| 2208 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2209 | memcpy(req->io->rw.iov, fast_iov, |
| 2210 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2211 | } else { |
| 2212 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2216 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2217 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2218 | if (!io_op_defs[req->opcode].async_ctx) |
| 2219 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2220 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2221 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2224 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2225 | struct iovec *iovec, struct iovec *fast_iov, |
| 2226 | struct iov_iter *iter) |
| 2227 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2228 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2229 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2230 | if (!req->io) { |
| 2231 | if (io_alloc_async_ctx(req)) |
| 2232 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2233 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2234 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2235 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2236 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2239 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2240 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2241 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2242 | struct io_async_ctx *io; |
| 2243 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2244 | ssize_t ret; |
| 2245 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2246 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2247 | if (ret) |
| 2248 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2249 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2250 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2251 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2252 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2253 | /* either don't need iovec imported or already have it */ |
| 2254 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2255 | return 0; |
| 2256 | |
| 2257 | io = req->io; |
| 2258 | io->rw.iov = io->rw.fast_iov; |
| 2259 | req->io = NULL; |
| 2260 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2261 | req->io = io; |
| 2262 | if (ret < 0) |
| 2263 | return ret; |
| 2264 | |
| 2265 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2266 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2269 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2270 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2271 | { |
| 2272 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2273 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2274 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2275 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2276 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2277 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2278 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2279 | if (ret < 0) |
| 2280 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2281 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2282 | /* Ensure we clear previously set non-block flag */ |
| 2283 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2284 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2285 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2286 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2287 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2288 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2289 | req->result = io_size; |
| 2290 | |
| 2291 | /* |
| 2292 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2293 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2294 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2295 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2296 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2297 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2298 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2299 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2300 | if (!ret) { |
| 2301 | ssize_t ret2; |
| 2302 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2303 | if (req->file->f_op->read_iter) |
| 2304 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2305 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2306 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2307 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2308 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2309 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2310 | kiocb_done(kiocb, ret2, nxt); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2311 | } else { |
| 2312 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2313 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2314 | inline_vecs, &iter); |
| 2315 | if (ret) |
| 2316 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2317 | /* any defer here is final, must blocking retry */ |
| 2318 | if (!(req->flags & REQ_F_NOWAIT)) |
| 2319 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2320 | return -EAGAIN; |
| 2321 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2322 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2323 | out_free: |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2324 | kfree(iovec); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2325 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2326 | return ret; |
| 2327 | } |
| 2328 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2329 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2330 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2331 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2332 | struct io_async_ctx *io; |
| 2333 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2334 | ssize_t ret; |
| 2335 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2336 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2337 | if (ret) |
| 2338 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2339 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2340 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2341 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2342 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2343 | /* either don't need iovec imported or already have it */ |
| 2344 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2345 | return 0; |
| 2346 | |
| 2347 | io = req->io; |
| 2348 | io->rw.iov = io->rw.fast_iov; |
| 2349 | req->io = NULL; |
| 2350 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2351 | req->io = io; |
| 2352 | if (ret < 0) |
| 2353 | return ret; |
| 2354 | |
| 2355 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2356 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2359 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2360 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2361 | { |
| 2362 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2363 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2364 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2365 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2366 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2367 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2368 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2369 | if (ret < 0) |
| 2370 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2371 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2372 | /* Ensure we clear previously set non-block flag */ |
| 2373 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2374 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2375 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2376 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2377 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2378 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2379 | req->result = io_size; |
| 2380 | |
| 2381 | /* |
| 2382 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2383 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2384 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2385 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2386 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2387 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2388 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2389 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2390 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2391 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2392 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2393 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2394 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2395 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2396 | ssize_t ret2; |
| 2397 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2398 | /* |
| 2399 | * Open-code file_start_write here to grab freeze protection, |
| 2400 | * which will be released by another thread in |
| 2401 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2402 | * released so that it doesn't complain about the held lock when |
| 2403 | * we return to userspace. |
| 2404 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2405 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2406 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2407 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2408 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2409 | SB_FREEZE_WRITE); |
| 2410 | } |
| 2411 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2412 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2413 | if (req->file->f_op->write_iter) |
| 2414 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2415 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2416 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 2417 | /* |
| 2418 | * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 2419 | * retry them without IOCB_NOWAIT. |
| 2420 | */ |
| 2421 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 2422 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2423 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2424 | kiocb_done(kiocb, ret2, nxt); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2425 | } else { |
| 2426 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2427 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2428 | inline_vecs, &iter); |
| 2429 | if (ret) |
| 2430 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2431 | /* any defer here is final, must blocking retry */ |
| 2432 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2433 | return -EAGAIN; |
| 2434 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2435 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2436 | out_free: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2437 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2438 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2439 | return ret; |
| 2440 | } |
| 2441 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2442 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2443 | { |
| 2444 | struct io_splice* sp = &req->splice; |
| 2445 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 2446 | int ret; |
| 2447 | |
| 2448 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2449 | return 0; |
| 2450 | |
| 2451 | sp->file_in = NULL; |
| 2452 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 2453 | sp->off_out = READ_ONCE(sqe->off); |
| 2454 | sp->len = READ_ONCE(sqe->len); |
| 2455 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 2456 | |
| 2457 | if (unlikely(sp->flags & ~valid_flags)) |
| 2458 | return -EINVAL; |
| 2459 | |
| 2460 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 2461 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2462 | if (ret) |
| 2463 | return ret; |
| 2464 | req->flags |= REQ_F_NEED_CLEANUP; |
| 2465 | |
| 2466 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) |
| 2467 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 2468 | |
| 2469 | return 0; |
| 2470 | } |
| 2471 | |
| 2472 | static bool io_splice_punt(struct file *file) |
| 2473 | { |
| 2474 | if (get_pipe_info(file)) |
| 2475 | return false; |
| 2476 | if (!io_file_supports_async(file)) |
| 2477 | return true; |
| 2478 | return !(file->f_mode & O_NONBLOCK); |
| 2479 | } |
| 2480 | |
| 2481 | static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2482 | bool force_nonblock) |
| 2483 | { |
| 2484 | struct io_splice *sp = &req->splice; |
| 2485 | struct file *in = sp->file_in; |
| 2486 | struct file *out = sp->file_out; |
| 2487 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 2488 | loff_t *poff_in, *poff_out; |
| 2489 | long ret; |
| 2490 | |
| 2491 | if (force_nonblock) { |
| 2492 | if (io_splice_punt(in) || io_splice_punt(out)) |
| 2493 | return -EAGAIN; |
| 2494 | flags |= SPLICE_F_NONBLOCK; |
| 2495 | } |
| 2496 | |
| 2497 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 2498 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
| 2499 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
| 2500 | if (force_nonblock && ret == -EAGAIN) |
| 2501 | return -EAGAIN; |
| 2502 | |
| 2503 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2504 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 2505 | |
| 2506 | io_cqring_add_event(req, ret); |
| 2507 | if (ret != sp->len) |
| 2508 | req_set_fail_links(req); |
| 2509 | io_put_req_find_next(req, nxt); |
| 2510 | return 0; |
| 2511 | } |
| 2512 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2513 | /* |
| 2514 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2515 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2516 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2517 | { |
| 2518 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2519 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2520 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2521 | return -EINVAL; |
| 2522 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2523 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2524 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2525 | return 0; |
| 2526 | } |
| 2527 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2528 | 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] | 2529 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2530 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2531 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2532 | if (!req->file) |
| 2533 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2534 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2535 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2536 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2537 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2538 | return -EINVAL; |
| 2539 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2540 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2541 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2542 | return -EINVAL; |
| 2543 | |
| 2544 | req->sync.off = READ_ONCE(sqe->off); |
| 2545 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2546 | return 0; |
| 2547 | } |
| 2548 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2549 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2550 | { |
| 2551 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2552 | req_set_fail_links(req); |
| 2553 | io_cqring_add_event(req, -ECANCELED); |
| 2554 | io_put_req(req); |
| 2555 | return true; |
| 2556 | } |
| 2557 | |
| 2558 | return false; |
| 2559 | } |
| 2560 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2561 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2562 | { |
| 2563 | struct io_wq_work *work = *workptr; |
| 2564 | struct io_kiocb *link = work->data; |
| 2565 | |
| 2566 | io_queue_linked_timeout(link); |
Pavel Begunkov | 5eae861 | 2020-02-28 10:36:38 +0300 | [diff] [blame] | 2567 | io_wq_submit_work(workptr); |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2571 | { |
| 2572 | struct io_kiocb *link; |
| 2573 | |
Pavel Begunkov | deb6dc0 | 2020-02-24 11:30:17 +0300 | [diff] [blame] | 2574 | io_prep_next_work(nxt, &link); |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2575 | *workptr = &nxt->work; |
| 2576 | if (link) { |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2577 | nxt->work.func = io_link_work_cb; |
| 2578 | nxt->work.data = link; |
| 2579 | } |
| 2580 | } |
| 2581 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2582 | static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2583 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2584 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2585 | int ret; |
| 2586 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2587 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2588 | end > 0 ? end : LLONG_MAX, |
| 2589 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2590 | if (ret < 0) |
| 2591 | req_set_fail_links(req); |
| 2592 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2593 | io_put_req_find_next(req, nxt); |
| 2594 | } |
| 2595 | |
| 2596 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2597 | { |
| 2598 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2599 | struct io_kiocb *nxt = NULL; |
| 2600 | |
| 2601 | if (io_req_cancelled(req)) |
| 2602 | return; |
| 2603 | __io_fsync(req, &nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2604 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2605 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2608 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2609 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2610 | { |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2611 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2612 | if (force_nonblock) { |
| 2613 | io_put_req(req); |
| 2614 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2615 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2616 | } |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2617 | __io_fsync(req, nxt); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2618 | return 0; |
| 2619 | } |
| 2620 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2621 | static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2622 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2623 | int ret; |
| 2624 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 2625 | if (io_req_cancelled(req)) |
| 2626 | return; |
| 2627 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2628 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2629 | req->sync.len); |
| 2630 | if (ret < 0) |
| 2631 | req_set_fail_links(req); |
| 2632 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2633 | io_put_req_find_next(req, nxt); |
| 2634 | } |
| 2635 | |
| 2636 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2637 | { |
| 2638 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2639 | struct io_kiocb *nxt = NULL; |
| 2640 | |
| 2641 | __io_fallocate(req, &nxt); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2642 | if (nxt) |
| 2643 | io_wq_assign_next(workptr, nxt); |
| 2644 | } |
| 2645 | |
| 2646 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2647 | const struct io_uring_sqe *sqe) |
| 2648 | { |
| 2649 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2650 | return -EINVAL; |
| 2651 | |
| 2652 | req->sync.off = READ_ONCE(sqe->off); |
| 2653 | req->sync.len = READ_ONCE(sqe->addr); |
| 2654 | req->sync.mode = READ_ONCE(sqe->len); |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
| 2658 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2659 | bool force_nonblock) |
| 2660 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2661 | /* fallocate always requiring blocking context */ |
| 2662 | if (force_nonblock) { |
| 2663 | io_put_req(req); |
| 2664 | req->work.func = io_fallocate_finish; |
| 2665 | return -EAGAIN; |
| 2666 | } |
| 2667 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2668 | __io_fallocate(req, nxt); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2669 | return 0; |
| 2670 | } |
| 2671 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2672 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2673 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2674 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2675 | int ret; |
| 2676 | |
| 2677 | if (sqe->ioprio || sqe->buf_index) |
| 2678 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2679 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2680 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2681 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2682 | return 0; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2683 | |
| 2684 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2685 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2686 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2687 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2688 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2689 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2690 | if (IS_ERR(req->open.filename)) { |
| 2691 | ret = PTR_ERR(req->open.filename); |
| 2692 | req->open.filename = NULL; |
| 2693 | return ret; |
| 2694 | } |
| 2695 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2696 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2697 | return 0; |
| 2698 | } |
| 2699 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2700 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2701 | { |
| 2702 | struct open_how __user *how; |
| 2703 | const char __user *fname; |
| 2704 | size_t len; |
| 2705 | int ret; |
| 2706 | |
| 2707 | if (sqe->ioprio || sqe->buf_index) |
| 2708 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2709 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2710 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2711 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2712 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2713 | |
| 2714 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2715 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2716 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2717 | len = READ_ONCE(sqe->len); |
| 2718 | |
| 2719 | if (len < OPEN_HOW_SIZE_VER0) |
| 2720 | return -EINVAL; |
| 2721 | |
| 2722 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2723 | len); |
| 2724 | if (ret) |
| 2725 | return ret; |
| 2726 | |
| 2727 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2728 | req->open.how.flags |= O_LARGEFILE; |
| 2729 | |
| 2730 | req->open.filename = getname(fname); |
| 2731 | if (IS_ERR(req->open.filename)) { |
| 2732 | ret = PTR_ERR(req->open.filename); |
| 2733 | req->open.filename = NULL; |
| 2734 | return ret; |
| 2735 | } |
| 2736 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2737 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2738 | return 0; |
| 2739 | } |
| 2740 | |
| 2741 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2742 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2743 | { |
| 2744 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2745 | struct file *file; |
| 2746 | int ret; |
| 2747 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2748 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2749 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2750 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2751 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2752 | if (ret) |
| 2753 | goto err; |
| 2754 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2755 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2756 | if (ret < 0) |
| 2757 | goto err; |
| 2758 | |
| 2759 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2760 | if (IS_ERR(file)) { |
| 2761 | put_unused_fd(ret); |
| 2762 | ret = PTR_ERR(file); |
| 2763 | } else { |
| 2764 | fsnotify_open(file); |
| 2765 | fd_install(ret, file); |
| 2766 | } |
| 2767 | err: |
| 2768 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2769 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2770 | if (ret < 0) |
| 2771 | req_set_fail_links(req); |
| 2772 | io_cqring_add_event(req, ret); |
| 2773 | io_put_req_find_next(req, nxt); |
| 2774 | return 0; |
| 2775 | } |
| 2776 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2777 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2778 | bool force_nonblock) |
| 2779 | { |
| 2780 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2781 | return io_openat2(req, nxt, force_nonblock); |
| 2782 | } |
| 2783 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 2784 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 2785 | const struct io_uring_sqe *sqe) |
| 2786 | { |
| 2787 | #if defined(CONFIG_EPOLL) |
| 2788 | if (sqe->ioprio || sqe->buf_index) |
| 2789 | return -EINVAL; |
| 2790 | |
| 2791 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 2792 | req->epoll.op = READ_ONCE(sqe->len); |
| 2793 | req->epoll.fd = READ_ONCE(sqe->off); |
| 2794 | |
| 2795 | if (ep_op_has_event(req->epoll.op)) { |
| 2796 | struct epoll_event __user *ev; |
| 2797 | |
| 2798 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2799 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 2800 | return -EFAULT; |
| 2801 | } |
| 2802 | |
| 2803 | return 0; |
| 2804 | #else |
| 2805 | return -EOPNOTSUPP; |
| 2806 | #endif |
| 2807 | } |
| 2808 | |
| 2809 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2810 | bool force_nonblock) |
| 2811 | { |
| 2812 | #if defined(CONFIG_EPOLL) |
| 2813 | struct io_epoll *ie = &req->epoll; |
| 2814 | int ret; |
| 2815 | |
| 2816 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 2817 | if (force_nonblock && ret == -EAGAIN) |
| 2818 | return -EAGAIN; |
| 2819 | |
| 2820 | if (ret < 0) |
| 2821 | req_set_fail_links(req); |
| 2822 | io_cqring_add_event(req, ret); |
| 2823 | io_put_req_find_next(req, nxt); |
| 2824 | return 0; |
| 2825 | #else |
| 2826 | return -EOPNOTSUPP; |
| 2827 | #endif |
| 2828 | } |
| 2829 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2830 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2831 | { |
| 2832 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2833 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2834 | return -EINVAL; |
| 2835 | |
| 2836 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2837 | req->madvise.len = READ_ONCE(sqe->len); |
| 2838 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2839 | return 0; |
| 2840 | #else |
| 2841 | return -EOPNOTSUPP; |
| 2842 | #endif |
| 2843 | } |
| 2844 | |
| 2845 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2846 | bool force_nonblock) |
| 2847 | { |
| 2848 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2849 | struct io_madvise *ma = &req->madvise; |
| 2850 | int ret; |
| 2851 | |
| 2852 | if (force_nonblock) |
| 2853 | return -EAGAIN; |
| 2854 | |
| 2855 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2856 | if (ret < 0) |
| 2857 | req_set_fail_links(req); |
| 2858 | io_cqring_add_event(req, ret); |
| 2859 | io_put_req_find_next(req, nxt); |
| 2860 | return 0; |
| 2861 | #else |
| 2862 | return -EOPNOTSUPP; |
| 2863 | #endif |
| 2864 | } |
| 2865 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2866 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2867 | { |
| 2868 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2869 | return -EINVAL; |
| 2870 | |
| 2871 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2872 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2873 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
| 2877 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2878 | bool force_nonblock) |
| 2879 | { |
| 2880 | struct io_fadvise *fa = &req->fadvise; |
| 2881 | int ret; |
| 2882 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 2883 | if (force_nonblock) { |
| 2884 | switch (fa->advice) { |
| 2885 | case POSIX_FADV_NORMAL: |
| 2886 | case POSIX_FADV_RANDOM: |
| 2887 | case POSIX_FADV_SEQUENTIAL: |
| 2888 | break; |
| 2889 | default: |
| 2890 | return -EAGAIN; |
| 2891 | } |
| 2892 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2893 | |
| 2894 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2895 | if (ret < 0) |
| 2896 | req_set_fail_links(req); |
| 2897 | io_cqring_add_event(req, ret); |
| 2898 | io_put_req_find_next(req, nxt); |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2902 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2903 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2904 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2905 | unsigned lookup_flags; |
| 2906 | int ret; |
| 2907 | |
| 2908 | if (sqe->ioprio || sqe->buf_index) |
| 2909 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2910 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2911 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2912 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2913 | return 0; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2914 | |
| 2915 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2916 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2917 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2918 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2919 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2920 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2921 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2922 | return -EINVAL; |
| 2923 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2924 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2925 | if (IS_ERR(req->open.filename)) { |
| 2926 | ret = PTR_ERR(req->open.filename); |
| 2927 | req->open.filename = NULL; |
| 2928 | return ret; |
| 2929 | } |
| 2930 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2931 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2932 | return 0; |
| 2933 | } |
| 2934 | |
| 2935 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2936 | bool force_nonblock) |
| 2937 | { |
| 2938 | struct io_open *ctx = &req->open; |
| 2939 | unsigned lookup_flags; |
| 2940 | struct path path; |
| 2941 | struct kstat stat; |
| 2942 | int ret; |
| 2943 | |
| 2944 | if (force_nonblock) |
| 2945 | return -EAGAIN; |
| 2946 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2947 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2948 | return -EINVAL; |
| 2949 | |
| 2950 | retry: |
| 2951 | /* filename_lookup() drops it, keep a reference */ |
| 2952 | ctx->filename->refcnt++; |
| 2953 | |
| 2954 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2955 | NULL); |
| 2956 | if (ret) |
| 2957 | goto err; |
| 2958 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2959 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2960 | path_put(&path); |
| 2961 | if (retry_estale(ret, lookup_flags)) { |
| 2962 | lookup_flags |= LOOKUP_REVAL; |
| 2963 | goto retry; |
| 2964 | } |
| 2965 | if (!ret) |
| 2966 | ret = cp_statx(&stat, ctx->buffer); |
| 2967 | err: |
| 2968 | putname(ctx->filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2969 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2970 | if (ret < 0) |
| 2971 | req_set_fail_links(req); |
| 2972 | io_cqring_add_event(req, ret); |
| 2973 | io_put_req_find_next(req, nxt); |
| 2974 | return 0; |
| 2975 | } |
| 2976 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2977 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2978 | { |
| 2979 | /* |
| 2980 | * If we queue this for async, it must not be cancellable. That would |
| 2981 | * leave the 'file' in an undeterminate state. |
| 2982 | */ |
| 2983 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2984 | |
| 2985 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2986 | sqe->rw_flags || sqe->buf_index) |
| 2987 | return -EINVAL; |
| 2988 | if (sqe->flags & IOSQE_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2989 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2990 | |
| 2991 | req->close.fd = READ_ONCE(sqe->fd); |
| 2992 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2993 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2994 | return -EBADF; |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 2999 | /* only called when __close_fd_get_file() is done */ |
| 3000 | static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3001 | { |
| 3002 | int ret; |
| 3003 | |
| 3004 | ret = filp_close(req->close.put_file, req->work.files); |
| 3005 | if (ret < 0) |
| 3006 | req_set_fail_links(req); |
| 3007 | io_cqring_add_event(req, ret); |
| 3008 | fput(req->close.put_file); |
| 3009 | io_put_req_find_next(req, nxt); |
| 3010 | } |
| 3011 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3012 | static void io_close_finish(struct io_wq_work **workptr) |
| 3013 | { |
| 3014 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3015 | struct io_kiocb *nxt = NULL; |
| 3016 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 3017 | /* not cancellable, don't do io_req_cancelled() */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3018 | __io_close_finish(req, &nxt); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3019 | if (nxt) |
| 3020 | io_wq_assign_next(workptr, nxt); |
| 3021 | } |
| 3022 | |
| 3023 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3024 | bool force_nonblock) |
| 3025 | { |
| 3026 | int ret; |
| 3027 | |
| 3028 | req->close.put_file = NULL; |
| 3029 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 3030 | if (ret < 0) |
| 3031 | return ret; |
| 3032 | |
| 3033 | /* 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] | 3034 | 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] | 3035 | goto eagain; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3036 | |
| 3037 | /* |
| 3038 | * No ->flush(), safely close from here and just punt the |
| 3039 | * fput() to async context. |
| 3040 | */ |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3041 | __io_close_finish(req, nxt); |
| 3042 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3043 | eagain: |
| 3044 | req->work.func = io_close_finish; |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 3045 | /* |
| 3046 | * Do manual async queue here to avoid grabbing files - we don't |
| 3047 | * need the files, and it'll cause io_close_finish() to close |
| 3048 | * the file again and cause a double CQE entry for this request |
| 3049 | */ |
| 3050 | io_queue_async_work(req); |
| 3051 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3052 | } |
| 3053 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3054 | 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] | 3055 | { |
| 3056 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3057 | |
| 3058 | if (!req->file) |
| 3059 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3060 | |
| 3061 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3062 | return -EINVAL; |
| 3063 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3064 | return -EINVAL; |
| 3065 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3066 | req->sync.off = READ_ONCE(sqe->off); |
| 3067 | req->sync.len = READ_ONCE(sqe->len); |
| 3068 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3069 | return 0; |
| 3070 | } |
| 3071 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3072 | static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3073 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3074 | int ret; |
| 3075 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3076 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3077 | req->sync.flags); |
| 3078 | if (ret < 0) |
| 3079 | req_set_fail_links(req); |
| 3080 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3081 | io_put_req_find_next(req, nxt); |
| 3082 | } |
| 3083 | |
| 3084 | |
| 3085 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 3086 | { |
| 3087 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3088 | struct io_kiocb *nxt = NULL; |
| 3089 | |
| 3090 | if (io_req_cancelled(req)) |
| 3091 | return; |
| 3092 | __io_sync_file_range(req, &nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3093 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3094 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3095 | } |
| 3096 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3097 | 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] | 3098 | bool force_nonblock) |
| 3099 | { |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3100 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3101 | if (force_nonblock) { |
| 3102 | io_put_req(req); |
| 3103 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3104 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3105 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3106 | |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3107 | __io_sync_file_range(req, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3108 | return 0; |
| 3109 | } |
| 3110 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3111 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3112 | struct io_async_msghdr *kmsg) |
| 3113 | { |
| 3114 | if (req->io) |
| 3115 | return -EAGAIN; |
| 3116 | if (io_alloc_async_ctx(req)) { |
| 3117 | if (kmsg->iov != kmsg->fast_iov) |
| 3118 | kfree(kmsg->iov); |
| 3119 | return -ENOMEM; |
| 3120 | } |
| 3121 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3122 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3123 | return -EAGAIN; |
| 3124 | } |
| 3125 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3126 | 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] | 3127 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3128 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3129 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3130 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3131 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3132 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3133 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3134 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3135 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3136 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3137 | #ifdef CONFIG_COMPAT |
| 3138 | if (req->ctx->compat) |
| 3139 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3140 | #endif |
| 3141 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3142 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3143 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3144 | /* iovec is already imported */ |
| 3145 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3146 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3147 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3148 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3149 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3150 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3151 | if (!ret) |
| 3152 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3153 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3154 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3155 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3156 | #endif |
| 3157 | } |
| 3158 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3159 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3160 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3161 | { |
| 3162 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3163 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3164 | struct socket *sock; |
| 3165 | int ret; |
| 3166 | |
| 3167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3168 | return -EINVAL; |
| 3169 | |
| 3170 | sock = sock_from_file(req->file, &ret); |
| 3171 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3172 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3173 | unsigned flags; |
| 3174 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3175 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3176 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3177 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3178 | /* if iov is set, it's allocated already */ |
| 3179 | if (!kmsg->iov) |
| 3180 | kmsg->iov = kmsg->fast_iov; |
| 3181 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3182 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3183 | struct io_sr_msg *sr = &req->sr_msg; |
| 3184 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3185 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3186 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3187 | |
| 3188 | io.msg.iov = io.msg.fast_iov; |
| 3189 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3190 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3191 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3192 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3195 | flags = req->sr_msg.msg_flags; |
| 3196 | if (flags & MSG_DONTWAIT) |
| 3197 | req->flags |= REQ_F_NOWAIT; |
| 3198 | else if (force_nonblock) |
| 3199 | flags |= MSG_DONTWAIT; |
| 3200 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3201 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3202 | if (force_nonblock && ret == -EAGAIN) |
| 3203 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3204 | if (ret == -ERESTARTSYS) |
| 3205 | ret = -EINTR; |
| 3206 | } |
| 3207 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3208 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3209 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3210 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3211 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3212 | if (ret < 0) |
| 3213 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3214 | io_put_req_find_next(req, nxt); |
| 3215 | return 0; |
| 3216 | #else |
| 3217 | return -EOPNOTSUPP; |
| 3218 | #endif |
| 3219 | } |
| 3220 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3221 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3222 | bool force_nonblock) |
| 3223 | { |
| 3224 | #if defined(CONFIG_NET) |
| 3225 | struct socket *sock; |
| 3226 | int ret; |
| 3227 | |
| 3228 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3229 | return -EINVAL; |
| 3230 | |
| 3231 | sock = sock_from_file(req->file, &ret); |
| 3232 | if (sock) { |
| 3233 | struct io_sr_msg *sr = &req->sr_msg; |
| 3234 | struct msghdr msg; |
| 3235 | struct iovec iov; |
| 3236 | unsigned flags; |
| 3237 | |
| 3238 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3239 | &msg.msg_iter); |
| 3240 | if (ret) |
| 3241 | return ret; |
| 3242 | |
| 3243 | msg.msg_name = NULL; |
| 3244 | msg.msg_control = NULL; |
| 3245 | msg.msg_controllen = 0; |
| 3246 | msg.msg_namelen = 0; |
| 3247 | |
| 3248 | flags = req->sr_msg.msg_flags; |
| 3249 | if (flags & MSG_DONTWAIT) |
| 3250 | req->flags |= REQ_F_NOWAIT; |
| 3251 | else if (force_nonblock) |
| 3252 | flags |= MSG_DONTWAIT; |
| 3253 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3254 | msg.msg_flags = flags; |
| 3255 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3256 | if (force_nonblock && ret == -EAGAIN) |
| 3257 | return -EAGAIN; |
| 3258 | if (ret == -ERESTARTSYS) |
| 3259 | ret = -EINTR; |
| 3260 | } |
| 3261 | |
| 3262 | io_cqring_add_event(req, ret); |
| 3263 | if (ret < 0) |
| 3264 | req_set_fail_links(req); |
| 3265 | io_put_req_find_next(req, nxt); |
| 3266 | return 0; |
| 3267 | #else |
| 3268 | return -EOPNOTSUPP; |
| 3269 | #endif |
| 3270 | } |
| 3271 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3272 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3273 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3274 | { |
| 3275 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3276 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3277 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3278 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3279 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3280 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3281 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3282 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3283 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3284 | #ifdef CONFIG_COMPAT |
| 3285 | if (req->ctx->compat) |
| 3286 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3287 | #endif |
| 3288 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3289 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3290 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3291 | /* iovec is already imported */ |
| 3292 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3293 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3294 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3295 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3296 | ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3297 | &io->msg.uaddr, &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3298 | if (!ret) |
| 3299 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3300 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3301 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3302 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3303 | #endif |
| 3304 | } |
| 3305 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3306 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3307 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3308 | { |
| 3309 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3310 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3311 | struct socket *sock; |
| 3312 | int ret; |
| 3313 | |
| 3314 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3315 | return -EINVAL; |
| 3316 | |
| 3317 | sock = sock_from_file(req->file, &ret); |
| 3318 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3319 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3320 | unsigned flags; |
| 3321 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3322 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3323 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3324 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3325 | /* if iov is set, it's allocated already */ |
| 3326 | if (!kmsg->iov) |
| 3327 | kmsg->iov = kmsg->fast_iov; |
| 3328 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3329 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3330 | struct io_sr_msg *sr = &req->sr_msg; |
| 3331 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3332 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3333 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3334 | |
| 3335 | io.msg.iov = io.msg.fast_iov; |
| 3336 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3337 | sr->msg_flags, &io.msg.uaddr, |
| 3338 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3339 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3340 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3341 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3342 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3343 | flags = req->sr_msg.msg_flags; |
| 3344 | if (flags & MSG_DONTWAIT) |
| 3345 | req->flags |= REQ_F_NOWAIT; |
| 3346 | else if (force_nonblock) |
| 3347 | flags |= MSG_DONTWAIT; |
| 3348 | |
| 3349 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3350 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3351 | if (force_nonblock && ret == -EAGAIN) |
| 3352 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3353 | if (ret == -ERESTARTSYS) |
| 3354 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3355 | } |
| 3356 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3357 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3358 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3359 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3360 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3361 | if (ret < 0) |
| 3362 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3363 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3364 | return 0; |
| 3365 | #else |
| 3366 | return -EOPNOTSUPP; |
| 3367 | #endif |
| 3368 | } |
| 3369 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3370 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3371 | bool force_nonblock) |
| 3372 | { |
| 3373 | #if defined(CONFIG_NET) |
| 3374 | struct socket *sock; |
| 3375 | int ret; |
| 3376 | |
| 3377 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3378 | return -EINVAL; |
| 3379 | |
| 3380 | sock = sock_from_file(req->file, &ret); |
| 3381 | if (sock) { |
| 3382 | struct io_sr_msg *sr = &req->sr_msg; |
| 3383 | struct msghdr msg; |
| 3384 | struct iovec iov; |
| 3385 | unsigned flags; |
| 3386 | |
| 3387 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3388 | &msg.msg_iter); |
| 3389 | if (ret) |
| 3390 | return ret; |
| 3391 | |
| 3392 | msg.msg_name = NULL; |
| 3393 | msg.msg_control = NULL; |
| 3394 | msg.msg_controllen = 0; |
| 3395 | msg.msg_namelen = 0; |
| 3396 | msg.msg_iocb = NULL; |
| 3397 | msg.msg_flags = 0; |
| 3398 | |
| 3399 | flags = req->sr_msg.msg_flags; |
| 3400 | if (flags & MSG_DONTWAIT) |
| 3401 | req->flags |= REQ_F_NOWAIT; |
| 3402 | else if (force_nonblock) |
| 3403 | flags |= MSG_DONTWAIT; |
| 3404 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3405 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3406 | if (force_nonblock && ret == -EAGAIN) |
| 3407 | return -EAGAIN; |
| 3408 | if (ret == -ERESTARTSYS) |
| 3409 | ret = -EINTR; |
| 3410 | } |
| 3411 | |
| 3412 | io_cqring_add_event(req, ret); |
| 3413 | if (ret < 0) |
| 3414 | req_set_fail_links(req); |
| 3415 | io_put_req_find_next(req, nxt); |
| 3416 | return 0; |
| 3417 | #else |
| 3418 | return -EOPNOTSUPP; |
| 3419 | #endif |
| 3420 | } |
| 3421 | |
| 3422 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3423 | 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] | 3424 | { |
| 3425 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3426 | struct io_accept *accept = &req->accept; |
| 3427 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3428 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3429 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3430 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3431 | return -EINVAL; |
| 3432 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3433 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3434 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3435 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3436 | return 0; |
| 3437 | #else |
| 3438 | return -EOPNOTSUPP; |
| 3439 | #endif |
| 3440 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3441 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3442 | #if defined(CONFIG_NET) |
| 3443 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3444 | bool force_nonblock) |
| 3445 | { |
| 3446 | struct io_accept *accept = &req->accept; |
| 3447 | unsigned file_flags; |
| 3448 | int ret; |
| 3449 | |
| 3450 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3451 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3452 | accept->addr_len, accept->flags); |
| 3453 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3454 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3455 | if (ret == -ERESTARTSYS) |
| 3456 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3457 | if (ret < 0) |
| 3458 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3459 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3460 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3461 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3465 | { |
| 3466 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3467 | struct io_kiocb *nxt = NULL; |
| 3468 | |
Jens Axboe | e441d1c | 2020-02-20 09:59:02 -0700 | [diff] [blame] | 3469 | io_put_req(req); |
| 3470 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3471 | if (io_req_cancelled(req)) |
| 3472 | return; |
| 3473 | __io_accept(req, &nxt, false); |
| 3474 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3475 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3476 | } |
| 3477 | #endif |
| 3478 | |
| 3479 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3480 | bool force_nonblock) |
| 3481 | { |
| 3482 | #if defined(CONFIG_NET) |
| 3483 | int ret; |
| 3484 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3485 | ret = __io_accept(req, nxt, force_nonblock); |
| 3486 | if (ret == -EAGAIN && force_nonblock) { |
| 3487 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3488 | return -EAGAIN; |
| 3489 | } |
| 3490 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3491 | #else |
| 3492 | return -EOPNOTSUPP; |
| 3493 | #endif |
| 3494 | } |
| 3495 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3496 | 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] | 3497 | { |
| 3498 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3499 | struct io_connect *conn = &req->connect; |
| 3500 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3501 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3502 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3503 | return -EINVAL; |
| 3504 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3505 | return -EINVAL; |
| 3506 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3507 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3508 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3509 | |
| 3510 | if (!io) |
| 3511 | return 0; |
| 3512 | |
| 3513 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3514 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3515 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3516 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3517 | #endif |
| 3518 | } |
| 3519 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3520 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3521 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3522 | { |
| 3523 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3524 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3525 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3526 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3527 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3528 | if (req->io) { |
| 3529 | io = req->io; |
| 3530 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3531 | ret = move_addr_to_kernel(req->connect.addr, |
| 3532 | req->connect.addr_len, |
| 3533 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3534 | if (ret) |
| 3535 | goto out; |
| 3536 | io = &__io; |
| 3537 | } |
| 3538 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3539 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3540 | |
| 3541 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3542 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3543 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3544 | if (req->io) |
| 3545 | return -EAGAIN; |
| 3546 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3547 | ret = -ENOMEM; |
| 3548 | goto out; |
| 3549 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3550 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3551 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3552 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3553 | if (ret == -ERESTARTSYS) |
| 3554 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3555 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3556 | if (ret < 0) |
| 3557 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3558 | io_cqring_add_event(req, ret); |
| 3559 | io_put_req_find_next(req, nxt); |
| 3560 | return 0; |
| 3561 | #else |
| 3562 | return -EOPNOTSUPP; |
| 3563 | #endif |
| 3564 | } |
| 3565 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3566 | static bool io_poll_remove_one(struct io_kiocb *req) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3567 | { |
| 3568 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3569 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3570 | |
| 3571 | spin_lock(&poll->head->lock); |
| 3572 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3573 | if (!list_empty(&poll->wait.entry)) { |
| 3574 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3575 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3576 | } |
| 3577 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3578 | hash_del(&req->hash_node); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3579 | if (do_complete) { |
| 3580 | io_cqring_fill_event(req, -ECANCELED); |
| 3581 | io_commit_cqring(req->ctx); |
| 3582 | req->flags |= REQ_F_COMP_LOCKED; |
| 3583 | io_put_req(req); |
| 3584 | } |
| 3585 | |
| 3586 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3590 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3591 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3592 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3593 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3594 | |
| 3595 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3596 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3597 | struct hlist_head *list; |
| 3598 | |
| 3599 | list = &ctx->cancel_hash[i]; |
| 3600 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3601 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3602 | } |
| 3603 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3604 | |
| 3605 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3606 | } |
| 3607 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3608 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3609 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3610 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3611 | struct io_kiocb *req; |
| 3612 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3613 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3614 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3615 | if (sqe_addr != req->user_data) |
| 3616 | continue; |
| 3617 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3618 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3619 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | return -ENOENT; |
| 3623 | } |
| 3624 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3625 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3626 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3627 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3628 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3629 | return -EINVAL; |
| 3630 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3631 | sqe->poll_events) |
| 3632 | return -EINVAL; |
| 3633 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3634 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3635 | return 0; |
| 3636 | } |
| 3637 | |
| 3638 | /* |
| 3639 | * Find a running poll command that matches one specified in sqe->addr, |
| 3640 | * and remove it if found. |
| 3641 | */ |
| 3642 | static int io_poll_remove(struct io_kiocb *req) |
| 3643 | { |
| 3644 | struct io_ring_ctx *ctx = req->ctx; |
| 3645 | u64 addr; |
| 3646 | int ret; |
| 3647 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3648 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3649 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3650 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3651 | spin_unlock_irq(&ctx->completion_lock); |
| 3652 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3653 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3654 | if (ret < 0) |
| 3655 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3656 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3657 | return 0; |
| 3658 | } |
| 3659 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3660 | 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] | 3661 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3662 | struct io_ring_ctx *ctx = req->ctx; |
| 3663 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3664 | req->poll.done = true; |
Pavel Begunkov | b0a2034 | 2020-02-28 10:36:35 +0300 | [diff] [blame] | 3665 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3666 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3669 | static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3670 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3671 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3672 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3673 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3674 | hash_del(&req->hash_node); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3675 | io_poll_complete(req, req->result, 0); |
| 3676 | req->flags |= REQ_F_COMP_LOCKED; |
| 3677 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3678 | spin_unlock_irq(&ctx->completion_lock); |
| 3679 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3680 | io_cqring_ev_posted(ctx); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3681 | } |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3682 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3683 | static void io_poll_task_func(struct callback_head *cb) |
| 3684 | { |
| 3685 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 3686 | struct io_kiocb *nxt = NULL; |
| 3687 | |
| 3688 | io_poll_task_handler(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3689 | if (nxt) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3690 | __io_queue_sqe(nxt, NULL); |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 3691 | } |
| 3692 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3693 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3694 | void *key) |
| 3695 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 3696 | struct io_kiocb *req = wait->private; |
| 3697 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3698 | __poll_t mask = key_to_poll(key); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3699 | struct task_struct *tsk; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3700 | |
| 3701 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3702 | if (mask && !(mask & poll->events)) |
| 3703 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3704 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3705 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3706 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3707 | tsk = req->task; |
| 3708 | req->result = mask; |
| 3709 | init_task_work(&req->task_work, io_poll_task_func); |
| 3710 | task_work_add(tsk, &req->task_work, true); |
| 3711 | wake_up_process(tsk); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3712 | return 1; |
| 3713 | } |
| 3714 | |
| 3715 | struct io_poll_table { |
| 3716 | struct poll_table_struct pt; |
| 3717 | struct io_kiocb *req; |
| 3718 | int error; |
| 3719 | }; |
| 3720 | |
| 3721 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3722 | struct poll_table_struct *p) |
| 3723 | { |
| 3724 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3725 | |
| 3726 | if (unlikely(pt->req->poll.head)) { |
| 3727 | pt->error = -EINVAL; |
| 3728 | return; |
| 3729 | } |
| 3730 | |
| 3731 | pt->error = 0; |
| 3732 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3733 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3734 | } |
| 3735 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3736 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3737 | { |
| 3738 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3739 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3740 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3741 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3742 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3745 | 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] | 3746 | { |
| 3747 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3748 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3749 | |
| 3750 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3751 | return -EINVAL; |
| 3752 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3753 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3754 | if (!poll->file) |
| 3755 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3756 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3757 | events = READ_ONCE(sqe->poll_events); |
| 3758 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 3759 | |
| 3760 | /* task will wait for requests on exit, don't need a ref */ |
| 3761 | req->task = current; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3762 | return 0; |
| 3763 | } |
| 3764 | |
| 3765 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3766 | { |
| 3767 | struct io_poll_iocb *poll = &req->poll; |
| 3768 | struct io_ring_ctx *ctx = req->ctx; |
| 3769 | struct io_poll_table ipt; |
| 3770 | bool cancel = false; |
| 3771 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3772 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3773 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3774 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3775 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3776 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3777 | poll->canceled = false; |
| 3778 | |
| 3779 | ipt.pt._qproc = io_poll_queue_proc; |
| 3780 | ipt.pt._key = poll->events; |
| 3781 | ipt.req = req; |
| 3782 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3783 | |
| 3784 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3785 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3786 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 3787 | poll->wait.private = req; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3788 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3789 | INIT_LIST_HEAD(&req->list); |
| 3790 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3791 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3792 | |
| 3793 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3794 | if (likely(poll->head)) { |
| 3795 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3796 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3797 | if (ipt.error) |
| 3798 | cancel = true; |
| 3799 | ipt.error = 0; |
| 3800 | mask = 0; |
| 3801 | } |
| 3802 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3803 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3804 | else if (cancel) |
| 3805 | WRITE_ONCE(poll->canceled, true); |
| 3806 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3807 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3808 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3809 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3810 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3811 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3812 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3813 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3814 | spin_unlock_irq(&ctx->completion_lock); |
| 3815 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3816 | if (mask) { |
| 3817 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3818 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3819 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3820 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3821 | } |
| 3822 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3823 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3824 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3825 | struct io_timeout_data *data = container_of(timer, |
| 3826 | struct io_timeout_data, timer); |
| 3827 | struct io_kiocb *req = data->req; |
| 3828 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3829 | unsigned long flags; |
| 3830 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3831 | atomic_inc(&ctx->cq_timeouts); |
| 3832 | |
| 3833 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3834 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3835 | * We could be racing with timeout deletion. If the list is empty, |
| 3836 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3837 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3838 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3839 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3840 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3841 | /* |
| 3842 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3843 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3844 | * pointer will be increased, otherwise other timeout reqs may |
| 3845 | * return in advance without waiting for enough wait_nr. |
| 3846 | */ |
| 3847 | prev = req; |
| 3848 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3849 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3850 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3851 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3852 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3853 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3854 | io_commit_cqring(ctx); |
| 3855 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3856 | |
| 3857 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3858 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3859 | io_put_req(req); |
| 3860 | return HRTIMER_NORESTART; |
| 3861 | } |
| 3862 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3863 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3864 | { |
| 3865 | struct io_kiocb *req; |
| 3866 | int ret = -ENOENT; |
| 3867 | |
| 3868 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3869 | if (user_data == req->user_data) { |
| 3870 | list_del_init(&req->list); |
| 3871 | ret = 0; |
| 3872 | break; |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | if (ret == -ENOENT) |
| 3877 | return ret; |
| 3878 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3879 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3880 | if (ret == -1) |
| 3881 | return -EALREADY; |
| 3882 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3883 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3884 | io_cqring_fill_event(req, -ECANCELED); |
| 3885 | io_put_req(req); |
| 3886 | return 0; |
| 3887 | } |
| 3888 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3889 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3890 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3891 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3892 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3893 | return -EINVAL; |
| 3894 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3895 | return -EINVAL; |
| 3896 | |
| 3897 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3898 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3899 | if (req->timeout.flags) |
| 3900 | return -EINVAL; |
| 3901 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3902 | return 0; |
| 3903 | } |
| 3904 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3905 | /* |
| 3906 | * Remove or update an existing timeout command |
| 3907 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3908 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3909 | { |
| 3910 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3911 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3912 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3913 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3914 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3915 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3916 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3917 | io_commit_cqring(ctx); |
| 3918 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3919 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3920 | if (ret < 0) |
| 3921 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3922 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3923 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3924 | } |
| 3925 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3926 | 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] | 3927 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3928 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3929 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3930 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3931 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3932 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3933 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3934 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3935 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3936 | if (sqe->off && is_timeout_link) |
| 3937 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3938 | flags = READ_ONCE(sqe->timeout_flags); |
| 3939 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3940 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3941 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3942 | req->timeout.count = READ_ONCE(sqe->off); |
| 3943 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3944 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3945 | return -ENOMEM; |
| 3946 | |
| 3947 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3948 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3949 | req->flags |= REQ_F_TIMEOUT; |
| 3950 | |
| 3951 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3952 | return -EFAULT; |
| 3953 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3954 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3955 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3956 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3957 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3958 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3959 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3960 | return 0; |
| 3961 | } |
| 3962 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3963 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3964 | { |
| 3965 | unsigned count; |
| 3966 | struct io_ring_ctx *ctx = req->ctx; |
| 3967 | struct io_timeout_data *data; |
| 3968 | struct list_head *entry; |
| 3969 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3970 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3971 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3972 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3973 | /* |
| 3974 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3975 | * timeout event to be satisfied. If it isn't set, then this is |
| 3976 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3977 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3978 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3979 | if (!count) { |
| 3980 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3981 | spin_lock_irq(&ctx->completion_lock); |
| 3982 | entry = ctx->timeout_list.prev; |
| 3983 | goto add; |
| 3984 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3985 | |
| 3986 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3987 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3988 | |
| 3989 | /* |
| 3990 | * Insertion sort, ensuring the first entry in the list is always |
| 3991 | * the one we need first. |
| 3992 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3993 | spin_lock_irq(&ctx->completion_lock); |
| 3994 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3995 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3996 | unsigned nxt_sq_head; |
| 3997 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3998 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3999 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4000 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 4001 | continue; |
| 4002 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4003 | /* |
| 4004 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 4005 | * long to store it. |
| 4006 | */ |
| 4007 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 4008 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 4009 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4010 | |
| 4011 | /* |
| 4012 | * cached_sq_head may overflow, and it will never overflow twice |
| 4013 | * once there is some timeout req still be valid. |
| 4014 | */ |
| 4015 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 4016 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4017 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4018 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4019 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4020 | |
| 4021 | /* |
| 4022 | * Sequence of reqs after the insert one and itself should |
| 4023 | * be adjusted because each timeout req consumes a slot. |
| 4024 | */ |
| 4025 | span++; |
| 4026 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4027 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4028 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4029 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4030 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4031 | data->timer.function = io_timeout_fn; |
| 4032 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4033 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4034 | return 0; |
| 4035 | } |
| 4036 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4037 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4038 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4039 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4040 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4041 | return req->user_data == (unsigned long) data; |
| 4042 | } |
| 4043 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4044 | 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] | 4045 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4046 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4047 | int ret = 0; |
| 4048 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4049 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 4050 | switch (cancel_ret) { |
| 4051 | case IO_WQ_CANCEL_OK: |
| 4052 | ret = 0; |
| 4053 | break; |
| 4054 | case IO_WQ_CANCEL_RUNNING: |
| 4055 | ret = -EALREADY; |
| 4056 | break; |
| 4057 | case IO_WQ_CANCEL_NOTFOUND: |
| 4058 | ret = -ENOENT; |
| 4059 | break; |
| 4060 | } |
| 4061 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4062 | return ret; |
| 4063 | } |
| 4064 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4065 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 4066 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4067 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4068 | { |
| 4069 | unsigned long flags; |
| 4070 | int ret; |
| 4071 | |
| 4072 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 4073 | if (ret != -ENOENT) { |
| 4074 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4075 | goto done; |
| 4076 | } |
| 4077 | |
| 4078 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4079 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4080 | if (ret != -ENOENT) |
| 4081 | goto done; |
| 4082 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4083 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4084 | if (!ret) |
| 4085 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4086 | io_cqring_fill_event(req, ret); |
| 4087 | io_commit_cqring(ctx); |
| 4088 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4089 | io_cqring_ev_posted(ctx); |
| 4090 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4091 | if (ret < 0) |
| 4092 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4093 | io_put_req_find_next(req, nxt); |
| 4094 | } |
| 4095 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4096 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4097 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4098 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4099 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4100 | return -EINVAL; |
| 4101 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4102 | sqe->cancel_flags) |
| 4103 | return -EINVAL; |
| 4104 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4105 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4106 | return 0; |
| 4107 | } |
| 4108 | |
| 4109 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 4110 | { |
| 4111 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4112 | |
| 4113 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4114 | return 0; |
| 4115 | } |
| 4116 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4117 | static int io_files_update_prep(struct io_kiocb *req, |
| 4118 | const struct io_uring_sqe *sqe) |
| 4119 | { |
| 4120 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4121 | return -EINVAL; |
| 4122 | |
| 4123 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4124 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4125 | if (!req->files_update.nr_args) |
| 4126 | return -EINVAL; |
| 4127 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4128 | return 0; |
| 4129 | } |
| 4130 | |
| 4131 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4132 | { |
| 4133 | struct io_ring_ctx *ctx = req->ctx; |
| 4134 | struct io_uring_files_update up; |
| 4135 | int ret; |
| 4136 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4137 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4138 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4139 | |
| 4140 | up.offset = req->files_update.offset; |
| 4141 | up.fds = req->files_update.arg; |
| 4142 | |
| 4143 | mutex_lock(&ctx->uring_lock); |
| 4144 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4145 | mutex_unlock(&ctx->uring_lock); |
| 4146 | |
| 4147 | if (ret < 0) |
| 4148 | req_set_fail_links(req); |
| 4149 | io_cqring_add_event(req, ret); |
| 4150 | io_put_req(req); |
| 4151 | return 0; |
| 4152 | } |
| 4153 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4154 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4155 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4156 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4157 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4158 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4159 | if (io_op_defs[req->opcode].file_table) { |
| 4160 | ret = io_grab_files(req); |
| 4161 | if (unlikely(ret)) |
| 4162 | return ret; |
| 4163 | } |
| 4164 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4165 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4166 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4167 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4168 | case IORING_OP_NOP: |
| 4169 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4170 | case IORING_OP_READV: |
| 4171 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4172 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4173 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4174 | break; |
| 4175 | case IORING_OP_WRITEV: |
| 4176 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4177 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4178 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4179 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4180 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4181 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4182 | break; |
| 4183 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4184 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4185 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4186 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4187 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4188 | break; |
| 4189 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4190 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4191 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4192 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4193 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4194 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4195 | break; |
| 4196 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4197 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4198 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4199 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4200 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4201 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4202 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4203 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4204 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4205 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4206 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4207 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4208 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4209 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4210 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4211 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4212 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4213 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4214 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4215 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4216 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4217 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4218 | case IORING_OP_FALLOCATE: |
| 4219 | ret = io_fallocate_prep(req, sqe); |
| 4220 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4221 | case IORING_OP_OPENAT: |
| 4222 | ret = io_openat_prep(req, sqe); |
| 4223 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4224 | case IORING_OP_CLOSE: |
| 4225 | ret = io_close_prep(req, sqe); |
| 4226 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4227 | case IORING_OP_FILES_UPDATE: |
| 4228 | ret = io_files_update_prep(req, sqe); |
| 4229 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4230 | case IORING_OP_STATX: |
| 4231 | ret = io_statx_prep(req, sqe); |
| 4232 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4233 | case IORING_OP_FADVISE: |
| 4234 | ret = io_fadvise_prep(req, sqe); |
| 4235 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4236 | case IORING_OP_MADVISE: |
| 4237 | ret = io_madvise_prep(req, sqe); |
| 4238 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4239 | case IORING_OP_OPENAT2: |
| 4240 | ret = io_openat2_prep(req, sqe); |
| 4241 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4242 | case IORING_OP_EPOLL_CTL: |
| 4243 | ret = io_epoll_ctl_prep(req, sqe); |
| 4244 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4245 | case IORING_OP_SPLICE: |
| 4246 | ret = io_splice_prep(req, sqe); |
| 4247 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4248 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4249 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4250 | req->opcode); |
| 4251 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4252 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4255 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4258 | 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] | 4259 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4260 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4261 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4262 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4263 | /* Still need defer if there is pending req in defer list. */ |
| 4264 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4265 | return 0; |
| 4266 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4267 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4268 | return -EAGAIN; |
| 4269 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4270 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4271 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4272 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4273 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4274 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4275 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4276 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4277 | return 0; |
| 4278 | } |
| 4279 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4280 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4281 | list_add_tail(&req->list, &ctx->defer_list); |
| 4282 | spin_unlock_irq(&ctx->completion_lock); |
| 4283 | return -EIOCBQUEUED; |
| 4284 | } |
| 4285 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4286 | static void io_cleanup_req(struct io_kiocb *req) |
| 4287 | { |
| 4288 | struct io_async_ctx *io = req->io; |
| 4289 | |
| 4290 | switch (req->opcode) { |
| 4291 | case IORING_OP_READV: |
| 4292 | case IORING_OP_READ_FIXED: |
| 4293 | case IORING_OP_READ: |
| 4294 | case IORING_OP_WRITEV: |
| 4295 | case IORING_OP_WRITE_FIXED: |
| 4296 | case IORING_OP_WRITE: |
| 4297 | if (io->rw.iov != io->rw.fast_iov) |
| 4298 | kfree(io->rw.iov); |
| 4299 | break; |
| 4300 | case IORING_OP_SENDMSG: |
| 4301 | case IORING_OP_RECVMSG: |
| 4302 | if (io->msg.iov != io->msg.fast_iov) |
| 4303 | kfree(io->msg.iov); |
| 4304 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4305 | case IORING_OP_OPENAT: |
| 4306 | case IORING_OP_OPENAT2: |
| 4307 | case IORING_OP_STATX: |
| 4308 | putname(req->open.filename); |
| 4309 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4310 | case IORING_OP_SPLICE: |
| 4311 | io_put_file(req, req->splice.file_in, |
| 4312 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 4313 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4317 | } |
| 4318 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4319 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4320 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4321 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4322 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4323 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4324 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4325 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4326 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4327 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4328 | break; |
| 4329 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4330 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4331 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4332 | if (sqe) { |
| 4333 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4334 | if (ret < 0) |
| 4335 | break; |
| 4336 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4337 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4338 | break; |
| 4339 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4340 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4341 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4342 | if (sqe) { |
| 4343 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4344 | if (ret < 0) |
| 4345 | break; |
| 4346 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4347 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4348 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4349 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4350 | if (sqe) { |
| 4351 | ret = io_prep_fsync(req, sqe); |
| 4352 | if (ret < 0) |
| 4353 | break; |
| 4354 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4355 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4356 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4357 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4358 | if (sqe) { |
| 4359 | ret = io_poll_add_prep(req, sqe); |
| 4360 | if (ret) |
| 4361 | break; |
| 4362 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4363 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4364 | break; |
| 4365 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4366 | if (sqe) { |
| 4367 | ret = io_poll_remove_prep(req, sqe); |
| 4368 | if (ret < 0) |
| 4369 | break; |
| 4370 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4371 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4372 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4373 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4374 | if (sqe) { |
| 4375 | ret = io_prep_sfr(req, sqe); |
| 4376 | if (ret < 0) |
| 4377 | break; |
| 4378 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4379 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4380 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4381 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4382 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4383 | if (sqe) { |
| 4384 | ret = io_sendmsg_prep(req, sqe); |
| 4385 | if (ret < 0) |
| 4386 | break; |
| 4387 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4388 | if (req->opcode == IORING_OP_SENDMSG) |
| 4389 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4390 | else |
| 4391 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4392 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4393 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4394 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4395 | if (sqe) { |
| 4396 | ret = io_recvmsg_prep(req, sqe); |
| 4397 | if (ret) |
| 4398 | break; |
| 4399 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4400 | if (req->opcode == IORING_OP_RECVMSG) |
| 4401 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4402 | else |
| 4403 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4404 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4405 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4406 | if (sqe) { |
| 4407 | ret = io_timeout_prep(req, sqe, false); |
| 4408 | if (ret) |
| 4409 | break; |
| 4410 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4411 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4412 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4413 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4414 | if (sqe) { |
| 4415 | ret = io_timeout_remove_prep(req, sqe); |
| 4416 | if (ret) |
| 4417 | break; |
| 4418 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4419 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4420 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4421 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4422 | if (sqe) { |
| 4423 | ret = io_accept_prep(req, sqe); |
| 4424 | if (ret) |
| 4425 | break; |
| 4426 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4427 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4428 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4429 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4430 | if (sqe) { |
| 4431 | ret = io_connect_prep(req, sqe); |
| 4432 | if (ret) |
| 4433 | break; |
| 4434 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4435 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4436 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4437 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4438 | if (sqe) { |
| 4439 | ret = io_async_cancel_prep(req, sqe); |
| 4440 | if (ret) |
| 4441 | break; |
| 4442 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4443 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4444 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4445 | case IORING_OP_FALLOCATE: |
| 4446 | if (sqe) { |
| 4447 | ret = io_fallocate_prep(req, sqe); |
| 4448 | if (ret) |
| 4449 | break; |
| 4450 | } |
| 4451 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4452 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4453 | case IORING_OP_OPENAT: |
| 4454 | if (sqe) { |
| 4455 | ret = io_openat_prep(req, sqe); |
| 4456 | if (ret) |
| 4457 | break; |
| 4458 | } |
| 4459 | ret = io_openat(req, nxt, force_nonblock); |
| 4460 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4461 | case IORING_OP_CLOSE: |
| 4462 | if (sqe) { |
| 4463 | ret = io_close_prep(req, sqe); |
| 4464 | if (ret) |
| 4465 | break; |
| 4466 | } |
| 4467 | ret = io_close(req, nxt, force_nonblock); |
| 4468 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4469 | case IORING_OP_FILES_UPDATE: |
| 4470 | if (sqe) { |
| 4471 | ret = io_files_update_prep(req, sqe); |
| 4472 | if (ret) |
| 4473 | break; |
| 4474 | } |
| 4475 | ret = io_files_update(req, force_nonblock); |
| 4476 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4477 | case IORING_OP_STATX: |
| 4478 | if (sqe) { |
| 4479 | ret = io_statx_prep(req, sqe); |
| 4480 | if (ret) |
| 4481 | break; |
| 4482 | } |
| 4483 | ret = io_statx(req, nxt, force_nonblock); |
| 4484 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4485 | case IORING_OP_FADVISE: |
| 4486 | if (sqe) { |
| 4487 | ret = io_fadvise_prep(req, sqe); |
| 4488 | if (ret) |
| 4489 | break; |
| 4490 | } |
| 4491 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4492 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4493 | case IORING_OP_MADVISE: |
| 4494 | if (sqe) { |
| 4495 | ret = io_madvise_prep(req, sqe); |
| 4496 | if (ret) |
| 4497 | break; |
| 4498 | } |
| 4499 | ret = io_madvise(req, nxt, force_nonblock); |
| 4500 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4501 | case IORING_OP_OPENAT2: |
| 4502 | if (sqe) { |
| 4503 | ret = io_openat2_prep(req, sqe); |
| 4504 | if (ret) |
| 4505 | break; |
| 4506 | } |
| 4507 | ret = io_openat2(req, nxt, force_nonblock); |
| 4508 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4509 | case IORING_OP_EPOLL_CTL: |
| 4510 | if (sqe) { |
| 4511 | ret = io_epoll_ctl_prep(req, sqe); |
| 4512 | if (ret) |
| 4513 | break; |
| 4514 | } |
| 4515 | ret = io_epoll_ctl(req, nxt, force_nonblock); |
| 4516 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4517 | case IORING_OP_SPLICE: |
| 4518 | if (sqe) { |
| 4519 | ret = io_splice_prep(req, sqe); |
| 4520 | if (ret < 0) |
| 4521 | break; |
| 4522 | } |
| 4523 | ret = io_splice(req, nxt, force_nonblock); |
| 4524 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4525 | default: |
| 4526 | ret = -EINVAL; |
| 4527 | break; |
| 4528 | } |
| 4529 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4530 | if (ret) |
| 4531 | return ret; |
| 4532 | |
| 4533 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4534 | const bool in_async = io_wq_current_is_worker(); |
| 4535 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4536 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4537 | return -EAGAIN; |
| 4538 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4539 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4540 | if (in_async) |
| 4541 | mutex_lock(&ctx->uring_lock); |
| 4542 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4543 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4544 | |
| 4545 | if (in_async) |
| 4546 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4550 | } |
| 4551 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4552 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4553 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4554 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4555 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4556 | struct io_kiocb *nxt = NULL; |
| 4557 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4558 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4559 | /* if NO_CANCEL is set, we must still run the work */ |
| 4560 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4561 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4562 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4563 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4564 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4565 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4566 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4567 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4568 | /* |
| 4569 | * We can get EAGAIN for polled IO even though we're |
| 4570 | * forcing a sync submission from here, since we can't |
| 4571 | * wait for request slots on the block side. |
| 4572 | */ |
| 4573 | if (ret != -EAGAIN) |
| 4574 | break; |
| 4575 | cond_resched(); |
| 4576 | } while (1); |
| 4577 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4578 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4579 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4580 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4581 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4582 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4583 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4584 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4585 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4586 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4587 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4588 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4589 | if (!ret && nxt) |
| 4590 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4591 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4592 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4593 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4594 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4595 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4596 | return 0; |
Jens Axboe | 0b5faf6 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 4597 | 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] | 4598 | return 0; |
| 4599 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4600 | } |
| 4601 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4602 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4603 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4604 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4605 | struct fixed_file_table *table; |
| 4606 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4607 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4608 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4609 | } |
| 4610 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4611 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 4612 | int fd, struct file **out_file, bool fixed) |
| 4613 | { |
| 4614 | struct io_ring_ctx *ctx = req->ctx; |
| 4615 | struct file *file; |
| 4616 | |
| 4617 | if (fixed) { |
| 4618 | if (unlikely(!ctx->file_data || |
| 4619 | (unsigned) fd >= ctx->nr_user_files)) |
| 4620 | return -EBADF; |
| 4621 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 4622 | file = io_file_from_index(ctx, fd); |
| 4623 | if (!file) |
| 4624 | return -EBADF; |
| 4625 | percpu_ref_get(&ctx->file_data->refs); |
| 4626 | } else { |
| 4627 | trace_io_uring_file_get(ctx, fd); |
| 4628 | file = __io_file_get(state, fd); |
| 4629 | if (unlikely(!file)) |
| 4630 | return -EBADF; |
| 4631 | } |
| 4632 | |
| 4633 | *out_file = file; |
| 4634 | return 0; |
| 4635 | } |
| 4636 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4637 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4638 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4639 | { |
| 4640 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4641 | int fd; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4642 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4643 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4644 | flags = READ_ONCE(sqe->flags); |
| 4645 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4646 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4647 | if (!io_req_needs_file(req, fd)) |
| 4648 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4649 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4650 | fixed = (flags & IOSQE_FIXED_FILE); |
| 4651 | if (unlikely(!fixed && req->needs_fixed_file)) |
| 4652 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4653 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4654 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4655 | } |
| 4656 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4657 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4658 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4659 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4660 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4661 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4662 | if (req->work.files) |
| 4663 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4664 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4665 | return -EBADF; |
| 4666 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4667 | rcu_read_lock(); |
| 4668 | spin_lock_irq(&ctx->inflight_lock); |
| 4669 | /* |
| 4670 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4671 | * out work accessing these files if the fd is closed. Check if |
| 4672 | * the fd has changed since we started down this path, and disallow |
| 4673 | * this operation if it has. |
| 4674 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4675 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4676 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4677 | req->flags |= REQ_F_INFLIGHT; |
| 4678 | req->work.files = current->files; |
| 4679 | ret = 0; |
| 4680 | } |
| 4681 | spin_unlock_irq(&ctx->inflight_lock); |
| 4682 | rcu_read_unlock(); |
| 4683 | |
| 4684 | return ret; |
| 4685 | } |
| 4686 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4687 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4688 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4689 | struct io_timeout_data *data = container_of(timer, |
| 4690 | struct io_timeout_data, timer); |
| 4691 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4692 | struct io_ring_ctx *ctx = req->ctx; |
| 4693 | struct io_kiocb *prev = NULL; |
| 4694 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4695 | |
| 4696 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4697 | |
| 4698 | /* |
| 4699 | * We don't expect the list to be empty, that will only happen if we |
| 4700 | * race with the completion of the linked work. |
| 4701 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4702 | if (!list_empty(&req->link_list)) { |
| 4703 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4704 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4705 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4706 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4707 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4708 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4709 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4713 | |
| 4714 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4715 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4716 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4717 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4718 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4719 | } else { |
| 4720 | io_cqring_add_event(req, -ETIME); |
| 4721 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4722 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4723 | return HRTIMER_NORESTART; |
| 4724 | } |
| 4725 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4726 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4727 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4728 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4729 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4730 | /* |
| 4731 | * If the list is now empty, then our linked request finished before |
| 4732 | * we got a chance to setup the timer |
| 4733 | */ |
| 4734 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4735 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4736 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4737 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4738 | data->timer.function = io_link_timeout_fn; |
| 4739 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4740 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4741 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4742 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4743 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4744 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4745 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4748 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4749 | { |
| 4750 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4751 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4752 | if (!(req->flags & REQ_F_LINK)) |
| 4753 | return NULL; |
| 4754 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4755 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4756 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4757 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4758 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4759 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4760 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4761 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4762 | } |
| 4763 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4764 | 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] | 4765 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4766 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4767 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4768 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4769 | int ret; |
| 4770 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4771 | again: |
| 4772 | linked_timeout = io_prep_linked_timeout(req); |
| 4773 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4774 | if (req->work.creds && req->work.creds != current_cred()) { |
| 4775 | if (old_creds) |
| 4776 | revert_creds(old_creds); |
| 4777 | if (old_creds == req->work.creds) |
| 4778 | old_creds = NULL; /* restored original creds */ |
| 4779 | else |
| 4780 | old_creds = override_creds(req->work.creds); |
| 4781 | } |
| 4782 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4783 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4784 | |
| 4785 | /* |
| 4786 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4787 | * doesn't support non-blocking read/write attempts |
| 4788 | */ |
| 4789 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4790 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4791 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4792 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4793 | ret = io_grab_files(req); |
| 4794 | if (ret) |
| 4795 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4796 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4797 | |
| 4798 | /* |
| 4799 | * Queued up for async execution, worker will release |
| 4800 | * submit reference when the iocb is actually submitted. |
| 4801 | */ |
| 4802 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4803 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4804 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4805 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4806 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4807 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 4808 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4809 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4810 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4811 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4812 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4813 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4814 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4815 | } |
| 4816 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4817 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4818 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4819 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4820 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4821 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4822 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4823 | done_req: |
| 4824 | if (nxt) { |
| 4825 | req = nxt; |
| 4826 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4827 | |
| 4828 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4829 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4830 | goto again; |
| 4831 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4832 | if (old_creds) |
| 4833 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4834 | } |
| 4835 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4836 | 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] | 4837 | { |
| 4838 | int ret; |
| 4839 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4840 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4841 | if (ret) { |
| 4842 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4843 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4844 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4845 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4846 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4847 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4848 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4849 | ret = io_req_defer_prep(req, sqe); |
| 4850 | if (unlikely(ret < 0)) |
| 4851 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4852 | /* |
| 4853 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4854 | * to async execution. |
| 4855 | */ |
| 4856 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4857 | io_queue_async_work(req); |
| 4858 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4859 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4860 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4861 | } |
| 4862 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4863 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4864 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4865 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4866 | io_cqring_add_event(req, -ECANCELED); |
| 4867 | io_double_put_req(req); |
| 4868 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4869 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4870 | } |
| 4871 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4872 | #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] | 4873 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4874 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4875 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4876 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4877 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4878 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4879 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4880 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4881 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4882 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4883 | |
| 4884 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4885 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4886 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4887 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4888 | } |
| 4889 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4890 | id = READ_ONCE(sqe->personality); |
| 4891 | if (id) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4892 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 4893 | if (unlikely(!req->work.creds)) { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4894 | ret = -EINVAL; |
| 4895 | goto err_req; |
| 4896 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 4897 | get_cred(req->work.creds); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 4898 | } |
| 4899 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4900 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 4901 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK | |
| 4902 | IOSQE_ASYNC | IOSQE_FIXED_FILE); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4903 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4904 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4905 | if (unlikely(ret)) { |
| 4906 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4907 | io_cqring_add_event(req, ret); |
| 4908 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4909 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4910 | } |
| 4911 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4912 | /* |
| 4913 | * If we already have a head request, queue this one for async |
| 4914 | * submittal once the head completes. If we don't have a head but |
| 4915 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4916 | * submitted sync once the chain is complete. If none of those |
| 4917 | * conditions are true (normal request), then just queue it. |
| 4918 | */ |
| 4919 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4920 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4921 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 4922 | /* |
| 4923 | * Taking sequential execution of a link, draining both sides |
| 4924 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 4925 | * requests in the link. So, it drains the head and the |
| 4926 | * next after the link request. The last one is done via |
| 4927 | * drain_next flag to persist the effect across calls. |
| 4928 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4929 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4930 | head->flags |= REQ_F_IO_DRAIN; |
| 4931 | ctx->drain_next = 1; |
| 4932 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4933 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4934 | ret = -EAGAIN; |
| 4935 | goto err_req; |
| 4936 | } |
| 4937 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4938 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4939 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4940 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4941 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4942 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4943 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4944 | trace_io_uring_link(ctx, req, head); |
| 4945 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4946 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4947 | /* last request of a link, enqueue the link */ |
| 4948 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4949 | io_queue_link_head(head); |
| 4950 | *link = NULL; |
| 4951 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4952 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4953 | if (unlikely(ctx->drain_next)) { |
| 4954 | req->flags |= REQ_F_IO_DRAIN; |
| 4955 | req->ctx->drain_next = 0; |
| 4956 | } |
| 4957 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4958 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4959 | INIT_LIST_HEAD(&req->link_list); |
| 4960 | ret = io_req_defer_prep(req, sqe); |
| 4961 | if (ret) |
| 4962 | req->flags |= REQ_F_FAIL_LINK; |
| 4963 | *link = req; |
| 4964 | } else { |
| 4965 | io_queue_sqe(req, sqe); |
| 4966 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4967 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4968 | |
| 4969 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4970 | } |
| 4971 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4972 | /* |
| 4973 | * Batched submission is done, ensure local IO is flushed out. |
| 4974 | */ |
| 4975 | static void io_submit_state_end(struct io_submit_state *state) |
| 4976 | { |
| 4977 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4978 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4979 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 4980 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | /* |
| 4984 | * Start submission side cache. |
| 4985 | */ |
| 4986 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4987 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4988 | { |
| 4989 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4990 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4991 | state->file = NULL; |
| 4992 | state->ios_left = max_ios; |
| 4993 | } |
| 4994 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4995 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4996 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4997 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4998 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4999 | /* |
| 5000 | * Ensure any loads from the SQEs are done at this point, |
| 5001 | * since once we write the new head, the application could |
| 5002 | * write new data to them. |
| 5003 | */ |
| 5004 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5008 | * 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] | 5009 | * that is mapped by userspace. This means that care needs to be taken to |
| 5010 | * ensure that reads are stable, as we cannot rely on userspace always |
| 5011 | * being a good citizen. If members of the sqe are validated and then later |
| 5012 | * used, it's important that those reads are done through READ_ONCE() to |
| 5013 | * prevent a re-load down the line. |
| 5014 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5015 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 5016 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5017 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5018 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5019 | unsigned head; |
| 5020 | |
| 5021 | /* |
| 5022 | * The cached sq head (or cq tail) serves two purposes: |
| 5023 | * |
| 5024 | * 1) allows us to batch the cost of updating the user visible |
| 5025 | * head updates. |
| 5026 | * 2) allows the kernel side to track the head on its own, even |
| 5027 | * though the application is the one updating it. |
| 5028 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5029 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 5030 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5031 | /* |
| 5032 | * All io need record the previous position, if LINK vs DARIN, |
| 5033 | * it can be used to mark the position of the first IO in the |
| 5034 | * link list. |
| 5035 | */ |
| 5036 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5037 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 5038 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 5039 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5040 | ctx->cached_sq_head++; |
| 5041 | return true; |
| 5042 | } |
| 5043 | |
| 5044 | /* drop invalid entries */ |
| 5045 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 5046 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5047 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5048 | return false; |
| 5049 | } |
| 5050 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5051 | 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] | 5052 | struct file *ring_file, int ring_fd, |
| 5053 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5054 | { |
| 5055 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5056 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5057 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5058 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5059 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 5060 | /* 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] | 5061 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 5062 | if (!list_empty(&ctx->cq_overflow_list) && |
| 5063 | !io_cqring_overflow_flush(ctx, false)) |
| 5064 | return -EBUSY; |
| 5065 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5066 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5067 | /* make sure SQ entry isn't read before tail */ |
| 5068 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 5069 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5070 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 5071 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5072 | |
| 5073 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5074 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5075 | statep = &state; |
| 5076 | } |
| 5077 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5078 | ctx->ring_fd = ring_fd; |
| 5079 | ctx->ring_file = ring_file; |
| 5080 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5081 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5082 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5083 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5084 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5085 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5086 | req = io_get_req(ctx, statep); |
| 5087 | if (unlikely(!req)) { |
| 5088 | if (!submitted) |
| 5089 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5090 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5091 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5092 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5093 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5094 | break; |
| 5095 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5096 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5097 | /* will complete beyond this point, count as submitted */ |
| 5098 | submitted++; |
| 5099 | |
| 5100 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5101 | err = -EINVAL; |
| 5102 | fail_req: |
| 5103 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5104 | io_double_put_req(req); |
| 5105 | break; |
| 5106 | } |
| 5107 | |
| 5108 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5109 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5110 | if (unlikely(mm_fault)) { |
| 5111 | err = -EFAULT; |
| 5112 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5113 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5114 | use_mm(ctx->sqo_mm); |
| 5115 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5116 | } |
| 5117 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5118 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 5119 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 5120 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5121 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5122 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5123 | } |
| 5124 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 5125 | if (unlikely(submitted != nr)) { |
| 5126 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 5127 | |
| 5128 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 5129 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5130 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5131 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5132 | if (statep) |
| 5133 | io_submit_state_end(&state); |
| 5134 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5135 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5136 | io_commit_sqring(ctx); |
| 5137 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5138 | return submitted; |
| 5139 | } |
| 5140 | |
| 5141 | static int io_sq_thread(void *data) |
| 5142 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5143 | struct io_ring_ctx *ctx = data; |
| 5144 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5145 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5146 | mm_segment_t old_fs; |
| 5147 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5148 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5149 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5150 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5151 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5152 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5153 | old_fs = get_fs(); |
| 5154 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5155 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5156 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5157 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5158 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5159 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5160 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5161 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5162 | unsigned nr_events = 0; |
| 5163 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5164 | mutex_lock(&ctx->uring_lock); |
| 5165 | if (!list_empty(&ctx->poll_list)) |
| 5166 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 5167 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5168 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5169 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5172 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5173 | |
| 5174 | /* |
| 5175 | * If submit got -EBUSY, flag us as needing the application |
| 5176 | * to enter the kernel to reap and flush events. |
| 5177 | */ |
| 5178 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5179 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 5180 | * Drop cur_mm before scheduling, we can't hold it for |
| 5181 | * long periods (or over schedule()). Do this before |
| 5182 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5183 | * may sleep. |
| 5184 | */ |
| 5185 | if (cur_mm) { |
| 5186 | unuse_mm(cur_mm); |
| 5187 | mmput(cur_mm); |
| 5188 | cur_mm = NULL; |
| 5189 | } |
| 5190 | |
| 5191 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5192 | * We're polling. If we're within the defined idle |
| 5193 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5194 | * to sleep. The exception is if we got EBUSY doing |
| 5195 | * more IO, we should wait for the application to |
| 5196 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5197 | */ |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5198 | if (!list_empty(&ctx->poll_list) || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5199 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5200 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5201 | if (current->task_works) |
| 5202 | task_work_run(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5203 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5204 | continue; |
| 5205 | } |
| 5206 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5207 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5208 | TASK_INTERRUPTIBLE); |
| 5209 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5210 | /* |
| 5211 | * While doing polled IO, before going to sleep, we need |
| 5212 | * to check if there are new reqs added to poll_list, it |
| 5213 | * is because reqs may have been punted to io worker and |
| 5214 | * will be added to poll_list later, hence check the |
| 5215 | * poll_list again. |
| 5216 | */ |
| 5217 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 5218 | !list_empty_careful(&ctx->poll_list)) { |
| 5219 | finish_wait(&ctx->sqo_wait, &wait); |
| 5220 | continue; |
| 5221 | } |
| 5222 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5223 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5224 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5225 | /* make sure to read SQ tail after writing flags */ |
| 5226 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5227 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5228 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5229 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5230 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5231 | finish_wait(&ctx->sqo_wait, &wait); |
| 5232 | break; |
| 5233 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5234 | if (current->task_works) { |
| 5235 | task_work_run(); |
| 5236 | continue; |
| 5237 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5238 | if (signal_pending(current)) |
| 5239 | flush_signals(current); |
| 5240 | schedule(); |
| 5241 | finish_wait(&ctx->sqo_wait, &wait); |
| 5242 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5243 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5244 | continue; |
| 5245 | } |
| 5246 | finish_wait(&ctx->sqo_wait, &wait); |
| 5247 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5248 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5251 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5252 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5253 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5254 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5255 | } |
| 5256 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5257 | if (current->task_works) |
| 5258 | task_work_run(); |
| 5259 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5260 | set_fs(old_fs); |
| 5261 | if (cur_mm) { |
| 5262 | unuse_mm(cur_mm); |
| 5263 | mmput(cur_mm); |
| 5264 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5265 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5266 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5267 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5268 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5269 | return 0; |
| 5270 | } |
| 5271 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5272 | struct io_wait_queue { |
| 5273 | struct wait_queue_entry wq; |
| 5274 | struct io_ring_ctx *ctx; |
| 5275 | unsigned to_wait; |
| 5276 | unsigned nr_timeouts; |
| 5277 | }; |
| 5278 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5279 | 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] | 5280 | { |
| 5281 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5282 | |
| 5283 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5284 | * 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] | 5285 | * started waiting. For timeouts, we always want to return to userspace, |
| 5286 | * regardless of event count. |
| 5287 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5288 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5289 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5290 | } |
| 5291 | |
| 5292 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5293 | int wake_flags, void *key) |
| 5294 | { |
| 5295 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5296 | wq); |
| 5297 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5298 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5299 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5300 | return -1; |
| 5301 | |
| 5302 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5303 | } |
| 5304 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5305 | /* |
| 5306 | * Wait until events become available, if we don't already have some. The |
| 5307 | * application must reap them itself, as they reside on the shared cq ring. |
| 5308 | */ |
| 5309 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5310 | const sigset_t __user *sig, size_t sigsz) |
| 5311 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5312 | struct io_wait_queue iowq = { |
| 5313 | .wq = { |
| 5314 | .private = current, |
| 5315 | .func = io_wake_function, |
| 5316 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5317 | }, |
| 5318 | .ctx = ctx, |
| 5319 | .to_wait = min_events, |
| 5320 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5321 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5322 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5323 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5324 | do { |
| 5325 | if (io_cqring_events(ctx, false) >= min_events) |
| 5326 | return 0; |
| 5327 | if (!current->task_works) |
| 5328 | break; |
| 5329 | task_work_run(); |
| 5330 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5331 | |
| 5332 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5333 | #ifdef CONFIG_COMPAT |
| 5334 | if (in_compat_syscall()) |
| 5335 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5336 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5337 | else |
| 5338 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5339 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5340 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5341 | if (ret) |
| 5342 | return ret; |
| 5343 | } |
| 5344 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5345 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5346 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5347 | do { |
| 5348 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5349 | TASK_INTERRUPTIBLE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5350 | if (current->task_works) |
| 5351 | task_work_run(); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5352 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5353 | break; |
| 5354 | schedule(); |
| 5355 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5356 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5357 | break; |
| 5358 | } |
| 5359 | } while (1); |
| 5360 | finish_wait(&ctx->wait, &iowq.wq); |
| 5361 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5362 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5363 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5364 | 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] | 5365 | } |
| 5366 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5367 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5368 | { |
| 5369 | #if defined(CONFIG_UNIX) |
| 5370 | if (ctx->ring_sock) { |
| 5371 | struct sock *sock = ctx->ring_sock->sk; |
| 5372 | struct sk_buff *skb; |
| 5373 | |
| 5374 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5375 | kfree_skb(skb); |
| 5376 | } |
| 5377 | #else |
| 5378 | int i; |
| 5379 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5380 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5381 | struct file *file; |
| 5382 | |
| 5383 | file = io_file_from_index(ctx, i); |
| 5384 | if (file) |
| 5385 | fput(file); |
| 5386 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5387 | #endif |
| 5388 | } |
| 5389 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5390 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5391 | { |
| 5392 | struct fixed_file_data *data; |
| 5393 | |
| 5394 | data = container_of(ref, struct fixed_file_data, refs); |
| 5395 | complete(&data->done); |
| 5396 | } |
| 5397 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5398 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5399 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5400 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5401 | unsigned nr_tables, i; |
| 5402 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5403 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5404 | return -ENXIO; |
| 5405 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5406 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5407 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5408 | wait_for_completion(&data->done); |
| 5409 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5410 | percpu_ref_exit(&data->refs); |
| 5411 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5412 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5413 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5414 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5415 | kfree(data->table[i].files); |
| 5416 | kfree(data->table); |
| 5417 | kfree(data); |
| 5418 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5419 | ctx->nr_user_files = 0; |
| 5420 | return 0; |
| 5421 | } |
| 5422 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5423 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5424 | { |
| 5425 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5426 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5427 | /* |
| 5428 | * The park is a bit of a work-around, without it we get |
| 5429 | * warning spews on shutdown with SQPOLL set and affinity |
| 5430 | * set to a single CPU. |
| 5431 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5432 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5433 | kthread_stop(ctx->sqo_thread); |
| 5434 | ctx->sqo_thread = NULL; |
| 5435 | } |
| 5436 | } |
| 5437 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5438 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5439 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5440 | io_sq_thread_stop(ctx); |
| 5441 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5442 | if (ctx->io_wq) { |
| 5443 | io_wq_destroy(ctx->io_wq); |
| 5444 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5449 | /* |
| 5450 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5451 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5452 | * loops in the file referencing. |
| 5453 | */ |
| 5454 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5455 | { |
| 5456 | struct sock *sk = ctx->ring_sock->sk; |
| 5457 | struct scm_fp_list *fpl; |
| 5458 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5459 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5460 | |
| 5461 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5462 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5463 | |
| 5464 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5465 | return -EMFILE; |
| 5466 | } |
| 5467 | |
| 5468 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5469 | if (!fpl) |
| 5470 | return -ENOMEM; |
| 5471 | |
| 5472 | skb = alloc_skb(0, GFP_KERNEL); |
| 5473 | if (!skb) { |
| 5474 | kfree(fpl); |
| 5475 | return -ENOMEM; |
| 5476 | } |
| 5477 | |
| 5478 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5479 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5480 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5481 | fpl->user = get_uid(ctx->user); |
| 5482 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5483 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5484 | |
| 5485 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5486 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5487 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5488 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5489 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5490 | } |
| 5491 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5492 | if (nr_files) { |
| 5493 | fpl->max = SCM_MAX_FD; |
| 5494 | fpl->count = nr_files; |
| 5495 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5496 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5497 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5498 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5499 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5500 | for (i = 0; i < nr_files; i++) |
| 5501 | fput(fpl->fp[i]); |
| 5502 | } else { |
| 5503 | kfree_skb(skb); |
| 5504 | kfree(fpl); |
| 5505 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5506 | |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
| 5510 | /* |
| 5511 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5512 | * causes regular reference counting to break down. We rely on the UNIX |
| 5513 | * garbage collection to take care of this problem for us. |
| 5514 | */ |
| 5515 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5516 | { |
| 5517 | unsigned left, total; |
| 5518 | int ret = 0; |
| 5519 | |
| 5520 | total = 0; |
| 5521 | left = ctx->nr_user_files; |
| 5522 | while (left) { |
| 5523 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5524 | |
| 5525 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5526 | if (ret) |
| 5527 | break; |
| 5528 | left -= this_files; |
| 5529 | total += this_files; |
| 5530 | } |
| 5531 | |
| 5532 | if (!ret) |
| 5533 | return 0; |
| 5534 | |
| 5535 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5536 | struct file *file = io_file_from_index(ctx, total); |
| 5537 | |
| 5538 | if (file) |
| 5539 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5540 | total++; |
| 5541 | } |
| 5542 | |
| 5543 | return ret; |
| 5544 | } |
| 5545 | #else |
| 5546 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5547 | { |
| 5548 | return 0; |
| 5549 | } |
| 5550 | #endif |
| 5551 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5552 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5553 | unsigned nr_files) |
| 5554 | { |
| 5555 | int i; |
| 5556 | |
| 5557 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5558 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5559 | unsigned this_files; |
| 5560 | |
| 5561 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5562 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5563 | GFP_KERNEL); |
| 5564 | if (!table->files) |
| 5565 | break; |
| 5566 | nr_files -= this_files; |
| 5567 | } |
| 5568 | |
| 5569 | if (i == nr_tables) |
| 5570 | return 0; |
| 5571 | |
| 5572 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5573 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5574 | kfree(table->files); |
| 5575 | } |
| 5576 | return 1; |
| 5577 | } |
| 5578 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5579 | 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] | 5580 | { |
| 5581 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5582 | struct sock *sock = ctx->ring_sock->sk; |
| 5583 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5584 | struct sk_buff *skb; |
| 5585 | int i; |
| 5586 | |
| 5587 | __skb_queue_head_init(&list); |
| 5588 | |
| 5589 | /* |
| 5590 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5591 | * remove this entry and rearrange the file array. |
| 5592 | */ |
| 5593 | skb = skb_dequeue(head); |
| 5594 | while (skb) { |
| 5595 | struct scm_fp_list *fp; |
| 5596 | |
| 5597 | fp = UNIXCB(skb).fp; |
| 5598 | for (i = 0; i < fp->count; i++) { |
| 5599 | int left; |
| 5600 | |
| 5601 | if (fp->fp[i] != file) |
| 5602 | continue; |
| 5603 | |
| 5604 | unix_notinflight(fp->user, fp->fp[i]); |
| 5605 | left = fp->count - 1 - i; |
| 5606 | if (left) { |
| 5607 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5608 | left * sizeof(struct file *)); |
| 5609 | } |
| 5610 | fp->count--; |
| 5611 | if (!fp->count) { |
| 5612 | kfree_skb(skb); |
| 5613 | skb = NULL; |
| 5614 | } else { |
| 5615 | __skb_queue_tail(&list, skb); |
| 5616 | } |
| 5617 | fput(file); |
| 5618 | file = NULL; |
| 5619 | break; |
| 5620 | } |
| 5621 | |
| 5622 | if (!file) |
| 5623 | break; |
| 5624 | |
| 5625 | __skb_queue_tail(&list, skb); |
| 5626 | |
| 5627 | skb = skb_dequeue(head); |
| 5628 | } |
| 5629 | |
| 5630 | if (skb_peek(&list)) { |
| 5631 | spin_lock_irq(&head->lock); |
| 5632 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5633 | __skb_queue_tail(head, skb); |
| 5634 | spin_unlock_irq(&head->lock); |
| 5635 | } |
| 5636 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5637 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5638 | #endif |
| 5639 | } |
| 5640 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5641 | struct io_file_put { |
| 5642 | struct llist_node llist; |
| 5643 | struct file *file; |
| 5644 | struct completion *done; |
| 5645 | }; |
| 5646 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5647 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5648 | { |
| 5649 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5650 | struct llist_node *node; |
| 5651 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5652 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5653 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5654 | io_ring_file_put(data->ctx, pfile->file); |
| 5655 | if (pfile->done) |
| 5656 | complete(pfile->done); |
| 5657 | else |
| 5658 | kfree(pfile); |
| 5659 | } |
| 5660 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5661 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5662 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5663 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5664 | { |
| 5665 | struct fixed_file_data *data; |
| 5666 | |
| 5667 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5668 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5669 | percpu_ref_switch_to_percpu(&data->refs); |
| 5670 | } |
| 5671 | |
| 5672 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5673 | { |
| 5674 | struct fixed_file_data *data; |
| 5675 | |
| 5676 | data = container_of(ref, struct fixed_file_data, refs); |
| 5677 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5678 | /* |
| 5679 | * We can't safely switch from inside this context, punt to wq. If |
| 5680 | * the table ref is going away, the table is being unregistered. |
| 5681 | * Don't queue up the async work for that case, the caller will |
| 5682 | * handle it. |
| 5683 | */ |
| 5684 | if (!percpu_ref_is_dying(&data->refs)) |
| 5685 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5686 | } |
| 5687 | |
| 5688 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5689 | unsigned nr_args) |
| 5690 | { |
| 5691 | __s32 __user *fds = (__s32 __user *) arg; |
| 5692 | unsigned nr_tables; |
| 5693 | struct file *file; |
| 5694 | int fd, ret = 0; |
| 5695 | unsigned i; |
| 5696 | |
| 5697 | if (ctx->file_data) |
| 5698 | return -EBUSY; |
| 5699 | if (!nr_args) |
| 5700 | return -EINVAL; |
| 5701 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5702 | return -EMFILE; |
| 5703 | |
| 5704 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5705 | if (!ctx->file_data) |
| 5706 | return -ENOMEM; |
| 5707 | ctx->file_data->ctx = ctx; |
| 5708 | init_completion(&ctx->file_data->done); |
| 5709 | |
| 5710 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5711 | ctx->file_data->table = kcalloc(nr_tables, |
| 5712 | sizeof(struct fixed_file_table), |
| 5713 | GFP_KERNEL); |
| 5714 | if (!ctx->file_data->table) { |
| 5715 | kfree(ctx->file_data); |
| 5716 | ctx->file_data = NULL; |
| 5717 | return -ENOMEM; |
| 5718 | } |
| 5719 | |
| 5720 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5721 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5722 | kfree(ctx->file_data->table); |
| 5723 | kfree(ctx->file_data); |
| 5724 | ctx->file_data = NULL; |
| 5725 | return -ENOMEM; |
| 5726 | } |
| 5727 | ctx->file_data->put_llist.first = NULL; |
| 5728 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5729 | |
| 5730 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5731 | percpu_ref_exit(&ctx->file_data->refs); |
| 5732 | kfree(ctx->file_data->table); |
| 5733 | kfree(ctx->file_data); |
| 5734 | ctx->file_data = NULL; |
| 5735 | return -ENOMEM; |
| 5736 | } |
| 5737 | |
| 5738 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5739 | struct fixed_file_table *table; |
| 5740 | unsigned index; |
| 5741 | |
| 5742 | ret = -EFAULT; |
| 5743 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5744 | break; |
| 5745 | /* allow sparse sets */ |
| 5746 | if (fd == -1) { |
| 5747 | ret = 0; |
| 5748 | continue; |
| 5749 | } |
| 5750 | |
| 5751 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5752 | index = i & IORING_FILE_TABLE_MASK; |
| 5753 | file = fget(fd); |
| 5754 | |
| 5755 | ret = -EBADF; |
| 5756 | if (!file) |
| 5757 | break; |
| 5758 | |
| 5759 | /* |
| 5760 | * Don't allow io_uring instances to be registered. If UNIX |
| 5761 | * isn't enabled, then this causes a reference cycle and this |
| 5762 | * instance can never get freed. If UNIX is enabled we'll |
| 5763 | * handle it just fine, but there's still no point in allowing |
| 5764 | * a ring fd as it doesn't support regular read/write anyway. |
| 5765 | */ |
| 5766 | if (file->f_op == &io_uring_fops) { |
| 5767 | fput(file); |
| 5768 | break; |
| 5769 | } |
| 5770 | ret = 0; |
| 5771 | table->files[index] = file; |
| 5772 | } |
| 5773 | |
| 5774 | if (ret) { |
| 5775 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5776 | file = io_file_from_index(ctx, i); |
| 5777 | if (file) |
| 5778 | fput(file); |
| 5779 | } |
| 5780 | for (i = 0; i < nr_tables; i++) |
| 5781 | kfree(ctx->file_data->table[i].files); |
| 5782 | |
| 5783 | kfree(ctx->file_data->table); |
| 5784 | kfree(ctx->file_data); |
| 5785 | ctx->file_data = NULL; |
| 5786 | ctx->nr_user_files = 0; |
| 5787 | return ret; |
| 5788 | } |
| 5789 | |
| 5790 | ret = io_sqe_files_scm(ctx); |
| 5791 | if (ret) |
| 5792 | io_sqe_files_unregister(ctx); |
| 5793 | |
| 5794 | return ret; |
| 5795 | } |
| 5796 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5797 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5798 | int index) |
| 5799 | { |
| 5800 | #if defined(CONFIG_UNIX) |
| 5801 | struct sock *sock = ctx->ring_sock->sk; |
| 5802 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5803 | struct sk_buff *skb; |
| 5804 | |
| 5805 | /* |
| 5806 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5807 | * file set. If there's no room, fall back to allocating a new skb |
| 5808 | * and filling it in. |
| 5809 | */ |
| 5810 | spin_lock_irq(&head->lock); |
| 5811 | skb = skb_peek(head); |
| 5812 | if (skb) { |
| 5813 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5814 | |
| 5815 | if (fpl->count < SCM_MAX_FD) { |
| 5816 | __skb_unlink(skb, head); |
| 5817 | spin_unlock_irq(&head->lock); |
| 5818 | fpl->fp[fpl->count] = get_file(file); |
| 5819 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5820 | fpl->count++; |
| 5821 | spin_lock_irq(&head->lock); |
| 5822 | __skb_queue_head(head, skb); |
| 5823 | } else { |
| 5824 | skb = NULL; |
| 5825 | } |
| 5826 | } |
| 5827 | spin_unlock_irq(&head->lock); |
| 5828 | |
| 5829 | if (skb) { |
| 5830 | fput(file); |
| 5831 | return 0; |
| 5832 | } |
| 5833 | |
| 5834 | return __io_sqe_files_scm(ctx, 1, index); |
| 5835 | #else |
| 5836 | return 0; |
| 5837 | #endif |
| 5838 | } |
| 5839 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5840 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5841 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5842 | struct fixed_file_data *data; |
| 5843 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5844 | /* |
| 5845 | * Juggle reference to ensure we hit zero, if needed, so we can |
| 5846 | * switch back to percpu mode |
| 5847 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5848 | data = container_of(ref, struct fixed_file_data, refs); |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5849 | percpu_ref_put(&data->refs); |
| 5850 | percpu_ref_get(&data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5854 | struct file *file) |
| 5855 | { |
| 5856 | struct io_file_put *pfile, pfile_stack; |
| 5857 | DECLARE_COMPLETION_ONSTACK(done); |
| 5858 | |
| 5859 | /* |
| 5860 | * If we fail allocating the struct we need for doing async reomval |
| 5861 | * of this file, just punt to sync and wait for it. |
| 5862 | */ |
| 5863 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5864 | if (!pfile) { |
| 5865 | pfile = &pfile_stack; |
| 5866 | pfile->done = &done; |
| 5867 | } |
| 5868 | |
| 5869 | pfile->file = file; |
| 5870 | llist_add(&pfile->llist, &data->put_llist); |
| 5871 | |
| 5872 | if (pfile == &pfile_stack) { |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5873 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5874 | wait_for_completion(&done); |
| 5875 | flush_work(&data->ref_work); |
| 5876 | return false; |
| 5877 | } |
| 5878 | |
| 5879 | return true; |
| 5880 | } |
| 5881 | |
| 5882 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5883 | struct io_uring_files_update *up, |
| 5884 | unsigned nr_args) |
| 5885 | { |
| 5886 | struct fixed_file_data *data = ctx->file_data; |
| 5887 | bool ref_switch = false; |
| 5888 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5889 | __s32 __user *fds; |
| 5890 | int fd, i, err; |
| 5891 | __u32 done; |
| 5892 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5893 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5894 | return -EOVERFLOW; |
| 5895 | if (done > ctx->nr_user_files) |
| 5896 | return -EINVAL; |
| 5897 | |
| 5898 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5899 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5900 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5901 | struct fixed_file_table *table; |
| 5902 | unsigned index; |
| 5903 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5904 | err = 0; |
| 5905 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5906 | err = -EFAULT; |
| 5907 | break; |
| 5908 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5909 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5910 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5911 | index = i & IORING_FILE_TABLE_MASK; |
| 5912 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5913 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5914 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5915 | if (io_queue_file_removal(data, file)) |
| 5916 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5917 | } |
| 5918 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5919 | file = fget(fd); |
| 5920 | if (!file) { |
| 5921 | err = -EBADF; |
| 5922 | break; |
| 5923 | } |
| 5924 | /* |
| 5925 | * Don't allow io_uring instances to be registered. If |
| 5926 | * UNIX isn't enabled, then this causes a reference |
| 5927 | * cycle and this instance can never get freed. If UNIX |
| 5928 | * is enabled we'll handle it just fine, but there's |
| 5929 | * still no point in allowing a ring fd as it doesn't |
| 5930 | * support regular read/write anyway. |
| 5931 | */ |
| 5932 | if (file->f_op == &io_uring_fops) { |
| 5933 | fput(file); |
| 5934 | err = -EBADF; |
| 5935 | break; |
| 5936 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5937 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5938 | err = io_sqe_file_register(ctx, file, i); |
| 5939 | if (err) |
| 5940 | break; |
| 5941 | } |
| 5942 | nr_args--; |
| 5943 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5944 | up->offset++; |
| 5945 | } |
| 5946 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 5947 | if (ref_switch) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5948 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5949 | |
| 5950 | return done ? done : err; |
| 5951 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5952 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5953 | unsigned nr_args) |
| 5954 | { |
| 5955 | struct io_uring_files_update up; |
| 5956 | |
| 5957 | if (!ctx->file_data) |
| 5958 | return -ENXIO; |
| 5959 | if (!nr_args) |
| 5960 | return -EINVAL; |
| 5961 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5962 | return -EFAULT; |
| 5963 | if (up.resv) |
| 5964 | return -EINVAL; |
| 5965 | |
| 5966 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5967 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5968 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5969 | static void io_put_work(struct io_wq_work *work) |
| 5970 | { |
| 5971 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5972 | |
| 5973 | io_put_req(req); |
| 5974 | } |
| 5975 | |
| 5976 | static void io_get_work(struct io_wq_work *work) |
| 5977 | { |
| 5978 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5979 | |
| 5980 | refcount_inc(&req->refs); |
| 5981 | } |
| 5982 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5983 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 5984 | struct io_uring_params *p) |
| 5985 | { |
| 5986 | struct io_wq_data data; |
| 5987 | struct fd f; |
| 5988 | struct io_ring_ctx *ctx_attach; |
| 5989 | unsigned int concurrency; |
| 5990 | int ret = 0; |
| 5991 | |
| 5992 | data.user = ctx->user; |
| 5993 | data.get_work = io_get_work; |
| 5994 | data.put_work = io_put_work; |
| 5995 | |
| 5996 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 5997 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5998 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 5999 | |
| 6000 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 6001 | if (IS_ERR(ctx->io_wq)) { |
| 6002 | ret = PTR_ERR(ctx->io_wq); |
| 6003 | ctx->io_wq = NULL; |
| 6004 | } |
| 6005 | return ret; |
| 6006 | } |
| 6007 | |
| 6008 | f = fdget(p->wq_fd); |
| 6009 | if (!f.file) |
| 6010 | return -EBADF; |
| 6011 | |
| 6012 | if (f.file->f_op != &io_uring_fops) { |
| 6013 | ret = -EINVAL; |
| 6014 | goto out_fput; |
| 6015 | } |
| 6016 | |
| 6017 | ctx_attach = f.file->private_data; |
| 6018 | /* @io_wq is protected by holding the fd */ |
| 6019 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 6020 | ret = -EINVAL; |
| 6021 | goto out_fput; |
| 6022 | } |
| 6023 | |
| 6024 | ctx->io_wq = ctx_attach->io_wq; |
| 6025 | out_fput: |
| 6026 | fdput(f); |
| 6027 | return ret; |
| 6028 | } |
| 6029 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6030 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 6031 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6032 | { |
| 6033 | int ret; |
| 6034 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6035 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6036 | mmgrab(current->mm); |
| 6037 | ctx->sqo_mm = current->mm; |
| 6038 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6039 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 6040 | ret = -EPERM; |
| 6041 | if (!capable(CAP_SYS_ADMIN)) |
| 6042 | goto err; |
| 6043 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6044 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 6045 | if (!ctx->sq_thread_idle) |
| 6046 | ctx->sq_thread_idle = HZ; |
| 6047 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6048 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6049 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6050 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6051 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6052 | if (cpu >= nr_cpu_ids) |
| 6053 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 6054 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6055 | goto err; |
| 6056 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6057 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 6058 | ctx, cpu, |
| 6059 | "io_uring-sq"); |
| 6060 | } else { |
| 6061 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 6062 | "io_uring-sq"); |
| 6063 | } |
| 6064 | if (IS_ERR(ctx->sqo_thread)) { |
| 6065 | ret = PTR_ERR(ctx->sqo_thread); |
| 6066 | ctx->sqo_thread = NULL; |
| 6067 | goto err; |
| 6068 | } |
| 6069 | wake_up_process(ctx->sqo_thread); |
| 6070 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 6071 | /* Can't have SQ_AFF without SQPOLL */ |
| 6072 | ret = -EINVAL; |
| 6073 | goto err; |
| 6074 | } |
| 6075 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6076 | ret = io_init_wq_offload(ctx, p); |
| 6077 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6078 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6079 | |
| 6080 | return 0; |
| 6081 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 6082 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6083 | mmdrop(ctx->sqo_mm); |
| 6084 | ctx->sqo_mm = NULL; |
| 6085 | return ret; |
| 6086 | } |
| 6087 | |
| 6088 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 6089 | { |
| 6090 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 6091 | } |
| 6092 | |
| 6093 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 6094 | { |
| 6095 | unsigned long page_limit, cur_pages, new_pages; |
| 6096 | |
| 6097 | /* Don't allow more pages than we can safely lock */ |
| 6098 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 6099 | |
| 6100 | do { |
| 6101 | cur_pages = atomic_long_read(&user->locked_vm); |
| 6102 | new_pages = cur_pages + nr_pages; |
| 6103 | if (new_pages > page_limit) |
| 6104 | return -ENOMEM; |
| 6105 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 6106 | new_pages) != cur_pages); |
| 6107 | |
| 6108 | return 0; |
| 6109 | } |
| 6110 | |
| 6111 | static void io_mem_free(void *ptr) |
| 6112 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6113 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6114 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6115 | if (!ptr) |
| 6116 | return; |
| 6117 | |
| 6118 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6119 | if (put_page_testzero(page)) |
| 6120 | free_compound_page(page); |
| 6121 | } |
| 6122 | |
| 6123 | static void *io_mem_alloc(size_t size) |
| 6124 | { |
| 6125 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 6126 | __GFP_NORETRY; |
| 6127 | |
| 6128 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 6129 | } |
| 6130 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6131 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 6132 | size_t *sq_offset) |
| 6133 | { |
| 6134 | struct io_rings *rings; |
| 6135 | size_t off, sq_array_size; |
| 6136 | |
| 6137 | off = struct_size(rings, cqes, cq_entries); |
| 6138 | if (off == SIZE_MAX) |
| 6139 | return SIZE_MAX; |
| 6140 | |
| 6141 | #ifdef CONFIG_SMP |
| 6142 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6143 | if (off == 0) |
| 6144 | return SIZE_MAX; |
| 6145 | #endif |
| 6146 | |
| 6147 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6148 | if (sq_array_size == SIZE_MAX) |
| 6149 | return SIZE_MAX; |
| 6150 | |
| 6151 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6152 | return SIZE_MAX; |
| 6153 | |
| 6154 | if (sq_offset) |
| 6155 | *sq_offset = off; |
| 6156 | |
| 6157 | return off; |
| 6158 | } |
| 6159 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6160 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6161 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6162 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6163 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6164 | pages = (size_t)1 << get_order( |
| 6165 | rings_size(sq_entries, cq_entries, NULL)); |
| 6166 | pages += (size_t)1 << get_order( |
| 6167 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6168 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6169 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6170 | } |
| 6171 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6172 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6173 | { |
| 6174 | int i, j; |
| 6175 | |
| 6176 | if (!ctx->user_bufs) |
| 6177 | return -ENXIO; |
| 6178 | |
| 6179 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6180 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6181 | |
| 6182 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6183 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6184 | |
| 6185 | if (ctx->account_mem) |
| 6186 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6187 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6188 | imu->nr_bvecs = 0; |
| 6189 | } |
| 6190 | |
| 6191 | kfree(ctx->user_bufs); |
| 6192 | ctx->user_bufs = NULL; |
| 6193 | ctx->nr_user_bufs = 0; |
| 6194 | return 0; |
| 6195 | } |
| 6196 | |
| 6197 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6198 | void __user *arg, unsigned index) |
| 6199 | { |
| 6200 | struct iovec __user *src; |
| 6201 | |
| 6202 | #ifdef CONFIG_COMPAT |
| 6203 | if (ctx->compat) { |
| 6204 | struct compat_iovec __user *ciovs; |
| 6205 | struct compat_iovec ciov; |
| 6206 | |
| 6207 | ciovs = (struct compat_iovec __user *) arg; |
| 6208 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6209 | return -EFAULT; |
| 6210 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6211 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6212 | dst->iov_len = ciov.iov_len; |
| 6213 | return 0; |
| 6214 | } |
| 6215 | #endif |
| 6216 | src = (struct iovec __user *) arg; |
| 6217 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6218 | return -EFAULT; |
| 6219 | return 0; |
| 6220 | } |
| 6221 | |
| 6222 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6223 | unsigned nr_args) |
| 6224 | { |
| 6225 | struct vm_area_struct **vmas = NULL; |
| 6226 | struct page **pages = NULL; |
| 6227 | int i, j, got_pages = 0; |
| 6228 | int ret = -EINVAL; |
| 6229 | |
| 6230 | if (ctx->user_bufs) |
| 6231 | return -EBUSY; |
| 6232 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6233 | return -EINVAL; |
| 6234 | |
| 6235 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6236 | GFP_KERNEL); |
| 6237 | if (!ctx->user_bufs) |
| 6238 | return -ENOMEM; |
| 6239 | |
| 6240 | for (i = 0; i < nr_args; i++) { |
| 6241 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6242 | unsigned long off, start, end, ubuf; |
| 6243 | int pret, nr_pages; |
| 6244 | struct iovec iov; |
| 6245 | size_t size; |
| 6246 | |
| 6247 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6248 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6249 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6250 | |
| 6251 | /* |
| 6252 | * Don't impose further limits on the size and buffer |
| 6253 | * constraints here, we'll -EINVAL later when IO is |
| 6254 | * submitted if they are wrong. |
| 6255 | */ |
| 6256 | ret = -EFAULT; |
| 6257 | if (!iov.iov_base || !iov.iov_len) |
| 6258 | goto err; |
| 6259 | |
| 6260 | /* arbitrary limit, but we need something */ |
| 6261 | if (iov.iov_len > SZ_1G) |
| 6262 | goto err; |
| 6263 | |
| 6264 | ubuf = (unsigned long) iov.iov_base; |
| 6265 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6266 | start = ubuf >> PAGE_SHIFT; |
| 6267 | nr_pages = end - start; |
| 6268 | |
| 6269 | if (ctx->account_mem) { |
| 6270 | ret = io_account_mem(ctx->user, nr_pages); |
| 6271 | if (ret) |
| 6272 | goto err; |
| 6273 | } |
| 6274 | |
| 6275 | ret = 0; |
| 6276 | if (!pages || nr_pages > got_pages) { |
| 6277 | kfree(vmas); |
| 6278 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6279 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6280 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6281 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6282 | sizeof(struct vm_area_struct *), |
| 6283 | GFP_KERNEL); |
| 6284 | if (!pages || !vmas) { |
| 6285 | ret = -ENOMEM; |
| 6286 | if (ctx->account_mem) |
| 6287 | io_unaccount_mem(ctx->user, nr_pages); |
| 6288 | goto err; |
| 6289 | } |
| 6290 | got_pages = nr_pages; |
| 6291 | } |
| 6292 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6293 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6294 | GFP_KERNEL); |
| 6295 | ret = -ENOMEM; |
| 6296 | if (!imu->bvec) { |
| 6297 | if (ctx->account_mem) |
| 6298 | io_unaccount_mem(ctx->user, nr_pages); |
| 6299 | goto err; |
| 6300 | } |
| 6301 | |
| 6302 | ret = 0; |
| 6303 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6304 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6305 | FOLL_WRITE | FOLL_LONGTERM, |
| 6306 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6307 | if (pret == nr_pages) { |
| 6308 | /* don't support file backed memory */ |
| 6309 | for (j = 0; j < nr_pages; j++) { |
| 6310 | struct vm_area_struct *vma = vmas[j]; |
| 6311 | |
| 6312 | if (vma->vm_file && |
| 6313 | !is_file_hugepages(vma->vm_file)) { |
| 6314 | ret = -EOPNOTSUPP; |
| 6315 | break; |
| 6316 | } |
| 6317 | } |
| 6318 | } else { |
| 6319 | ret = pret < 0 ? pret : -EFAULT; |
| 6320 | } |
| 6321 | up_read(¤t->mm->mmap_sem); |
| 6322 | if (ret) { |
| 6323 | /* |
| 6324 | * if we did partial map, or found file backed vmas, |
| 6325 | * release any pages we did get |
| 6326 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6327 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6328 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6329 | if (ctx->account_mem) |
| 6330 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6331 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6332 | goto err; |
| 6333 | } |
| 6334 | |
| 6335 | off = ubuf & ~PAGE_MASK; |
| 6336 | size = iov.iov_len; |
| 6337 | for (j = 0; j < nr_pages; j++) { |
| 6338 | size_t vec_len; |
| 6339 | |
| 6340 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6341 | imu->bvec[j].bv_page = pages[j]; |
| 6342 | imu->bvec[j].bv_len = vec_len; |
| 6343 | imu->bvec[j].bv_offset = off; |
| 6344 | off = 0; |
| 6345 | size -= vec_len; |
| 6346 | } |
| 6347 | /* store original address for later verification */ |
| 6348 | imu->ubuf = ubuf; |
| 6349 | imu->len = iov.iov_len; |
| 6350 | imu->nr_bvecs = nr_pages; |
| 6351 | |
| 6352 | ctx->nr_user_bufs++; |
| 6353 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6354 | kvfree(pages); |
| 6355 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6356 | return 0; |
| 6357 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6358 | kvfree(pages); |
| 6359 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6360 | io_sqe_buffer_unregister(ctx); |
| 6361 | return ret; |
| 6362 | } |
| 6363 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6364 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6365 | { |
| 6366 | __s32 __user *fds = arg; |
| 6367 | int fd; |
| 6368 | |
| 6369 | if (ctx->cq_ev_fd) |
| 6370 | return -EBUSY; |
| 6371 | |
| 6372 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6373 | return -EFAULT; |
| 6374 | |
| 6375 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6376 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6377 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6378 | ctx->cq_ev_fd = NULL; |
| 6379 | return ret; |
| 6380 | } |
| 6381 | |
| 6382 | return 0; |
| 6383 | } |
| 6384 | |
| 6385 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6386 | { |
| 6387 | if (ctx->cq_ev_fd) { |
| 6388 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6389 | ctx->cq_ev_fd = NULL; |
| 6390 | return 0; |
| 6391 | } |
| 6392 | |
| 6393 | return -ENXIO; |
| 6394 | } |
| 6395 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6396 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6397 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6398 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6399 | if (ctx->sqo_mm) |
| 6400 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6401 | |
| 6402 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6403 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6404 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6405 | io_eventfd_unregister(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 6406 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6407 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6408 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6409 | if (ctx->ring_sock) { |
| 6410 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6411 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6412 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6413 | #endif |
| 6414 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6415 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6416 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6417 | |
| 6418 | percpu_ref_exit(&ctx->refs); |
| 6419 | if (ctx->account_mem) |
| 6420 | io_unaccount_mem(ctx->user, |
| 6421 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6422 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6423 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6424 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6425 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6426 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6427 | kfree(ctx); |
| 6428 | } |
| 6429 | |
| 6430 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6431 | { |
| 6432 | struct io_ring_ctx *ctx = file->private_data; |
| 6433 | __poll_t mask = 0; |
| 6434 | |
| 6435 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6436 | /* |
| 6437 | * synchronizes with barrier from wq_has_sleeper call in |
| 6438 | * io_commit_cqring |
| 6439 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6440 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6441 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6442 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6443 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 6444 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6445 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6446 | |
| 6447 | return mask; |
| 6448 | } |
| 6449 | |
| 6450 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6451 | { |
| 6452 | struct io_ring_ctx *ctx = file->private_data; |
| 6453 | |
| 6454 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6455 | } |
| 6456 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6457 | static int io_remove_personalities(int id, void *p, void *data) |
| 6458 | { |
| 6459 | struct io_ring_ctx *ctx = data; |
| 6460 | const struct cred *cred; |
| 6461 | |
| 6462 | cred = idr_remove(&ctx->personality_idr, id); |
| 6463 | if (cred) |
| 6464 | put_cred(cred); |
| 6465 | return 0; |
| 6466 | } |
| 6467 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6468 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6469 | { |
| 6470 | mutex_lock(&ctx->uring_lock); |
| 6471 | percpu_ref_kill(&ctx->refs); |
| 6472 | mutex_unlock(&ctx->uring_lock); |
| 6473 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6474 | /* |
| 6475 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 6476 | * work after we've killed the ctx ref above. This is important to do |
| 6477 | * before we cancel existing commands, as the thread could otherwise |
| 6478 | * be queueing new work post that. If that's work we need to cancel, |
| 6479 | * it could cause shutdown to hang. |
| 6480 | */ |
| 6481 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 6482 | cpu_relax(); |
| 6483 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6484 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6485 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6486 | |
| 6487 | if (ctx->io_wq) |
| 6488 | io_wq_cancel_all(ctx->io_wq); |
| 6489 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6490 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6491 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6492 | if (ctx->rings) |
| 6493 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6494 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6495 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6496 | io_ring_ctx_free(ctx); |
| 6497 | } |
| 6498 | |
| 6499 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6500 | { |
| 6501 | struct io_ring_ctx *ctx = file->private_data; |
| 6502 | |
| 6503 | file->private_data = NULL; |
| 6504 | io_ring_ctx_wait_and_kill(ctx); |
| 6505 | return 0; |
| 6506 | } |
| 6507 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6508 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6509 | struct files_struct *files) |
| 6510 | { |
| 6511 | struct io_kiocb *req; |
| 6512 | DEFINE_WAIT(wait); |
| 6513 | |
| 6514 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6515 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6516 | |
| 6517 | spin_lock_irq(&ctx->inflight_lock); |
| 6518 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6519 | if (req->work.files != files) |
| 6520 | continue; |
| 6521 | /* req is being completed, ignore */ |
| 6522 | if (!refcount_inc_not_zero(&req->refs)) |
| 6523 | continue; |
| 6524 | cancel_req = req; |
| 6525 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6526 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6527 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6528 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6529 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6530 | spin_unlock_irq(&ctx->inflight_lock); |
| 6531 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6532 | /* We need to keep going until we don't find a matching req */ |
| 6533 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6534 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6535 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 6536 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 6537 | spin_lock_irq(&ctx->completion_lock); |
| 6538 | list_del(&cancel_req->list); |
| 6539 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 6540 | if (list_empty(&ctx->cq_overflow_list)) { |
| 6541 | clear_bit(0, &ctx->sq_check_overflow); |
| 6542 | clear_bit(0, &ctx->cq_check_overflow); |
| 6543 | } |
| 6544 | spin_unlock_irq(&ctx->completion_lock); |
| 6545 | |
| 6546 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 6547 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 6548 | |
| 6549 | /* |
| 6550 | * Put inflight ref and overflow ref. If that's |
| 6551 | * all we had, then we're done with this request. |
| 6552 | */ |
| 6553 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
| 6554 | io_put_req(cancel_req); |
| 6555 | continue; |
| 6556 | } |
| 6557 | } |
| 6558 | |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6559 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6560 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6561 | schedule(); |
| 6562 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6563 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6564 | } |
| 6565 | |
| 6566 | static int io_uring_flush(struct file *file, void *data) |
| 6567 | { |
| 6568 | struct io_ring_ctx *ctx = file->private_data; |
| 6569 | |
| 6570 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 6571 | |
| 6572 | /* |
| 6573 | * If the task is going away, cancel work it may have pending |
| 6574 | */ |
| 6575 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 6576 | io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current)); |
| 6577 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6578 | return 0; |
| 6579 | } |
| 6580 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6581 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6582 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6583 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6584 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6585 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6586 | struct page *page; |
| 6587 | void *ptr; |
| 6588 | |
| 6589 | switch (offset) { |
| 6590 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6591 | case IORING_OFF_CQ_RING: |
| 6592 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6593 | break; |
| 6594 | case IORING_OFF_SQES: |
| 6595 | ptr = ctx->sq_sqes; |
| 6596 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6597 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6598 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6599 | } |
| 6600 | |
| 6601 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6602 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6603 | return ERR_PTR(-EINVAL); |
| 6604 | |
| 6605 | return ptr; |
| 6606 | } |
| 6607 | |
| 6608 | #ifdef CONFIG_MMU |
| 6609 | |
| 6610 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6611 | { |
| 6612 | size_t sz = vma->vm_end - vma->vm_start; |
| 6613 | unsigned long pfn; |
| 6614 | void *ptr; |
| 6615 | |
| 6616 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6617 | if (IS_ERR(ptr)) |
| 6618 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6619 | |
| 6620 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6621 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6622 | } |
| 6623 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6624 | #else /* !CONFIG_MMU */ |
| 6625 | |
| 6626 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6627 | { |
| 6628 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6629 | } |
| 6630 | |
| 6631 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6632 | { |
| 6633 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6634 | } |
| 6635 | |
| 6636 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6637 | unsigned long addr, unsigned long len, |
| 6638 | unsigned long pgoff, unsigned long flags) |
| 6639 | { |
| 6640 | void *ptr; |
| 6641 | |
| 6642 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6643 | if (IS_ERR(ptr)) |
| 6644 | return PTR_ERR(ptr); |
| 6645 | |
| 6646 | return (unsigned long) ptr; |
| 6647 | } |
| 6648 | |
| 6649 | #endif /* !CONFIG_MMU */ |
| 6650 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6651 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6652 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6653 | size_t, sigsz) |
| 6654 | { |
| 6655 | struct io_ring_ctx *ctx; |
| 6656 | long ret = -EBADF; |
| 6657 | int submitted = 0; |
| 6658 | struct fd f; |
| 6659 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6660 | if (current->task_works) |
| 6661 | task_work_run(); |
| 6662 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6663 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6664 | return -EINVAL; |
| 6665 | |
| 6666 | f = fdget(fd); |
| 6667 | if (!f.file) |
| 6668 | return -EBADF; |
| 6669 | |
| 6670 | ret = -EOPNOTSUPP; |
| 6671 | if (f.file->f_op != &io_uring_fops) |
| 6672 | goto out_fput; |
| 6673 | |
| 6674 | ret = -ENXIO; |
| 6675 | ctx = f.file->private_data; |
| 6676 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6677 | goto out_fput; |
| 6678 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6679 | /* |
| 6680 | * For SQ polling, the thread will do all submissions and completions. |
| 6681 | * Just return the requested submit count, and wake the thread if |
| 6682 | * we were asked to. |
| 6683 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6684 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6685 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6686 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6687 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6688 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6689 | wake_up(&ctx->sqo_wait); |
| 6690 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6691 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6692 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6693 | |
| 6694 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6695 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6696 | cur_mm = ctx->sqo_mm; |
| 6697 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6698 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6699 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6700 | |
| 6701 | if (submitted != to_submit) |
| 6702 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6703 | } |
| 6704 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6705 | unsigned nr_events = 0; |
| 6706 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6707 | min_complete = min(min_complete, ctx->cq_entries); |
| 6708 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6709 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6710 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6711 | } else { |
| 6712 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6713 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6714 | } |
| 6715 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6716 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6717 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6718 | out_fput: |
| 6719 | fdput(f); |
| 6720 | return submitted ? submitted : ret; |
| 6721 | } |
| 6722 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6723 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6724 | static int io_uring_show_cred(int id, void *p, void *data) |
| 6725 | { |
| 6726 | const struct cred *cred = p; |
| 6727 | struct seq_file *m = data; |
| 6728 | struct user_namespace *uns = seq_user_ns(m); |
| 6729 | struct group_info *gi; |
| 6730 | kernel_cap_t cap; |
| 6731 | unsigned __capi; |
| 6732 | int g; |
| 6733 | |
| 6734 | seq_printf(m, "%5d\n", id); |
| 6735 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 6736 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 6737 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 6738 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 6739 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 6740 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 6741 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 6742 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 6743 | seq_puts(m, "\n\tGroups:\t"); |
| 6744 | gi = cred->group_info; |
| 6745 | for (g = 0; g < gi->ngroups; g++) { |
| 6746 | seq_put_decimal_ull(m, g ? " " : "", |
| 6747 | from_kgid_munged(uns, gi->gid[g])); |
| 6748 | } |
| 6749 | seq_puts(m, "\n\tCapEff:\t"); |
| 6750 | cap = cred->cap_effective; |
| 6751 | CAP_FOR_EACH_U32(__capi) |
| 6752 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 6753 | seq_putc(m, '\n'); |
| 6754 | return 0; |
| 6755 | } |
| 6756 | |
| 6757 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 6758 | { |
| 6759 | int i; |
| 6760 | |
| 6761 | mutex_lock(&ctx->uring_lock); |
| 6762 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 6763 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6764 | struct fixed_file_table *table; |
| 6765 | struct file *f; |
| 6766 | |
| 6767 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6768 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 6769 | if (f) |
| 6770 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 6771 | else |
| 6772 | seq_printf(m, "%5u: <none>\n", i); |
| 6773 | } |
| 6774 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 6775 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6776 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 6777 | |
| 6778 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 6779 | (unsigned int) buf->len); |
| 6780 | } |
| 6781 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 6782 | seq_printf(m, "Personalities:\n"); |
| 6783 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 6784 | } |
| 6785 | mutex_unlock(&ctx->uring_lock); |
| 6786 | } |
| 6787 | |
| 6788 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 6789 | { |
| 6790 | struct io_ring_ctx *ctx = f->private_data; |
| 6791 | |
| 6792 | if (percpu_ref_tryget(&ctx->refs)) { |
| 6793 | __io_uring_show_fdinfo(ctx, m); |
| 6794 | percpu_ref_put(&ctx->refs); |
| 6795 | } |
| 6796 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6797 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6798 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6799 | static const struct file_operations io_uring_fops = { |
| 6800 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6801 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6802 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6803 | #ifndef CONFIG_MMU |
| 6804 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6805 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6806 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6807 | .poll = io_uring_poll, |
| 6808 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6809 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 6810 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 6811 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6812 | }; |
| 6813 | |
| 6814 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6815 | struct io_uring_params *p) |
| 6816 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6817 | struct io_rings *rings; |
| 6818 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6819 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6820 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6821 | if (size == SIZE_MAX) |
| 6822 | return -EOVERFLOW; |
| 6823 | |
| 6824 | rings = io_mem_alloc(size); |
| 6825 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6826 | return -ENOMEM; |
| 6827 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6828 | ctx->rings = rings; |
| 6829 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6830 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6831 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6832 | rings->sq_ring_entries = p->sq_entries; |
| 6833 | rings->cq_ring_entries = p->cq_entries; |
| 6834 | ctx->sq_mask = rings->sq_ring_mask; |
| 6835 | ctx->cq_mask = rings->cq_ring_mask; |
| 6836 | ctx->sq_entries = rings->sq_ring_entries; |
| 6837 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6838 | |
| 6839 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6840 | if (size == SIZE_MAX) { |
| 6841 | io_mem_free(ctx->rings); |
| 6842 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6843 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6844 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6845 | |
| 6846 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6847 | if (!ctx->sq_sqes) { |
| 6848 | io_mem_free(ctx->rings); |
| 6849 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6850 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6851 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6852 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6853 | return 0; |
| 6854 | } |
| 6855 | |
| 6856 | /* |
| 6857 | * Allocate an anonymous fd, this is what constitutes the application |
| 6858 | * visible backing of an io_uring instance. The application mmaps this |
| 6859 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6860 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6861 | */ |
| 6862 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6863 | { |
| 6864 | struct file *file; |
| 6865 | int ret; |
| 6866 | |
| 6867 | #if defined(CONFIG_UNIX) |
| 6868 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6869 | &ctx->ring_sock); |
| 6870 | if (ret) |
| 6871 | return ret; |
| 6872 | #endif |
| 6873 | |
| 6874 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6875 | if (ret < 0) |
| 6876 | goto err; |
| 6877 | |
| 6878 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6879 | O_RDWR | O_CLOEXEC); |
| 6880 | if (IS_ERR(file)) { |
| 6881 | put_unused_fd(ret); |
| 6882 | ret = PTR_ERR(file); |
| 6883 | goto err; |
| 6884 | } |
| 6885 | |
| 6886 | #if defined(CONFIG_UNIX) |
| 6887 | ctx->ring_sock->file = file; |
| 6888 | #endif |
| 6889 | fd_install(ret, file); |
| 6890 | return ret; |
| 6891 | err: |
| 6892 | #if defined(CONFIG_UNIX) |
| 6893 | sock_release(ctx->ring_sock); |
| 6894 | ctx->ring_sock = NULL; |
| 6895 | #endif |
| 6896 | return ret; |
| 6897 | } |
| 6898 | |
| 6899 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6900 | { |
| 6901 | struct user_struct *user = NULL; |
| 6902 | struct io_ring_ctx *ctx; |
| 6903 | bool account_mem; |
| 6904 | int ret; |
| 6905 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6906 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6907 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6908 | if (entries > IORING_MAX_ENTRIES) { |
| 6909 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6910 | return -EINVAL; |
| 6911 | entries = IORING_MAX_ENTRIES; |
| 6912 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6913 | |
| 6914 | /* |
| 6915 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6916 | * application to drive a higher depth than the size of the SQ ring, |
| 6917 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6918 | * some flexibility in overcommitting a bit. If the application has |
| 6919 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6920 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6921 | */ |
| 6922 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6923 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6924 | /* |
| 6925 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6926 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6927 | * any cq vs sq ring sizing. |
| 6928 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6929 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6930 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6931 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6932 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6933 | return -EINVAL; |
| 6934 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6935 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6936 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6937 | } else { |
| 6938 | p->cq_entries = 2 * p->sq_entries; |
| 6939 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6940 | |
| 6941 | user = get_uid(current_user()); |
| 6942 | account_mem = !capable(CAP_IPC_LOCK); |
| 6943 | |
| 6944 | if (account_mem) { |
| 6945 | ret = io_account_mem(user, |
| 6946 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6947 | if (ret) { |
| 6948 | free_uid(user); |
| 6949 | return ret; |
| 6950 | } |
| 6951 | } |
| 6952 | |
| 6953 | ctx = io_ring_ctx_alloc(p); |
| 6954 | if (!ctx) { |
| 6955 | if (account_mem) |
| 6956 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6957 | p->cq_entries)); |
| 6958 | free_uid(user); |
| 6959 | return -ENOMEM; |
| 6960 | } |
| 6961 | ctx->compat = in_compat_syscall(); |
| 6962 | ctx->account_mem = account_mem; |
| 6963 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6964 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6965 | |
| 6966 | ret = io_allocate_scq_urings(ctx, p); |
| 6967 | if (ret) |
| 6968 | goto err; |
| 6969 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6970 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6971 | if (ret) |
| 6972 | goto err; |
| 6973 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6974 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6975 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6976 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6977 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6978 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6979 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6980 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6981 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6982 | |
| 6983 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6984 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6985 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6986 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6987 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6988 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6989 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6990 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6991 | /* |
| 6992 | * Install ring fd as the very last thing, so we don't risk someone |
| 6993 | * having closed it before we finish setup |
| 6994 | */ |
| 6995 | ret = io_uring_get_fd(ctx); |
| 6996 | if (ret < 0) |
| 6997 | goto err; |
| 6998 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6999 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 7000 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
| 7001 | IORING_FEAT_CUR_PERSONALITY; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7002 | 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] | 7003 | return ret; |
| 7004 | err: |
| 7005 | io_ring_ctx_wait_and_kill(ctx); |
| 7006 | return ret; |
| 7007 | } |
| 7008 | |
| 7009 | /* |
| 7010 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 7011 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 7012 | * params structure passed in. |
| 7013 | */ |
| 7014 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 7015 | { |
| 7016 | struct io_uring_params p; |
| 7017 | long ret; |
| 7018 | int i; |
| 7019 | |
| 7020 | if (copy_from_user(&p, params, sizeof(p))) |
| 7021 | return -EFAULT; |
| 7022 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 7023 | if (p.resv[i]) |
| 7024 | return -EINVAL; |
| 7025 | } |
| 7026 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7027 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7028 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7029 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7030 | return -EINVAL; |
| 7031 | |
| 7032 | ret = io_uring_create(entries, &p); |
| 7033 | if (ret < 0) |
| 7034 | return ret; |
| 7035 | |
| 7036 | if (copy_to_user(params, &p, sizeof(p))) |
| 7037 | return -EFAULT; |
| 7038 | |
| 7039 | return ret; |
| 7040 | } |
| 7041 | |
| 7042 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 7043 | struct io_uring_params __user *, params) |
| 7044 | { |
| 7045 | return io_uring_setup(entries, params); |
| 7046 | } |
| 7047 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7048 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 7049 | { |
| 7050 | struct io_uring_probe *p; |
| 7051 | size_t size; |
| 7052 | int i, ret; |
| 7053 | |
| 7054 | size = struct_size(p, ops, nr_args); |
| 7055 | if (size == SIZE_MAX) |
| 7056 | return -EOVERFLOW; |
| 7057 | p = kzalloc(size, GFP_KERNEL); |
| 7058 | if (!p) |
| 7059 | return -ENOMEM; |
| 7060 | |
| 7061 | ret = -EFAULT; |
| 7062 | if (copy_from_user(p, arg, size)) |
| 7063 | goto out; |
| 7064 | ret = -EINVAL; |
| 7065 | if (memchr_inv(p, 0, size)) |
| 7066 | goto out; |
| 7067 | |
| 7068 | p->last_op = IORING_OP_LAST - 1; |
| 7069 | if (nr_args > IORING_OP_LAST) |
| 7070 | nr_args = IORING_OP_LAST; |
| 7071 | |
| 7072 | for (i = 0; i < nr_args; i++) { |
| 7073 | p->ops[i].op = i; |
| 7074 | if (!io_op_defs[i].not_supported) |
| 7075 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 7076 | } |
| 7077 | p->ops_len = i; |
| 7078 | |
| 7079 | ret = 0; |
| 7080 | if (copy_to_user(arg, p, size)) |
| 7081 | ret = -EFAULT; |
| 7082 | out: |
| 7083 | kfree(p); |
| 7084 | return ret; |
| 7085 | } |
| 7086 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7087 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 7088 | { |
| 7089 | const struct cred *creds = get_current_cred(); |
| 7090 | int id; |
| 7091 | |
| 7092 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 7093 | USHRT_MAX, GFP_KERNEL); |
| 7094 | if (id < 0) |
| 7095 | put_cred(creds); |
| 7096 | return id; |
| 7097 | } |
| 7098 | |
| 7099 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 7100 | { |
| 7101 | const struct cred *old_creds; |
| 7102 | |
| 7103 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 7104 | if (old_creds) { |
| 7105 | put_cred(old_creds); |
| 7106 | return 0; |
| 7107 | } |
| 7108 | |
| 7109 | return -EINVAL; |
| 7110 | } |
| 7111 | |
| 7112 | static bool io_register_op_must_quiesce(int op) |
| 7113 | { |
| 7114 | switch (op) { |
| 7115 | case IORING_UNREGISTER_FILES: |
| 7116 | case IORING_REGISTER_FILES_UPDATE: |
| 7117 | case IORING_REGISTER_PROBE: |
| 7118 | case IORING_REGISTER_PERSONALITY: |
| 7119 | case IORING_UNREGISTER_PERSONALITY: |
| 7120 | return false; |
| 7121 | default: |
| 7122 | return true; |
| 7123 | } |
| 7124 | } |
| 7125 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7126 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 7127 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7128 | __releases(ctx->uring_lock) |
| 7129 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7130 | { |
| 7131 | int ret; |
| 7132 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 7133 | /* |
| 7134 | * We're inside the ring mutex, if the ref is already dying, then |
| 7135 | * someone else killed the ctx or is already going through |
| 7136 | * io_uring_register(). |
| 7137 | */ |
| 7138 | if (percpu_ref_is_dying(&ctx->refs)) |
| 7139 | return -ENXIO; |
| 7140 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7141 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7142 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7143 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7144 | /* |
| 7145 | * Drop uring mutex before waiting for references to exit. If |
| 7146 | * another thread is currently inside io_uring_enter() it might |
| 7147 | * need to grab the uring_lock to make progress. If we hold it |
| 7148 | * here across the drain wait, then we can deadlock. It's safe |
| 7149 | * to drop the mutex here, since no new references will come in |
| 7150 | * after we've killed the percpu ref. |
| 7151 | */ |
| 7152 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7153 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7154 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7155 | if (ret) { |
| 7156 | percpu_ref_resurrect(&ctx->refs); |
| 7157 | ret = -EINTR; |
| 7158 | goto out; |
| 7159 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7160 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7161 | |
| 7162 | switch (opcode) { |
| 7163 | case IORING_REGISTER_BUFFERS: |
| 7164 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 7165 | break; |
| 7166 | case IORING_UNREGISTER_BUFFERS: |
| 7167 | ret = -EINVAL; |
| 7168 | if (arg || nr_args) |
| 7169 | break; |
| 7170 | ret = io_sqe_buffer_unregister(ctx); |
| 7171 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7172 | case IORING_REGISTER_FILES: |
| 7173 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 7174 | break; |
| 7175 | case IORING_UNREGISTER_FILES: |
| 7176 | ret = -EINVAL; |
| 7177 | if (arg || nr_args) |
| 7178 | break; |
| 7179 | ret = io_sqe_files_unregister(ctx); |
| 7180 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7181 | case IORING_REGISTER_FILES_UPDATE: |
| 7182 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7183 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7184 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7185 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7186 | ret = -EINVAL; |
| 7187 | if (nr_args != 1) |
| 7188 | break; |
| 7189 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7190 | if (ret) |
| 7191 | break; |
| 7192 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7193 | ctx->eventfd_async = 1; |
| 7194 | else |
| 7195 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7196 | break; |
| 7197 | case IORING_UNREGISTER_EVENTFD: |
| 7198 | ret = -EINVAL; |
| 7199 | if (arg || nr_args) |
| 7200 | break; |
| 7201 | ret = io_eventfd_unregister(ctx); |
| 7202 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7203 | case IORING_REGISTER_PROBE: |
| 7204 | ret = -EINVAL; |
| 7205 | if (!arg || nr_args > 256) |
| 7206 | break; |
| 7207 | ret = io_probe(ctx, arg, nr_args); |
| 7208 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7209 | case IORING_REGISTER_PERSONALITY: |
| 7210 | ret = -EINVAL; |
| 7211 | if (arg || nr_args) |
| 7212 | break; |
| 7213 | ret = io_register_personality(ctx); |
| 7214 | break; |
| 7215 | case IORING_UNREGISTER_PERSONALITY: |
| 7216 | ret = -EINVAL; |
| 7217 | if (arg) |
| 7218 | break; |
| 7219 | ret = io_unregister_personality(ctx, nr_args); |
| 7220 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7221 | default: |
| 7222 | ret = -EINVAL; |
| 7223 | break; |
| 7224 | } |
| 7225 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7226 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7227 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7228 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7229 | out: |
| 7230 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7231 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7232 | return ret; |
| 7233 | } |
| 7234 | |
| 7235 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7236 | void __user *, arg, unsigned int, nr_args) |
| 7237 | { |
| 7238 | struct io_ring_ctx *ctx; |
| 7239 | long ret = -EBADF; |
| 7240 | struct fd f; |
| 7241 | |
| 7242 | f = fdget(fd); |
| 7243 | if (!f.file) |
| 7244 | return -EBADF; |
| 7245 | |
| 7246 | ret = -EOPNOTSUPP; |
| 7247 | if (f.file->f_op != &io_uring_fops) |
| 7248 | goto out_fput; |
| 7249 | |
| 7250 | ctx = f.file->private_data; |
| 7251 | |
| 7252 | mutex_lock(&ctx->uring_lock); |
| 7253 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7254 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7255 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7256 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7257 | out_fput: |
| 7258 | fdput(f); |
| 7259 | return ret; |
| 7260 | } |
| 7261 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7262 | static int __init io_uring_init(void) |
| 7263 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7264 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7265 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7266 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7267 | } while (0) |
| 7268 | |
| 7269 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7270 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7271 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7272 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7273 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7274 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7275 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7276 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7277 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7278 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7279 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7280 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7281 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7282 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7283 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7284 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7285 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7286 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7287 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7288 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7289 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7290 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7291 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7292 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7293 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7294 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7295 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7296 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7297 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7298 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7299 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7300 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7301 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 7302 | return 0; |
| 7303 | }; |
| 7304 | __initcall(io_uring_init); |