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> |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 47 | #include <net/compat.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 48 | #include <linux/refcount.h> |
| 49 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 50 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/sched/signal.h> |
| 53 | #include <linux/fs.h> |
| 54 | #include <linux/file.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/mm.h> |
| 57 | #include <linux/mman.h> |
| 58 | #include <linux/mmu_context.h> |
| 59 | #include <linux/percpu.h> |
| 60 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 61 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 63 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 64 | #include <linux/net.h> |
| 65 | #include <net/sock.h> |
| 66 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 67 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 68 | #include <linux/anon_inodes.h> |
| 69 | #include <linux/sched/mm.h> |
| 70 | #include <linux/uaccess.h> |
| 71 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 72 | #include <linux/sizes.h> |
| 73 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 74 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 75 | #include <linux/namei.h> |
| 76 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 77 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 78 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 79 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 80 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 81 | #include <linux/task_work.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 82 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 83 | #define CREATE_TRACE_POINTS |
| 84 | #include <trace/events/io_uring.h> |
| 85 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 86 | #include <uapi/linux/io_uring.h> |
| 87 | |
| 88 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 89 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 90 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 91 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 92 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 96 | */ |
| 97 | #define IORING_FILE_TABLE_SHIFT 9 |
| 98 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 99 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 100 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 101 | |
| 102 | struct io_uring { |
| 103 | u32 head ____cacheline_aligned_in_smp; |
| 104 | u32 tail ____cacheline_aligned_in_smp; |
| 105 | }; |
| 106 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 107 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 108 | * This data is shared with the application through the mmap at offsets |
| 109 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 110 | * |
| 111 | * The offsets to the member fields are published through struct |
| 112 | * io_sqring_offsets when calling io_uring_setup. |
| 113 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 114 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | /* |
| 116 | * Head and tail offsets into the ring; the offsets need to be |
| 117 | * masked to get valid indices. |
| 118 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 120 | * and the application controls tail of the sq ring and the head of the |
| 121 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 122 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 123 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 125 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 126 | * ring_entries - 1) |
| 127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | u32 sq_ring_mask, cq_ring_mask; |
| 129 | /* Ring sizes (constant, power of 2) */ |
| 130 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Number of invalid entries dropped by the kernel due to |
| 133 | * invalid index stored in array |
| 134 | * |
| 135 | * Written by the kernel, shouldn't be modified by the |
| 136 | * application (i.e. get number of "new events" by comparing to |
| 137 | * cached value). |
| 138 | * |
| 139 | * After a new SQ head value was read by the application this |
| 140 | * counter includes all submissions that were dropped reaching |
| 141 | * the new SQ head (and possibly more). |
| 142 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 143 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 144 | /* |
| 145 | * Runtime flags |
| 146 | * |
| 147 | * Written by the kernel, shouldn't be modified by the |
| 148 | * application. |
| 149 | * |
| 150 | * The application needs a full memory barrier before checking |
| 151 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 152 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 153 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Number of completion events lost because the queue was full; |
| 156 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 157 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 158 | * the completion queue. |
| 159 | * |
| 160 | * Written by the kernel, shouldn't be modified by the |
| 161 | * application (i.e. get number of "new events" by comparing to |
| 162 | * cached value). |
| 163 | * |
| 164 | * As completion events come in out of order this counter is not |
| 165 | * ordered with any other data. |
| 166 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 167 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Ring buffer of completion events. |
| 170 | * |
| 171 | * The kernel writes completion events fresh every time they are |
| 172 | * produced, so the application is allowed to modify pending |
| 173 | * entries. |
| 174 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 175 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 178 | struct io_mapped_ubuf { |
| 179 | u64 ubuf; |
| 180 | size_t len; |
| 181 | struct bio_vec *bvec; |
| 182 | unsigned int nr_bvecs; |
| 183 | }; |
| 184 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 185 | struct fixed_file_table { |
| 186 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 189 | struct fixed_file_data { |
| 190 | struct fixed_file_table *table; |
| 191 | struct io_ring_ctx *ctx; |
| 192 | |
| 193 | struct percpu_ref refs; |
| 194 | struct llist_head put_llist; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 195 | struct work_struct ref_work; |
| 196 | struct completion done; |
| 197 | }; |
| 198 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 199 | struct io_buffer { |
| 200 | struct list_head list; |
| 201 | __u64 addr; |
| 202 | __s32 len; |
| 203 | __u16 bid; |
| 204 | }; |
| 205 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 206 | struct io_ring_ctx { |
| 207 | struct { |
| 208 | struct percpu_ref refs; |
| 209 | } ____cacheline_aligned_in_smp; |
| 210 | |
| 211 | struct { |
| 212 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 213 | unsigned int compat: 1; |
| 214 | unsigned int account_mem: 1; |
| 215 | unsigned int cq_overflow_flushed: 1; |
| 216 | unsigned int drain_next: 1; |
| 217 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 218 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 219 | /* |
| 220 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 221 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 222 | * |
| 223 | * This indirection could e.g. be used to assign fixed |
| 224 | * io_uring_sqe entries to operations and only submit them to |
| 225 | * the queue when needed. |
| 226 | * |
| 227 | * The kernel modifies neither the indices array nor the entries |
| 228 | * array. |
| 229 | */ |
| 230 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 231 | unsigned cached_sq_head; |
| 232 | unsigned sq_entries; |
| 233 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 234 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 235 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 236 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 237 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 238 | |
| 239 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 240 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 241 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 242 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 243 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 244 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 245 | } ____cacheline_aligned_in_smp; |
| 246 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 247 | struct io_rings *rings; |
| 248 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 249 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 250 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 251 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 252 | struct mm_struct *sqo_mm; |
| 253 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 254 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 255 | /* |
| 256 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 257 | * readers must ensure that ->refs is alive as long as the file* is |
| 258 | * used. Only updated through io_uring_register(2). |
| 259 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 260 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 261 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 262 | int ring_fd; |
| 263 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 264 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 265 | /* if used, fixed mapped user buffers */ |
| 266 | unsigned nr_user_bufs; |
| 267 | struct io_mapped_ubuf *user_bufs; |
| 268 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 269 | struct user_struct *user; |
| 270 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 271 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 272 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 273 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 274 | struct completion *completions; |
| 275 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 276 | /* if all else fails... */ |
| 277 | struct io_kiocb *fallback_req; |
| 278 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 279 | #if defined(CONFIG_UNIX) |
| 280 | struct socket *ring_sock; |
| 281 | #endif |
| 282 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 283 | struct idr io_buffer_idr; |
| 284 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 285 | struct idr personality_idr; |
| 286 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 287 | struct { |
| 288 | unsigned cached_cq_tail; |
| 289 | unsigned cq_entries; |
| 290 | unsigned cq_mask; |
| 291 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 292 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 293 | struct wait_queue_head cq_wait; |
| 294 | struct fasync_struct *cq_fasync; |
| 295 | struct eventfd_ctx *cq_ev_fd; |
| 296 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 297 | |
| 298 | struct { |
| 299 | struct mutex uring_lock; |
| 300 | wait_queue_head_t wait; |
| 301 | } ____cacheline_aligned_in_smp; |
| 302 | |
| 303 | struct { |
| 304 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 305 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 306 | /* |
| 307 | * ->poll_list is protected by the ctx->uring_lock for |
| 308 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 309 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 310 | * manipulate the list, hence no extra locking is needed there. |
| 311 | */ |
| 312 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 313 | struct hlist_head *cancel_hash; |
| 314 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 315 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 316 | |
| 317 | spinlock_t inflight_lock; |
| 318 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 319 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 322 | /* |
| 323 | * First field must be the file pointer in all the |
| 324 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 325 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 326 | struct io_poll_iocb { |
| 327 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 328 | union { |
| 329 | struct wait_queue_head *head; |
| 330 | u64 addr; |
| 331 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 332 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 333 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 334 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 335 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 338 | struct io_close { |
| 339 | struct file *file; |
| 340 | struct file *put_file; |
| 341 | int fd; |
| 342 | }; |
| 343 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 344 | struct io_timeout_data { |
| 345 | struct io_kiocb *req; |
| 346 | struct hrtimer timer; |
| 347 | struct timespec64 ts; |
| 348 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 349 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 350 | }; |
| 351 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 352 | struct io_accept { |
| 353 | struct file *file; |
| 354 | struct sockaddr __user *addr; |
| 355 | int __user *addr_len; |
| 356 | int flags; |
| 357 | }; |
| 358 | |
| 359 | struct io_sync { |
| 360 | struct file *file; |
| 361 | loff_t len; |
| 362 | loff_t off; |
| 363 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 364 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 367 | struct io_cancel { |
| 368 | struct file *file; |
| 369 | u64 addr; |
| 370 | }; |
| 371 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 372 | struct io_timeout { |
| 373 | struct file *file; |
| 374 | u64 addr; |
| 375 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 376 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 379 | struct io_rw { |
| 380 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 381 | struct kiocb kiocb; |
| 382 | u64 addr; |
| 383 | u64 len; |
| 384 | }; |
| 385 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 386 | struct io_connect { |
| 387 | struct file *file; |
| 388 | struct sockaddr __user *addr; |
| 389 | int addr_len; |
| 390 | }; |
| 391 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 392 | struct io_sr_msg { |
| 393 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 394 | union { |
| 395 | struct user_msghdr __user *msg; |
| 396 | void __user *buf; |
| 397 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 398 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 399 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 400 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 401 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 404 | struct io_open { |
| 405 | struct file *file; |
| 406 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 407 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 408 | unsigned mask; |
| 409 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 410 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 411 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 412 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 415 | struct io_files_update { |
| 416 | struct file *file; |
| 417 | u64 arg; |
| 418 | u32 nr_args; |
| 419 | u32 offset; |
| 420 | }; |
| 421 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 422 | struct io_fadvise { |
| 423 | struct file *file; |
| 424 | u64 offset; |
| 425 | u32 len; |
| 426 | u32 advice; |
| 427 | }; |
| 428 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 429 | struct io_madvise { |
| 430 | struct file *file; |
| 431 | u64 addr; |
| 432 | u32 len; |
| 433 | u32 advice; |
| 434 | }; |
| 435 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 436 | struct io_epoll { |
| 437 | struct file *file; |
| 438 | int epfd; |
| 439 | int op; |
| 440 | int fd; |
| 441 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 444 | struct io_splice { |
| 445 | struct file *file_out; |
| 446 | struct file *file_in; |
| 447 | loff_t off_out; |
| 448 | loff_t off_in; |
| 449 | u64 len; |
| 450 | unsigned int flags; |
| 451 | }; |
| 452 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 453 | struct io_provide_buf { |
| 454 | struct file *file; |
| 455 | __u64 addr; |
| 456 | __s32 len; |
| 457 | __u32 bgid; |
| 458 | __u16 nbufs; |
| 459 | __u16 bid; |
| 460 | }; |
| 461 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 462 | struct io_async_connect { |
| 463 | struct sockaddr_storage address; |
| 464 | }; |
| 465 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 466 | struct io_async_msghdr { |
| 467 | struct iovec fast_iov[UIO_FASTIOV]; |
| 468 | struct iovec *iov; |
| 469 | struct sockaddr __user *uaddr; |
| 470 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 471 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 474 | struct io_async_rw { |
| 475 | struct iovec fast_iov[UIO_FASTIOV]; |
| 476 | struct iovec *iov; |
| 477 | ssize_t nr_segs; |
| 478 | ssize_t size; |
| 479 | }; |
| 480 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 481 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 482 | union { |
| 483 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 484 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 485 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 486 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 487 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 488 | }; |
| 489 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 490 | enum { |
| 491 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 492 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 493 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 494 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 495 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 496 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 497 | |
| 498 | REQ_F_LINK_NEXT_BIT, |
| 499 | REQ_F_FAIL_LINK_BIT, |
| 500 | REQ_F_INFLIGHT_BIT, |
| 501 | REQ_F_CUR_POS_BIT, |
| 502 | REQ_F_NOWAIT_BIT, |
| 503 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 504 | REQ_F_LINK_TIMEOUT_BIT, |
| 505 | REQ_F_TIMEOUT_BIT, |
| 506 | REQ_F_ISREG_BIT, |
| 507 | REQ_F_MUST_PUNT_BIT, |
| 508 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 509 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 510 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 511 | REQ_F_OVERFLOW_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 512 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 513 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | enum { |
| 517 | /* ctx owns file */ |
| 518 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 519 | /* drain existing IO first */ |
| 520 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 521 | /* linked sqes */ |
| 522 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 523 | /* doesn't sever on completion < 0 */ |
| 524 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 525 | /* IOSQE_ASYNC */ |
| 526 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 527 | /* IOSQE_BUFFER_SELECT */ |
| 528 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 529 | |
| 530 | /* already grabbed next link */ |
| 531 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 532 | /* fail rest of links */ |
| 533 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 534 | /* on inflight list */ |
| 535 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 536 | /* read/write uses file position */ |
| 537 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 538 | /* must not punt to workers */ |
| 539 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 540 | /* polled IO has completed */ |
| 541 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 542 | /* has linked timeout */ |
| 543 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 544 | /* timeout request */ |
| 545 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 546 | /* regular file */ |
| 547 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 548 | /* must be punted even for NONBLOCK */ |
| 549 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 550 | /* no timeout sequence */ |
| 551 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 552 | /* completion under lock */ |
| 553 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 554 | /* needs cleanup */ |
| 555 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 556 | /* in overflow list */ |
| 557 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 558 | /* already went through poll handler */ |
| 559 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 560 | /* buffer already selected */ |
| 561 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | struct async_poll { |
| 565 | struct io_poll_iocb poll; |
| 566 | struct io_wq_work work; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 567 | }; |
| 568 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 569 | /* |
| 570 | * NOTE! Each of the iocb union members has the file pointer |
| 571 | * as the first entry in their struct definition. So you can |
| 572 | * access the file pointer through any of the sub-structs, |
| 573 | * or directly as just 'ki_filp' in this struct. |
| 574 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 575 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 576 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 577 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 578 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 579 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 580 | struct io_accept accept; |
| 581 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 582 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 583 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 584 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 585 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 586 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 587 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 588 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 589 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 590 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 591 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 592 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 593 | struct io_provide_buf pbuf; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 594 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 595 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 596 | struct io_async_ctx *io; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 597 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 598 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 599 | |
| 600 | struct io_ring_ctx *ctx; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 601 | struct list_head list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 602 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 603 | refcount_t refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 604 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 605 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 606 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 607 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 608 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 609 | struct list_head link_list; |
| 610 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 611 | struct list_head inflight_entry; |
| 612 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 613 | union { |
| 614 | /* |
| 615 | * Only commands that never go async can use the below fields, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 616 | * obviously. Right now only IORING_OP_POLL_ADD uses them, and |
| 617 | * async armed poll handlers for regular commands. The latter |
| 618 | * restore the work, if needed. |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 619 | */ |
| 620 | struct { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 621 | struct callback_head task_work; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 622 | struct hlist_node hash_node; |
| 623 | struct async_poll *apoll; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 624 | int cflags; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 625 | }; |
| 626 | struct io_wq_work work; |
| 627 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 631 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 632 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 633 | struct io_submit_state { |
| 634 | struct blk_plug plug; |
| 635 | |
| 636 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 637 | * io_kiocb alloc cache |
| 638 | */ |
| 639 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 640 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 641 | |
| 642 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 643 | * File reference cache |
| 644 | */ |
| 645 | struct file *file; |
| 646 | unsigned int fd; |
| 647 | unsigned int has_refs; |
| 648 | unsigned int used_refs; |
| 649 | unsigned int ios_left; |
| 650 | }; |
| 651 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 652 | struct io_op_def { |
| 653 | /* needs req->io allocated for deferral/async */ |
| 654 | unsigned async_ctx : 1; |
| 655 | /* needs current->mm setup, does mm access */ |
| 656 | unsigned needs_mm : 1; |
| 657 | /* needs req->file assigned */ |
| 658 | unsigned needs_file : 1; |
| 659 | /* needs req->file assigned IFF fd is >= 0 */ |
| 660 | unsigned fd_non_neg : 1; |
| 661 | /* hash wq insertion if file is a regular file */ |
| 662 | unsigned hash_reg_file : 1; |
| 663 | /* unbound wq insertion if file is a non-regular file */ |
| 664 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 665 | /* opcode is not supported by this kernel */ |
| 666 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 667 | /* needs file table */ |
| 668 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 669 | /* needs ->fs */ |
| 670 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 671 | /* set if opcode supports polled "wait" */ |
| 672 | unsigned pollin : 1; |
| 673 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 674 | /* op supports buffer selection */ |
| 675 | unsigned buffer_select : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 679 | [IORING_OP_NOP] = {}, |
| 680 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 681 | .async_ctx = 1, |
| 682 | .needs_mm = 1, |
| 683 | .needs_file = 1, |
| 684 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 685 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 686 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 687 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 688 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 689 | .async_ctx = 1, |
| 690 | .needs_mm = 1, |
| 691 | .needs_file = 1, |
| 692 | .hash_reg_file = 1, |
| 693 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 694 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 695 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 696 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 697 | .needs_file = 1, |
| 698 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 699 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 700 | .needs_file = 1, |
| 701 | .unbound_nonreg_file = 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_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 705 | .needs_file = 1, |
| 706 | .hash_reg_file = 1, |
| 707 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 708 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 709 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 710 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 711 | .needs_file = 1, |
| 712 | .unbound_nonreg_file = 1, |
| 713 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 714 | [IORING_OP_POLL_REMOVE] = {}, |
| 715 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 716 | .needs_file = 1, |
| 717 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 718 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 719 | .async_ctx = 1, |
| 720 | .needs_mm = 1, |
| 721 | .needs_file = 1, |
| 722 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 723 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 724 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 725 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 726 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 727 | .async_ctx = 1, |
| 728 | .needs_mm = 1, |
| 729 | .needs_file = 1, |
| 730 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 731 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 732 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 733 | .buffer_select = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 734 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 735 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 736 | .async_ctx = 1, |
| 737 | .needs_mm = 1, |
| 738 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 739 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 740 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 741 | .needs_mm = 1, |
| 742 | .needs_file = 1, |
| 743 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 744 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 745 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 746 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 747 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 748 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 749 | .async_ctx = 1, |
| 750 | .needs_mm = 1, |
| 751 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 752 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 753 | .async_ctx = 1, |
| 754 | .needs_mm = 1, |
| 755 | .needs_file = 1, |
| 756 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 757 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 758 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 759 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 760 | .needs_file = 1, |
| 761 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 762 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 763 | .needs_file = 1, |
| 764 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 765 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 766 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 767 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 768 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 769 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 770 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 771 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 772 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 773 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 774 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 775 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 776 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 777 | .needs_mm = 1, |
| 778 | .needs_file = 1, |
| 779 | .fd_non_neg = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 780 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 781 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 782 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 783 | .needs_mm = 1, |
| 784 | .needs_file = 1, |
| 785 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 786 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 787 | .buffer_select = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 788 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 789 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 790 | .needs_mm = 1, |
| 791 | .needs_file = 1, |
| 792 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 793 | .pollout = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 794 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 795 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 796 | .needs_file = 1, |
| 797 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 798 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 799 | .needs_mm = 1, |
| 800 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 801 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 802 | .needs_mm = 1, |
| 803 | .needs_file = 1, |
| 804 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 805 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 806 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 807 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 808 | .needs_mm = 1, |
| 809 | .needs_file = 1, |
| 810 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 811 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 812 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 813 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 814 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 815 | .needs_file = 1, |
| 816 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 817 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 818 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 819 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 820 | [IORING_OP_EPOLL_CTL] = { |
| 821 | .unbound_nonreg_file = 1, |
| 822 | .file_table = 1, |
| 823 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 824 | [IORING_OP_SPLICE] = { |
| 825 | .needs_file = 1, |
| 826 | .hash_reg_file = 1, |
| 827 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 828 | }, |
| 829 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame^] | 830 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 831 | }; |
| 832 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 833 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 834 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 835 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 836 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 837 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 838 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 839 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 840 | struct io_uring_files_update *ip, |
| 841 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 842 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 843 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 844 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 845 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 846 | int fd, struct file **out_file, bool fixed); |
| 847 | static void __io_queue_sqe(struct io_kiocb *req, |
| 848 | const struct io_uring_sqe *sqe); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 849 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 850 | static struct kmem_cache *req_cachep; |
| 851 | |
| 852 | static const struct file_operations io_uring_fops; |
| 853 | |
| 854 | struct sock *io_uring_get_socket(struct file *file) |
| 855 | { |
| 856 | #if defined(CONFIG_UNIX) |
| 857 | if (file->f_op == &io_uring_fops) { |
| 858 | struct io_ring_ctx *ctx = file->private_data; |
| 859 | |
| 860 | return ctx->ring_sock->sk; |
| 861 | } |
| 862 | #endif |
| 863 | return NULL; |
| 864 | } |
| 865 | EXPORT_SYMBOL(io_uring_get_socket); |
| 866 | |
| 867 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 868 | { |
| 869 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 870 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 871 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 875 | { |
| 876 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 877 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 878 | |
| 879 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 880 | if (!ctx) |
| 881 | return NULL; |
| 882 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 883 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 884 | if (!ctx->fallback_req) |
| 885 | goto err; |
| 886 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 887 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 888 | if (!ctx->completions) |
| 889 | goto err; |
| 890 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 891 | /* |
| 892 | * Use 5 bits less than the max cq entries, that should give us around |
| 893 | * 32 entries per hash list if totally full and uniformly spread. |
| 894 | */ |
| 895 | hash_bits = ilog2(p->cq_entries); |
| 896 | hash_bits -= 5; |
| 897 | if (hash_bits <= 0) |
| 898 | hash_bits = 1; |
| 899 | ctx->cancel_hash_bits = hash_bits; |
| 900 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 901 | GFP_KERNEL); |
| 902 | if (!ctx->cancel_hash) |
| 903 | goto err; |
| 904 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 905 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 906 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 907 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 908 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 909 | |
| 910 | ctx->flags = p->flags; |
| 911 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 912 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 913 | init_completion(&ctx->completions[0]); |
| 914 | init_completion(&ctx->completions[1]); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 915 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 916 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 917 | mutex_init(&ctx->uring_lock); |
| 918 | init_waitqueue_head(&ctx->wait); |
| 919 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 920 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 921 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 922 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 923 | init_waitqueue_head(&ctx->inflight_wait); |
| 924 | spin_lock_init(&ctx->inflight_lock); |
| 925 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 926 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 927 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 928 | if (ctx->fallback_req) |
| 929 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 930 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 931 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 932 | kfree(ctx); |
| 933 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 936 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 937 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 938 | struct io_ring_ctx *ctx = req->ctx; |
| 939 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 940 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 941 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 942 | } |
| 943 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 944 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 945 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 946 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 947 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 948 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 949 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | 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] | 953 | { |
| 954 | struct io_kiocb *req; |
| 955 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 956 | 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] | 957 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 958 | list_del_init(&req->list); |
| 959 | return req; |
| 960 | } |
| 961 | |
| 962 | return NULL; |
| 963 | } |
| 964 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 965 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 966 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 967 | struct io_kiocb *req; |
| 968 | |
| 969 | 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] | 970 | if (req) { |
| 971 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 972 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 973 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 974 | list_del_init(&req->list); |
| 975 | return req; |
| 976 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 980 | } |
| 981 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 982 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 983 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 984 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 985 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 986 | /* order cqe stores with ring update */ |
| 987 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 988 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 989 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 990 | wake_up_interruptible(&ctx->cq_wait); |
| 991 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 995 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 996 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 997 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 998 | if (!req->work.mm && def->needs_mm) { |
| 999 | mmgrab(current->mm); |
| 1000 | req->work.mm = current->mm; |
| 1001 | } |
| 1002 | if (!req->work.creds) |
| 1003 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1004 | if (!req->work.fs && def->needs_fs) { |
| 1005 | spin_lock(¤t->fs->lock); |
| 1006 | if (!current->fs->in_exec) { |
| 1007 | req->work.fs = current->fs; |
| 1008 | req->work.fs->users++; |
| 1009 | } else { |
| 1010 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1011 | } |
| 1012 | spin_unlock(¤t->fs->lock); |
| 1013 | } |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 1014 | if (!req->work.task_pid) |
| 1015 | req->work.task_pid = task_pid_vnr(current); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 1019 | { |
| 1020 | if (req->work.mm) { |
| 1021 | mmdrop(req->work.mm); |
| 1022 | req->work.mm = NULL; |
| 1023 | } |
| 1024 | if (req->work.creds) { |
| 1025 | put_cred(req->work.creds); |
| 1026 | req->work.creds = NULL; |
| 1027 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1028 | if (req->work.fs) { |
| 1029 | struct fs_struct *fs = req->work.fs; |
| 1030 | |
| 1031 | spin_lock(&req->work.fs->lock); |
| 1032 | if (--fs->users) |
| 1033 | fs = NULL; |
| 1034 | spin_unlock(&req->work.fs->lock); |
| 1035 | if (fs) |
| 1036 | free_fs_struct(fs); |
| 1037 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1038 | } |
| 1039 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1040 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 1041 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1042 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1043 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1044 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1045 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1046 | if (req->flags & REQ_F_ISREG) { |
| 1047 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1048 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1049 | } else { |
| 1050 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1051 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1052 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1053 | |
| 1054 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1055 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1056 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1057 | return do_hashed; |
| 1058 | } |
| 1059 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1060 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1061 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1062 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1063 | struct io_kiocb *link; |
| 1064 | bool do_hashed; |
| 1065 | |
| 1066 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1067 | |
| 1068 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 1069 | req->flags); |
| 1070 | if (!do_hashed) { |
| 1071 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 1072 | } else { |
| 1073 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 1074 | file_inode(req->file)); |
| 1075 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1076 | |
| 1077 | if (link) |
| 1078 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1079 | } |
| 1080 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1081 | static void io_kill_timeout(struct io_kiocb *req) |
| 1082 | { |
| 1083 | int ret; |
| 1084 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1085 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1086 | if (ret != -1) { |
| 1087 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1088 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1089 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1090 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1095 | { |
| 1096 | struct io_kiocb *req, *tmp; |
| 1097 | |
| 1098 | spin_lock_irq(&ctx->completion_lock); |
| 1099 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1100 | io_kill_timeout(req); |
| 1101 | spin_unlock_irq(&ctx->completion_lock); |
| 1102 | } |
| 1103 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1104 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1105 | { |
| 1106 | struct io_kiocb *req; |
| 1107 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1108 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 1109 | io_kill_timeout(req); |
| 1110 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1111 | __io_commit_cqring(ctx); |
| 1112 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1113 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1114 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1115 | } |
| 1116 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1117 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1118 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1119 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1120 | unsigned tail; |
| 1121 | |
| 1122 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1123 | /* |
| 1124 | * writes to the cq entry need to come after reading head; the |
| 1125 | * control dependency is enough as we're using WRITE_ONCE to |
| 1126 | * fill the cq entry |
| 1127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1128 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1129 | return NULL; |
| 1130 | |
| 1131 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1132 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1135 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1136 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1137 | if (!ctx->cq_ev_fd) |
| 1138 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1139 | if (!ctx->eventfd_async) |
| 1140 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1141 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1144 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1145 | { |
| 1146 | if (waitqueue_active(&ctx->wait)) |
| 1147 | wake_up(&ctx->wait); |
| 1148 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1149 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1150 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1151 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1152 | } |
| 1153 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1154 | /* Returns true if there are no backlogged entries after the flush */ |
| 1155 | 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] | 1156 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1157 | struct io_rings *rings = ctx->rings; |
| 1158 | struct io_uring_cqe *cqe; |
| 1159 | struct io_kiocb *req; |
| 1160 | unsigned long flags; |
| 1161 | LIST_HEAD(list); |
| 1162 | |
| 1163 | if (!force) { |
| 1164 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1165 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1166 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1167 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1168 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1172 | |
| 1173 | /* if force is set, the ring is going away. always drop after that */ |
| 1174 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1175 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1176 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1177 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1178 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1179 | cqe = io_get_cqring(ctx); |
| 1180 | if (!cqe && !force) |
| 1181 | break; |
| 1182 | |
| 1183 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1184 | list); |
| 1185 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1186 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1187 | if (cqe) { |
| 1188 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1189 | WRITE_ONCE(cqe->res, req->result); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1190 | WRITE_ONCE(cqe->flags, req->cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1191 | } else { |
| 1192 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1193 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1198 | if (cqe) { |
| 1199 | clear_bit(0, &ctx->sq_check_overflow); |
| 1200 | clear_bit(0, &ctx->cq_check_overflow); |
| 1201 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1202 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1203 | io_cqring_ev_posted(ctx); |
| 1204 | |
| 1205 | while (!list_empty(&list)) { |
| 1206 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1207 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1208 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1209 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1210 | |
| 1211 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1214 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1215 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1216 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1217 | struct io_uring_cqe *cqe; |
| 1218 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1219 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1220 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1221 | /* |
| 1222 | * If we can't get a cq entry, userspace overflowed the |
| 1223 | * submission (by quite a lot). Increment the overflow count in |
| 1224 | * the ring. |
| 1225 | */ |
| 1226 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1227 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1228 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1229 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1230 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1231 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1232 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1233 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1234 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1235 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1236 | set_bit(0, &ctx->sq_check_overflow); |
| 1237 | set_bit(0, &ctx->cq_check_overflow); |
| 1238 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1239 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1240 | refcount_inc(&req->refs); |
| 1241 | req->result = res; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1242 | req->cflags = cflags; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1243 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1247 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1248 | { |
| 1249 | __io_cqring_fill_event(req, res, 0); |
| 1250 | } |
| 1251 | |
| 1252 | static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1253 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1254 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1255 | unsigned long flags; |
| 1256 | |
| 1257 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1258 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1259 | io_commit_cqring(ctx); |
| 1260 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1261 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1262 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1265 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
| 1266 | { |
| 1267 | __io_cqring_add_event(req, res, 0); |
| 1268 | } |
| 1269 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1270 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1271 | { |
| 1272 | return req == (struct io_kiocb *) |
| 1273 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1274 | } |
| 1275 | |
| 1276 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1277 | { |
| 1278 | struct io_kiocb *req; |
| 1279 | |
| 1280 | req = ctx->fallback_req; |
| 1281 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1282 | return req; |
| 1283 | |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1287 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1288 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1289 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1290 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1291 | struct io_kiocb *req; |
| 1292 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1293 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1294 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1295 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1296 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1297 | } else if (!state->free_reqs) { |
| 1298 | size_t sz; |
| 1299 | int ret; |
| 1300 | |
| 1301 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1302 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1303 | |
| 1304 | /* |
| 1305 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1306 | * retry single alloc to be on the safe side. |
| 1307 | */ |
| 1308 | if (unlikely(ret <= 0)) { |
| 1309 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1310 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1311 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1312 | ret = 1; |
| 1313 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1314 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1315 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1316 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1317 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1318 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1321 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1322 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1323 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1324 | req->ctx = ctx; |
| 1325 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1326 | /* one is dropped after submission, the other at completion */ |
| 1327 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1328 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1329 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1330 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1331 | fallback: |
| 1332 | req = io_get_fallback_req(ctx); |
| 1333 | if (req) |
| 1334 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1335 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1336 | return NULL; |
| 1337 | } |
| 1338 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1339 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1340 | bool fixed) |
| 1341 | { |
| 1342 | if (fixed) |
| 1343 | percpu_ref_put(&req->ctx->file_data->refs); |
| 1344 | else |
| 1345 | fput(file); |
| 1346 | } |
| 1347 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1348 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1349 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1350 | if (likely(!io_is_fallback_req(req))) |
| 1351 | kmem_cache_free(req_cachep, req); |
| 1352 | else |
| 1353 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1354 | } |
| 1355 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1356 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1357 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1358 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1359 | io_cleanup_req(req); |
| 1360 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1361 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1362 | if (req->file) |
| 1363 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1364 | |
| 1365 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | static void __io_free_req(struct io_kiocb *req) |
| 1369 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1370 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1371 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1372 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1373 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1374 | unsigned long flags; |
| 1375 | |
| 1376 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1377 | list_del(&req->inflight_entry); |
| 1378 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1379 | wake_up(&ctx->inflight_wait); |
| 1380 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1381 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1382 | |
| 1383 | percpu_ref_put(&req->ctx->refs); |
| 1384 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1385 | } |
| 1386 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1387 | struct req_batch { |
| 1388 | void *reqs[IO_IOPOLL_BATCH]; |
| 1389 | int to_free; |
| 1390 | int need_iter; |
| 1391 | }; |
| 1392 | |
| 1393 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1394 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1395 | int fixed_refs = rb->to_free; |
| 1396 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1397 | if (!rb->to_free) |
| 1398 | return; |
| 1399 | if (rb->need_iter) { |
| 1400 | int i, inflight = 0; |
| 1401 | unsigned long flags; |
| 1402 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1403 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1404 | for (i = 0; i < rb->to_free; i++) { |
| 1405 | struct io_kiocb *req = rb->reqs[i]; |
| 1406 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1407 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1408 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1409 | fixed_refs++; |
| 1410 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1411 | if (req->flags & REQ_F_INFLIGHT) |
| 1412 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1413 | __io_req_aux_free(req); |
| 1414 | } |
| 1415 | if (!inflight) |
| 1416 | goto do_free; |
| 1417 | |
| 1418 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1419 | for (i = 0; i < rb->to_free; i++) { |
| 1420 | struct io_kiocb *req = rb->reqs[i]; |
| 1421 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1422 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1423 | list_del(&req->inflight_entry); |
| 1424 | if (!--inflight) |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1429 | |
| 1430 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1431 | wake_up(&ctx->inflight_wait); |
| 1432 | } |
| 1433 | do_free: |
| 1434 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1435 | if (fixed_refs) |
| 1436 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1437 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1438 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1439 | } |
| 1440 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1441 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1442 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1443 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1444 | int ret; |
| 1445 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1446 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1447 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1448 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1449 | io_commit_cqring(ctx); |
| 1450 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1451 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1452 | return true; |
| 1453 | } |
| 1454 | |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1458 | 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] | 1459 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1460 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1461 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1462 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1463 | /* Already got next link */ |
| 1464 | if (req->flags & REQ_F_LINK_NEXT) |
| 1465 | return; |
| 1466 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1467 | /* |
| 1468 | * The list should never be empty when we are called here. But could |
| 1469 | * potentially happen if the chain is messed up, check to be on the |
| 1470 | * safe side. |
| 1471 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1472 | while (!list_empty(&req->link_list)) { |
| 1473 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1474 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1475 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1476 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1477 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1478 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1479 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1480 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1481 | continue; |
| 1482 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1483 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1484 | list_del_init(&req->link_list); |
| 1485 | if (!list_empty(&nxt->link_list)) |
| 1486 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1487 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1488 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1489 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1490 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1491 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1492 | if (wake_ev) |
| 1493 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1498 | */ |
| 1499 | static void io_fail_links(struct io_kiocb *req) |
| 1500 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1501 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1502 | unsigned long flags; |
| 1503 | |
| 1504 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1505 | |
| 1506 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1507 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1508 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1509 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1510 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1511 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1512 | |
| 1513 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1514 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1515 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1516 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1517 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1518 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1519 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1520 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1521 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1522 | |
| 1523 | io_commit_cqring(ctx); |
| 1524 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1525 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1526 | } |
| 1527 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1528 | 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] | 1529 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1530 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1531 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1532 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1533 | /* |
| 1534 | * If LINK is set, we have dependent requests in this chain. If we |
| 1535 | * didn't fail this request, queue the first one up, moving any other |
| 1536 | * dependencies to the next request. In case of failure, fail the rest |
| 1537 | * of the chain. |
| 1538 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1539 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1540 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1541 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1542 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1543 | struct io_ring_ctx *ctx = req->ctx; |
| 1544 | unsigned long flags; |
| 1545 | |
| 1546 | /* |
| 1547 | * If this is a timeout link, we could be racing with the |
| 1548 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1549 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1550 | */ |
| 1551 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1552 | io_req_link_next(req, nxt); |
| 1553 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1554 | } else { |
| 1555 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1556 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1557 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1558 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1559 | static void io_free_req(struct io_kiocb *req) |
| 1560 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1561 | struct io_kiocb *nxt = NULL; |
| 1562 | |
| 1563 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1564 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1565 | |
| 1566 | if (nxt) |
| 1567 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1568 | } |
| 1569 | |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1570 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 1571 | { |
| 1572 | struct io_wq_work *work = *workptr; |
| 1573 | struct io_kiocb *link = work->data; |
| 1574 | |
| 1575 | io_queue_linked_timeout(link); |
| 1576 | io_wq_submit_work(workptr); |
| 1577 | } |
| 1578 | |
| 1579 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 1580 | { |
| 1581 | struct io_kiocb *link; |
| 1582 | |
| 1583 | *workptr = &nxt->work; |
| 1584 | link = io_prep_linked_timeout(nxt); |
| 1585 | if (link) { |
| 1586 | nxt->work.func = io_link_work_cb; |
| 1587 | nxt->work.data = link; |
| 1588 | } |
| 1589 | } |
| 1590 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1591 | /* |
| 1592 | * Drop reference to request, return next in chain (if there is one) if this |
| 1593 | * was the last reference to this request. |
| 1594 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1595 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1596 | 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] | 1597 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1598 | if (refcount_dec_and_test(&req->refs)) { |
| 1599 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1600 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1601 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1604 | static void io_put_req(struct io_kiocb *req) |
| 1605 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1606 | if (refcount_dec_and_test(&req->refs)) |
| 1607 | io_free_req(req); |
| 1608 | } |
| 1609 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1610 | static void io_steal_work(struct io_kiocb *req, |
| 1611 | struct io_wq_work **workptr) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1612 | { |
| 1613 | /* |
| 1614 | * It's in an io-wq worker, so there always should be at least |
| 1615 | * one reference, which will be dropped in io_put_work() just |
| 1616 | * after the current handler returns. |
| 1617 | * |
| 1618 | * It also means, that if the counter dropped to 1, then there is |
| 1619 | * no asynchronous users left, so it's safe to steal the next work. |
| 1620 | */ |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1621 | if (refcount_read(&req->refs) == 1) { |
| 1622 | struct io_kiocb *nxt = NULL; |
| 1623 | |
| 1624 | io_req_find_next(req, &nxt); |
| 1625 | if (nxt) |
| 1626 | io_wq_assign_next(workptr, nxt); |
| 1627 | } |
| 1628 | } |
| 1629 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1630 | /* |
| 1631 | * Must only be used if we don't need to care about links, usually from |
| 1632 | * within the completion handling itself. |
| 1633 | */ |
| 1634 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1635 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1636 | /* drop both submit and complete references */ |
| 1637 | if (refcount_sub_and_test(2, &req->refs)) |
| 1638 | __io_free_req(req); |
| 1639 | } |
| 1640 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1641 | static void io_double_put_req(struct io_kiocb *req) |
| 1642 | { |
| 1643 | /* drop both submit and complete references */ |
| 1644 | if (refcount_sub_and_test(2, &req->refs)) |
| 1645 | io_free_req(req); |
| 1646 | } |
| 1647 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1648 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1649 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1650 | struct io_rings *rings = ctx->rings; |
| 1651 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1652 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1653 | /* |
| 1654 | * noflush == true is from the waitqueue handler, just ensure |
| 1655 | * we wake up the task, and the next invocation will flush the |
| 1656 | * entries. We cannot safely to it from here. |
| 1657 | */ |
| 1658 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1659 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1660 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1661 | io_cqring_overflow_flush(ctx, false); |
| 1662 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1663 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1664 | /* See comment at the top of this file */ |
| 1665 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1666 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1667 | } |
| 1668 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1669 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1670 | { |
| 1671 | struct io_rings *rings = ctx->rings; |
| 1672 | |
| 1673 | /* make sure SQ entry isn't read before tail */ |
| 1674 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1675 | } |
| 1676 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1677 | 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] | 1678 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1679 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1680 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1681 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1682 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1683 | rb->need_iter++; |
| 1684 | |
| 1685 | rb->reqs[rb->to_free++] = req; |
| 1686 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1687 | io_free_req_many(req->ctx, rb); |
| 1688 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1691 | static int io_put_kbuf(struct io_kiocb *req) |
| 1692 | { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 1693 | struct io_buffer *kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1694 | int cflags; |
| 1695 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 1696 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1697 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1698 | cflags |= IORING_CQE_F_BUFFER; |
| 1699 | req->rw.addr = 0; |
| 1700 | kfree(kbuf); |
| 1701 | return cflags; |
| 1702 | } |
| 1703 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1704 | /* |
| 1705 | * Find and free completed poll iocbs |
| 1706 | */ |
| 1707 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1708 | struct list_head *done) |
| 1709 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1710 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1711 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1712 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1713 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1714 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1715 | int cflags = 0; |
| 1716 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1717 | req = list_first_entry(done, struct io_kiocb, list); |
| 1718 | list_del(&req->list); |
| 1719 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1720 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1721 | cflags = io_put_kbuf(req); |
| 1722 | |
| 1723 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1724 | (*nr_events)++; |
| 1725 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1726 | if (refcount_dec_and_test(&req->refs) && |
| 1727 | !io_req_multi_free(&rb, req)) |
| 1728 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1729 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1730 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1731 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1732 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1736 | long min) |
| 1737 | { |
| 1738 | struct io_kiocb *req, *tmp; |
| 1739 | LIST_HEAD(done); |
| 1740 | bool spin; |
| 1741 | int ret; |
| 1742 | |
| 1743 | /* |
| 1744 | * Only spin for completions if we don't have multiple devices hanging |
| 1745 | * off our complete list, and we're under the requested amount. |
| 1746 | */ |
| 1747 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1748 | |
| 1749 | ret = 0; |
| 1750 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1751 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1752 | |
| 1753 | /* |
| 1754 | * Move completed entries to our local list. If we find a |
| 1755 | * request that requires polling, break out and complete |
| 1756 | * the done list first, if we have entries there. |
| 1757 | */ |
| 1758 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1759 | list_move_tail(&req->list, &done); |
| 1760 | continue; |
| 1761 | } |
| 1762 | if (!list_empty(&done)) |
| 1763 | break; |
| 1764 | |
| 1765 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1766 | if (ret < 0) |
| 1767 | break; |
| 1768 | |
| 1769 | if (ret && spin) |
| 1770 | spin = false; |
| 1771 | ret = 0; |
| 1772 | } |
| 1773 | |
| 1774 | if (!list_empty(&done)) |
| 1775 | io_iopoll_complete(ctx, nr_events, &done); |
| 1776 | |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
| 1780 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1781 | * 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] | 1782 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1783 | * as a non-spinning completion check. |
| 1784 | */ |
| 1785 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1786 | long min) |
| 1787 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1788 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1789 | int ret; |
| 1790 | |
| 1791 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1792 | if (ret < 0) |
| 1793 | return ret; |
| 1794 | if (!min || *nr_events >= min) |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | return 1; |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * We can't just wait for polled events to come to us, we have to actively |
| 1803 | * find and complete them. |
| 1804 | */ |
| 1805 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1806 | { |
| 1807 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1808 | return; |
| 1809 | |
| 1810 | mutex_lock(&ctx->uring_lock); |
| 1811 | while (!list_empty(&ctx->poll_list)) { |
| 1812 | unsigned int nr_events = 0; |
| 1813 | |
| 1814 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1815 | |
| 1816 | /* |
| 1817 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1818 | * in this case we need to ensure that we reap all events. |
| 1819 | */ |
| 1820 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1821 | } |
| 1822 | mutex_unlock(&ctx->uring_lock); |
| 1823 | } |
| 1824 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1825 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1826 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1827 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1828 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1829 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1830 | /* |
| 1831 | * We disallow the app entering submit/complete with polling, but we |
| 1832 | * still need to lock the ring to prevent racing with polled issue |
| 1833 | * that got punted to a workqueue. |
| 1834 | */ |
| 1835 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1836 | do { |
| 1837 | int tmin = 0; |
| 1838 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1839 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1840 | * Don't enter poll loop if we already have events pending. |
| 1841 | * If we do, we can potentially be spinning for commands that |
| 1842 | * already triggered a CQE (eg in error). |
| 1843 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1844 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1845 | break; |
| 1846 | |
| 1847 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1848 | * If a submit got punted to a workqueue, we can have the |
| 1849 | * application entering polling for a command before it gets |
| 1850 | * issued. That app will hold the uring_lock for the duration |
| 1851 | * of the poll right here, so we need to take a breather every |
| 1852 | * now and then to ensure that the issue has a chance to add |
| 1853 | * the poll to the issued list. Otherwise we can spin here |
| 1854 | * forever, while the workqueue is stuck trying to acquire the |
| 1855 | * very same mutex. |
| 1856 | */ |
| 1857 | if (!(++iters & 7)) { |
| 1858 | mutex_unlock(&ctx->uring_lock); |
| 1859 | mutex_lock(&ctx->uring_lock); |
| 1860 | } |
| 1861 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1862 | if (*nr_events < min) |
| 1863 | tmin = min - *nr_events; |
| 1864 | |
| 1865 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1866 | if (ret <= 0) |
| 1867 | break; |
| 1868 | ret = 0; |
| 1869 | } while (min && !*nr_events && !need_resched()); |
| 1870 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1871 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1872 | return ret; |
| 1873 | } |
| 1874 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1875 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1876 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1877 | /* |
| 1878 | * Tell lockdep we inherited freeze protection from submission |
| 1879 | * thread. |
| 1880 | */ |
| 1881 | if (req->flags & REQ_F_ISREG) { |
| 1882 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1883 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1884 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1885 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1886 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1889 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1890 | { |
| 1891 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1892 | req->flags |= REQ_F_FAIL_LINK; |
| 1893 | } |
| 1894 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1895 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1896 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1897 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1898 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1899 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1900 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1901 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1902 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1903 | if (res != req->result) |
| 1904 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1905 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1906 | cflags = io_put_kbuf(req); |
| 1907 | __io_cqring_add_event(req, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1911 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1912 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1913 | |
| 1914 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1915 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1918 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1919 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1920 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1921 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1922 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1923 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1924 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1925 | if (res != req->result) |
| 1926 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1927 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1928 | if (res != -EAGAIN) |
| 1929 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1934 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1935 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1936 | * accessing the kiocb cookie. |
| 1937 | */ |
| 1938 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1939 | { |
| 1940 | struct io_ring_ctx *ctx = req->ctx; |
| 1941 | |
| 1942 | /* |
| 1943 | * Track whether we have multiple files in our lists. This will impact |
| 1944 | * how we do polling eventually, not spinning if we're on potentially |
| 1945 | * different devices. |
| 1946 | */ |
| 1947 | if (list_empty(&ctx->poll_list)) { |
| 1948 | ctx->poll_multi_file = false; |
| 1949 | } else if (!ctx->poll_multi_file) { |
| 1950 | struct io_kiocb *list_req; |
| 1951 | |
| 1952 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1953 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1954 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1955 | ctx->poll_multi_file = true; |
| 1956 | } |
| 1957 | |
| 1958 | /* |
| 1959 | * For fast devices, IO may have already completed. If it has, add |
| 1960 | * it to the front so we find it first. |
| 1961 | */ |
| 1962 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1963 | list_add(&req->list, &ctx->poll_list); |
| 1964 | else |
| 1965 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 1966 | |
| 1967 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 1968 | wq_has_sleeper(&ctx->sqo_wait)) |
| 1969 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1972 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1973 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1974 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1975 | int diff = state->has_refs - state->used_refs; |
| 1976 | |
| 1977 | if (diff) |
| 1978 | fput_many(state->file, diff); |
| 1979 | state->file = NULL; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * Get as many references to a file as we have IOs left in this submission, |
| 1985 | * assuming most submissions are for one file, or at least that each file |
| 1986 | * has more than one submission. |
| 1987 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1988 | 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] | 1989 | { |
| 1990 | if (!state) |
| 1991 | return fget(fd); |
| 1992 | |
| 1993 | if (state->file) { |
| 1994 | if (state->fd == fd) { |
| 1995 | state->used_refs++; |
| 1996 | state->ios_left--; |
| 1997 | return state->file; |
| 1998 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1999 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2000 | } |
| 2001 | state->file = fget_many(fd, state->ios_left); |
| 2002 | if (!state->file) |
| 2003 | return NULL; |
| 2004 | |
| 2005 | state->fd = fd; |
| 2006 | state->has_refs = state->ios_left; |
| 2007 | state->used_refs = 1; |
| 2008 | state->ios_left--; |
| 2009 | return state->file; |
| 2010 | } |
| 2011 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2012 | /* |
| 2013 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2014 | * any file. For now, just ensure that anything potentially problematic is done |
| 2015 | * inline. |
| 2016 | */ |
| 2017 | static bool io_file_supports_async(struct file *file) |
| 2018 | { |
| 2019 | umode_t mode = file_inode(file)->i_mode; |
| 2020 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2021 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2022 | return true; |
| 2023 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 2024 | return true; |
| 2025 | |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2029 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2030 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2031 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2032 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2033 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2034 | unsigned ioprio; |
| 2035 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2036 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2037 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2038 | req->flags |= REQ_F_ISREG; |
| 2039 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2040 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2041 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2042 | req->flags |= REQ_F_CUR_POS; |
| 2043 | kiocb->ki_pos = req->file->f_pos; |
| 2044 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2045 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2046 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2047 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2048 | if (unlikely(ret)) |
| 2049 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2050 | |
| 2051 | ioprio = READ_ONCE(sqe->ioprio); |
| 2052 | if (ioprio) { |
| 2053 | ret = ioprio_check_cap(ioprio); |
| 2054 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2055 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2056 | |
| 2057 | kiocb->ki_ioprio = ioprio; |
| 2058 | } else |
| 2059 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2060 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2061 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2062 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 2063 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2064 | req->flags |= REQ_F_NOWAIT; |
| 2065 | |
| 2066 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2067 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2068 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2069 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2070 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2071 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2072 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2073 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2074 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2075 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 2076 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2077 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2078 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2079 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2080 | kiocb->ki_complete = io_complete_rw; |
| 2081 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2082 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2083 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2084 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2085 | /* we own ->private, reuse it for the buffer index / buffer ID */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2086 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2087 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2088 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2092 | { |
| 2093 | switch (ret) { |
| 2094 | case -EIOCBQUEUED: |
| 2095 | break; |
| 2096 | case -ERESTARTSYS: |
| 2097 | case -ERESTARTNOINTR: |
| 2098 | case -ERESTARTNOHAND: |
| 2099 | case -ERESTART_RESTARTBLOCK: |
| 2100 | /* |
| 2101 | * We can't just restart the syscall, since previously |
| 2102 | * submitted sqes may already be in progress. Just fail this |
| 2103 | * IO with EINTR. |
| 2104 | */ |
| 2105 | ret = -EINTR; |
| 2106 | /* fall through */ |
| 2107 | default: |
| 2108 | kiocb->ki_complete(kiocb, ret, 0); |
| 2109 | } |
| 2110 | } |
| 2111 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2112 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2113 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2114 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2115 | |
| 2116 | if (req->flags & REQ_F_CUR_POS) |
| 2117 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2118 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2119 | io_complete_rw(kiocb, ret, 0); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2120 | else |
| 2121 | io_rw_done(kiocb, ret); |
| 2122 | } |
| 2123 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2124 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2125 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2126 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2127 | struct io_ring_ctx *ctx = req->ctx; |
| 2128 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2129 | struct io_mapped_ubuf *imu; |
| 2130 | unsigned index, buf_index; |
| 2131 | size_t offset; |
| 2132 | u64 buf_addr; |
| 2133 | |
| 2134 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2135 | if (unlikely(!ctx->user_bufs)) |
| 2136 | return -EFAULT; |
| 2137 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2138 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2139 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2140 | return -EFAULT; |
| 2141 | |
| 2142 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2143 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2144 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2145 | |
| 2146 | /* overflow */ |
| 2147 | if (buf_addr + len < buf_addr) |
| 2148 | return -EFAULT; |
| 2149 | /* not inside the mapped region */ |
| 2150 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2151 | return -EFAULT; |
| 2152 | |
| 2153 | /* |
| 2154 | * May not be a start of buffer, set size appropriately |
| 2155 | * and advance us to the beginning. |
| 2156 | */ |
| 2157 | offset = buf_addr - imu->ubuf; |
| 2158 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2159 | |
| 2160 | if (offset) { |
| 2161 | /* |
| 2162 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2163 | * using the latter parts of a big fixed buffer - it iterates |
| 2164 | * over each segment manually. We can cheat a bit here, because |
| 2165 | * we know that: |
| 2166 | * |
| 2167 | * 1) it's a BVEC iter, we set it up |
| 2168 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2169 | * first and last bvec |
| 2170 | * |
| 2171 | * So just find our index, and adjust the iterator afterwards. |
| 2172 | * If the offset is within the first bvec (or the whole first |
| 2173 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2174 | * since we can just skip the first segment, which may not |
| 2175 | * be PAGE_SIZE aligned. |
| 2176 | */ |
| 2177 | const struct bio_vec *bvec = imu->bvec; |
| 2178 | |
| 2179 | if (offset <= bvec->bv_len) { |
| 2180 | iov_iter_advance(iter, offset); |
| 2181 | } else { |
| 2182 | unsigned long seg_skip; |
| 2183 | |
| 2184 | /* skip first vec */ |
| 2185 | offset -= bvec->bv_len; |
| 2186 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2187 | |
| 2188 | iter->bvec = bvec + seg_skip; |
| 2189 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2190 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2191 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2195 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2198 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2199 | { |
| 2200 | if (needs_lock) |
| 2201 | mutex_unlock(&ctx->uring_lock); |
| 2202 | } |
| 2203 | |
| 2204 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2205 | { |
| 2206 | /* |
| 2207 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2208 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2209 | * The only exception is when we've detached the request and issue it |
| 2210 | * from an async worker thread, grab the lock for that case. |
| 2211 | */ |
| 2212 | if (needs_lock) |
| 2213 | mutex_lock(&ctx->uring_lock); |
| 2214 | } |
| 2215 | |
| 2216 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2217 | int bgid, struct io_buffer *kbuf, |
| 2218 | bool needs_lock) |
| 2219 | { |
| 2220 | struct io_buffer *head; |
| 2221 | |
| 2222 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2223 | return kbuf; |
| 2224 | |
| 2225 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2226 | |
| 2227 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2228 | |
| 2229 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2230 | if (head) { |
| 2231 | if (!list_empty(&head->list)) { |
| 2232 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2233 | list); |
| 2234 | list_del(&kbuf->list); |
| 2235 | } else { |
| 2236 | kbuf = head; |
| 2237 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2238 | } |
| 2239 | if (*len > kbuf->len) |
| 2240 | *len = kbuf->len; |
| 2241 | } else { |
| 2242 | kbuf = ERR_PTR(-ENOBUFS); |
| 2243 | } |
| 2244 | |
| 2245 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2246 | |
| 2247 | return kbuf; |
| 2248 | } |
| 2249 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2250 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2251 | bool needs_lock) |
| 2252 | { |
| 2253 | struct io_buffer *kbuf; |
| 2254 | int bgid; |
| 2255 | |
| 2256 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2257 | bgid = (int) (unsigned long) req->rw.kiocb.private; |
| 2258 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2259 | if (IS_ERR(kbuf)) |
| 2260 | return kbuf; |
| 2261 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2262 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2263 | return u64_to_user_ptr(kbuf->addr); |
| 2264 | } |
| 2265 | |
| 2266 | #ifdef CONFIG_COMPAT |
| 2267 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2268 | bool needs_lock) |
| 2269 | { |
| 2270 | struct compat_iovec __user *uiov; |
| 2271 | compat_ssize_t clen; |
| 2272 | void __user *buf; |
| 2273 | ssize_t len; |
| 2274 | |
| 2275 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2276 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2277 | return -EFAULT; |
| 2278 | if (__get_user(clen, &uiov->iov_len)) |
| 2279 | return -EFAULT; |
| 2280 | if (clen < 0) |
| 2281 | return -EINVAL; |
| 2282 | |
| 2283 | len = clen; |
| 2284 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2285 | if (IS_ERR(buf)) |
| 2286 | return PTR_ERR(buf); |
| 2287 | iov[0].iov_base = buf; |
| 2288 | iov[0].iov_len = (compat_size_t) len; |
| 2289 | return 0; |
| 2290 | } |
| 2291 | #endif |
| 2292 | |
| 2293 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2294 | bool needs_lock) |
| 2295 | { |
| 2296 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2297 | void __user *buf; |
| 2298 | ssize_t len; |
| 2299 | |
| 2300 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2301 | return -EFAULT; |
| 2302 | |
| 2303 | len = iov[0].iov_len; |
| 2304 | if (len < 0) |
| 2305 | return -EINVAL; |
| 2306 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2307 | if (IS_ERR(buf)) |
| 2308 | return PTR_ERR(buf); |
| 2309 | iov[0].iov_base = buf; |
| 2310 | iov[0].iov_len = len; |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2315 | bool needs_lock) |
| 2316 | { |
| 2317 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2318 | return 0; |
| 2319 | if (!req->rw.len) |
| 2320 | return 0; |
| 2321 | else if (req->rw.len > 1) |
| 2322 | return -EINVAL; |
| 2323 | |
| 2324 | #ifdef CONFIG_COMPAT |
| 2325 | if (req->ctx->compat) |
| 2326 | return io_compat_import(req, iov, needs_lock); |
| 2327 | #endif |
| 2328 | |
| 2329 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2330 | } |
| 2331 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2332 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2333 | struct iovec **iovec, struct iov_iter *iter, |
| 2334 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2335 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2336 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2337 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2338 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2339 | u8 opcode; |
| 2340 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2341 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2342 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2343 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2344 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2345 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2346 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2347 | /* buffer index only valid with fixed read/write, or buffer select */ |
| 2348 | if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2349 | return -EINVAL; |
| 2350 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2351 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2352 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2353 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
| 2354 | if (IS_ERR(buf)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2355 | *iovec = NULL; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2356 | return PTR_ERR(buf); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2357 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2360 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2361 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2362 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2365 | if (req->io) { |
| 2366 | struct io_async_rw *iorw = &req->io->rw; |
| 2367 | |
| 2368 | *iovec = iorw->iov; |
| 2369 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2370 | if (iorw->iov == iorw->fast_iov) |
| 2371 | *iovec = NULL; |
| 2372 | return iorw->size; |
| 2373 | } |
| 2374 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2375 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2376 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
| 2377 | if (!ret) |
| 2378 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
| 2379 | *iovec = NULL; |
| 2380 | return ret; |
| 2381 | } |
| 2382 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2383 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2384 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2385 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2386 | iovec, iter); |
| 2387 | #endif |
| 2388 | |
| 2389 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2390 | } |
| 2391 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2392 | /* |
| 2393 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2394 | * by looping over ->read() or ->write() manually. |
| 2395 | */ |
| 2396 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2397 | struct iov_iter *iter) |
| 2398 | { |
| 2399 | ssize_t ret = 0; |
| 2400 | |
| 2401 | /* |
| 2402 | * Don't support polled IO through this interface, and we can't |
| 2403 | * support non-blocking either. For the latter, this just causes |
| 2404 | * the kiocb to be handled from an async context. |
| 2405 | */ |
| 2406 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2407 | return -EOPNOTSUPP; |
| 2408 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2409 | return -EAGAIN; |
| 2410 | |
| 2411 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2412 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2413 | ssize_t nr; |
| 2414 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2415 | if (!iov_iter_is_bvec(iter)) { |
| 2416 | iovec = iov_iter_iovec(iter); |
| 2417 | } else { |
| 2418 | /* fixed buffers import bvec */ |
| 2419 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2420 | + iter->iov_offset; |
| 2421 | iovec.iov_len = min(iter->count, |
| 2422 | iter->bvec->bv_len - iter->iov_offset); |
| 2423 | } |
| 2424 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2425 | if (rw == READ) { |
| 2426 | nr = file->f_op->read(file, iovec.iov_base, |
| 2427 | iovec.iov_len, &kiocb->ki_pos); |
| 2428 | } else { |
| 2429 | nr = file->f_op->write(file, iovec.iov_base, |
| 2430 | iovec.iov_len, &kiocb->ki_pos); |
| 2431 | } |
| 2432 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2433 | if (iov_iter_is_bvec(iter)) |
| 2434 | kunmap(iter->bvec->bv_page); |
| 2435 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2436 | if (nr < 0) { |
| 2437 | if (!ret) |
| 2438 | ret = nr; |
| 2439 | break; |
| 2440 | } |
| 2441 | ret += nr; |
| 2442 | if (nr != iovec.iov_len) |
| 2443 | break; |
| 2444 | iov_iter_advance(iter, nr); |
| 2445 | } |
| 2446 | |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2450 | 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] | 2451 | struct iovec *iovec, struct iovec *fast_iov, |
| 2452 | struct iov_iter *iter) |
| 2453 | { |
| 2454 | req->io->rw.nr_segs = iter->nr_segs; |
| 2455 | req->io->rw.size = io_size; |
| 2456 | req->io->rw.iov = iovec; |
| 2457 | if (!req->io->rw.iov) { |
| 2458 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2459 | memcpy(req->io->rw.iov, fast_iov, |
| 2460 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2461 | } else { |
| 2462 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2463 | } |
| 2464 | } |
| 2465 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2466 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2467 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2468 | if (!io_op_defs[req->opcode].async_ctx) |
| 2469 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2470 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2471 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2474 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2475 | struct iovec *iovec, struct iovec *fast_iov, |
| 2476 | struct iov_iter *iter) |
| 2477 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2478 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2479 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2480 | if (!req->io) { |
| 2481 | if (io_alloc_async_ctx(req)) |
| 2482 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2483 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2484 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2485 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2486 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2487 | } |
| 2488 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2489 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2490 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2491 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2492 | struct io_async_ctx *io; |
| 2493 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2494 | ssize_t ret; |
| 2495 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2496 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2497 | if (ret) |
| 2498 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2499 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2500 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2501 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2502 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2503 | /* either don't need iovec imported or already have it */ |
| 2504 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2505 | return 0; |
| 2506 | |
| 2507 | io = req->io; |
| 2508 | io->rw.iov = io->rw.fast_iov; |
| 2509 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2510 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2511 | req->io = io; |
| 2512 | if (ret < 0) |
| 2513 | return ret; |
| 2514 | |
| 2515 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2516 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2519 | static int io_read(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2520 | { |
| 2521 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2522 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2523 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2524 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2525 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2526 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2527 | ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2528 | if (ret < 0) |
| 2529 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2530 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2531 | /* Ensure we clear previously set non-block flag */ |
| 2532 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2533 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2534 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2535 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2536 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2537 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2538 | req->result = io_size; |
| 2539 | |
| 2540 | /* |
| 2541 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2542 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2543 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2544 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2545 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2546 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2547 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2548 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2549 | if (!ret) { |
| 2550 | ssize_t ret2; |
| 2551 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2552 | if (req->file->f_op->read_iter) |
| 2553 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2554 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2555 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2556 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2557 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2558 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2559 | kiocb_done(kiocb, ret2); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2560 | } else { |
| 2561 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2562 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2563 | inline_vecs, &iter); |
| 2564 | if (ret) |
| 2565 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2566 | /* any defer here is final, must blocking retry */ |
| 2567 | if (!(req->flags & REQ_F_NOWAIT)) |
| 2568 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2569 | return -EAGAIN; |
| 2570 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2571 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2572 | out_free: |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2573 | kfree(iovec); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2574 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2575 | return ret; |
| 2576 | } |
| 2577 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2578 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2579 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2580 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2581 | struct io_async_ctx *io; |
| 2582 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2583 | ssize_t ret; |
| 2584 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2585 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2586 | if (ret) |
| 2587 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2588 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2589 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2590 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2591 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2592 | /* either don't need iovec imported or already have it */ |
| 2593 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2594 | return 0; |
| 2595 | |
| 2596 | io = req->io; |
| 2597 | io->rw.iov = io->rw.fast_iov; |
| 2598 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2599 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2600 | req->io = io; |
| 2601 | if (ret < 0) |
| 2602 | return ret; |
| 2603 | |
| 2604 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2605 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2608 | static int io_write(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2609 | { |
| 2610 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2611 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2612 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2613 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2614 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2615 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2616 | ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2617 | if (ret < 0) |
| 2618 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2619 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2620 | /* Ensure we clear previously set non-block flag */ |
| 2621 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2622 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2623 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2624 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2625 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2626 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2627 | req->result = io_size; |
| 2628 | |
| 2629 | /* |
| 2630 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2631 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2632 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2633 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2634 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2635 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2636 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2637 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2638 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2639 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2640 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2641 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2642 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2643 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2644 | ssize_t ret2; |
| 2645 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2646 | /* |
| 2647 | * Open-code file_start_write here to grab freeze protection, |
| 2648 | * which will be released by another thread in |
| 2649 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2650 | * released so that it doesn't complain about the held lock when |
| 2651 | * we return to userspace. |
| 2652 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2653 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2654 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2655 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2656 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2657 | SB_FREEZE_WRITE); |
| 2658 | } |
| 2659 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2660 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2661 | if (req->file->f_op->write_iter) |
| 2662 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2663 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2664 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 2665 | /* |
| 2666 | * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 2667 | * retry them without IOCB_NOWAIT. |
| 2668 | */ |
| 2669 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 2670 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2671 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2672 | kiocb_done(kiocb, ret2); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2673 | } else { |
| 2674 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2675 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2676 | inline_vecs, &iter); |
| 2677 | if (ret) |
| 2678 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2679 | /* any defer here is final, must blocking retry */ |
| 2680 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2681 | return -EAGAIN; |
| 2682 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2683 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2684 | out_free: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2685 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2686 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2687 | return ret; |
| 2688 | } |
| 2689 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2690 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2691 | { |
| 2692 | struct io_splice* sp = &req->splice; |
| 2693 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 2694 | int ret; |
| 2695 | |
| 2696 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2697 | return 0; |
| 2698 | |
| 2699 | sp->file_in = NULL; |
| 2700 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 2701 | sp->off_out = READ_ONCE(sqe->off); |
| 2702 | sp->len = READ_ONCE(sqe->len); |
| 2703 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 2704 | |
| 2705 | if (unlikely(sp->flags & ~valid_flags)) |
| 2706 | return -EINVAL; |
| 2707 | |
| 2708 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 2709 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2710 | if (ret) |
| 2711 | return ret; |
| 2712 | req->flags |= REQ_F_NEED_CLEANUP; |
| 2713 | |
| 2714 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) |
| 2715 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 2716 | |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
| 2720 | static bool io_splice_punt(struct file *file) |
| 2721 | { |
| 2722 | if (get_pipe_info(file)) |
| 2723 | return false; |
| 2724 | if (!io_file_supports_async(file)) |
| 2725 | return true; |
| 2726 | return !(file->f_mode & O_NONBLOCK); |
| 2727 | } |
| 2728 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2729 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2730 | { |
| 2731 | struct io_splice *sp = &req->splice; |
| 2732 | struct file *in = sp->file_in; |
| 2733 | struct file *out = sp->file_out; |
| 2734 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 2735 | loff_t *poff_in, *poff_out; |
| 2736 | long ret; |
| 2737 | |
| 2738 | if (force_nonblock) { |
| 2739 | if (io_splice_punt(in) || io_splice_punt(out)) |
| 2740 | return -EAGAIN; |
| 2741 | flags |= SPLICE_F_NONBLOCK; |
| 2742 | } |
| 2743 | |
| 2744 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 2745 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
| 2746 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
| 2747 | if (force_nonblock && ret == -EAGAIN) |
| 2748 | return -EAGAIN; |
| 2749 | |
| 2750 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2751 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 2752 | |
| 2753 | io_cqring_add_event(req, ret); |
| 2754 | if (ret != sp->len) |
| 2755 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2756 | io_put_req(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2757 | return 0; |
| 2758 | } |
| 2759 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2760 | /* |
| 2761 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2762 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2763 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2764 | { |
| 2765 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2766 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2767 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2768 | return -EINVAL; |
| 2769 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2770 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2771 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2772 | return 0; |
| 2773 | } |
| 2774 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2775 | 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] | 2776 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2777 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2778 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2779 | if (!req->file) |
| 2780 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2781 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2782 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2783 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2784 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2785 | return -EINVAL; |
| 2786 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2787 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2788 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2789 | return -EINVAL; |
| 2790 | |
| 2791 | req->sync.off = READ_ONCE(sqe->off); |
| 2792 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
| 2795 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2796 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2797 | { |
| 2798 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2799 | req_set_fail_links(req); |
| 2800 | io_cqring_add_event(req, -ECANCELED); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 2801 | io_put_req(req); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2802 | return true; |
| 2803 | } |
| 2804 | |
| 2805 | return false; |
| 2806 | } |
| 2807 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2808 | static void __io_fsync(struct io_kiocb *req) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2809 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2810 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2811 | int ret; |
| 2812 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2813 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2814 | end > 0 ? end : LLONG_MAX, |
| 2815 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2816 | if (ret < 0) |
| 2817 | req_set_fail_links(req); |
| 2818 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2819 | io_put_req(req); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2823 | { |
| 2824 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2825 | |
| 2826 | if (io_req_cancelled(req)) |
| 2827 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2828 | __io_fsync(req); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 2829 | io_steal_work(req, workptr); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2830 | } |
| 2831 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2832 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2833 | { |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2834 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2835 | if (force_nonblock) { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2836 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2837 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2838 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2839 | __io_fsync(req); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2840 | return 0; |
| 2841 | } |
| 2842 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2843 | static void __io_fallocate(struct io_kiocb *req) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2844 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2845 | int ret; |
| 2846 | |
| 2847 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2848 | req->sync.len); |
| 2849 | if (ret < 0) |
| 2850 | req_set_fail_links(req); |
| 2851 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2852 | io_put_req(req); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2856 | { |
| 2857 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2858 | |
Pavel Begunkov | 594506f | 2020-03-03 21:33:11 +0300 | [diff] [blame] | 2859 | if (io_req_cancelled(req)) |
| 2860 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2861 | __io_fallocate(req); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 2862 | io_steal_work(req, workptr); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2866 | const struct io_uring_sqe *sqe) |
| 2867 | { |
| 2868 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2869 | return -EINVAL; |
| 2870 | |
| 2871 | req->sync.off = READ_ONCE(sqe->off); |
| 2872 | req->sync.len = READ_ONCE(sqe->addr); |
| 2873 | req->sync.mode = READ_ONCE(sqe->len); |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2877 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2878 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2879 | /* fallocate always requiring blocking context */ |
| 2880 | if (force_nonblock) { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2881 | req->work.func = io_fallocate_finish; |
| 2882 | return -EAGAIN; |
| 2883 | } |
| 2884 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2885 | __io_fallocate(req); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2886 | return 0; |
| 2887 | } |
| 2888 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2889 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2890 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2891 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2892 | int ret; |
| 2893 | |
| 2894 | if (sqe->ioprio || sqe->buf_index) |
| 2895 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2896 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2897 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2898 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2899 | return 0; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2900 | |
| 2901 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2902 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2903 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2904 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2905 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2906 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2907 | if (IS_ERR(req->open.filename)) { |
| 2908 | ret = PTR_ERR(req->open.filename); |
| 2909 | req->open.filename = NULL; |
| 2910 | return ret; |
| 2911 | } |
| 2912 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2913 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2914 | return 0; |
| 2915 | } |
| 2916 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2917 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2918 | { |
| 2919 | struct open_how __user *how; |
| 2920 | const char __user *fname; |
| 2921 | size_t len; |
| 2922 | int ret; |
| 2923 | |
| 2924 | if (sqe->ioprio || sqe->buf_index) |
| 2925 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2926 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2927 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2928 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2929 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2930 | |
| 2931 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2932 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2933 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2934 | len = READ_ONCE(sqe->len); |
| 2935 | |
| 2936 | if (len < OPEN_HOW_SIZE_VER0) |
| 2937 | return -EINVAL; |
| 2938 | |
| 2939 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2940 | len); |
| 2941 | if (ret) |
| 2942 | return ret; |
| 2943 | |
| 2944 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2945 | req->open.how.flags |= O_LARGEFILE; |
| 2946 | |
| 2947 | req->open.filename = getname(fname); |
| 2948 | if (IS_ERR(req->open.filename)) { |
| 2949 | ret = PTR_ERR(req->open.filename); |
| 2950 | req->open.filename = NULL; |
| 2951 | return ret; |
| 2952 | } |
| 2953 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2954 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2955 | return 0; |
| 2956 | } |
| 2957 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2958 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2959 | { |
| 2960 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2961 | struct file *file; |
| 2962 | int ret; |
| 2963 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2964 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2965 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2966 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2967 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2968 | if (ret) |
| 2969 | goto err; |
| 2970 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2971 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2972 | if (ret < 0) |
| 2973 | goto err; |
| 2974 | |
| 2975 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2976 | if (IS_ERR(file)) { |
| 2977 | put_unused_fd(ret); |
| 2978 | ret = PTR_ERR(file); |
| 2979 | } else { |
| 2980 | fsnotify_open(file); |
| 2981 | fd_install(ret, file); |
| 2982 | } |
| 2983 | err: |
| 2984 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2985 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2986 | if (ret < 0) |
| 2987 | req_set_fail_links(req); |
| 2988 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2989 | io_put_req(req); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2990 | return 0; |
| 2991 | } |
| 2992 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2993 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2994 | { |
| 2995 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2996 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame^] | 2999 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3000 | const struct io_uring_sqe *sqe) |
| 3001 | { |
| 3002 | struct io_provide_buf *p = &req->pbuf; |
| 3003 | u64 tmp; |
| 3004 | |
| 3005 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3006 | return -EINVAL; |
| 3007 | |
| 3008 | tmp = READ_ONCE(sqe->fd); |
| 3009 | if (!tmp || tmp > USHRT_MAX) |
| 3010 | return -EINVAL; |
| 3011 | |
| 3012 | memset(p, 0, sizeof(*p)); |
| 3013 | p->nbufs = tmp; |
| 3014 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
| 3018 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3019 | int bgid, unsigned nbufs) |
| 3020 | { |
| 3021 | unsigned i = 0; |
| 3022 | |
| 3023 | /* shouldn't happen */ |
| 3024 | if (!nbufs) |
| 3025 | return 0; |
| 3026 | |
| 3027 | /* the head kbuf is the list itself */ |
| 3028 | while (!list_empty(&buf->list)) { |
| 3029 | struct io_buffer *nxt; |
| 3030 | |
| 3031 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3032 | list_del(&nxt->list); |
| 3033 | kfree(nxt); |
| 3034 | if (++i == nbufs) |
| 3035 | return i; |
| 3036 | } |
| 3037 | i++; |
| 3038 | kfree(buf); |
| 3039 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3040 | |
| 3041 | return i; |
| 3042 | } |
| 3043 | |
| 3044 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock) |
| 3045 | { |
| 3046 | struct io_provide_buf *p = &req->pbuf; |
| 3047 | struct io_ring_ctx *ctx = req->ctx; |
| 3048 | struct io_buffer *head; |
| 3049 | int ret = 0; |
| 3050 | |
| 3051 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3052 | |
| 3053 | lockdep_assert_held(&ctx->uring_lock); |
| 3054 | |
| 3055 | ret = -ENOENT; |
| 3056 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3057 | if (head) |
| 3058 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
| 3059 | |
| 3060 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3061 | if (ret < 0) |
| 3062 | req_set_fail_links(req); |
| 3063 | io_cqring_add_event(req, ret); |
| 3064 | io_put_req(req); |
| 3065 | return 0; |
| 3066 | } |
| 3067 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3068 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3069 | const struct io_uring_sqe *sqe) |
| 3070 | { |
| 3071 | struct io_provide_buf *p = &req->pbuf; |
| 3072 | u64 tmp; |
| 3073 | |
| 3074 | if (sqe->ioprio || sqe->rw_flags) |
| 3075 | return -EINVAL; |
| 3076 | |
| 3077 | tmp = READ_ONCE(sqe->fd); |
| 3078 | if (!tmp || tmp > USHRT_MAX) |
| 3079 | return -E2BIG; |
| 3080 | p->nbufs = tmp; |
| 3081 | p->addr = READ_ONCE(sqe->addr); |
| 3082 | p->len = READ_ONCE(sqe->len); |
| 3083 | |
| 3084 | if (!access_ok(u64_to_user_ptr(p->addr), p->len)) |
| 3085 | return -EFAULT; |
| 3086 | |
| 3087 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3088 | tmp = READ_ONCE(sqe->off); |
| 3089 | if (tmp > USHRT_MAX) |
| 3090 | return -E2BIG; |
| 3091 | p->bid = tmp; |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
| 3095 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3096 | { |
| 3097 | struct io_buffer *buf; |
| 3098 | u64 addr = pbuf->addr; |
| 3099 | int i, bid = pbuf->bid; |
| 3100 | |
| 3101 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3102 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3103 | if (!buf) |
| 3104 | break; |
| 3105 | |
| 3106 | buf->addr = addr; |
| 3107 | buf->len = pbuf->len; |
| 3108 | buf->bid = bid; |
| 3109 | addr += pbuf->len; |
| 3110 | bid++; |
| 3111 | if (!*head) { |
| 3112 | INIT_LIST_HEAD(&buf->list); |
| 3113 | *head = buf; |
| 3114 | } else { |
| 3115 | list_add_tail(&buf->list, &(*head)->list); |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | return i ? i : -ENOMEM; |
| 3120 | } |
| 3121 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3122 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock) |
| 3123 | { |
| 3124 | struct io_provide_buf *p = &req->pbuf; |
| 3125 | struct io_ring_ctx *ctx = req->ctx; |
| 3126 | struct io_buffer *head, *list; |
| 3127 | int ret = 0; |
| 3128 | |
| 3129 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3130 | |
| 3131 | lockdep_assert_held(&ctx->uring_lock); |
| 3132 | |
| 3133 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3134 | |
| 3135 | ret = io_add_buffers(p, &head); |
| 3136 | if (ret < 0) |
| 3137 | goto out; |
| 3138 | |
| 3139 | if (!list) { |
| 3140 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3141 | GFP_KERNEL); |
| 3142 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame^] | 3143 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3144 | goto out; |
| 3145 | } |
| 3146 | } |
| 3147 | out: |
| 3148 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3149 | if (ret < 0) |
| 3150 | req_set_fail_links(req); |
| 3151 | io_cqring_add_event(req, ret); |
| 3152 | io_put_req(req); |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3156 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3157 | const struct io_uring_sqe *sqe) |
| 3158 | { |
| 3159 | #if defined(CONFIG_EPOLL) |
| 3160 | if (sqe->ioprio || sqe->buf_index) |
| 3161 | return -EINVAL; |
| 3162 | |
| 3163 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 3164 | req->epoll.op = READ_ONCE(sqe->len); |
| 3165 | req->epoll.fd = READ_ONCE(sqe->off); |
| 3166 | |
| 3167 | if (ep_op_has_event(req->epoll.op)) { |
| 3168 | struct epoll_event __user *ev; |
| 3169 | |
| 3170 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3171 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 3172 | return -EFAULT; |
| 3173 | } |
| 3174 | |
| 3175 | return 0; |
| 3176 | #else |
| 3177 | return -EOPNOTSUPP; |
| 3178 | #endif |
| 3179 | } |
| 3180 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3181 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3182 | { |
| 3183 | #if defined(CONFIG_EPOLL) |
| 3184 | struct io_epoll *ie = &req->epoll; |
| 3185 | int ret; |
| 3186 | |
| 3187 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 3188 | if (force_nonblock && ret == -EAGAIN) |
| 3189 | return -EAGAIN; |
| 3190 | |
| 3191 | if (ret < 0) |
| 3192 | req_set_fail_links(req); |
| 3193 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3194 | io_put_req(req); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3195 | return 0; |
| 3196 | #else |
| 3197 | return -EOPNOTSUPP; |
| 3198 | #endif |
| 3199 | } |
| 3200 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3201 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3202 | { |
| 3203 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3204 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 3205 | return -EINVAL; |
| 3206 | |
| 3207 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 3208 | req->madvise.len = READ_ONCE(sqe->len); |
| 3209 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3210 | return 0; |
| 3211 | #else |
| 3212 | return -EOPNOTSUPP; |
| 3213 | #endif |
| 3214 | } |
| 3215 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3216 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3217 | { |
| 3218 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3219 | struct io_madvise *ma = &req->madvise; |
| 3220 | int ret; |
| 3221 | |
| 3222 | if (force_nonblock) |
| 3223 | return -EAGAIN; |
| 3224 | |
| 3225 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 3226 | if (ret < 0) |
| 3227 | req_set_fail_links(req); |
| 3228 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3229 | io_put_req(req); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3230 | return 0; |
| 3231 | #else |
| 3232 | return -EOPNOTSUPP; |
| 3233 | #endif |
| 3234 | } |
| 3235 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3236 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3237 | { |
| 3238 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 3239 | return -EINVAL; |
| 3240 | |
| 3241 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 3242 | req->fadvise.len = READ_ONCE(sqe->len); |
| 3243 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3244 | return 0; |
| 3245 | } |
| 3246 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3247 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3248 | { |
| 3249 | struct io_fadvise *fa = &req->fadvise; |
| 3250 | int ret; |
| 3251 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 3252 | if (force_nonblock) { |
| 3253 | switch (fa->advice) { |
| 3254 | case POSIX_FADV_NORMAL: |
| 3255 | case POSIX_FADV_RANDOM: |
| 3256 | case POSIX_FADV_SEQUENTIAL: |
| 3257 | break; |
| 3258 | default: |
| 3259 | return -EAGAIN; |
| 3260 | } |
| 3261 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3262 | |
| 3263 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 3264 | if (ret < 0) |
| 3265 | req_set_fail_links(req); |
| 3266 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3267 | io_put_req(req); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3268 | return 0; |
| 3269 | } |
| 3270 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3271 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3272 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3273 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3274 | unsigned lookup_flags; |
| 3275 | int ret; |
| 3276 | |
| 3277 | if (sqe->ioprio || sqe->buf_index) |
| 3278 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3279 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 3280 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 3281 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3282 | return 0; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3283 | |
| 3284 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3285 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3286 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3287 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3288 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3289 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3290 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3291 | return -EINVAL; |
| 3292 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3293 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3294 | if (IS_ERR(req->open.filename)) { |
| 3295 | ret = PTR_ERR(req->open.filename); |
| 3296 | req->open.filename = NULL; |
| 3297 | return ret; |
| 3298 | } |
| 3299 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3300 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3301 | return 0; |
| 3302 | } |
| 3303 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3304 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3305 | { |
| 3306 | struct io_open *ctx = &req->open; |
| 3307 | unsigned lookup_flags; |
| 3308 | struct path path; |
| 3309 | struct kstat stat; |
| 3310 | int ret; |
| 3311 | |
| 3312 | if (force_nonblock) |
| 3313 | return -EAGAIN; |
| 3314 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3315 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3316 | return -EINVAL; |
| 3317 | |
| 3318 | retry: |
| 3319 | /* filename_lookup() drops it, keep a reference */ |
| 3320 | ctx->filename->refcnt++; |
| 3321 | |
| 3322 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 3323 | NULL); |
| 3324 | if (ret) |
| 3325 | goto err; |
| 3326 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3327 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3328 | path_put(&path); |
| 3329 | if (retry_estale(ret, lookup_flags)) { |
| 3330 | lookup_flags |= LOOKUP_REVAL; |
| 3331 | goto retry; |
| 3332 | } |
| 3333 | if (!ret) |
| 3334 | ret = cp_statx(&stat, ctx->buffer); |
| 3335 | err: |
| 3336 | putname(ctx->filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3337 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3338 | if (ret < 0) |
| 3339 | req_set_fail_links(req); |
| 3340 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3341 | io_put_req(req); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3342 | return 0; |
| 3343 | } |
| 3344 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3345 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3346 | { |
| 3347 | /* |
| 3348 | * If we queue this for async, it must not be cancellable. That would |
| 3349 | * leave the 'file' in an undeterminate state. |
| 3350 | */ |
| 3351 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 3352 | |
| 3353 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 3354 | sqe->rw_flags || sqe->buf_index) |
| 3355 | return -EINVAL; |
| 3356 | if (sqe->flags & IOSQE_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3357 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3358 | |
| 3359 | req->close.fd = READ_ONCE(sqe->fd); |
| 3360 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 3361 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3362 | return -EBADF; |
| 3363 | |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3367 | /* only called when __close_fd_get_file() is done */ |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3368 | static void __io_close_finish(struct io_kiocb *req) |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3369 | { |
| 3370 | int ret; |
| 3371 | |
| 3372 | ret = filp_close(req->close.put_file, req->work.files); |
| 3373 | if (ret < 0) |
| 3374 | req_set_fail_links(req); |
| 3375 | io_cqring_add_event(req, ret); |
| 3376 | fput(req->close.put_file); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3377 | io_put_req(req); |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3378 | } |
| 3379 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3380 | static void io_close_finish(struct io_wq_work **workptr) |
| 3381 | { |
| 3382 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3383 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 3384 | /* not cancellable, don't do io_req_cancelled() */ |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3385 | __io_close_finish(req); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 3386 | io_steal_work(req, workptr); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3389 | static int io_close(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3390 | { |
| 3391 | int ret; |
| 3392 | |
| 3393 | req->close.put_file = NULL; |
| 3394 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 3395 | if (ret < 0) |
| 3396 | return ret; |
| 3397 | |
| 3398 | /* if the file has a flush method, be safe and punt to async */ |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 3399 | if (req->close.put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 594506f | 2020-03-03 21:33:11 +0300 | [diff] [blame] | 3400 | /* submission ref will be dropped, take it for async */ |
| 3401 | refcount_inc(&req->refs); |
| 3402 | |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 3403 | req->work.func = io_close_finish; |
| 3404 | /* |
| 3405 | * Do manual async queue here to avoid grabbing files - we don't |
| 3406 | * need the files, and it'll cause io_close_finish() to close |
| 3407 | * the file again and cause a double CQE entry for this request |
| 3408 | */ |
| 3409 | io_queue_async_work(req); |
| 3410 | return 0; |
| 3411 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3412 | |
| 3413 | /* |
| 3414 | * No ->flush(), safely close from here and just punt the |
| 3415 | * fput() to async context. |
| 3416 | */ |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3417 | __io_close_finish(req); |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3418 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3421 | 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] | 3422 | { |
| 3423 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3424 | |
| 3425 | if (!req->file) |
| 3426 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3427 | |
| 3428 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3429 | return -EINVAL; |
| 3430 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3431 | return -EINVAL; |
| 3432 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3433 | req->sync.off = READ_ONCE(sqe->off); |
| 3434 | req->sync.len = READ_ONCE(sqe->len); |
| 3435 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3436 | return 0; |
| 3437 | } |
| 3438 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3439 | static void __io_sync_file_range(struct io_kiocb *req) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3440 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3441 | int ret; |
| 3442 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3443 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3444 | req->sync.flags); |
| 3445 | if (ret < 0) |
| 3446 | req_set_fail_links(req); |
| 3447 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3448 | io_put_req(req); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 3453 | { |
| 3454 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3455 | struct io_kiocb *nxt = NULL; |
| 3456 | |
| 3457 | if (io_req_cancelled(req)) |
| 3458 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3459 | __io_sync_file_range(req); |
Pavel Begunkov | 594506f | 2020-03-03 21:33:11 +0300 | [diff] [blame] | 3460 | io_put_req(req); /* put submission ref */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3461 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3462 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3463 | } |
| 3464 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3465 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3466 | { |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3467 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3468 | if (force_nonblock) { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3469 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3470 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3471 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3472 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3473 | __io_sync_file_range(req); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3474 | return 0; |
| 3475 | } |
| 3476 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3477 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3478 | struct io_async_msghdr *kmsg) |
| 3479 | { |
| 3480 | if (req->io) |
| 3481 | return -EAGAIN; |
| 3482 | if (io_alloc_async_ctx(req)) { |
| 3483 | if (kmsg->iov != kmsg->fast_iov) |
| 3484 | kfree(kmsg->iov); |
| 3485 | return -ENOMEM; |
| 3486 | } |
| 3487 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3488 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3489 | return -EAGAIN; |
| 3490 | } |
| 3491 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3492 | 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] | 3493 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3494 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3495 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3496 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3497 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3498 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3499 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3500 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3501 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3502 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3503 | #ifdef CONFIG_COMPAT |
| 3504 | if (req->ctx->compat) |
| 3505 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3506 | #endif |
| 3507 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3508 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3509 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3510 | /* iovec is already imported */ |
| 3511 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3512 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3513 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3514 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3515 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3516 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3517 | if (!ret) |
| 3518 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3519 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3520 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3521 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3522 | #endif |
| 3523 | } |
| 3524 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3525 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3526 | { |
| 3527 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3528 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3529 | struct socket *sock; |
| 3530 | int ret; |
| 3531 | |
| 3532 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3533 | return -EINVAL; |
| 3534 | |
| 3535 | sock = sock_from_file(req->file, &ret); |
| 3536 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3537 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3538 | unsigned flags; |
| 3539 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3540 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3541 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3542 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3543 | /* if iov is set, it's allocated already */ |
| 3544 | if (!kmsg->iov) |
| 3545 | kmsg->iov = kmsg->fast_iov; |
| 3546 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3547 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3548 | struct io_sr_msg *sr = &req->sr_msg; |
| 3549 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3550 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3551 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3552 | |
| 3553 | io.msg.iov = io.msg.fast_iov; |
| 3554 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3555 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3556 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3557 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3558 | } |
| 3559 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3560 | flags = req->sr_msg.msg_flags; |
| 3561 | if (flags & MSG_DONTWAIT) |
| 3562 | req->flags |= REQ_F_NOWAIT; |
| 3563 | else if (force_nonblock) |
| 3564 | flags |= MSG_DONTWAIT; |
| 3565 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3566 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3567 | if (force_nonblock && ret == -EAGAIN) |
| 3568 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3569 | if (ret == -ERESTARTSYS) |
| 3570 | ret = -EINTR; |
| 3571 | } |
| 3572 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3573 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3574 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3575 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3576 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3577 | if (ret < 0) |
| 3578 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3579 | io_put_req(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3580 | return 0; |
| 3581 | #else |
| 3582 | return -EOPNOTSUPP; |
| 3583 | #endif |
| 3584 | } |
| 3585 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3586 | static int io_send(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3587 | { |
| 3588 | #if defined(CONFIG_NET) |
| 3589 | struct socket *sock; |
| 3590 | int ret; |
| 3591 | |
| 3592 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3593 | return -EINVAL; |
| 3594 | |
| 3595 | sock = sock_from_file(req->file, &ret); |
| 3596 | if (sock) { |
| 3597 | struct io_sr_msg *sr = &req->sr_msg; |
| 3598 | struct msghdr msg; |
| 3599 | struct iovec iov; |
| 3600 | unsigned flags; |
| 3601 | |
| 3602 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3603 | &msg.msg_iter); |
| 3604 | if (ret) |
| 3605 | return ret; |
| 3606 | |
| 3607 | msg.msg_name = NULL; |
| 3608 | msg.msg_control = NULL; |
| 3609 | msg.msg_controllen = 0; |
| 3610 | msg.msg_namelen = 0; |
| 3611 | |
| 3612 | flags = req->sr_msg.msg_flags; |
| 3613 | if (flags & MSG_DONTWAIT) |
| 3614 | req->flags |= REQ_F_NOWAIT; |
| 3615 | else if (force_nonblock) |
| 3616 | flags |= MSG_DONTWAIT; |
| 3617 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3618 | msg.msg_flags = flags; |
| 3619 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3620 | if (force_nonblock && ret == -EAGAIN) |
| 3621 | return -EAGAIN; |
| 3622 | if (ret == -ERESTARTSYS) |
| 3623 | ret = -EINTR; |
| 3624 | } |
| 3625 | |
| 3626 | io_cqring_add_event(req, ret); |
| 3627 | if (ret < 0) |
| 3628 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3629 | io_put_req(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3630 | return 0; |
| 3631 | #else |
| 3632 | return -EOPNOTSUPP; |
| 3633 | #endif |
| 3634 | } |
| 3635 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3636 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) |
| 3637 | { |
| 3638 | struct io_sr_msg *sr = &req->sr_msg; |
| 3639 | struct iovec __user *uiov; |
| 3640 | size_t iov_len; |
| 3641 | int ret; |
| 3642 | |
| 3643 | ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr, |
| 3644 | &uiov, &iov_len); |
| 3645 | if (ret) |
| 3646 | return ret; |
| 3647 | |
| 3648 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3649 | if (iov_len > 1) |
| 3650 | return -EINVAL; |
| 3651 | if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov))) |
| 3652 | return -EFAULT; |
| 3653 | sr->len = io->msg.iov[0].iov_len; |
| 3654 | iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1, |
| 3655 | sr->len); |
| 3656 | io->msg.iov = NULL; |
| 3657 | } else { |
| 3658 | ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
| 3659 | &io->msg.iov, &io->msg.msg.msg_iter); |
| 3660 | if (ret > 0) |
| 3661 | ret = 0; |
| 3662 | } |
| 3663 | |
| 3664 | return ret; |
| 3665 | } |
| 3666 | |
| 3667 | #ifdef CONFIG_COMPAT |
| 3668 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
| 3669 | struct io_async_ctx *io) |
| 3670 | { |
| 3671 | struct compat_msghdr __user *msg_compat; |
| 3672 | struct io_sr_msg *sr = &req->sr_msg; |
| 3673 | struct compat_iovec __user *uiov; |
| 3674 | compat_uptr_t ptr; |
| 3675 | compat_size_t len; |
| 3676 | int ret; |
| 3677 | |
| 3678 | msg_compat = (struct compat_msghdr __user *) sr->msg; |
| 3679 | ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr, |
| 3680 | &ptr, &len); |
| 3681 | if (ret) |
| 3682 | return ret; |
| 3683 | |
| 3684 | uiov = compat_ptr(ptr); |
| 3685 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3686 | compat_ssize_t clen; |
| 3687 | |
| 3688 | if (len > 1) |
| 3689 | return -EINVAL; |
| 3690 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3691 | return -EFAULT; |
| 3692 | if (__get_user(clen, &uiov->iov_len)) |
| 3693 | return -EFAULT; |
| 3694 | if (clen < 0) |
| 3695 | return -EINVAL; |
| 3696 | sr->len = io->msg.iov[0].iov_len; |
| 3697 | io->msg.iov = NULL; |
| 3698 | } else { |
| 3699 | ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV, |
| 3700 | &io->msg.iov, |
| 3701 | &io->msg.msg.msg_iter); |
| 3702 | if (ret < 0) |
| 3703 | return ret; |
| 3704 | } |
| 3705 | |
| 3706 | return 0; |
| 3707 | } |
| 3708 | #endif |
| 3709 | |
| 3710 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) |
| 3711 | { |
| 3712 | io->msg.iov = io->msg.fast_iov; |
| 3713 | |
| 3714 | #ifdef CONFIG_COMPAT |
| 3715 | if (req->ctx->compat) |
| 3716 | return __io_compat_recvmsg_copy_hdr(req, io); |
| 3717 | #endif |
| 3718 | |
| 3719 | return __io_recvmsg_copy_hdr(req, io); |
| 3720 | } |
| 3721 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3722 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
| 3723 | int *cflags, bool needs_lock) |
| 3724 | { |
| 3725 | struct io_sr_msg *sr = &req->sr_msg; |
| 3726 | struct io_buffer *kbuf; |
| 3727 | |
| 3728 | if (!(req->flags & REQ_F_BUFFER_SELECT)) |
| 3729 | return NULL; |
| 3730 | |
| 3731 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 3732 | if (IS_ERR(kbuf)) |
| 3733 | return kbuf; |
| 3734 | |
| 3735 | sr->kbuf = kbuf; |
| 3736 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3737 | |
| 3738 | *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 3739 | *cflags |= IORING_CQE_F_BUFFER; |
| 3740 | return kbuf; |
| 3741 | } |
| 3742 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3743 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3744 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3745 | { |
| 3746 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3747 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3748 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3749 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3750 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3751 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3752 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3753 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3754 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3755 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3756 | #ifdef CONFIG_COMPAT |
| 3757 | if (req->ctx->compat) |
| 3758 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3759 | #endif |
| 3760 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3761 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3762 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3763 | /* iovec is already imported */ |
| 3764 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3765 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3766 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3767 | ret = io_recvmsg_copy_hdr(req, io); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3768 | if (!ret) |
| 3769 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3770 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3771 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3772 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3773 | #endif |
| 3774 | } |
| 3775 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3776 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3777 | { |
| 3778 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3779 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3780 | struct socket *sock; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3781 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3782 | |
| 3783 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3784 | return -EINVAL; |
| 3785 | |
| 3786 | sock = sock_from_file(req->file, &ret); |
| 3787 | if (sock) { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3788 | struct io_buffer *kbuf; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3789 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3790 | unsigned flags; |
| 3791 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3792 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3793 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3794 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3795 | /* if iov is set, it's allocated already */ |
| 3796 | if (!kmsg->iov) |
| 3797 | kmsg->iov = kmsg->fast_iov; |
| 3798 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3799 | } else { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3800 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3801 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3802 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3803 | ret = io_recvmsg_copy_hdr(req, &io); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3804 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3805 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3806 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3807 | |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3808 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 3809 | if (IS_ERR(kbuf)) { |
| 3810 | return PTR_ERR(kbuf); |
| 3811 | } else if (kbuf) { |
| 3812 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3813 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 3814 | 1, req->sr_msg.len); |
| 3815 | } |
| 3816 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3817 | flags = req->sr_msg.msg_flags; |
| 3818 | if (flags & MSG_DONTWAIT) |
| 3819 | req->flags |= REQ_F_NOWAIT; |
| 3820 | else if (force_nonblock) |
| 3821 | flags |= MSG_DONTWAIT; |
| 3822 | |
| 3823 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3824 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3825 | if (force_nonblock && ret == -EAGAIN) |
| 3826 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3827 | if (ret == -ERESTARTSYS) |
| 3828 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3829 | } |
| 3830 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3831 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3832 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3833 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 3834 | __io_cqring_add_event(req, ret, cflags); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3835 | if (ret < 0) |
| 3836 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3837 | io_put_req(req); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3838 | return 0; |
| 3839 | #else |
| 3840 | return -EOPNOTSUPP; |
| 3841 | #endif |
| 3842 | } |
| 3843 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3844 | static int io_recv(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3845 | { |
| 3846 | #if defined(CONFIG_NET) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3847 | struct io_buffer *kbuf = NULL; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3848 | struct socket *sock; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3849 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3850 | |
| 3851 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3852 | return -EINVAL; |
| 3853 | |
| 3854 | sock = sock_from_file(req->file, &ret); |
| 3855 | if (sock) { |
| 3856 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3857 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3858 | struct msghdr msg; |
| 3859 | struct iovec iov; |
| 3860 | unsigned flags; |
| 3861 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3862 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 3863 | if (IS_ERR(kbuf)) |
| 3864 | return PTR_ERR(kbuf); |
| 3865 | else if (kbuf) |
| 3866 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3867 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3868 | ret = import_single_range(READ, buf, sr->len, &iov, |
| 3869 | &msg.msg_iter); |
| 3870 | if (ret) { |
| 3871 | kfree(kbuf); |
| 3872 | return ret; |
| 3873 | } |
| 3874 | |
| 3875 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3876 | msg.msg_name = NULL; |
| 3877 | msg.msg_control = NULL; |
| 3878 | msg.msg_controllen = 0; |
| 3879 | msg.msg_namelen = 0; |
| 3880 | msg.msg_iocb = NULL; |
| 3881 | msg.msg_flags = 0; |
| 3882 | |
| 3883 | flags = req->sr_msg.msg_flags; |
| 3884 | if (flags & MSG_DONTWAIT) |
| 3885 | req->flags |= REQ_F_NOWAIT; |
| 3886 | else if (force_nonblock) |
| 3887 | flags |= MSG_DONTWAIT; |
| 3888 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3889 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3890 | if (force_nonblock && ret == -EAGAIN) |
| 3891 | return -EAGAIN; |
| 3892 | if (ret == -ERESTARTSYS) |
| 3893 | ret = -EINTR; |
| 3894 | } |
| 3895 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3896 | kfree(kbuf); |
| 3897 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3898 | __io_cqring_add_event(req, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3899 | if (ret < 0) |
| 3900 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3901 | io_put_req(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3902 | return 0; |
| 3903 | #else |
| 3904 | return -EOPNOTSUPP; |
| 3905 | #endif |
| 3906 | } |
| 3907 | |
| 3908 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3909 | 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] | 3910 | { |
| 3911 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3912 | struct io_accept *accept = &req->accept; |
| 3913 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3914 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3915 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3916 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3917 | return -EINVAL; |
| 3918 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3919 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3920 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3921 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3922 | return 0; |
| 3923 | #else |
| 3924 | return -EOPNOTSUPP; |
| 3925 | #endif |
| 3926 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3927 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3928 | #if defined(CONFIG_NET) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3929 | static int __io_accept(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3930 | { |
| 3931 | struct io_accept *accept = &req->accept; |
| 3932 | unsigned file_flags; |
| 3933 | int ret; |
| 3934 | |
| 3935 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3936 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3937 | accept->addr_len, accept->flags); |
| 3938 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3939 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3940 | if (ret == -ERESTARTSYS) |
| 3941 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3942 | if (ret < 0) |
| 3943 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3944 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3945 | io_put_req(req); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3946 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3950 | { |
| 3951 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3952 | |
| 3953 | if (io_req_cancelled(req)) |
| 3954 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3955 | __io_accept(req, false); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 3956 | io_steal_work(req, workptr); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3957 | } |
| 3958 | #endif |
| 3959 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3960 | static int io_accept(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3961 | { |
| 3962 | #if defined(CONFIG_NET) |
| 3963 | int ret; |
| 3964 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3965 | ret = __io_accept(req, force_nonblock); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3966 | if (ret == -EAGAIN && force_nonblock) { |
| 3967 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3968 | return -EAGAIN; |
| 3969 | } |
| 3970 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3971 | #else |
| 3972 | return -EOPNOTSUPP; |
| 3973 | #endif |
| 3974 | } |
| 3975 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3976 | 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] | 3977 | { |
| 3978 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3979 | struct io_connect *conn = &req->connect; |
| 3980 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3981 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3982 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3983 | return -EINVAL; |
| 3984 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3985 | return -EINVAL; |
| 3986 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3987 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3988 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3989 | |
| 3990 | if (!io) |
| 3991 | return 0; |
| 3992 | |
| 3993 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3994 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3995 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3996 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3997 | #endif |
| 3998 | } |
| 3999 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4000 | static int io_connect(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4001 | { |
| 4002 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4003 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4004 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4005 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4006 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4007 | if (req->io) { |
| 4008 | io = req->io; |
| 4009 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4010 | ret = move_addr_to_kernel(req->connect.addr, |
| 4011 | req->connect.addr_len, |
| 4012 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4013 | if (ret) |
| 4014 | goto out; |
| 4015 | io = &__io; |
| 4016 | } |
| 4017 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4018 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4019 | |
| 4020 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 4021 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4022 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4023 | if (req->io) |
| 4024 | return -EAGAIN; |
| 4025 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4026 | ret = -ENOMEM; |
| 4027 | goto out; |
| 4028 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4029 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4030 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4031 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4032 | if (ret == -ERESTARTSYS) |
| 4033 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4034 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4035 | if (ret < 0) |
| 4036 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4037 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4038 | io_put_req(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4039 | return 0; |
| 4040 | #else |
| 4041 | return -EOPNOTSUPP; |
| 4042 | #endif |
| 4043 | } |
| 4044 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4045 | struct io_poll_table { |
| 4046 | struct poll_table_struct pt; |
| 4047 | struct io_kiocb *req; |
| 4048 | int error; |
| 4049 | }; |
| 4050 | |
| 4051 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
| 4052 | struct wait_queue_head *head) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4053 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4054 | if (unlikely(poll->head)) { |
| 4055 | pt->error = -EINVAL; |
| 4056 | return; |
| 4057 | } |
| 4058 | |
| 4059 | pt->error = 0; |
| 4060 | poll->head = head; |
| 4061 | add_wait_queue(head, &poll->wait); |
| 4062 | } |
| 4063 | |
| 4064 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4065 | struct poll_table_struct *p) |
| 4066 | { |
| 4067 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4068 | |
| 4069 | __io_queue_proc(&pt->req->apoll->poll, pt, head); |
| 4070 | } |
| 4071 | |
| 4072 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4073 | __poll_t mask, task_work_func_t func) |
| 4074 | { |
| 4075 | struct task_struct *tsk; |
| 4076 | |
| 4077 | /* for instances that support it check for an event match first: */ |
| 4078 | if (mask && !(mask & poll->events)) |
| 4079 | return 0; |
| 4080 | |
| 4081 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4082 | |
| 4083 | list_del_init(&poll->wait.entry); |
| 4084 | |
| 4085 | tsk = req->task; |
| 4086 | req->result = mask; |
| 4087 | init_task_work(&req->task_work, func); |
| 4088 | /* |
| 4089 | * If this fails, then the task is exiting. If that is the case, then |
| 4090 | * the exit check will ultimately cancel these work items. Hence we |
| 4091 | * don't need to check here and handle it specifically. |
| 4092 | */ |
| 4093 | task_work_add(tsk, &req->task_work, true); |
| 4094 | wake_up_process(tsk); |
| 4095 | return 1; |
| 4096 | } |
| 4097 | |
| 4098 | static void io_async_task_func(struct callback_head *cb) |
| 4099 | { |
| 4100 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4101 | struct async_poll *apoll = req->apoll; |
| 4102 | struct io_ring_ctx *ctx = req->ctx; |
| 4103 | |
| 4104 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 4105 | |
| 4106 | WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry)); |
| 4107 | |
| 4108 | if (hash_hashed(&req->hash_node)) { |
| 4109 | spin_lock_irq(&ctx->completion_lock); |
| 4110 | hash_del(&req->hash_node); |
| 4111 | spin_unlock_irq(&ctx->completion_lock); |
| 4112 | } |
| 4113 | |
| 4114 | /* restore ->work in case we need to retry again */ |
| 4115 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
| 4116 | |
| 4117 | __set_current_state(TASK_RUNNING); |
| 4118 | mutex_lock(&ctx->uring_lock); |
| 4119 | __io_queue_sqe(req, NULL); |
| 4120 | mutex_unlock(&ctx->uring_lock); |
| 4121 | |
| 4122 | kfree(apoll); |
| 4123 | } |
| 4124 | |
| 4125 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4126 | void *key) |
| 4127 | { |
| 4128 | struct io_kiocb *req = wait->private; |
| 4129 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 4130 | |
| 4131 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 4132 | key_to_poll(key)); |
| 4133 | |
| 4134 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 4135 | } |
| 4136 | |
| 4137 | static void io_poll_req_insert(struct io_kiocb *req) |
| 4138 | { |
| 4139 | struct io_ring_ctx *ctx = req->ctx; |
| 4140 | struct hlist_head *list; |
| 4141 | |
| 4142 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 4143 | hlist_add_head(&req->hash_node, list); |
| 4144 | } |
| 4145 | |
| 4146 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 4147 | struct io_poll_iocb *poll, |
| 4148 | struct io_poll_table *ipt, __poll_t mask, |
| 4149 | wait_queue_func_t wake_func) |
| 4150 | __acquires(&ctx->completion_lock) |
| 4151 | { |
| 4152 | struct io_ring_ctx *ctx = req->ctx; |
| 4153 | bool cancel = false; |
| 4154 | |
| 4155 | poll->file = req->file; |
| 4156 | poll->head = NULL; |
| 4157 | poll->done = poll->canceled = false; |
| 4158 | poll->events = mask; |
| 4159 | |
| 4160 | ipt->pt._key = mask; |
| 4161 | ipt->req = req; |
| 4162 | ipt->error = -EINVAL; |
| 4163 | |
| 4164 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4165 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4166 | poll->wait.private = req; |
| 4167 | |
| 4168 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 4169 | |
| 4170 | spin_lock_irq(&ctx->completion_lock); |
| 4171 | if (likely(poll->head)) { |
| 4172 | spin_lock(&poll->head->lock); |
| 4173 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 4174 | if (ipt->error) |
| 4175 | cancel = true; |
| 4176 | ipt->error = 0; |
| 4177 | mask = 0; |
| 4178 | } |
| 4179 | if (mask || ipt->error) |
| 4180 | list_del_init(&poll->wait.entry); |
| 4181 | else if (cancel) |
| 4182 | WRITE_ONCE(poll->canceled, true); |
| 4183 | else if (!poll->done) /* actually waiting for an event */ |
| 4184 | io_poll_req_insert(req); |
| 4185 | spin_unlock(&poll->head->lock); |
| 4186 | } |
| 4187 | |
| 4188 | return mask; |
| 4189 | } |
| 4190 | |
| 4191 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 4192 | { |
| 4193 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 4194 | struct io_ring_ctx *ctx = req->ctx; |
| 4195 | struct async_poll *apoll; |
| 4196 | struct io_poll_table ipt; |
| 4197 | __poll_t mask, ret; |
| 4198 | |
| 4199 | if (!req->file || !file_can_poll(req->file)) |
| 4200 | return false; |
| 4201 | if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED)) |
| 4202 | return false; |
| 4203 | if (!def->pollin && !def->pollout) |
| 4204 | return false; |
| 4205 | |
| 4206 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 4207 | if (unlikely(!apoll)) |
| 4208 | return false; |
| 4209 | |
| 4210 | req->flags |= REQ_F_POLLED; |
| 4211 | memcpy(&apoll->work, &req->work, sizeof(req->work)); |
| 4212 | |
| 4213 | /* |
| 4214 | * Don't need a reference here, as we're adding it to the task |
| 4215 | * task_works list. If the task exits, the list is pruned. |
| 4216 | */ |
| 4217 | req->task = current; |
| 4218 | req->apoll = apoll; |
| 4219 | INIT_HLIST_NODE(&req->hash_node); |
| 4220 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4221 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4222 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 4223 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4224 | if (def->pollout) |
| 4225 | mask |= POLLOUT | POLLWRNORM; |
| 4226 | mask |= POLLERR | POLLPRI; |
| 4227 | |
| 4228 | ipt.pt._qproc = io_async_queue_proc; |
| 4229 | |
| 4230 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 4231 | io_async_wake); |
| 4232 | if (ret) { |
| 4233 | ipt.error = 0; |
| 4234 | apoll->poll.done = true; |
| 4235 | spin_unlock_irq(&ctx->completion_lock); |
| 4236 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
| 4237 | kfree(apoll); |
| 4238 | return false; |
| 4239 | } |
| 4240 | spin_unlock_irq(&ctx->completion_lock); |
| 4241 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 4242 | apoll->poll.events); |
| 4243 | return true; |
| 4244 | } |
| 4245 | |
| 4246 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 4247 | struct io_poll_iocb *poll) |
| 4248 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4249 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4250 | |
| 4251 | spin_lock(&poll->head->lock); |
| 4252 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 4253 | if (!list_empty(&poll->wait.entry)) { |
| 4254 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4255 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4256 | } |
| 4257 | spin_unlock(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4258 | return do_complete; |
| 4259 | } |
| 4260 | |
| 4261 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 4262 | { |
| 4263 | bool do_complete; |
| 4264 | |
| 4265 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 4266 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 4267 | } else { |
| 4268 | /* non-poll requests have submit ref still */ |
| 4269 | do_complete = __io_poll_remove_one(req, &req->apoll->poll); |
| 4270 | if (do_complete) |
| 4271 | io_put_req(req); |
| 4272 | } |
| 4273 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4274 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4275 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4276 | if (do_complete) { |
| 4277 | io_cqring_fill_event(req, -ECANCELED); |
| 4278 | io_commit_cqring(req->ctx); |
| 4279 | req->flags |= REQ_F_COMP_LOCKED; |
| 4280 | io_put_req(req); |
| 4281 | } |
| 4282 | |
| 4283 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 4287 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4288 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4289 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4290 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4291 | |
| 4292 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4293 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 4294 | struct hlist_head *list; |
| 4295 | |
| 4296 | list = &ctx->cancel_hash[i]; |
| 4297 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 4298 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4299 | } |
| 4300 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4301 | |
| 4302 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4303 | } |
| 4304 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4305 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 4306 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4307 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4308 | struct io_kiocb *req; |
| 4309 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4310 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 4311 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4312 | if (sqe_addr != req->user_data) |
| 4313 | continue; |
| 4314 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4315 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4316 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | return -ENOENT; |
| 4320 | } |
| 4321 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4322 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 4323 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4324 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4325 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4326 | return -EINVAL; |
| 4327 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 4328 | sqe->poll_events) |
| 4329 | return -EINVAL; |
| 4330 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4331 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4332 | return 0; |
| 4333 | } |
| 4334 | |
| 4335 | /* |
| 4336 | * Find a running poll command that matches one specified in sqe->addr, |
| 4337 | * and remove it if found. |
| 4338 | */ |
| 4339 | static int io_poll_remove(struct io_kiocb *req) |
| 4340 | { |
| 4341 | struct io_ring_ctx *ctx = req->ctx; |
| 4342 | u64 addr; |
| 4343 | int ret; |
| 4344 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4345 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4346 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4347 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4348 | spin_unlock_irq(&ctx->completion_lock); |
| 4349 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4350 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4351 | if (ret < 0) |
| 4352 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4353 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4354 | return 0; |
| 4355 | } |
| 4356 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4357 | 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] | 4358 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4359 | struct io_ring_ctx *ctx = req->ctx; |
| 4360 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4361 | req->poll.done = true; |
Pavel Begunkov | b0a2034 | 2020-02-28 10:36:35 +0300 | [diff] [blame] | 4362 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4363 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4364 | } |
| 4365 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4366 | 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] | 4367 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4368 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4369 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4370 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4371 | hash_del(&req->hash_node); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4372 | io_poll_complete(req, req->result, 0); |
| 4373 | req->flags |= REQ_F_COMP_LOCKED; |
| 4374 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4375 | spin_unlock_irq(&ctx->completion_lock); |
| 4376 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4377 | io_cqring_ev_posted(ctx); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4378 | } |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 4379 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4380 | static void io_poll_task_func(struct callback_head *cb) |
| 4381 | { |
| 4382 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4383 | struct io_kiocb *nxt = NULL; |
| 4384 | |
| 4385 | io_poll_task_handler(req, &nxt); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4386 | if (nxt) { |
| 4387 | struct io_ring_ctx *ctx = nxt->ctx; |
| 4388 | |
| 4389 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4390 | __io_queue_sqe(nxt, NULL); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4391 | mutex_unlock(&ctx->uring_lock); |
| 4392 | } |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 4393 | } |
| 4394 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4395 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4396 | void *key) |
| 4397 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 4398 | struct io_kiocb *req = wait->private; |
| 4399 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4400 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4401 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4404 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4405 | struct poll_table_struct *p) |
| 4406 | { |
| 4407 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4408 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4409 | __io_queue_proc(&pt->req->poll, pt, head); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4410 | } |
| 4411 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4412 | 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] | 4413 | { |
| 4414 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4415 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4416 | |
| 4417 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4418 | return -EINVAL; |
| 4419 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 4420 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4421 | if (!poll->file) |
| 4422 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4423 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4424 | events = READ_ONCE(sqe->poll_events); |
| 4425 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4426 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4427 | /* |
| 4428 | * Don't need a reference here, as we're adding it to the task |
| 4429 | * task_works list. If the task exits, the list is pruned. |
| 4430 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4431 | req->task = current; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4432 | return 0; |
| 4433 | } |
| 4434 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4435 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4436 | { |
| 4437 | struct io_poll_iocb *poll = &req->poll; |
| 4438 | struct io_ring_ctx *ctx = req->ctx; |
| 4439 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4440 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4441 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4442 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4443 | INIT_LIST_HEAD(&req->list); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4444 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4445 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4446 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 4447 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4448 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4449 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4450 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4451 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4452 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4453 | spin_unlock_irq(&ctx->completion_lock); |
| 4454 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4455 | if (mask) { |
| 4456 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4457 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4458 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4459 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4460 | } |
| 4461 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4462 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 4463 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4464 | struct io_timeout_data *data = container_of(timer, |
| 4465 | struct io_timeout_data, timer); |
| 4466 | struct io_kiocb *req = data->req; |
| 4467 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4468 | unsigned long flags; |
| 4469 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4470 | atomic_inc(&ctx->cq_timeouts); |
| 4471 | |
| 4472 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4473 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4474 | * We could be racing with timeout deletion. If the list is empty, |
| 4475 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4476 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4477 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4478 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4479 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4480 | /* |
| 4481 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4482 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4483 | * pointer will be increased, otherwise other timeout reqs may |
| 4484 | * return in advance without waiting for enough wait_nr. |
| 4485 | */ |
| 4486 | prev = req; |
| 4487 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 4488 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4489 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4490 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4491 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4492 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4493 | io_commit_cqring(ctx); |
| 4494 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4495 | |
| 4496 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4497 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4498 | io_put_req(req); |
| 4499 | return HRTIMER_NORESTART; |
| 4500 | } |
| 4501 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4502 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 4503 | { |
| 4504 | struct io_kiocb *req; |
| 4505 | int ret = -ENOENT; |
| 4506 | |
| 4507 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 4508 | if (user_data == req->user_data) { |
| 4509 | list_del_init(&req->list); |
| 4510 | ret = 0; |
| 4511 | break; |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | if (ret == -ENOENT) |
| 4516 | return ret; |
| 4517 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4518 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4519 | if (ret == -1) |
| 4520 | return -EALREADY; |
| 4521 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4522 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4523 | io_cqring_fill_event(req, -ECANCELED); |
| 4524 | io_put_req(req); |
| 4525 | return 0; |
| 4526 | } |
| 4527 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4528 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 4529 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4530 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4531 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4532 | return -EINVAL; |
| 4533 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 4534 | return -EINVAL; |
| 4535 | |
| 4536 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 4537 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 4538 | if (req->timeout.flags) |
| 4539 | return -EINVAL; |
| 4540 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4541 | return 0; |
| 4542 | } |
| 4543 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4544 | /* |
| 4545 | * Remove or update an existing timeout command |
| 4546 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4547 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4548 | { |
| 4549 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4550 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4551 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4552 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4553 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4554 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4555 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4556 | io_commit_cqring(ctx); |
| 4557 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4558 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4559 | if (ret < 0) |
| 4560 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4561 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4562 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4563 | } |
| 4564 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4565 | 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] | 4566 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4567 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4568 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4569 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4570 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4571 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4572 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4573 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4574 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4575 | if (sqe->off && is_timeout_link) |
| 4576 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4577 | flags = READ_ONCE(sqe->timeout_flags); |
| 4578 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4579 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 4580 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4581 | req->timeout.count = READ_ONCE(sqe->off); |
| 4582 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4583 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4584 | return -ENOMEM; |
| 4585 | |
| 4586 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4587 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4588 | req->flags |= REQ_F_TIMEOUT; |
| 4589 | |
| 4590 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4591 | return -EFAULT; |
| 4592 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4593 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4594 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4595 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4596 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4597 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4598 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 4599 | return 0; |
| 4600 | } |
| 4601 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4602 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4603 | { |
| 4604 | unsigned count; |
| 4605 | struct io_ring_ctx *ctx = req->ctx; |
| 4606 | struct io_timeout_data *data; |
| 4607 | struct list_head *entry; |
| 4608 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4609 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4610 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4611 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4612 | /* |
| 4613 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4614 | * timeout event to be satisfied. If it isn't set, then this is |
| 4615 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4616 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4617 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4618 | if (!count) { |
| 4619 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 4620 | spin_lock_irq(&ctx->completion_lock); |
| 4621 | entry = ctx->timeout_list.prev; |
| 4622 | goto add; |
| 4623 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4624 | |
| 4625 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4626 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4627 | |
| 4628 | /* |
| 4629 | * Insertion sort, ensuring the first entry in the list is always |
| 4630 | * the one we need first. |
| 4631 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4632 | spin_lock_irq(&ctx->completion_lock); |
| 4633 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 4634 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4635 | unsigned nxt_sq_head; |
| 4636 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4637 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4638 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4639 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 4640 | continue; |
| 4641 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4642 | /* |
| 4643 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 4644 | * long to store it. |
| 4645 | */ |
| 4646 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 4647 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 4648 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4649 | |
| 4650 | /* |
| 4651 | * cached_sq_head may overflow, and it will never overflow twice |
| 4652 | * once there is some timeout req still be valid. |
| 4653 | */ |
| 4654 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 4655 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4656 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4657 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4658 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4659 | |
| 4660 | /* |
| 4661 | * Sequence of reqs after the insert one and itself should |
| 4662 | * be adjusted because each timeout req consumes a slot. |
| 4663 | */ |
| 4664 | span++; |
| 4665 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4666 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4667 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4668 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4669 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4670 | data->timer.function = io_timeout_fn; |
| 4671 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4672 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4673 | return 0; |
| 4674 | } |
| 4675 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4676 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4677 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4678 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4679 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4680 | return req->user_data == (unsigned long) data; |
| 4681 | } |
| 4682 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4683 | 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] | 4684 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4685 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4686 | int ret = 0; |
| 4687 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4688 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 4689 | switch (cancel_ret) { |
| 4690 | case IO_WQ_CANCEL_OK: |
| 4691 | ret = 0; |
| 4692 | break; |
| 4693 | case IO_WQ_CANCEL_RUNNING: |
| 4694 | ret = -EALREADY; |
| 4695 | break; |
| 4696 | case IO_WQ_CANCEL_NOTFOUND: |
| 4697 | ret = -ENOENT; |
| 4698 | break; |
| 4699 | } |
| 4700 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4701 | return ret; |
| 4702 | } |
| 4703 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4704 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 4705 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4706 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4707 | { |
| 4708 | unsigned long flags; |
| 4709 | int ret; |
| 4710 | |
| 4711 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 4712 | if (ret != -ENOENT) { |
| 4713 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4714 | goto done; |
| 4715 | } |
| 4716 | |
| 4717 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4718 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4719 | if (ret != -ENOENT) |
| 4720 | goto done; |
| 4721 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4722 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4723 | if (!ret) |
| 4724 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4725 | io_cqring_fill_event(req, ret); |
| 4726 | io_commit_cqring(ctx); |
| 4727 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4728 | io_cqring_ev_posted(ctx); |
| 4729 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4730 | if (ret < 0) |
| 4731 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4732 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4733 | } |
| 4734 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4735 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4736 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4737 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4738 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4739 | return -EINVAL; |
| 4740 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4741 | sqe->cancel_flags) |
| 4742 | return -EINVAL; |
| 4743 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4744 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4745 | return 0; |
| 4746 | } |
| 4747 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4748 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4749 | { |
| 4750 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4751 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4752 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4753 | return 0; |
| 4754 | } |
| 4755 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4756 | static int io_files_update_prep(struct io_kiocb *req, |
| 4757 | const struct io_uring_sqe *sqe) |
| 4758 | { |
| 4759 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4760 | return -EINVAL; |
| 4761 | |
| 4762 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4763 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4764 | if (!req->files_update.nr_args) |
| 4765 | return -EINVAL; |
| 4766 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4767 | return 0; |
| 4768 | } |
| 4769 | |
| 4770 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4771 | { |
| 4772 | struct io_ring_ctx *ctx = req->ctx; |
| 4773 | struct io_uring_files_update up; |
| 4774 | int ret; |
| 4775 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4776 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4777 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4778 | |
| 4779 | up.offset = req->files_update.offset; |
| 4780 | up.fds = req->files_update.arg; |
| 4781 | |
| 4782 | mutex_lock(&ctx->uring_lock); |
| 4783 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4784 | mutex_unlock(&ctx->uring_lock); |
| 4785 | |
| 4786 | if (ret < 0) |
| 4787 | req_set_fail_links(req); |
| 4788 | io_cqring_add_event(req, ret); |
| 4789 | io_put_req(req); |
| 4790 | return 0; |
| 4791 | } |
| 4792 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4793 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4794 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4795 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4796 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4797 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4798 | if (io_op_defs[req->opcode].file_table) { |
| 4799 | ret = io_grab_files(req); |
| 4800 | if (unlikely(ret)) |
| 4801 | return ret; |
| 4802 | } |
| 4803 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4804 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4805 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4806 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4807 | case IORING_OP_NOP: |
| 4808 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4809 | case IORING_OP_READV: |
| 4810 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4811 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4812 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4813 | break; |
| 4814 | case IORING_OP_WRITEV: |
| 4815 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4816 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4817 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4818 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4819 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4820 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4821 | break; |
| 4822 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4823 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4824 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4825 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4826 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4827 | break; |
| 4828 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4829 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4830 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4831 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4832 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4833 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4834 | break; |
| 4835 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4836 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4837 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4838 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4839 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4840 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4841 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4842 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4843 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4844 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4845 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4846 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4847 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4848 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4849 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4850 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4851 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4852 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4853 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4854 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4855 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4856 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4857 | case IORING_OP_FALLOCATE: |
| 4858 | ret = io_fallocate_prep(req, sqe); |
| 4859 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4860 | case IORING_OP_OPENAT: |
| 4861 | ret = io_openat_prep(req, sqe); |
| 4862 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4863 | case IORING_OP_CLOSE: |
| 4864 | ret = io_close_prep(req, sqe); |
| 4865 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4866 | case IORING_OP_FILES_UPDATE: |
| 4867 | ret = io_files_update_prep(req, sqe); |
| 4868 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4869 | case IORING_OP_STATX: |
| 4870 | ret = io_statx_prep(req, sqe); |
| 4871 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4872 | case IORING_OP_FADVISE: |
| 4873 | ret = io_fadvise_prep(req, sqe); |
| 4874 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4875 | case IORING_OP_MADVISE: |
| 4876 | ret = io_madvise_prep(req, sqe); |
| 4877 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4878 | case IORING_OP_OPENAT2: |
| 4879 | ret = io_openat2_prep(req, sqe); |
| 4880 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4881 | case IORING_OP_EPOLL_CTL: |
| 4882 | ret = io_epoll_ctl_prep(req, sqe); |
| 4883 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4884 | case IORING_OP_SPLICE: |
| 4885 | ret = io_splice_prep(req, sqe); |
| 4886 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4887 | case IORING_OP_PROVIDE_BUFFERS: |
| 4888 | ret = io_provide_buffers_prep(req, sqe); |
| 4889 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame^] | 4890 | case IORING_OP_REMOVE_BUFFERS: |
| 4891 | ret = io_remove_buffers_prep(req, sqe); |
| 4892 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4893 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4894 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4895 | req->opcode); |
| 4896 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4897 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4898 | } |
| 4899 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4900 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4901 | } |
| 4902 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4903 | 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] | 4904 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4905 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4906 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4907 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4908 | /* Still need defer if there is pending req in defer list. */ |
| 4909 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4910 | return 0; |
| 4911 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4912 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4913 | return -EAGAIN; |
| 4914 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4915 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4916 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4917 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4918 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4919 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4920 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4921 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4922 | return 0; |
| 4923 | } |
| 4924 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4925 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4926 | list_add_tail(&req->list, &ctx->defer_list); |
| 4927 | spin_unlock_irq(&ctx->completion_lock); |
| 4928 | return -EIOCBQUEUED; |
| 4929 | } |
| 4930 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4931 | static void io_cleanup_req(struct io_kiocb *req) |
| 4932 | { |
| 4933 | struct io_async_ctx *io = req->io; |
| 4934 | |
| 4935 | switch (req->opcode) { |
| 4936 | case IORING_OP_READV: |
| 4937 | case IORING_OP_READ_FIXED: |
| 4938 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4939 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4940 | kfree((void *)(unsigned long)req->rw.addr); |
| 4941 | /* fallthrough */ |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4942 | case IORING_OP_WRITEV: |
| 4943 | case IORING_OP_WRITE_FIXED: |
| 4944 | case IORING_OP_WRITE: |
| 4945 | if (io->rw.iov != io->rw.fast_iov) |
| 4946 | kfree(io->rw.iov); |
| 4947 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4948 | case IORING_OP_RECVMSG: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4949 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4950 | kfree(req->sr_msg.kbuf); |
| 4951 | /* fallthrough */ |
| 4952 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4953 | if (io->msg.iov != io->msg.fast_iov) |
| 4954 | kfree(io->msg.iov); |
| 4955 | break; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4956 | case IORING_OP_RECV: |
| 4957 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4958 | kfree(req->sr_msg.kbuf); |
| 4959 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4960 | case IORING_OP_OPENAT: |
| 4961 | case IORING_OP_OPENAT2: |
| 4962 | case IORING_OP_STATX: |
| 4963 | putname(req->open.filename); |
| 4964 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4965 | case IORING_OP_SPLICE: |
| 4966 | io_put_file(req, req->splice.file_in, |
| 4967 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 4968 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4972 | } |
| 4973 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4974 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4975 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4976 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4977 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4978 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4979 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4980 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4981 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4982 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4983 | break; |
| 4984 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4985 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4986 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4987 | if (sqe) { |
| 4988 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4989 | if (ret < 0) |
| 4990 | break; |
| 4991 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4992 | ret = io_read(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4993 | break; |
| 4994 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4995 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4996 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4997 | if (sqe) { |
| 4998 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4999 | if (ret < 0) |
| 5000 | break; |
| 5001 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5002 | ret = io_write(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5003 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5004 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5005 | if (sqe) { |
| 5006 | ret = io_prep_fsync(req, sqe); |
| 5007 | if (ret < 0) |
| 5008 | break; |
| 5009 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5010 | ret = io_fsync(req, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 5011 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5012 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5013 | if (sqe) { |
| 5014 | ret = io_poll_add_prep(req, sqe); |
| 5015 | if (ret) |
| 5016 | break; |
| 5017 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5018 | ret = io_poll_add(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5019 | break; |
| 5020 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5021 | if (sqe) { |
| 5022 | ret = io_poll_remove_prep(req, sqe); |
| 5023 | if (ret < 0) |
| 5024 | break; |
| 5025 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5026 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5027 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5028 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5029 | if (sqe) { |
| 5030 | ret = io_prep_sfr(req, sqe); |
| 5031 | if (ret < 0) |
| 5032 | break; |
| 5033 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5034 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 5035 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5036 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5037 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5038 | if (sqe) { |
| 5039 | ret = io_sendmsg_prep(req, sqe); |
| 5040 | if (ret < 0) |
| 5041 | break; |
| 5042 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5043 | if (req->opcode == IORING_OP_SENDMSG) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5044 | ret = io_sendmsg(req, force_nonblock); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5045 | else |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5046 | ret = io_send(req, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5047 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5048 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5049 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5050 | if (sqe) { |
| 5051 | ret = io_recvmsg_prep(req, sqe); |
| 5052 | if (ret) |
| 5053 | break; |
| 5054 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5055 | if (req->opcode == IORING_OP_RECVMSG) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5056 | ret = io_recvmsg(req, force_nonblock); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5057 | else |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5058 | ret = io_recv(req, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 5059 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5060 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5061 | if (sqe) { |
| 5062 | ret = io_timeout_prep(req, sqe, false); |
| 5063 | if (ret) |
| 5064 | break; |
| 5065 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5066 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5067 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5068 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5069 | if (sqe) { |
| 5070 | ret = io_timeout_remove_prep(req, sqe); |
| 5071 | if (ret) |
| 5072 | break; |
| 5073 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5074 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5075 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5076 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5077 | if (sqe) { |
| 5078 | ret = io_accept_prep(req, sqe); |
| 5079 | if (ret) |
| 5080 | break; |
| 5081 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5082 | ret = io_accept(req, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5083 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5084 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5085 | if (sqe) { |
| 5086 | ret = io_connect_prep(req, sqe); |
| 5087 | if (ret) |
| 5088 | break; |
| 5089 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5090 | ret = io_connect(req, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5091 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5092 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5093 | if (sqe) { |
| 5094 | ret = io_async_cancel_prep(req, sqe); |
| 5095 | if (ret) |
| 5096 | break; |
| 5097 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5098 | ret = io_async_cancel(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5099 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5100 | case IORING_OP_FALLOCATE: |
| 5101 | if (sqe) { |
| 5102 | ret = io_fallocate_prep(req, sqe); |
| 5103 | if (ret) |
| 5104 | break; |
| 5105 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5106 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5107 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5108 | case IORING_OP_OPENAT: |
| 5109 | if (sqe) { |
| 5110 | ret = io_openat_prep(req, sqe); |
| 5111 | if (ret) |
| 5112 | break; |
| 5113 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5114 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5115 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5116 | case IORING_OP_CLOSE: |
| 5117 | if (sqe) { |
| 5118 | ret = io_close_prep(req, sqe); |
| 5119 | if (ret) |
| 5120 | break; |
| 5121 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5122 | ret = io_close(req, force_nonblock); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5123 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5124 | case IORING_OP_FILES_UPDATE: |
| 5125 | if (sqe) { |
| 5126 | ret = io_files_update_prep(req, sqe); |
| 5127 | if (ret) |
| 5128 | break; |
| 5129 | } |
| 5130 | ret = io_files_update(req, force_nonblock); |
| 5131 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5132 | case IORING_OP_STATX: |
| 5133 | if (sqe) { |
| 5134 | ret = io_statx_prep(req, sqe); |
| 5135 | if (ret) |
| 5136 | break; |
| 5137 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5138 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5139 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5140 | case IORING_OP_FADVISE: |
| 5141 | if (sqe) { |
| 5142 | ret = io_fadvise_prep(req, sqe); |
| 5143 | if (ret) |
| 5144 | break; |
| 5145 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5146 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5147 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5148 | case IORING_OP_MADVISE: |
| 5149 | if (sqe) { |
| 5150 | ret = io_madvise_prep(req, sqe); |
| 5151 | if (ret) |
| 5152 | break; |
| 5153 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5154 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5155 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5156 | case IORING_OP_OPENAT2: |
| 5157 | if (sqe) { |
| 5158 | ret = io_openat2_prep(req, sqe); |
| 5159 | if (ret) |
| 5160 | break; |
| 5161 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5162 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5163 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5164 | case IORING_OP_EPOLL_CTL: |
| 5165 | if (sqe) { |
| 5166 | ret = io_epoll_ctl_prep(req, sqe); |
| 5167 | if (ret) |
| 5168 | break; |
| 5169 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5170 | ret = io_epoll_ctl(req, force_nonblock); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5171 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5172 | case IORING_OP_SPLICE: |
| 5173 | if (sqe) { |
| 5174 | ret = io_splice_prep(req, sqe); |
| 5175 | if (ret < 0) |
| 5176 | break; |
| 5177 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5178 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5179 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5180 | case IORING_OP_PROVIDE_BUFFERS: |
| 5181 | if (sqe) { |
| 5182 | ret = io_provide_buffers_prep(req, sqe); |
| 5183 | if (ret) |
| 5184 | break; |
| 5185 | } |
| 5186 | ret = io_provide_buffers(req, force_nonblock); |
| 5187 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame^] | 5188 | case IORING_OP_REMOVE_BUFFERS: |
| 5189 | if (sqe) { |
| 5190 | ret = io_remove_buffers_prep(req, sqe); |
| 5191 | if (ret) |
| 5192 | break; |
| 5193 | } |
| 5194 | ret = io_remove_buffers(req, force_nonblock); |
| 5195 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5196 | default: |
| 5197 | ret = -EINVAL; |
| 5198 | break; |
| 5199 | } |
| 5200 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5201 | if (ret) |
| 5202 | return ret; |
| 5203 | |
| 5204 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5205 | const bool in_async = io_wq_current_is_worker(); |
| 5206 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5207 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5208 | return -EAGAIN; |
| 5209 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5210 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 5211 | if (in_async) |
| 5212 | mutex_lock(&ctx->uring_lock); |
| 5213 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5214 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 5215 | |
| 5216 | if (in_async) |
| 5217 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5221 | } |
| 5222 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5223 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5224 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5225 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5226 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5227 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5228 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5229 | /* if NO_CANCEL is set, we must still run the work */ |
| 5230 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 5231 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5232 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 5233 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5234 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5235 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5236 | do { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5237 | ret = io_issue_sqe(req, NULL, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5238 | /* |
| 5239 | * We can get EAGAIN for polled IO even though we're |
| 5240 | * forcing a sync submission from here, since we can't |
| 5241 | * wait for request slots on the block side. |
| 5242 | */ |
| 5243 | if (ret != -EAGAIN) |
| 5244 | break; |
| 5245 | cond_resched(); |
| 5246 | } while (1); |
| 5247 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5248 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5249 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5250 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5251 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 5252 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5253 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5254 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 5255 | io_steal_work(req, workptr); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5256 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5257 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5258 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 5259 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5260 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 5261 | return 0; |
Jens Axboe | 0b5faf6 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 5262 | 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] | 5263 | return 0; |
| 5264 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5265 | } |
| 5266 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5267 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 5268 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5269 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5270 | struct fixed_file_table *table; |
| 5271 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5272 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 5273 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5274 | } |
| 5275 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5276 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 5277 | int fd, struct file **out_file, bool fixed) |
| 5278 | { |
| 5279 | struct io_ring_ctx *ctx = req->ctx; |
| 5280 | struct file *file; |
| 5281 | |
| 5282 | if (fixed) { |
| 5283 | if (unlikely(!ctx->file_data || |
| 5284 | (unsigned) fd >= ctx->nr_user_files)) |
| 5285 | return -EBADF; |
| 5286 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 5287 | file = io_file_from_index(ctx, fd); |
| 5288 | if (!file) |
| 5289 | return -EBADF; |
| 5290 | percpu_ref_get(&ctx->file_data->refs); |
| 5291 | } else { |
| 5292 | trace_io_uring_file_get(ctx, fd); |
| 5293 | file = __io_file_get(state, fd); |
| 5294 | if (unlikely(!file)) |
| 5295 | return -EBADF; |
| 5296 | } |
| 5297 | |
| 5298 | *out_file = file; |
| 5299 | return 0; |
| 5300 | } |
| 5301 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5302 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 5303 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5304 | { |
| 5305 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5306 | int fd; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5307 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5308 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5309 | flags = READ_ONCE(sqe->flags); |
| 5310 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5311 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5312 | if (!io_req_needs_file(req, fd)) |
| 5313 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5314 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5315 | fixed = (flags & IOSQE_FIXED_FILE); |
| 5316 | if (unlikely(!fixed && req->needs_fixed_file)) |
| 5317 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5318 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5319 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5320 | } |
| 5321 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5322 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5323 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5324 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5325 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5326 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5327 | if (req->work.files) |
| 5328 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5329 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5330 | return -EBADF; |
| 5331 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5332 | rcu_read_lock(); |
| 5333 | spin_lock_irq(&ctx->inflight_lock); |
| 5334 | /* |
| 5335 | * We use the f_ops->flush() handler to ensure that we can flush |
| 5336 | * out work accessing these files if the fd is closed. Check if |
| 5337 | * the fd has changed since we started down this path, and disallow |
| 5338 | * this operation if it has. |
| 5339 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5340 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5341 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 5342 | req->flags |= REQ_F_INFLIGHT; |
| 5343 | req->work.files = current->files; |
| 5344 | ret = 0; |
| 5345 | } |
| 5346 | spin_unlock_irq(&ctx->inflight_lock); |
| 5347 | rcu_read_unlock(); |
| 5348 | |
| 5349 | return ret; |
| 5350 | } |
| 5351 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5352 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 5353 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5354 | struct io_timeout_data *data = container_of(timer, |
| 5355 | struct io_timeout_data, timer); |
| 5356 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5357 | struct io_ring_ctx *ctx = req->ctx; |
| 5358 | struct io_kiocb *prev = NULL; |
| 5359 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5360 | |
| 5361 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5362 | |
| 5363 | /* |
| 5364 | * We don't expect the list to be empty, that will only happen if we |
| 5365 | * race with the completion of the linked work. |
| 5366 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5367 | if (!list_empty(&req->link_list)) { |
| 5368 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 5369 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5370 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5371 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5372 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 5373 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5374 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5378 | |
| 5379 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5380 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5381 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5382 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5383 | } else { |
| 5384 | io_cqring_add_event(req, -ETIME); |
| 5385 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5386 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5387 | return HRTIMER_NORESTART; |
| 5388 | } |
| 5389 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5390 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5391 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5392 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5393 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5394 | /* |
| 5395 | * If the list is now empty, then our linked request finished before |
| 5396 | * we got a chance to setup the timer |
| 5397 | */ |
| 5398 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5399 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5400 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5401 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5402 | data->timer.function = io_link_timeout_fn; |
| 5403 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 5404 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5405 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5406 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5407 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5408 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5409 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5410 | } |
| 5411 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5412 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5413 | { |
| 5414 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5415 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5416 | if (!(req->flags & REQ_F_LINK)) |
| 5417 | return NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5418 | /* for polled retry, if flag is set, we already went through here */ |
| 5419 | if (req->flags & REQ_F_POLLED) |
| 5420 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5421 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5422 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 5423 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5424 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5425 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5426 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5427 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5428 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5429 | } |
| 5430 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5431 | 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] | 5432 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5433 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5434 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5435 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5436 | int ret; |
| 5437 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5438 | again: |
| 5439 | linked_timeout = io_prep_linked_timeout(req); |
| 5440 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5441 | if (req->work.creds && req->work.creds != current_cred()) { |
| 5442 | if (old_creds) |
| 5443 | revert_creds(old_creds); |
| 5444 | if (old_creds == req->work.creds) |
| 5445 | old_creds = NULL; /* restored original creds */ |
| 5446 | else |
| 5447 | old_creds = override_creds(req->work.creds); |
| 5448 | } |
| 5449 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5450 | ret = io_issue_sqe(req, sqe, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 5451 | |
| 5452 | /* |
| 5453 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 5454 | * doesn't support non-blocking read/write attempts |
| 5455 | */ |
| 5456 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 5457 | (req->flags & REQ_F_MUST_PUNT))) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5458 | if (io_arm_poll_handler(req)) { |
| 5459 | if (linked_timeout) |
| 5460 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5461 | goto exit; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5462 | } |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5463 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5464 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5465 | ret = io_grab_files(req); |
| 5466 | if (ret) |
| 5467 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5468 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5469 | |
| 5470 | /* |
| 5471 | * Queued up for async execution, worker will release |
| 5472 | * submit reference when the iocb is actually submitted. |
| 5473 | */ |
| 5474 | io_queue_async_work(req); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5475 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5476 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5477 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5478 | err: |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5479 | nxt = NULL; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5480 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 5481 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5482 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5483 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5484 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5485 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5486 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5487 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5488 | } |
| 5489 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5490 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5491 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5492 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5493 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5494 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5495 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5496 | if (nxt) { |
| 5497 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5498 | |
| 5499 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 5500 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5501 | goto again; |
| 5502 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5503 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5504 | if (old_creds) |
| 5505 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5506 | } |
| 5507 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5508 | 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] | 5509 | { |
| 5510 | int ret; |
| 5511 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5512 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5513 | if (ret) { |
| 5514 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5515 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5516 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5517 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5518 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5519 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 5520 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5521 | ret = io_req_defer_prep(req, sqe); |
| 5522 | if (unlikely(ret < 0)) |
| 5523 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5524 | /* |
| 5525 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 5526 | * to async execution. |
| 5527 | */ |
| 5528 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 5529 | io_queue_async_work(req); |
| 5530 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5531 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5532 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5533 | } |
| 5534 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5535 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5536 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5537 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5538 | io_cqring_add_event(req, -ECANCELED); |
| 5539 | io_double_put_req(req); |
| 5540 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5541 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5542 | } |
| 5543 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5544 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5545 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 5546 | IOSQE_BUFFER_SELECT) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5547 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5548 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 5549 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5550 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5551 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5552 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5553 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5554 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5555 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5556 | |
| 5557 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5558 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5559 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5560 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5561 | } |
| 5562 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5563 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 5564 | !io_op_defs[req->opcode].buffer_select) { |
| 5565 | ret = -EOPNOTSUPP; |
| 5566 | goto err_req; |
| 5567 | } |
| 5568 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5569 | id = READ_ONCE(sqe->personality); |
| 5570 | if (id) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5571 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 5572 | if (unlikely(!req->work.creds)) { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5573 | ret = -EINVAL; |
| 5574 | goto err_req; |
| 5575 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5576 | get_cred(req->work.creds); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5577 | } |
| 5578 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 5579 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5580 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5581 | IOSQE_ASYNC | IOSQE_FIXED_FILE | |
| 5582 | IOSQE_BUFFER_SELECT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5583 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5584 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5585 | if (unlikely(ret)) { |
| 5586 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5587 | io_cqring_add_event(req, ret); |
| 5588 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5589 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5590 | } |
| 5591 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5592 | /* |
| 5593 | * If we already have a head request, queue this one for async |
| 5594 | * submittal once the head completes. If we don't have a head but |
| 5595 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 5596 | * submitted sync once the chain is complete. If none of those |
| 5597 | * conditions are true (normal request), then just queue it. |
| 5598 | */ |
| 5599 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5600 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5601 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 5602 | /* |
| 5603 | * Taking sequential execution of a link, draining both sides |
| 5604 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 5605 | * requests in the link. So, it drains the head and the |
| 5606 | * next after the link request. The last one is done via |
| 5607 | * drain_next flag to persist the effect across calls. |
| 5608 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5609 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 5610 | head->flags |= REQ_F_IO_DRAIN; |
| 5611 | ctx->drain_next = 1; |
| 5612 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5613 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5614 | ret = -EAGAIN; |
| 5615 | goto err_req; |
| 5616 | } |
| 5617 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5618 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5619 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5620 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5621 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5622 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5623 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5624 | trace_io_uring_link(ctx, req, head); |
| 5625 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5626 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5627 | /* last request of a link, enqueue the link */ |
| 5628 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 5629 | io_queue_link_head(head); |
| 5630 | *link = NULL; |
| 5631 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5632 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5633 | if (unlikely(ctx->drain_next)) { |
| 5634 | req->flags |= REQ_F_IO_DRAIN; |
| 5635 | req->ctx->drain_next = 0; |
| 5636 | } |
| 5637 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 5638 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5639 | INIT_LIST_HEAD(&req->link_list); |
| 5640 | ret = io_req_defer_prep(req, sqe); |
| 5641 | if (ret) |
| 5642 | req->flags |= REQ_F_FAIL_LINK; |
| 5643 | *link = req; |
| 5644 | } else { |
| 5645 | io_queue_sqe(req, sqe); |
| 5646 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5647 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5648 | |
| 5649 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5650 | } |
| 5651 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5652 | /* |
| 5653 | * Batched submission is done, ensure local IO is flushed out. |
| 5654 | */ |
| 5655 | static void io_submit_state_end(struct io_submit_state *state) |
| 5656 | { |
| 5657 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 5658 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5659 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 5660 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5661 | } |
| 5662 | |
| 5663 | /* |
| 5664 | * Start submission side cache. |
| 5665 | */ |
| 5666 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5667 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5668 | { |
| 5669 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5670 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5671 | state->file = NULL; |
| 5672 | state->ios_left = max_ios; |
| 5673 | } |
| 5674 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5675 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 5676 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5677 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5678 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 5679 | /* |
| 5680 | * Ensure any loads from the SQEs are done at this point, |
| 5681 | * since once we write the new head, the application could |
| 5682 | * write new data to them. |
| 5683 | */ |
| 5684 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5688 | * 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] | 5689 | * that is mapped by userspace. This means that care needs to be taken to |
| 5690 | * ensure that reads are stable, as we cannot rely on userspace always |
| 5691 | * being a good citizen. If members of the sqe are validated and then later |
| 5692 | * used, it's important that those reads are done through READ_ONCE() to |
| 5693 | * prevent a re-load down the line. |
| 5694 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5695 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 5696 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5697 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5698 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5699 | unsigned head; |
| 5700 | |
| 5701 | /* |
| 5702 | * The cached sq head (or cq tail) serves two purposes: |
| 5703 | * |
| 5704 | * 1) allows us to batch the cost of updating the user visible |
| 5705 | * head updates. |
| 5706 | * 2) allows the kernel side to track the head on its own, even |
| 5707 | * though the application is the one updating it. |
| 5708 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5709 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 5710 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5711 | /* |
| 5712 | * All io need record the previous position, if LINK vs DARIN, |
| 5713 | * it can be used to mark the position of the first IO in the |
| 5714 | * link list. |
| 5715 | */ |
| 5716 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5717 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 5718 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 5719 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5720 | ctx->cached_sq_head++; |
| 5721 | return true; |
| 5722 | } |
| 5723 | |
| 5724 | /* drop invalid entries */ |
| 5725 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 5726 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5727 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5728 | return false; |
| 5729 | } |
| 5730 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5731 | 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] | 5732 | struct file *ring_file, int ring_fd, |
| 5733 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5734 | { |
| 5735 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5736 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5737 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5738 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5739 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 5740 | /* 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] | 5741 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 5742 | if (!list_empty(&ctx->cq_overflow_list) && |
| 5743 | !io_cqring_overflow_flush(ctx, false)) |
| 5744 | return -EBUSY; |
| 5745 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5746 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5747 | /* make sure SQ entry isn't read before tail */ |
| 5748 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 5749 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5750 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 5751 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5752 | |
| 5753 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5754 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5755 | statep = &state; |
| 5756 | } |
| 5757 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5758 | ctx->ring_fd = ring_fd; |
| 5759 | ctx->ring_file = ring_file; |
| 5760 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5761 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5762 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5763 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5764 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5765 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5766 | req = io_get_req(ctx, statep); |
| 5767 | if (unlikely(!req)) { |
| 5768 | if (!submitted) |
| 5769 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5770 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5771 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5772 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5773 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5774 | break; |
| 5775 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5776 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5777 | /* will complete beyond this point, count as submitted */ |
| 5778 | submitted++; |
| 5779 | |
| 5780 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5781 | err = -EINVAL; |
| 5782 | fail_req: |
| 5783 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5784 | io_double_put_req(req); |
| 5785 | break; |
| 5786 | } |
| 5787 | |
| 5788 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5789 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5790 | if (unlikely(mm_fault)) { |
| 5791 | err = -EFAULT; |
| 5792 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5793 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5794 | use_mm(ctx->sqo_mm); |
| 5795 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5796 | } |
| 5797 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5798 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 5799 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 5800 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5801 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5802 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5803 | } |
| 5804 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 5805 | if (unlikely(submitted != nr)) { |
| 5806 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 5807 | |
| 5808 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 5809 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5810 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5811 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5812 | if (statep) |
| 5813 | io_submit_state_end(&state); |
| 5814 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5815 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5816 | io_commit_sqring(ctx); |
| 5817 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5818 | return submitted; |
| 5819 | } |
| 5820 | |
| 5821 | static int io_sq_thread(void *data) |
| 5822 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5823 | struct io_ring_ctx *ctx = data; |
| 5824 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5825 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5826 | mm_segment_t old_fs; |
| 5827 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5828 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5829 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5830 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5831 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5832 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5833 | old_fs = get_fs(); |
| 5834 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5835 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5836 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5837 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5838 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5839 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5840 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5841 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5842 | unsigned nr_events = 0; |
| 5843 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5844 | mutex_lock(&ctx->uring_lock); |
| 5845 | if (!list_empty(&ctx->poll_list)) |
| 5846 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 5847 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5848 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5849 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5850 | } |
| 5851 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5852 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5853 | |
| 5854 | /* |
| 5855 | * If submit got -EBUSY, flag us as needing the application |
| 5856 | * to enter the kernel to reap and flush events. |
| 5857 | */ |
| 5858 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5859 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 5860 | * Drop cur_mm before scheduling, we can't hold it for |
| 5861 | * long periods (or over schedule()). Do this before |
| 5862 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5863 | * may sleep. |
| 5864 | */ |
| 5865 | if (cur_mm) { |
| 5866 | unuse_mm(cur_mm); |
| 5867 | mmput(cur_mm); |
| 5868 | cur_mm = NULL; |
| 5869 | } |
| 5870 | |
| 5871 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5872 | * We're polling. If we're within the defined idle |
| 5873 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5874 | * to sleep. The exception is if we got EBUSY doing |
| 5875 | * more IO, we should wait for the application to |
| 5876 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5877 | */ |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5878 | if (!list_empty(&ctx->poll_list) || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5879 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5880 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5881 | if (current->task_works) |
| 5882 | task_work_run(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5883 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5884 | continue; |
| 5885 | } |
| 5886 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5887 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5888 | TASK_INTERRUPTIBLE); |
| 5889 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5890 | /* |
| 5891 | * While doing polled IO, before going to sleep, we need |
| 5892 | * to check if there are new reqs added to poll_list, it |
| 5893 | * is because reqs may have been punted to io worker and |
| 5894 | * will be added to poll_list later, hence check the |
| 5895 | * poll_list again. |
| 5896 | */ |
| 5897 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 5898 | !list_empty_careful(&ctx->poll_list)) { |
| 5899 | finish_wait(&ctx->sqo_wait, &wait); |
| 5900 | continue; |
| 5901 | } |
| 5902 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5903 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5904 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5905 | /* make sure to read SQ tail after writing flags */ |
| 5906 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5907 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5908 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5909 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5910 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5911 | finish_wait(&ctx->sqo_wait, &wait); |
| 5912 | break; |
| 5913 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5914 | if (current->task_works) { |
| 5915 | task_work_run(); |
| 5916 | continue; |
| 5917 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5918 | if (signal_pending(current)) |
| 5919 | flush_signals(current); |
| 5920 | schedule(); |
| 5921 | finish_wait(&ctx->sqo_wait, &wait); |
| 5922 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5923 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5924 | continue; |
| 5925 | } |
| 5926 | finish_wait(&ctx->sqo_wait, &wait); |
| 5927 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5928 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5929 | } |
| 5930 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5931 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5932 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5933 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5934 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5935 | } |
| 5936 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5937 | if (current->task_works) |
| 5938 | task_work_run(); |
| 5939 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5940 | set_fs(old_fs); |
| 5941 | if (cur_mm) { |
| 5942 | unuse_mm(cur_mm); |
| 5943 | mmput(cur_mm); |
| 5944 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5945 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5946 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5947 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5948 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5949 | return 0; |
| 5950 | } |
| 5951 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5952 | struct io_wait_queue { |
| 5953 | struct wait_queue_entry wq; |
| 5954 | struct io_ring_ctx *ctx; |
| 5955 | unsigned to_wait; |
| 5956 | unsigned nr_timeouts; |
| 5957 | }; |
| 5958 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5959 | 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] | 5960 | { |
| 5961 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5962 | |
| 5963 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5964 | * 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] | 5965 | * started waiting. For timeouts, we always want to return to userspace, |
| 5966 | * regardless of event count. |
| 5967 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5968 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5969 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5970 | } |
| 5971 | |
| 5972 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5973 | int wake_flags, void *key) |
| 5974 | { |
| 5975 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5976 | wq); |
| 5977 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5978 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5979 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5980 | return -1; |
| 5981 | |
| 5982 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5983 | } |
| 5984 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5985 | /* |
| 5986 | * Wait until events become available, if we don't already have some. The |
| 5987 | * application must reap them itself, as they reside on the shared cq ring. |
| 5988 | */ |
| 5989 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5990 | const sigset_t __user *sig, size_t sigsz) |
| 5991 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5992 | struct io_wait_queue iowq = { |
| 5993 | .wq = { |
| 5994 | .private = current, |
| 5995 | .func = io_wake_function, |
| 5996 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5997 | }, |
| 5998 | .ctx = ctx, |
| 5999 | .to_wait = min_events, |
| 6000 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6001 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6002 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6003 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6004 | do { |
| 6005 | if (io_cqring_events(ctx, false) >= min_events) |
| 6006 | return 0; |
| 6007 | if (!current->task_works) |
| 6008 | break; |
| 6009 | task_work_run(); |
| 6010 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6011 | |
| 6012 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6013 | #ifdef CONFIG_COMPAT |
| 6014 | if (in_compat_syscall()) |
| 6015 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6016 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6017 | else |
| 6018 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6019 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6020 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6021 | if (ret) |
| 6022 | return ret; |
| 6023 | } |
| 6024 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6025 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6026 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6027 | do { |
| 6028 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6029 | TASK_INTERRUPTIBLE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6030 | if (current->task_works) |
| 6031 | task_work_run(); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6032 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6033 | break; |
| 6034 | schedule(); |
| 6035 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6036 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6037 | break; |
| 6038 | } |
| 6039 | } while (1); |
| 6040 | finish_wait(&ctx->wait, &iowq.wq); |
| 6041 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 6042 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6043 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6044 | 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] | 6045 | } |
| 6046 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6047 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6048 | { |
| 6049 | #if defined(CONFIG_UNIX) |
| 6050 | if (ctx->ring_sock) { |
| 6051 | struct sock *sock = ctx->ring_sock->sk; |
| 6052 | struct sk_buff *skb; |
| 6053 | |
| 6054 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6055 | kfree_skb(skb); |
| 6056 | } |
| 6057 | #else |
| 6058 | int i; |
| 6059 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6060 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6061 | struct file *file; |
| 6062 | |
| 6063 | file = io_file_from_index(ctx, i); |
| 6064 | if (file) |
| 6065 | fput(file); |
| 6066 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6067 | #endif |
| 6068 | } |
| 6069 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6070 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 6071 | { |
| 6072 | struct fixed_file_data *data; |
| 6073 | |
| 6074 | data = container_of(ref, struct fixed_file_data, refs); |
| 6075 | complete(&data->done); |
| 6076 | } |
| 6077 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6078 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6079 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6080 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6081 | unsigned nr_tables, i; |
| 6082 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6083 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6084 | return -ENXIO; |
| 6085 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6086 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 6087 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6088 | wait_for_completion(&data->done); |
| 6089 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6090 | percpu_ref_exit(&data->refs); |
| 6091 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6092 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6093 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 6094 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6095 | kfree(data->table[i].files); |
| 6096 | kfree(data->table); |
| 6097 | kfree(data); |
| 6098 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6099 | ctx->nr_user_files = 0; |
| 6100 | return 0; |
| 6101 | } |
| 6102 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6103 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 6104 | { |
| 6105 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6106 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 6107 | /* |
| 6108 | * The park is a bit of a work-around, without it we get |
| 6109 | * warning spews on shutdown with SQPOLL set and affinity |
| 6110 | * set to a single CPU. |
| 6111 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6112 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6113 | kthread_stop(ctx->sqo_thread); |
| 6114 | ctx->sqo_thread = NULL; |
| 6115 | } |
| 6116 | } |
| 6117 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6118 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 6119 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6120 | io_sq_thread_stop(ctx); |
| 6121 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6122 | if (ctx->io_wq) { |
| 6123 | io_wq_destroy(ctx->io_wq); |
| 6124 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6125 | } |
| 6126 | } |
| 6127 | |
| 6128 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6129 | /* |
| 6130 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 6131 | * the io_uring can be safely unregistered on process exit, even if we have |
| 6132 | * loops in the file referencing. |
| 6133 | */ |
| 6134 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 6135 | { |
| 6136 | struct sock *sk = ctx->ring_sock->sk; |
| 6137 | struct scm_fp_list *fpl; |
| 6138 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6139 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6140 | |
| 6141 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 6142 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 6143 | |
| 6144 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 6145 | return -EMFILE; |
| 6146 | } |
| 6147 | |
| 6148 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 6149 | if (!fpl) |
| 6150 | return -ENOMEM; |
| 6151 | |
| 6152 | skb = alloc_skb(0, GFP_KERNEL); |
| 6153 | if (!skb) { |
| 6154 | kfree(fpl); |
| 6155 | return -ENOMEM; |
| 6156 | } |
| 6157 | |
| 6158 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6159 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6160 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6161 | fpl->user = get_uid(ctx->user); |
| 6162 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6163 | struct file *file = io_file_from_index(ctx, i + offset); |
| 6164 | |
| 6165 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6166 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6167 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6168 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 6169 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6170 | } |
| 6171 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6172 | if (nr_files) { |
| 6173 | fpl->max = SCM_MAX_FD; |
| 6174 | fpl->count = nr_files; |
| 6175 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6176 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6177 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 6178 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6179 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 6180 | for (i = 0; i < nr_files; i++) |
| 6181 | fput(fpl->fp[i]); |
| 6182 | } else { |
| 6183 | kfree_skb(skb); |
| 6184 | kfree(fpl); |
| 6185 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6186 | |
| 6187 | return 0; |
| 6188 | } |
| 6189 | |
| 6190 | /* |
| 6191 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 6192 | * causes regular reference counting to break down. We rely on the UNIX |
| 6193 | * garbage collection to take care of this problem for us. |
| 6194 | */ |
| 6195 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6196 | { |
| 6197 | unsigned left, total; |
| 6198 | int ret = 0; |
| 6199 | |
| 6200 | total = 0; |
| 6201 | left = ctx->nr_user_files; |
| 6202 | while (left) { |
| 6203 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6204 | |
| 6205 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 6206 | if (ret) |
| 6207 | break; |
| 6208 | left -= this_files; |
| 6209 | total += this_files; |
| 6210 | } |
| 6211 | |
| 6212 | if (!ret) |
| 6213 | return 0; |
| 6214 | |
| 6215 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6216 | struct file *file = io_file_from_index(ctx, total); |
| 6217 | |
| 6218 | if (file) |
| 6219 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6220 | total++; |
| 6221 | } |
| 6222 | |
| 6223 | return ret; |
| 6224 | } |
| 6225 | #else |
| 6226 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 6227 | { |
| 6228 | return 0; |
| 6229 | } |
| 6230 | #endif |
| 6231 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6232 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 6233 | unsigned nr_files) |
| 6234 | { |
| 6235 | int i; |
| 6236 | |
| 6237 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6238 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6239 | unsigned this_files; |
| 6240 | |
| 6241 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 6242 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 6243 | GFP_KERNEL); |
| 6244 | if (!table->files) |
| 6245 | break; |
| 6246 | nr_files -= this_files; |
| 6247 | } |
| 6248 | |
| 6249 | if (i == nr_tables) |
| 6250 | return 0; |
| 6251 | |
| 6252 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6253 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6254 | kfree(table->files); |
| 6255 | } |
| 6256 | return 1; |
| 6257 | } |
| 6258 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6259 | 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] | 6260 | { |
| 6261 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6262 | struct sock *sock = ctx->ring_sock->sk; |
| 6263 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 6264 | struct sk_buff *skb; |
| 6265 | int i; |
| 6266 | |
| 6267 | __skb_queue_head_init(&list); |
| 6268 | |
| 6269 | /* |
| 6270 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 6271 | * remove this entry and rearrange the file array. |
| 6272 | */ |
| 6273 | skb = skb_dequeue(head); |
| 6274 | while (skb) { |
| 6275 | struct scm_fp_list *fp; |
| 6276 | |
| 6277 | fp = UNIXCB(skb).fp; |
| 6278 | for (i = 0; i < fp->count; i++) { |
| 6279 | int left; |
| 6280 | |
| 6281 | if (fp->fp[i] != file) |
| 6282 | continue; |
| 6283 | |
| 6284 | unix_notinflight(fp->user, fp->fp[i]); |
| 6285 | left = fp->count - 1 - i; |
| 6286 | if (left) { |
| 6287 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 6288 | left * sizeof(struct file *)); |
| 6289 | } |
| 6290 | fp->count--; |
| 6291 | if (!fp->count) { |
| 6292 | kfree_skb(skb); |
| 6293 | skb = NULL; |
| 6294 | } else { |
| 6295 | __skb_queue_tail(&list, skb); |
| 6296 | } |
| 6297 | fput(file); |
| 6298 | file = NULL; |
| 6299 | break; |
| 6300 | } |
| 6301 | |
| 6302 | if (!file) |
| 6303 | break; |
| 6304 | |
| 6305 | __skb_queue_tail(&list, skb); |
| 6306 | |
| 6307 | skb = skb_dequeue(head); |
| 6308 | } |
| 6309 | |
| 6310 | if (skb_peek(&list)) { |
| 6311 | spin_lock_irq(&head->lock); |
| 6312 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 6313 | __skb_queue_tail(head, skb); |
| 6314 | spin_unlock_irq(&head->lock); |
| 6315 | } |
| 6316 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6317 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6318 | #endif |
| 6319 | } |
| 6320 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6321 | struct io_file_put { |
| 6322 | struct llist_node llist; |
| 6323 | struct file *file; |
| 6324 | struct completion *done; |
| 6325 | }; |
| 6326 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6327 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6328 | { |
| 6329 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6330 | struct llist_node *node; |
| 6331 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6332 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 6333 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 6334 | io_ring_file_put(data->ctx, pfile->file); |
| 6335 | if (pfile->done) |
| 6336 | complete(pfile->done); |
| 6337 | else |
| 6338 | kfree(pfile); |
| 6339 | } |
| 6340 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6341 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6342 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6343 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 6344 | { |
| 6345 | struct fixed_file_data *data; |
| 6346 | |
| 6347 | data = container_of(work, struct fixed_file_data, ref_work); |
| 6348 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6349 | percpu_ref_switch_to_percpu(&data->refs); |
| 6350 | } |
| 6351 | |
| 6352 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 6353 | { |
| 6354 | struct fixed_file_data *data; |
| 6355 | |
| 6356 | data = container_of(ref, struct fixed_file_data, refs); |
| 6357 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6358 | /* |
| 6359 | * We can't safely switch from inside this context, punt to wq. If |
| 6360 | * the table ref is going away, the table is being unregistered. |
| 6361 | * Don't queue up the async work for that case, the caller will |
| 6362 | * handle it. |
| 6363 | */ |
| 6364 | if (!percpu_ref_is_dying(&data->refs)) |
| 6365 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6369 | unsigned nr_args) |
| 6370 | { |
| 6371 | __s32 __user *fds = (__s32 __user *) arg; |
| 6372 | unsigned nr_tables; |
| 6373 | struct file *file; |
| 6374 | int fd, ret = 0; |
| 6375 | unsigned i; |
| 6376 | |
| 6377 | if (ctx->file_data) |
| 6378 | return -EBUSY; |
| 6379 | if (!nr_args) |
| 6380 | return -EINVAL; |
| 6381 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 6382 | return -EMFILE; |
| 6383 | |
| 6384 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 6385 | if (!ctx->file_data) |
| 6386 | return -ENOMEM; |
| 6387 | ctx->file_data->ctx = ctx; |
| 6388 | init_completion(&ctx->file_data->done); |
| 6389 | |
| 6390 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 6391 | ctx->file_data->table = kcalloc(nr_tables, |
| 6392 | sizeof(struct fixed_file_table), |
| 6393 | GFP_KERNEL); |
| 6394 | if (!ctx->file_data->table) { |
| 6395 | kfree(ctx->file_data); |
| 6396 | ctx->file_data = NULL; |
| 6397 | return -ENOMEM; |
| 6398 | } |
| 6399 | |
| 6400 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 6401 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 6402 | kfree(ctx->file_data->table); |
| 6403 | kfree(ctx->file_data); |
| 6404 | ctx->file_data = NULL; |
| 6405 | return -ENOMEM; |
| 6406 | } |
| 6407 | ctx->file_data->put_llist.first = NULL; |
| 6408 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 6409 | |
| 6410 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 6411 | percpu_ref_exit(&ctx->file_data->refs); |
| 6412 | kfree(ctx->file_data->table); |
| 6413 | kfree(ctx->file_data); |
| 6414 | ctx->file_data = NULL; |
| 6415 | return -ENOMEM; |
| 6416 | } |
| 6417 | |
| 6418 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 6419 | struct fixed_file_table *table; |
| 6420 | unsigned index; |
| 6421 | |
| 6422 | ret = -EFAULT; |
| 6423 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 6424 | break; |
| 6425 | /* allow sparse sets */ |
| 6426 | if (fd == -1) { |
| 6427 | ret = 0; |
| 6428 | continue; |
| 6429 | } |
| 6430 | |
| 6431 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6432 | index = i & IORING_FILE_TABLE_MASK; |
| 6433 | file = fget(fd); |
| 6434 | |
| 6435 | ret = -EBADF; |
| 6436 | if (!file) |
| 6437 | break; |
| 6438 | |
| 6439 | /* |
| 6440 | * Don't allow io_uring instances to be registered. If UNIX |
| 6441 | * isn't enabled, then this causes a reference cycle and this |
| 6442 | * instance can never get freed. If UNIX is enabled we'll |
| 6443 | * handle it just fine, but there's still no point in allowing |
| 6444 | * a ring fd as it doesn't support regular read/write anyway. |
| 6445 | */ |
| 6446 | if (file->f_op == &io_uring_fops) { |
| 6447 | fput(file); |
| 6448 | break; |
| 6449 | } |
| 6450 | ret = 0; |
| 6451 | table->files[index] = file; |
| 6452 | } |
| 6453 | |
| 6454 | if (ret) { |
| 6455 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6456 | file = io_file_from_index(ctx, i); |
| 6457 | if (file) |
| 6458 | fput(file); |
| 6459 | } |
| 6460 | for (i = 0; i < nr_tables; i++) |
| 6461 | kfree(ctx->file_data->table[i].files); |
| 6462 | |
| 6463 | kfree(ctx->file_data->table); |
| 6464 | kfree(ctx->file_data); |
| 6465 | ctx->file_data = NULL; |
| 6466 | ctx->nr_user_files = 0; |
| 6467 | return ret; |
| 6468 | } |
| 6469 | |
| 6470 | ret = io_sqe_files_scm(ctx); |
| 6471 | if (ret) |
| 6472 | io_sqe_files_unregister(ctx); |
| 6473 | |
| 6474 | return ret; |
| 6475 | } |
| 6476 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6477 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 6478 | int index) |
| 6479 | { |
| 6480 | #if defined(CONFIG_UNIX) |
| 6481 | struct sock *sock = ctx->ring_sock->sk; |
| 6482 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 6483 | struct sk_buff *skb; |
| 6484 | |
| 6485 | /* |
| 6486 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 6487 | * file set. If there's no room, fall back to allocating a new skb |
| 6488 | * and filling it in. |
| 6489 | */ |
| 6490 | spin_lock_irq(&head->lock); |
| 6491 | skb = skb_peek(head); |
| 6492 | if (skb) { |
| 6493 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 6494 | |
| 6495 | if (fpl->count < SCM_MAX_FD) { |
| 6496 | __skb_unlink(skb, head); |
| 6497 | spin_unlock_irq(&head->lock); |
| 6498 | fpl->fp[fpl->count] = get_file(file); |
| 6499 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 6500 | fpl->count++; |
| 6501 | spin_lock_irq(&head->lock); |
| 6502 | __skb_queue_head(head, skb); |
| 6503 | } else { |
| 6504 | skb = NULL; |
| 6505 | } |
| 6506 | } |
| 6507 | spin_unlock_irq(&head->lock); |
| 6508 | |
| 6509 | if (skb) { |
| 6510 | fput(file); |
| 6511 | return 0; |
| 6512 | } |
| 6513 | |
| 6514 | return __io_sqe_files_scm(ctx, 1, index); |
| 6515 | #else |
| 6516 | return 0; |
| 6517 | #endif |
| 6518 | } |
| 6519 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6520 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6521 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6522 | struct fixed_file_data *data; |
| 6523 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6524 | /* |
| 6525 | * Juggle reference to ensure we hit zero, if needed, so we can |
| 6526 | * switch back to percpu mode |
| 6527 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6528 | data = container_of(ref, struct fixed_file_data, refs); |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6529 | percpu_ref_put(&data->refs); |
| 6530 | percpu_ref_get(&data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 6534 | struct file *file) |
| 6535 | { |
| 6536 | struct io_file_put *pfile, pfile_stack; |
| 6537 | DECLARE_COMPLETION_ONSTACK(done); |
| 6538 | |
| 6539 | /* |
| 6540 | * If we fail allocating the struct we need for doing async reomval |
| 6541 | * of this file, just punt to sync and wait for it. |
| 6542 | */ |
| 6543 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 6544 | if (!pfile) { |
| 6545 | pfile = &pfile_stack; |
| 6546 | pfile->done = &done; |
| 6547 | } |
| 6548 | |
| 6549 | pfile->file = file; |
| 6550 | llist_add(&pfile->llist, &data->put_llist); |
| 6551 | |
| 6552 | if (pfile == &pfile_stack) { |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6553 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6554 | wait_for_completion(&done); |
| 6555 | flush_work(&data->ref_work); |
| 6556 | return false; |
| 6557 | } |
| 6558 | |
| 6559 | return true; |
| 6560 | } |
| 6561 | |
| 6562 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 6563 | struct io_uring_files_update *up, |
| 6564 | unsigned nr_args) |
| 6565 | { |
| 6566 | struct fixed_file_data *data = ctx->file_data; |
| 6567 | bool ref_switch = false; |
| 6568 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6569 | __s32 __user *fds; |
| 6570 | int fd, i, err; |
| 6571 | __u32 done; |
| 6572 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6573 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6574 | return -EOVERFLOW; |
| 6575 | if (done > ctx->nr_user_files) |
| 6576 | return -EINVAL; |
| 6577 | |
| 6578 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6579 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6580 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6581 | struct fixed_file_table *table; |
| 6582 | unsigned index; |
| 6583 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6584 | err = 0; |
| 6585 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 6586 | err = -EFAULT; |
| 6587 | break; |
| 6588 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6589 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 6590 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6591 | index = i & IORING_FILE_TABLE_MASK; |
| 6592 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6593 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6594 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6595 | if (io_queue_file_removal(data, file)) |
| 6596 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6597 | } |
| 6598 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6599 | file = fget(fd); |
| 6600 | if (!file) { |
| 6601 | err = -EBADF; |
| 6602 | break; |
| 6603 | } |
| 6604 | /* |
| 6605 | * Don't allow io_uring instances to be registered. If |
| 6606 | * UNIX isn't enabled, then this causes a reference |
| 6607 | * cycle and this instance can never get freed. If UNIX |
| 6608 | * is enabled we'll handle it just fine, but there's |
| 6609 | * still no point in allowing a ring fd as it doesn't |
| 6610 | * support regular read/write anyway. |
| 6611 | */ |
| 6612 | if (file->f_op == &io_uring_fops) { |
| 6613 | fput(file); |
| 6614 | err = -EBADF; |
| 6615 | break; |
| 6616 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6617 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6618 | err = io_sqe_file_register(ctx, file, i); |
| 6619 | if (err) |
| 6620 | break; |
| 6621 | } |
| 6622 | nr_args--; |
| 6623 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6624 | up->offset++; |
| 6625 | } |
| 6626 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6627 | if (ref_switch) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6628 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6629 | |
| 6630 | return done ? done : err; |
| 6631 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6632 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 6633 | unsigned nr_args) |
| 6634 | { |
| 6635 | struct io_uring_files_update up; |
| 6636 | |
| 6637 | if (!ctx->file_data) |
| 6638 | return -ENXIO; |
| 6639 | if (!nr_args) |
| 6640 | return -EINVAL; |
| 6641 | if (copy_from_user(&up, arg, sizeof(up))) |
| 6642 | return -EFAULT; |
| 6643 | if (up.resv) |
| 6644 | return -EINVAL; |
| 6645 | |
| 6646 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 6647 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6648 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 6649 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 6650 | { |
| 6651 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6652 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 6653 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 6654 | io_put_req(req); |
| 6655 | } |
| 6656 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6657 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 6658 | struct io_uring_params *p) |
| 6659 | { |
| 6660 | struct io_wq_data data; |
| 6661 | struct fd f; |
| 6662 | struct io_ring_ctx *ctx_attach; |
| 6663 | unsigned int concurrency; |
| 6664 | int ret = 0; |
| 6665 | |
| 6666 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 6667 | data.free_work = io_free_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6668 | |
| 6669 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 6670 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 6671 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 6672 | |
| 6673 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 6674 | if (IS_ERR(ctx->io_wq)) { |
| 6675 | ret = PTR_ERR(ctx->io_wq); |
| 6676 | ctx->io_wq = NULL; |
| 6677 | } |
| 6678 | return ret; |
| 6679 | } |
| 6680 | |
| 6681 | f = fdget(p->wq_fd); |
| 6682 | if (!f.file) |
| 6683 | return -EBADF; |
| 6684 | |
| 6685 | if (f.file->f_op != &io_uring_fops) { |
| 6686 | ret = -EINVAL; |
| 6687 | goto out_fput; |
| 6688 | } |
| 6689 | |
| 6690 | ctx_attach = f.file->private_data; |
| 6691 | /* @io_wq is protected by holding the fd */ |
| 6692 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 6693 | ret = -EINVAL; |
| 6694 | goto out_fput; |
| 6695 | } |
| 6696 | |
| 6697 | ctx->io_wq = ctx_attach->io_wq; |
| 6698 | out_fput: |
| 6699 | fdput(f); |
| 6700 | return ret; |
| 6701 | } |
| 6702 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6703 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 6704 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6705 | { |
| 6706 | int ret; |
| 6707 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6708 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6709 | mmgrab(current->mm); |
| 6710 | ctx->sqo_mm = current->mm; |
| 6711 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6712 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 6713 | ret = -EPERM; |
| 6714 | if (!capable(CAP_SYS_ADMIN)) |
| 6715 | goto err; |
| 6716 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6717 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 6718 | if (!ctx->sq_thread_idle) |
| 6719 | ctx->sq_thread_idle = HZ; |
| 6720 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6721 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6722 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6723 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6724 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6725 | if (cpu >= nr_cpu_ids) |
| 6726 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 6727 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6728 | goto err; |
| 6729 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6730 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 6731 | ctx, cpu, |
| 6732 | "io_uring-sq"); |
| 6733 | } else { |
| 6734 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 6735 | "io_uring-sq"); |
| 6736 | } |
| 6737 | if (IS_ERR(ctx->sqo_thread)) { |
| 6738 | ret = PTR_ERR(ctx->sqo_thread); |
| 6739 | ctx->sqo_thread = NULL; |
| 6740 | goto err; |
| 6741 | } |
| 6742 | wake_up_process(ctx->sqo_thread); |
| 6743 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 6744 | /* Can't have SQ_AFF without SQPOLL */ |
| 6745 | ret = -EINVAL; |
| 6746 | goto err; |
| 6747 | } |
| 6748 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6749 | ret = io_init_wq_offload(ctx, p); |
| 6750 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6751 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6752 | |
| 6753 | return 0; |
| 6754 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 6755 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6756 | mmdrop(ctx->sqo_mm); |
| 6757 | ctx->sqo_mm = NULL; |
| 6758 | return ret; |
| 6759 | } |
| 6760 | |
| 6761 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 6762 | { |
| 6763 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 6764 | } |
| 6765 | |
| 6766 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 6767 | { |
| 6768 | unsigned long page_limit, cur_pages, new_pages; |
| 6769 | |
| 6770 | /* Don't allow more pages than we can safely lock */ |
| 6771 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 6772 | |
| 6773 | do { |
| 6774 | cur_pages = atomic_long_read(&user->locked_vm); |
| 6775 | new_pages = cur_pages + nr_pages; |
| 6776 | if (new_pages > page_limit) |
| 6777 | return -ENOMEM; |
| 6778 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 6779 | new_pages) != cur_pages); |
| 6780 | |
| 6781 | return 0; |
| 6782 | } |
| 6783 | |
| 6784 | static void io_mem_free(void *ptr) |
| 6785 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6786 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6787 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6788 | if (!ptr) |
| 6789 | return; |
| 6790 | |
| 6791 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6792 | if (put_page_testzero(page)) |
| 6793 | free_compound_page(page); |
| 6794 | } |
| 6795 | |
| 6796 | static void *io_mem_alloc(size_t size) |
| 6797 | { |
| 6798 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 6799 | __GFP_NORETRY; |
| 6800 | |
| 6801 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 6802 | } |
| 6803 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6804 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 6805 | size_t *sq_offset) |
| 6806 | { |
| 6807 | struct io_rings *rings; |
| 6808 | size_t off, sq_array_size; |
| 6809 | |
| 6810 | off = struct_size(rings, cqes, cq_entries); |
| 6811 | if (off == SIZE_MAX) |
| 6812 | return SIZE_MAX; |
| 6813 | |
| 6814 | #ifdef CONFIG_SMP |
| 6815 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6816 | if (off == 0) |
| 6817 | return SIZE_MAX; |
| 6818 | #endif |
| 6819 | |
| 6820 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6821 | if (sq_array_size == SIZE_MAX) |
| 6822 | return SIZE_MAX; |
| 6823 | |
| 6824 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6825 | return SIZE_MAX; |
| 6826 | |
| 6827 | if (sq_offset) |
| 6828 | *sq_offset = off; |
| 6829 | |
| 6830 | return off; |
| 6831 | } |
| 6832 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6833 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6834 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6835 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6836 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6837 | pages = (size_t)1 << get_order( |
| 6838 | rings_size(sq_entries, cq_entries, NULL)); |
| 6839 | pages += (size_t)1 << get_order( |
| 6840 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6841 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6842 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6843 | } |
| 6844 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6845 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6846 | { |
| 6847 | int i, j; |
| 6848 | |
| 6849 | if (!ctx->user_bufs) |
| 6850 | return -ENXIO; |
| 6851 | |
| 6852 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6853 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6854 | |
| 6855 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6856 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6857 | |
| 6858 | if (ctx->account_mem) |
| 6859 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6860 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6861 | imu->nr_bvecs = 0; |
| 6862 | } |
| 6863 | |
| 6864 | kfree(ctx->user_bufs); |
| 6865 | ctx->user_bufs = NULL; |
| 6866 | ctx->nr_user_bufs = 0; |
| 6867 | return 0; |
| 6868 | } |
| 6869 | |
| 6870 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6871 | void __user *arg, unsigned index) |
| 6872 | { |
| 6873 | struct iovec __user *src; |
| 6874 | |
| 6875 | #ifdef CONFIG_COMPAT |
| 6876 | if (ctx->compat) { |
| 6877 | struct compat_iovec __user *ciovs; |
| 6878 | struct compat_iovec ciov; |
| 6879 | |
| 6880 | ciovs = (struct compat_iovec __user *) arg; |
| 6881 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6882 | return -EFAULT; |
| 6883 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6884 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6885 | dst->iov_len = ciov.iov_len; |
| 6886 | return 0; |
| 6887 | } |
| 6888 | #endif |
| 6889 | src = (struct iovec __user *) arg; |
| 6890 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6891 | return -EFAULT; |
| 6892 | return 0; |
| 6893 | } |
| 6894 | |
| 6895 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6896 | unsigned nr_args) |
| 6897 | { |
| 6898 | struct vm_area_struct **vmas = NULL; |
| 6899 | struct page **pages = NULL; |
| 6900 | int i, j, got_pages = 0; |
| 6901 | int ret = -EINVAL; |
| 6902 | |
| 6903 | if (ctx->user_bufs) |
| 6904 | return -EBUSY; |
| 6905 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6906 | return -EINVAL; |
| 6907 | |
| 6908 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6909 | GFP_KERNEL); |
| 6910 | if (!ctx->user_bufs) |
| 6911 | return -ENOMEM; |
| 6912 | |
| 6913 | for (i = 0; i < nr_args; i++) { |
| 6914 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6915 | unsigned long off, start, end, ubuf; |
| 6916 | int pret, nr_pages; |
| 6917 | struct iovec iov; |
| 6918 | size_t size; |
| 6919 | |
| 6920 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6921 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6922 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6923 | |
| 6924 | /* |
| 6925 | * Don't impose further limits on the size and buffer |
| 6926 | * constraints here, we'll -EINVAL later when IO is |
| 6927 | * submitted if they are wrong. |
| 6928 | */ |
| 6929 | ret = -EFAULT; |
| 6930 | if (!iov.iov_base || !iov.iov_len) |
| 6931 | goto err; |
| 6932 | |
| 6933 | /* arbitrary limit, but we need something */ |
| 6934 | if (iov.iov_len > SZ_1G) |
| 6935 | goto err; |
| 6936 | |
| 6937 | ubuf = (unsigned long) iov.iov_base; |
| 6938 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6939 | start = ubuf >> PAGE_SHIFT; |
| 6940 | nr_pages = end - start; |
| 6941 | |
| 6942 | if (ctx->account_mem) { |
| 6943 | ret = io_account_mem(ctx->user, nr_pages); |
| 6944 | if (ret) |
| 6945 | goto err; |
| 6946 | } |
| 6947 | |
| 6948 | ret = 0; |
| 6949 | if (!pages || nr_pages > got_pages) { |
| 6950 | kfree(vmas); |
| 6951 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6952 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6953 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6954 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6955 | sizeof(struct vm_area_struct *), |
| 6956 | GFP_KERNEL); |
| 6957 | if (!pages || !vmas) { |
| 6958 | ret = -ENOMEM; |
| 6959 | if (ctx->account_mem) |
| 6960 | io_unaccount_mem(ctx->user, nr_pages); |
| 6961 | goto err; |
| 6962 | } |
| 6963 | got_pages = nr_pages; |
| 6964 | } |
| 6965 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6966 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6967 | GFP_KERNEL); |
| 6968 | ret = -ENOMEM; |
| 6969 | if (!imu->bvec) { |
| 6970 | if (ctx->account_mem) |
| 6971 | io_unaccount_mem(ctx->user, nr_pages); |
| 6972 | goto err; |
| 6973 | } |
| 6974 | |
| 6975 | ret = 0; |
| 6976 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6977 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6978 | FOLL_WRITE | FOLL_LONGTERM, |
| 6979 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6980 | if (pret == nr_pages) { |
| 6981 | /* don't support file backed memory */ |
| 6982 | for (j = 0; j < nr_pages; j++) { |
| 6983 | struct vm_area_struct *vma = vmas[j]; |
| 6984 | |
| 6985 | if (vma->vm_file && |
| 6986 | !is_file_hugepages(vma->vm_file)) { |
| 6987 | ret = -EOPNOTSUPP; |
| 6988 | break; |
| 6989 | } |
| 6990 | } |
| 6991 | } else { |
| 6992 | ret = pret < 0 ? pret : -EFAULT; |
| 6993 | } |
| 6994 | up_read(¤t->mm->mmap_sem); |
| 6995 | if (ret) { |
| 6996 | /* |
| 6997 | * if we did partial map, or found file backed vmas, |
| 6998 | * release any pages we did get |
| 6999 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 7000 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 7001 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7002 | if (ctx->account_mem) |
| 7003 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7004 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7005 | goto err; |
| 7006 | } |
| 7007 | |
| 7008 | off = ubuf & ~PAGE_MASK; |
| 7009 | size = iov.iov_len; |
| 7010 | for (j = 0; j < nr_pages; j++) { |
| 7011 | size_t vec_len; |
| 7012 | |
| 7013 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 7014 | imu->bvec[j].bv_page = pages[j]; |
| 7015 | imu->bvec[j].bv_len = vec_len; |
| 7016 | imu->bvec[j].bv_offset = off; |
| 7017 | off = 0; |
| 7018 | size -= vec_len; |
| 7019 | } |
| 7020 | /* store original address for later verification */ |
| 7021 | imu->ubuf = ubuf; |
| 7022 | imu->len = iov.iov_len; |
| 7023 | imu->nr_bvecs = nr_pages; |
| 7024 | |
| 7025 | ctx->nr_user_bufs++; |
| 7026 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7027 | kvfree(pages); |
| 7028 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7029 | return 0; |
| 7030 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 7031 | kvfree(pages); |
| 7032 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7033 | io_sqe_buffer_unregister(ctx); |
| 7034 | return ret; |
| 7035 | } |
| 7036 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7037 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 7038 | { |
| 7039 | __s32 __user *fds = arg; |
| 7040 | int fd; |
| 7041 | |
| 7042 | if (ctx->cq_ev_fd) |
| 7043 | return -EBUSY; |
| 7044 | |
| 7045 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 7046 | return -EFAULT; |
| 7047 | |
| 7048 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 7049 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 7050 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 7051 | ctx->cq_ev_fd = NULL; |
| 7052 | return ret; |
| 7053 | } |
| 7054 | |
| 7055 | return 0; |
| 7056 | } |
| 7057 | |
| 7058 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 7059 | { |
| 7060 | if (ctx->cq_ev_fd) { |
| 7061 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 7062 | ctx->cq_ev_fd = NULL; |
| 7063 | return 0; |
| 7064 | } |
| 7065 | |
| 7066 | return -ENXIO; |
| 7067 | } |
| 7068 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7069 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 7070 | { |
| 7071 | struct io_ring_ctx *ctx = data; |
| 7072 | struct io_buffer *buf = p; |
| 7073 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame^] | 7074 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7075 | return 0; |
| 7076 | } |
| 7077 | |
| 7078 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 7079 | { |
| 7080 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 7081 | idr_destroy(&ctx->io_buffer_idr); |
| 7082 | } |
| 7083 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7084 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 7085 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7086 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7087 | if (ctx->sqo_mm) |
| 7088 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7089 | |
| 7090 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7091 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7092 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7093 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 7094 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 7095 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7096 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7097 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7098 | if (ctx->ring_sock) { |
| 7099 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7100 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 7101 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7102 | #endif |
| 7103 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7104 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7105 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7106 | |
| 7107 | percpu_ref_exit(&ctx->refs); |
| 7108 | if (ctx->account_mem) |
| 7109 | io_unaccount_mem(ctx->user, |
| 7110 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 7111 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 7112 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 7113 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 7114 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 7115 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7116 | kfree(ctx); |
| 7117 | } |
| 7118 | |
| 7119 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 7120 | { |
| 7121 | struct io_ring_ctx *ctx = file->private_data; |
| 7122 | __poll_t mask = 0; |
| 7123 | |
| 7124 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 7125 | /* |
| 7126 | * synchronizes with barrier from wq_has_sleeper call in |
| 7127 | * io_commit_cqring |
| 7128 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7129 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7130 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 7131 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7132 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 7133 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7134 | mask |= EPOLLIN | EPOLLRDNORM; |
| 7135 | |
| 7136 | return mask; |
| 7137 | } |
| 7138 | |
| 7139 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 7140 | { |
| 7141 | struct io_ring_ctx *ctx = file->private_data; |
| 7142 | |
| 7143 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 7144 | } |
| 7145 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7146 | static int io_remove_personalities(int id, void *p, void *data) |
| 7147 | { |
| 7148 | struct io_ring_ctx *ctx = data; |
| 7149 | const struct cred *cred; |
| 7150 | |
| 7151 | cred = idr_remove(&ctx->personality_idr, id); |
| 7152 | if (cred) |
| 7153 | put_cred(cred); |
| 7154 | return 0; |
| 7155 | } |
| 7156 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7157 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 7158 | { |
| 7159 | mutex_lock(&ctx->uring_lock); |
| 7160 | percpu_ref_kill(&ctx->refs); |
| 7161 | mutex_unlock(&ctx->uring_lock); |
| 7162 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 7163 | /* |
| 7164 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 7165 | * work after we've killed the ctx ref above. This is important to do |
| 7166 | * before we cancel existing commands, as the thread could otherwise |
| 7167 | * be queueing new work post that. If that's work we need to cancel, |
| 7168 | * it could cause shutdown to hang. |
| 7169 | */ |
| 7170 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 7171 | cpu_relax(); |
| 7172 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 7173 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 7174 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7175 | |
| 7176 | if (ctx->io_wq) |
| 7177 | io_wq_cancel_all(ctx->io_wq); |
| 7178 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7179 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 7180 | /* if we failed setting up the ctx, we might not have any rings */ |
| 7181 | if (ctx->rings) |
| 7182 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7183 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 7184 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7185 | io_ring_ctx_free(ctx); |
| 7186 | } |
| 7187 | |
| 7188 | static int io_uring_release(struct inode *inode, struct file *file) |
| 7189 | { |
| 7190 | struct io_ring_ctx *ctx = file->private_data; |
| 7191 | |
| 7192 | file->private_data = NULL; |
| 7193 | io_ring_ctx_wait_and_kill(ctx); |
| 7194 | return 0; |
| 7195 | } |
| 7196 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7197 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 7198 | struct files_struct *files) |
| 7199 | { |
| 7200 | struct io_kiocb *req; |
| 7201 | DEFINE_WAIT(wait); |
| 7202 | |
| 7203 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7204 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7205 | |
| 7206 | spin_lock_irq(&ctx->inflight_lock); |
| 7207 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7208 | if (req->work.files != files) |
| 7209 | continue; |
| 7210 | /* req is being completed, ignore */ |
| 7211 | if (!refcount_inc_not_zero(&req->refs)) |
| 7212 | continue; |
| 7213 | cancel_req = req; |
| 7214 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7215 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7216 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7217 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7218 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7219 | spin_unlock_irq(&ctx->inflight_lock); |
| 7220 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7221 | /* We need to keep going until we don't find a matching req */ |
| 7222 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7223 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 7224 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 7225 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 7226 | spin_lock_irq(&ctx->completion_lock); |
| 7227 | list_del(&cancel_req->list); |
| 7228 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 7229 | if (list_empty(&ctx->cq_overflow_list)) { |
| 7230 | clear_bit(0, &ctx->sq_check_overflow); |
| 7231 | clear_bit(0, &ctx->cq_check_overflow); |
| 7232 | } |
| 7233 | spin_unlock_irq(&ctx->completion_lock); |
| 7234 | |
| 7235 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 7236 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 7237 | |
| 7238 | /* |
| 7239 | * Put inflight ref and overflow ref. If that's |
| 7240 | * all we had, then we're done with this request. |
| 7241 | */ |
| 7242 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
| 7243 | io_put_req(cancel_req); |
| 7244 | continue; |
| 7245 | } |
| 7246 | } |
| 7247 | |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 7248 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 7249 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7250 | schedule(); |
| 7251 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7252 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7253 | } |
| 7254 | |
| 7255 | static int io_uring_flush(struct file *file, void *data) |
| 7256 | { |
| 7257 | struct io_ring_ctx *ctx = file->private_data; |
| 7258 | |
| 7259 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7260 | |
| 7261 | /* |
| 7262 | * If the task is going away, cancel work it may have pending |
| 7263 | */ |
| 7264 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 7265 | io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current)); |
| 7266 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7267 | return 0; |
| 7268 | } |
| 7269 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7270 | static void *io_uring_validate_mmap_request(struct file *file, |
| 7271 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7272 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7273 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7274 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7275 | struct page *page; |
| 7276 | void *ptr; |
| 7277 | |
| 7278 | switch (offset) { |
| 7279 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7280 | case IORING_OFF_CQ_RING: |
| 7281 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7282 | break; |
| 7283 | case IORING_OFF_SQES: |
| 7284 | ptr = ctx->sq_sqes; |
| 7285 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7286 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7287 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7288 | } |
| 7289 | |
| 7290 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 7291 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7292 | return ERR_PTR(-EINVAL); |
| 7293 | |
| 7294 | return ptr; |
| 7295 | } |
| 7296 | |
| 7297 | #ifdef CONFIG_MMU |
| 7298 | |
| 7299 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7300 | { |
| 7301 | size_t sz = vma->vm_end - vma->vm_start; |
| 7302 | unsigned long pfn; |
| 7303 | void *ptr; |
| 7304 | |
| 7305 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 7306 | if (IS_ERR(ptr)) |
| 7307 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7308 | |
| 7309 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 7310 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 7311 | } |
| 7312 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7313 | #else /* !CONFIG_MMU */ |
| 7314 | |
| 7315 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7316 | { |
| 7317 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 7318 | } |
| 7319 | |
| 7320 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 7321 | { |
| 7322 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 7323 | } |
| 7324 | |
| 7325 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 7326 | unsigned long addr, unsigned long len, |
| 7327 | unsigned long pgoff, unsigned long flags) |
| 7328 | { |
| 7329 | void *ptr; |
| 7330 | |
| 7331 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 7332 | if (IS_ERR(ptr)) |
| 7333 | return PTR_ERR(ptr); |
| 7334 | |
| 7335 | return (unsigned long) ptr; |
| 7336 | } |
| 7337 | |
| 7338 | #endif /* !CONFIG_MMU */ |
| 7339 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7340 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 7341 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 7342 | size_t, sigsz) |
| 7343 | { |
| 7344 | struct io_ring_ctx *ctx; |
| 7345 | long ret = -EBADF; |
| 7346 | int submitted = 0; |
| 7347 | struct fd f; |
| 7348 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7349 | if (current->task_works) |
| 7350 | task_work_run(); |
| 7351 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7352 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7353 | return -EINVAL; |
| 7354 | |
| 7355 | f = fdget(fd); |
| 7356 | if (!f.file) |
| 7357 | return -EBADF; |
| 7358 | |
| 7359 | ret = -EOPNOTSUPP; |
| 7360 | if (f.file->f_op != &io_uring_fops) |
| 7361 | goto out_fput; |
| 7362 | |
| 7363 | ret = -ENXIO; |
| 7364 | ctx = f.file->private_data; |
| 7365 | if (!percpu_ref_tryget(&ctx->refs)) |
| 7366 | goto out_fput; |
| 7367 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7368 | /* |
| 7369 | * For SQ polling, the thread will do all submissions and completions. |
| 7370 | * Just return the requested submit count, and wake the thread if |
| 7371 | * we were asked to. |
| 7372 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7373 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7374 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7375 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 7376 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7377 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 7378 | wake_up(&ctx->sqo_wait); |
| 7379 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7380 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7381 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7382 | |
| 7383 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7384 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 7385 | cur_mm = ctx->sqo_mm; |
| 7386 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 7387 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7388 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7389 | |
| 7390 | if (submitted != to_submit) |
| 7391 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7392 | } |
| 7393 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7394 | unsigned nr_events = 0; |
| 7395 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7396 | min_complete = min(min_complete, ctx->cq_entries); |
| 7397 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7398 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7399 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7400 | } else { |
| 7401 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 7402 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7403 | } |
| 7404 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7405 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 7406 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7407 | out_fput: |
| 7408 | fdput(f); |
| 7409 | return submitted ? submitted : ret; |
| 7410 | } |
| 7411 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7412 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7413 | static int io_uring_show_cred(int id, void *p, void *data) |
| 7414 | { |
| 7415 | const struct cred *cred = p; |
| 7416 | struct seq_file *m = data; |
| 7417 | struct user_namespace *uns = seq_user_ns(m); |
| 7418 | struct group_info *gi; |
| 7419 | kernel_cap_t cap; |
| 7420 | unsigned __capi; |
| 7421 | int g; |
| 7422 | |
| 7423 | seq_printf(m, "%5d\n", id); |
| 7424 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 7425 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 7426 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 7427 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 7428 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 7429 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 7430 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 7431 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 7432 | seq_puts(m, "\n\tGroups:\t"); |
| 7433 | gi = cred->group_info; |
| 7434 | for (g = 0; g < gi->ngroups; g++) { |
| 7435 | seq_put_decimal_ull(m, g ? " " : "", |
| 7436 | from_kgid_munged(uns, gi->gid[g])); |
| 7437 | } |
| 7438 | seq_puts(m, "\n\tCapEff:\t"); |
| 7439 | cap = cred->cap_effective; |
| 7440 | CAP_FOR_EACH_U32(__capi) |
| 7441 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 7442 | seq_putc(m, '\n'); |
| 7443 | return 0; |
| 7444 | } |
| 7445 | |
| 7446 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 7447 | { |
| 7448 | int i; |
| 7449 | |
| 7450 | mutex_lock(&ctx->uring_lock); |
| 7451 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 7452 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7453 | struct fixed_file_table *table; |
| 7454 | struct file *f; |
| 7455 | |
| 7456 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7457 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 7458 | if (f) |
| 7459 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 7460 | else |
| 7461 | seq_printf(m, "%5u: <none>\n", i); |
| 7462 | } |
| 7463 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 7464 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7465 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 7466 | |
| 7467 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 7468 | (unsigned int) buf->len); |
| 7469 | } |
| 7470 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 7471 | seq_printf(m, "Personalities:\n"); |
| 7472 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 7473 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 7474 | seq_printf(m, "PollList:\n"); |
| 7475 | spin_lock_irq(&ctx->completion_lock); |
| 7476 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 7477 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 7478 | struct io_kiocb *req; |
| 7479 | |
| 7480 | hlist_for_each_entry(req, list, hash_node) |
| 7481 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 7482 | req->task->task_works != NULL); |
| 7483 | } |
| 7484 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7485 | mutex_unlock(&ctx->uring_lock); |
| 7486 | } |
| 7487 | |
| 7488 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 7489 | { |
| 7490 | struct io_ring_ctx *ctx = f->private_data; |
| 7491 | |
| 7492 | if (percpu_ref_tryget(&ctx->refs)) { |
| 7493 | __io_uring_show_fdinfo(ctx, m); |
| 7494 | percpu_ref_put(&ctx->refs); |
| 7495 | } |
| 7496 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7497 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7498 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7499 | static const struct file_operations io_uring_fops = { |
| 7500 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7501 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7502 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7503 | #ifndef CONFIG_MMU |
| 7504 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 7505 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 7506 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7507 | .poll = io_uring_poll, |
| 7508 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7509 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7510 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7511 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7512 | }; |
| 7513 | |
| 7514 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 7515 | struct io_uring_params *p) |
| 7516 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7517 | struct io_rings *rings; |
| 7518 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7519 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7520 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 7521 | if (size == SIZE_MAX) |
| 7522 | return -EOVERFLOW; |
| 7523 | |
| 7524 | rings = io_mem_alloc(size); |
| 7525 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7526 | return -ENOMEM; |
| 7527 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7528 | ctx->rings = rings; |
| 7529 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 7530 | rings->sq_ring_mask = p->sq_entries - 1; |
| 7531 | rings->cq_ring_mask = p->cq_entries - 1; |
| 7532 | rings->sq_ring_entries = p->sq_entries; |
| 7533 | rings->cq_ring_entries = p->cq_entries; |
| 7534 | ctx->sq_mask = rings->sq_ring_mask; |
| 7535 | ctx->cq_mask = rings->cq_ring_mask; |
| 7536 | ctx->sq_entries = rings->sq_ring_entries; |
| 7537 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7538 | |
| 7539 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7540 | if (size == SIZE_MAX) { |
| 7541 | io_mem_free(ctx->rings); |
| 7542 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7543 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7544 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7545 | |
| 7546 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7547 | if (!ctx->sq_sqes) { |
| 7548 | io_mem_free(ctx->rings); |
| 7549 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7550 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7551 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7552 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7553 | return 0; |
| 7554 | } |
| 7555 | |
| 7556 | /* |
| 7557 | * Allocate an anonymous fd, this is what constitutes the application |
| 7558 | * visible backing of an io_uring instance. The application mmaps this |
| 7559 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 7560 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 7561 | */ |
| 7562 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 7563 | { |
| 7564 | struct file *file; |
| 7565 | int ret; |
| 7566 | |
| 7567 | #if defined(CONFIG_UNIX) |
| 7568 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 7569 | &ctx->ring_sock); |
| 7570 | if (ret) |
| 7571 | return ret; |
| 7572 | #endif |
| 7573 | |
| 7574 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 7575 | if (ret < 0) |
| 7576 | goto err; |
| 7577 | |
| 7578 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 7579 | O_RDWR | O_CLOEXEC); |
| 7580 | if (IS_ERR(file)) { |
| 7581 | put_unused_fd(ret); |
| 7582 | ret = PTR_ERR(file); |
| 7583 | goto err; |
| 7584 | } |
| 7585 | |
| 7586 | #if defined(CONFIG_UNIX) |
| 7587 | ctx->ring_sock->file = file; |
| 7588 | #endif |
| 7589 | fd_install(ret, file); |
| 7590 | return ret; |
| 7591 | err: |
| 7592 | #if defined(CONFIG_UNIX) |
| 7593 | sock_release(ctx->ring_sock); |
| 7594 | ctx->ring_sock = NULL; |
| 7595 | #endif |
| 7596 | return ret; |
| 7597 | } |
| 7598 | |
| 7599 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 7600 | { |
| 7601 | struct user_struct *user = NULL; |
| 7602 | struct io_ring_ctx *ctx; |
| 7603 | bool account_mem; |
| 7604 | int ret; |
| 7605 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7606 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7607 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7608 | if (entries > IORING_MAX_ENTRIES) { |
| 7609 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 7610 | return -EINVAL; |
| 7611 | entries = IORING_MAX_ENTRIES; |
| 7612 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7613 | |
| 7614 | /* |
| 7615 | * Use twice as many entries for the CQ ring. It's possible for the |
| 7616 | * application to drive a higher depth than the size of the SQ ring, |
| 7617 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7618 | * some flexibility in overcommitting a bit. If the application has |
| 7619 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 7620 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7621 | */ |
| 7622 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7623 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 7624 | /* |
| 7625 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 7626 | * to a power-of-two, if it isn't already. We do NOT impose |
| 7627 | * any cq vs sq ring sizing. |
| 7628 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7629 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7630 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7631 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 7632 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 7633 | return -EINVAL; |
| 7634 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 7635 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7636 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 7637 | } else { |
| 7638 | p->cq_entries = 2 * p->sq_entries; |
| 7639 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7640 | |
| 7641 | user = get_uid(current_user()); |
| 7642 | account_mem = !capable(CAP_IPC_LOCK); |
| 7643 | |
| 7644 | if (account_mem) { |
| 7645 | ret = io_account_mem(user, |
| 7646 | ring_pages(p->sq_entries, p->cq_entries)); |
| 7647 | if (ret) { |
| 7648 | free_uid(user); |
| 7649 | return ret; |
| 7650 | } |
| 7651 | } |
| 7652 | |
| 7653 | ctx = io_ring_ctx_alloc(p); |
| 7654 | if (!ctx) { |
| 7655 | if (account_mem) |
| 7656 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 7657 | p->cq_entries)); |
| 7658 | free_uid(user); |
| 7659 | return -ENOMEM; |
| 7660 | } |
| 7661 | ctx->compat = in_compat_syscall(); |
| 7662 | ctx->account_mem = account_mem; |
| 7663 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 7664 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7665 | |
| 7666 | ret = io_allocate_scq_urings(ctx, p); |
| 7667 | if (ret) |
| 7668 | goto err; |
| 7669 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7670 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7671 | if (ret) |
| 7672 | goto err; |
| 7673 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7674 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7675 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 7676 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 7677 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 7678 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 7679 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 7680 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 7681 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7682 | |
| 7683 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7684 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 7685 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 7686 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 7687 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 7688 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 7689 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 7690 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 7691 | /* |
| 7692 | * Install ring fd as the very last thing, so we don't risk someone |
| 7693 | * having closed it before we finish setup |
| 7694 | */ |
| 7695 | ret = io_uring_get_fd(ctx); |
| 7696 | if (ret < 0) |
| 7697 | goto err; |
| 7698 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 7699 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 7700 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 7701 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7702 | 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] | 7703 | return ret; |
| 7704 | err: |
| 7705 | io_ring_ctx_wait_and_kill(ctx); |
| 7706 | return ret; |
| 7707 | } |
| 7708 | |
| 7709 | /* |
| 7710 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 7711 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 7712 | * params structure passed in. |
| 7713 | */ |
| 7714 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 7715 | { |
| 7716 | struct io_uring_params p; |
| 7717 | long ret; |
| 7718 | int i; |
| 7719 | |
| 7720 | if (copy_from_user(&p, params, sizeof(p))) |
| 7721 | return -EFAULT; |
| 7722 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 7723 | if (p.resv[i]) |
| 7724 | return -EINVAL; |
| 7725 | } |
| 7726 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7727 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7728 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7729 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7730 | return -EINVAL; |
| 7731 | |
| 7732 | ret = io_uring_create(entries, &p); |
| 7733 | if (ret < 0) |
| 7734 | return ret; |
| 7735 | |
| 7736 | if (copy_to_user(params, &p, sizeof(p))) |
| 7737 | return -EFAULT; |
| 7738 | |
| 7739 | return ret; |
| 7740 | } |
| 7741 | |
| 7742 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 7743 | struct io_uring_params __user *, params) |
| 7744 | { |
| 7745 | return io_uring_setup(entries, params); |
| 7746 | } |
| 7747 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7748 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 7749 | { |
| 7750 | struct io_uring_probe *p; |
| 7751 | size_t size; |
| 7752 | int i, ret; |
| 7753 | |
| 7754 | size = struct_size(p, ops, nr_args); |
| 7755 | if (size == SIZE_MAX) |
| 7756 | return -EOVERFLOW; |
| 7757 | p = kzalloc(size, GFP_KERNEL); |
| 7758 | if (!p) |
| 7759 | return -ENOMEM; |
| 7760 | |
| 7761 | ret = -EFAULT; |
| 7762 | if (copy_from_user(p, arg, size)) |
| 7763 | goto out; |
| 7764 | ret = -EINVAL; |
| 7765 | if (memchr_inv(p, 0, size)) |
| 7766 | goto out; |
| 7767 | |
| 7768 | p->last_op = IORING_OP_LAST - 1; |
| 7769 | if (nr_args > IORING_OP_LAST) |
| 7770 | nr_args = IORING_OP_LAST; |
| 7771 | |
| 7772 | for (i = 0; i < nr_args; i++) { |
| 7773 | p->ops[i].op = i; |
| 7774 | if (!io_op_defs[i].not_supported) |
| 7775 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 7776 | } |
| 7777 | p->ops_len = i; |
| 7778 | |
| 7779 | ret = 0; |
| 7780 | if (copy_to_user(arg, p, size)) |
| 7781 | ret = -EFAULT; |
| 7782 | out: |
| 7783 | kfree(p); |
| 7784 | return ret; |
| 7785 | } |
| 7786 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7787 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 7788 | { |
| 7789 | const struct cred *creds = get_current_cred(); |
| 7790 | int id; |
| 7791 | |
| 7792 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 7793 | USHRT_MAX, GFP_KERNEL); |
| 7794 | if (id < 0) |
| 7795 | put_cred(creds); |
| 7796 | return id; |
| 7797 | } |
| 7798 | |
| 7799 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 7800 | { |
| 7801 | const struct cred *old_creds; |
| 7802 | |
| 7803 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 7804 | if (old_creds) { |
| 7805 | put_cred(old_creds); |
| 7806 | return 0; |
| 7807 | } |
| 7808 | |
| 7809 | return -EINVAL; |
| 7810 | } |
| 7811 | |
| 7812 | static bool io_register_op_must_quiesce(int op) |
| 7813 | { |
| 7814 | switch (op) { |
| 7815 | case IORING_UNREGISTER_FILES: |
| 7816 | case IORING_REGISTER_FILES_UPDATE: |
| 7817 | case IORING_REGISTER_PROBE: |
| 7818 | case IORING_REGISTER_PERSONALITY: |
| 7819 | case IORING_UNREGISTER_PERSONALITY: |
| 7820 | return false; |
| 7821 | default: |
| 7822 | return true; |
| 7823 | } |
| 7824 | } |
| 7825 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7826 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 7827 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7828 | __releases(ctx->uring_lock) |
| 7829 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7830 | { |
| 7831 | int ret; |
| 7832 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 7833 | /* |
| 7834 | * We're inside the ring mutex, if the ref is already dying, then |
| 7835 | * someone else killed the ctx or is already going through |
| 7836 | * io_uring_register(). |
| 7837 | */ |
| 7838 | if (percpu_ref_is_dying(&ctx->refs)) |
| 7839 | return -ENXIO; |
| 7840 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7841 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7842 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7843 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7844 | /* |
| 7845 | * Drop uring mutex before waiting for references to exit. If |
| 7846 | * another thread is currently inside io_uring_enter() it might |
| 7847 | * need to grab the uring_lock to make progress. If we hold it |
| 7848 | * here across the drain wait, then we can deadlock. It's safe |
| 7849 | * to drop the mutex here, since no new references will come in |
| 7850 | * after we've killed the percpu ref. |
| 7851 | */ |
| 7852 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7853 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7854 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7855 | if (ret) { |
| 7856 | percpu_ref_resurrect(&ctx->refs); |
| 7857 | ret = -EINTR; |
| 7858 | goto out; |
| 7859 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7860 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7861 | |
| 7862 | switch (opcode) { |
| 7863 | case IORING_REGISTER_BUFFERS: |
| 7864 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 7865 | break; |
| 7866 | case IORING_UNREGISTER_BUFFERS: |
| 7867 | ret = -EINVAL; |
| 7868 | if (arg || nr_args) |
| 7869 | break; |
| 7870 | ret = io_sqe_buffer_unregister(ctx); |
| 7871 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7872 | case IORING_REGISTER_FILES: |
| 7873 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 7874 | break; |
| 7875 | case IORING_UNREGISTER_FILES: |
| 7876 | ret = -EINVAL; |
| 7877 | if (arg || nr_args) |
| 7878 | break; |
| 7879 | ret = io_sqe_files_unregister(ctx); |
| 7880 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7881 | case IORING_REGISTER_FILES_UPDATE: |
| 7882 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7883 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7884 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7885 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7886 | ret = -EINVAL; |
| 7887 | if (nr_args != 1) |
| 7888 | break; |
| 7889 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7890 | if (ret) |
| 7891 | break; |
| 7892 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7893 | ctx->eventfd_async = 1; |
| 7894 | else |
| 7895 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7896 | break; |
| 7897 | case IORING_UNREGISTER_EVENTFD: |
| 7898 | ret = -EINVAL; |
| 7899 | if (arg || nr_args) |
| 7900 | break; |
| 7901 | ret = io_eventfd_unregister(ctx); |
| 7902 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7903 | case IORING_REGISTER_PROBE: |
| 7904 | ret = -EINVAL; |
| 7905 | if (!arg || nr_args > 256) |
| 7906 | break; |
| 7907 | ret = io_probe(ctx, arg, nr_args); |
| 7908 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7909 | case IORING_REGISTER_PERSONALITY: |
| 7910 | ret = -EINVAL; |
| 7911 | if (arg || nr_args) |
| 7912 | break; |
| 7913 | ret = io_register_personality(ctx); |
| 7914 | break; |
| 7915 | case IORING_UNREGISTER_PERSONALITY: |
| 7916 | ret = -EINVAL; |
| 7917 | if (arg) |
| 7918 | break; |
| 7919 | ret = io_unregister_personality(ctx, nr_args); |
| 7920 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7921 | default: |
| 7922 | ret = -EINVAL; |
| 7923 | break; |
| 7924 | } |
| 7925 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7926 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7927 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7928 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7929 | out: |
| 7930 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7931 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7932 | return ret; |
| 7933 | } |
| 7934 | |
| 7935 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7936 | void __user *, arg, unsigned int, nr_args) |
| 7937 | { |
| 7938 | struct io_ring_ctx *ctx; |
| 7939 | long ret = -EBADF; |
| 7940 | struct fd f; |
| 7941 | |
| 7942 | f = fdget(fd); |
| 7943 | if (!f.file) |
| 7944 | return -EBADF; |
| 7945 | |
| 7946 | ret = -EOPNOTSUPP; |
| 7947 | if (f.file->f_op != &io_uring_fops) |
| 7948 | goto out_fput; |
| 7949 | |
| 7950 | ctx = f.file->private_data; |
| 7951 | |
| 7952 | mutex_lock(&ctx->uring_lock); |
| 7953 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7954 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7955 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7956 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7957 | out_fput: |
| 7958 | fdput(f); |
| 7959 | return ret; |
| 7960 | } |
| 7961 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7962 | static int __init io_uring_init(void) |
| 7963 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7964 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7965 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7966 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7967 | } while (0) |
| 7968 | |
| 7969 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7970 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7971 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7972 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7973 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7974 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7975 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7976 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7977 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7978 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7979 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7980 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7981 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7982 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7983 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7984 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7985 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7986 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7987 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7988 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7989 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7990 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7991 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7992 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7993 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7994 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7995 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7996 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7997 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7998 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7999 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 8000 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8001 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 8002 | return 0; |
| 8003 | }; |
| 8004 | __initcall(io_uring_init); |