Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 49 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 50 | |
| 51 | #include <linux/sched/signal.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/file.h> |
| 54 | #include <linux/fdtable.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/mman.h> |
| 57 | #include <linux/mmu_context.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 77 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 78 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 79 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 80 | #include <linux/task_work.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 81 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 82 | #define CREATE_TRACE_POINTS |
| 83 | #include <trace/events/io_uring.h> |
| 84 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | #include <uapi/linux/io_uring.h> |
| 86 | |
| 87 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 88 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 89 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 90 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 91 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 95 | */ |
| 96 | #define IORING_FILE_TABLE_SHIFT 9 |
| 97 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 98 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 99 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 100 | |
| 101 | struct io_uring { |
| 102 | u32 head ____cacheline_aligned_in_smp; |
| 103 | u32 tail ____cacheline_aligned_in_smp; |
| 104 | }; |
| 105 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 107 | * This data is shared with the application through the mmap at offsets |
| 108 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 109 | * |
| 110 | * The offsets to the member fields are published through struct |
| 111 | * io_sqring_offsets when calling io_uring_setup. |
| 112 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 114 | /* |
| 115 | * Head and tail offsets into the ring; the offsets need to be |
| 116 | * masked to get valid indices. |
| 117 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 118 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 119 | * and the application controls tail of the sq ring and the head of the |
| 120 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 123 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | * ring_entries - 1) |
| 126 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 127 | u32 sq_ring_mask, cq_ring_mask; |
| 128 | /* Ring sizes (constant, power of 2) */ |
| 129 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 130 | /* |
| 131 | * Number of invalid entries dropped by the kernel due to |
| 132 | * invalid index stored in array |
| 133 | * |
| 134 | * Written by the kernel, shouldn't be modified by the |
| 135 | * application (i.e. get number of "new events" by comparing to |
| 136 | * cached value). |
| 137 | * |
| 138 | * After a new SQ head value was read by the application this |
| 139 | * counter includes all submissions that were dropped reaching |
| 140 | * the new SQ head (and possibly more). |
| 141 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 142 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 143 | /* |
| 144 | * Runtime flags |
| 145 | * |
| 146 | * Written by the kernel, shouldn't be modified by the |
| 147 | * application. |
| 148 | * |
| 149 | * The application needs a full memory barrier before checking |
| 150 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 151 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 152 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 153 | /* |
| 154 | * Number of completion events lost because the queue was full; |
| 155 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 156 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 157 | * the completion queue. |
| 158 | * |
| 159 | * Written by the kernel, shouldn't be modified by the |
| 160 | * application (i.e. get number of "new events" by comparing to |
| 161 | * cached value). |
| 162 | * |
| 163 | * As completion events come in out of order this counter is not |
| 164 | * ordered with any other data. |
| 165 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 166 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Ring buffer of completion events. |
| 169 | * |
| 170 | * The kernel writes completion events fresh every time they are |
| 171 | * produced, so the application is allowed to modify pending |
| 172 | * entries. |
| 173 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 174 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 177 | struct io_mapped_ubuf { |
| 178 | u64 ubuf; |
| 179 | size_t len; |
| 180 | struct bio_vec *bvec; |
| 181 | unsigned int nr_bvecs; |
| 182 | }; |
| 183 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 184 | struct fixed_file_table { |
| 185 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 188 | struct fixed_file_data { |
| 189 | struct fixed_file_table *table; |
| 190 | struct io_ring_ctx *ctx; |
| 191 | |
| 192 | struct percpu_ref refs; |
| 193 | struct llist_head put_llist; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 194 | struct work_struct ref_work; |
| 195 | struct completion done; |
| 196 | }; |
| 197 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 198 | struct io_buffer { |
| 199 | struct list_head list; |
| 200 | __u64 addr; |
| 201 | __s32 len; |
| 202 | __u16 bid; |
| 203 | }; |
| 204 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 205 | struct io_ring_ctx { |
| 206 | struct { |
| 207 | struct percpu_ref refs; |
| 208 | } ____cacheline_aligned_in_smp; |
| 209 | |
| 210 | struct { |
| 211 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 212 | unsigned int compat: 1; |
| 213 | unsigned int account_mem: 1; |
| 214 | unsigned int cq_overflow_flushed: 1; |
| 215 | unsigned int drain_next: 1; |
| 216 | unsigned int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 217 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 218 | /* |
| 219 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 220 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 221 | * |
| 222 | * This indirection could e.g. be used to assign fixed |
| 223 | * io_uring_sqe entries to operations and only submit them to |
| 224 | * the queue when needed. |
| 225 | * |
| 226 | * The kernel modifies neither the indices array nor the entries |
| 227 | * array. |
| 228 | */ |
| 229 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 230 | unsigned cached_sq_head; |
| 231 | unsigned sq_entries; |
| 232 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 233 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 234 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 235 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 236 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 237 | |
| 238 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 239 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 240 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 241 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 242 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 243 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 244 | } ____cacheline_aligned_in_smp; |
| 245 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 246 | struct io_rings *rings; |
| 247 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 248 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 249 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 250 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 251 | struct mm_struct *sqo_mm; |
| 252 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 253 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 254 | /* |
| 255 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 256 | * readers must ensure that ->refs is alive as long as the file* is |
| 257 | * used. Only updated through io_uring_register(2). |
| 258 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 259 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 260 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 261 | int ring_fd; |
| 262 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 263 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 264 | /* if used, fixed mapped user buffers */ |
| 265 | unsigned nr_user_bufs; |
| 266 | struct io_mapped_ubuf *user_bufs; |
| 267 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 268 | struct user_struct *user; |
| 269 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 270 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 271 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 272 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 273 | struct completion *completions; |
| 274 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 275 | /* if all else fails... */ |
| 276 | struct io_kiocb *fallback_req; |
| 277 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 278 | #if defined(CONFIG_UNIX) |
| 279 | struct socket *ring_sock; |
| 280 | #endif |
| 281 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 282 | struct idr io_buffer_idr; |
| 283 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 284 | struct idr personality_idr; |
| 285 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 286 | struct { |
| 287 | unsigned cached_cq_tail; |
| 288 | unsigned cq_entries; |
| 289 | unsigned cq_mask; |
| 290 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 291 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 292 | struct wait_queue_head cq_wait; |
| 293 | struct fasync_struct *cq_fasync; |
| 294 | struct eventfd_ctx *cq_ev_fd; |
| 295 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 296 | |
| 297 | struct { |
| 298 | struct mutex uring_lock; |
| 299 | wait_queue_head_t wait; |
| 300 | } ____cacheline_aligned_in_smp; |
| 301 | |
| 302 | struct { |
| 303 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 304 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 305 | /* |
| 306 | * ->poll_list is protected by the ctx->uring_lock for |
| 307 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 308 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 309 | * manipulate the list, hence no extra locking is needed there. |
| 310 | */ |
| 311 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 312 | struct hlist_head *cancel_hash; |
| 313 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 314 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 315 | |
| 316 | spinlock_t inflight_lock; |
| 317 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 318 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 321 | /* |
| 322 | * First field must be the file pointer in all the |
| 323 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 324 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 325 | struct io_poll_iocb { |
| 326 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 327 | union { |
| 328 | struct wait_queue_head *head; |
| 329 | u64 addr; |
| 330 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 331 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 332 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 333 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 334 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 335 | }; |
| 336 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 337 | struct io_close { |
| 338 | struct file *file; |
| 339 | struct file *put_file; |
| 340 | int fd; |
| 341 | }; |
| 342 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 343 | struct io_timeout_data { |
| 344 | struct io_kiocb *req; |
| 345 | struct hrtimer timer; |
| 346 | struct timespec64 ts; |
| 347 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 348 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 351 | struct io_accept { |
| 352 | struct file *file; |
| 353 | struct sockaddr __user *addr; |
| 354 | int __user *addr_len; |
| 355 | int flags; |
| 356 | }; |
| 357 | |
| 358 | struct io_sync { |
| 359 | struct file *file; |
| 360 | loff_t len; |
| 361 | loff_t off; |
| 362 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 363 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 366 | struct io_cancel { |
| 367 | struct file *file; |
| 368 | u64 addr; |
| 369 | }; |
| 370 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 371 | struct io_timeout { |
| 372 | struct file *file; |
| 373 | u64 addr; |
| 374 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 375 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 376 | }; |
| 377 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 378 | struct io_rw { |
| 379 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 380 | struct kiocb kiocb; |
| 381 | u64 addr; |
| 382 | u64 len; |
| 383 | }; |
| 384 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 385 | struct io_connect { |
| 386 | struct file *file; |
| 387 | struct sockaddr __user *addr; |
| 388 | int addr_len; |
| 389 | }; |
| 390 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 391 | struct io_sr_msg { |
| 392 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 393 | union { |
| 394 | struct user_msghdr __user *msg; |
| 395 | void __user *buf; |
| 396 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 397 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 398 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 399 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 400 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 403 | struct io_open { |
| 404 | struct file *file; |
| 405 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 406 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 407 | unsigned mask; |
| 408 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 409 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 410 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 411 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 414 | struct io_files_update { |
| 415 | struct file *file; |
| 416 | u64 arg; |
| 417 | u32 nr_args; |
| 418 | u32 offset; |
| 419 | }; |
| 420 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 421 | struct io_fadvise { |
| 422 | struct file *file; |
| 423 | u64 offset; |
| 424 | u32 len; |
| 425 | u32 advice; |
| 426 | }; |
| 427 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 428 | struct io_madvise { |
| 429 | struct file *file; |
| 430 | u64 addr; |
| 431 | u32 len; |
| 432 | u32 advice; |
| 433 | }; |
| 434 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 435 | struct io_epoll { |
| 436 | struct file *file; |
| 437 | int epfd; |
| 438 | int op; |
| 439 | int fd; |
| 440 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 441 | }; |
| 442 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 443 | struct io_splice { |
| 444 | struct file *file_out; |
| 445 | struct file *file_in; |
| 446 | loff_t off_out; |
| 447 | loff_t off_in; |
| 448 | u64 len; |
| 449 | unsigned int flags; |
| 450 | }; |
| 451 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 452 | struct io_provide_buf { |
| 453 | struct file *file; |
| 454 | __u64 addr; |
| 455 | __s32 len; |
| 456 | __u32 bgid; |
| 457 | __u16 nbufs; |
| 458 | __u16 bid; |
| 459 | }; |
| 460 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 461 | struct io_async_connect { |
| 462 | struct sockaddr_storage address; |
| 463 | }; |
| 464 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 465 | struct io_async_msghdr { |
| 466 | struct iovec fast_iov[UIO_FASTIOV]; |
| 467 | struct iovec *iov; |
| 468 | struct sockaddr __user *uaddr; |
| 469 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 470 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 471 | }; |
| 472 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 473 | struct io_async_rw { |
| 474 | struct iovec fast_iov[UIO_FASTIOV]; |
| 475 | struct iovec *iov; |
| 476 | ssize_t nr_segs; |
| 477 | ssize_t size; |
| 478 | }; |
| 479 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 480 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 481 | union { |
| 482 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 483 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 484 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 485 | struct io_timeout_data timeout; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 486 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 489 | enum { |
| 490 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 491 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 492 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 493 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 494 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 495 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 496 | |
| 497 | REQ_F_LINK_NEXT_BIT, |
| 498 | REQ_F_FAIL_LINK_BIT, |
| 499 | REQ_F_INFLIGHT_BIT, |
| 500 | REQ_F_CUR_POS_BIT, |
| 501 | REQ_F_NOWAIT_BIT, |
| 502 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 503 | REQ_F_LINK_TIMEOUT_BIT, |
| 504 | REQ_F_TIMEOUT_BIT, |
| 505 | REQ_F_ISREG_BIT, |
| 506 | REQ_F_MUST_PUNT_BIT, |
| 507 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 508 | REQ_F_COMP_LOCKED_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 509 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 510 | REQ_F_OVERFLOW_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 511 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 512 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | enum { |
| 516 | /* ctx owns file */ |
| 517 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 518 | /* drain existing IO first */ |
| 519 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 520 | /* linked sqes */ |
| 521 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 522 | /* doesn't sever on completion < 0 */ |
| 523 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 524 | /* IOSQE_ASYNC */ |
| 525 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 526 | /* IOSQE_BUFFER_SELECT */ |
| 527 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 528 | |
| 529 | /* already grabbed next link */ |
| 530 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 531 | /* fail rest of links */ |
| 532 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 533 | /* on inflight list */ |
| 534 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 535 | /* read/write uses file position */ |
| 536 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 537 | /* must not punt to workers */ |
| 538 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 539 | /* polled IO has completed */ |
| 540 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 541 | /* has linked timeout */ |
| 542 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 543 | /* timeout request */ |
| 544 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 545 | /* regular file */ |
| 546 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 547 | /* must be punted even for NONBLOCK */ |
| 548 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 549 | /* no timeout sequence */ |
| 550 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 551 | /* completion under lock */ |
| 552 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 553 | /* needs cleanup */ |
| 554 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 555 | /* in overflow list */ |
| 556 | REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 557 | /* already went through poll handler */ |
| 558 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 559 | /* buffer already selected */ |
| 560 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | struct async_poll { |
| 564 | struct io_poll_iocb poll; |
| 565 | struct io_wq_work work; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 566 | }; |
| 567 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 568 | /* |
| 569 | * NOTE! Each of the iocb union members has the file pointer |
| 570 | * as the first entry in their struct definition. So you can |
| 571 | * access the file pointer through any of the sub-structs, |
| 572 | * or directly as just 'ki_filp' in this struct. |
| 573 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 574 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 575 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 576 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 577 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 578 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 579 | struct io_accept accept; |
| 580 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 581 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 582 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 583 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 584 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 585 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 586 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 587 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 588 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 589 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 590 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 591 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 592 | struct io_provide_buf pbuf; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 593 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 594 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 595 | struct io_async_ctx *io; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 596 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 597 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 598 | |
| 599 | struct io_ring_ctx *ctx; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 600 | struct list_head list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 601 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 602 | refcount_t refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 603 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 604 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 605 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 606 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 607 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 608 | struct list_head link_list; |
| 609 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 610 | struct list_head inflight_entry; |
| 611 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 612 | union { |
| 613 | /* |
| 614 | * Only commands that never go async can use the below fields, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 615 | * obviously. Right now only IORING_OP_POLL_ADD uses them, and |
| 616 | * async armed poll handlers for regular commands. The latter |
| 617 | * restore the work, if needed. |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 618 | */ |
| 619 | struct { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 620 | struct callback_head task_work; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 621 | struct hlist_node hash_node; |
| 622 | struct async_poll *apoll; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 623 | int cflags; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 624 | }; |
| 625 | struct io_wq_work work; |
| 626 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 630 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 631 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 632 | struct io_submit_state { |
| 633 | struct blk_plug plug; |
| 634 | |
| 635 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 636 | * io_kiocb alloc cache |
| 637 | */ |
| 638 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 639 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 640 | |
| 641 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 642 | * File reference cache |
| 643 | */ |
| 644 | struct file *file; |
| 645 | unsigned int fd; |
| 646 | unsigned int has_refs; |
| 647 | unsigned int used_refs; |
| 648 | unsigned int ios_left; |
| 649 | }; |
| 650 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 651 | struct io_op_def { |
| 652 | /* needs req->io allocated for deferral/async */ |
| 653 | unsigned async_ctx : 1; |
| 654 | /* needs current->mm setup, does mm access */ |
| 655 | unsigned needs_mm : 1; |
| 656 | /* needs req->file assigned */ |
| 657 | unsigned needs_file : 1; |
| 658 | /* needs req->file assigned IFF fd is >= 0 */ |
| 659 | unsigned fd_non_neg : 1; |
| 660 | /* hash wq insertion if file is a regular file */ |
| 661 | unsigned hash_reg_file : 1; |
| 662 | /* unbound wq insertion if file is a non-regular file */ |
| 663 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 664 | /* opcode is not supported by this kernel */ |
| 665 | unsigned not_supported : 1; |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 666 | /* needs file table */ |
| 667 | unsigned file_table : 1; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 668 | /* needs ->fs */ |
| 669 | unsigned needs_fs : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 670 | /* set if opcode supports polled "wait" */ |
| 671 | unsigned pollin : 1; |
| 672 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 673 | /* op supports buffer selection */ |
| 674 | unsigned buffer_select : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 678 | [IORING_OP_NOP] = {}, |
| 679 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 680 | .async_ctx = 1, |
| 681 | .needs_mm = 1, |
| 682 | .needs_file = 1, |
| 683 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 684 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 685 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 686 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 687 | .async_ctx = 1, |
| 688 | .needs_mm = 1, |
| 689 | .needs_file = 1, |
| 690 | .hash_reg_file = 1, |
| 691 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 692 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 693 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 694 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 695 | .needs_file = 1, |
| 696 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 697 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 698 | .needs_file = 1, |
| 699 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 700 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 701 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 702 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 703 | .needs_file = 1, |
| 704 | .hash_reg_file = 1, |
| 705 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 706 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 707 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 708 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 709 | .needs_file = 1, |
| 710 | .unbound_nonreg_file = 1, |
| 711 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 712 | [IORING_OP_POLL_REMOVE] = {}, |
| 713 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 714 | .needs_file = 1, |
| 715 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 716 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 717 | .async_ctx = 1, |
| 718 | .needs_mm = 1, |
| 719 | .needs_file = 1, |
| 720 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 721 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 722 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 723 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 724 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 725 | .async_ctx = 1, |
| 726 | .needs_mm = 1, |
| 727 | .needs_file = 1, |
| 728 | .unbound_nonreg_file = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 729 | .needs_fs = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 730 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 731 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 732 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 733 | .async_ctx = 1, |
| 734 | .needs_mm = 1, |
| 735 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 736 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 737 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 738 | .needs_mm = 1, |
| 739 | .needs_file = 1, |
| 740 | .unbound_nonreg_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 741 | .file_table = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 742 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 743 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 744 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 745 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 746 | .async_ctx = 1, |
| 747 | .needs_mm = 1, |
| 748 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 749 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 750 | .async_ctx = 1, |
| 751 | .needs_mm = 1, |
| 752 | .needs_file = 1, |
| 753 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 754 | .pollout = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 755 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 756 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 757 | .needs_file = 1, |
| 758 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 759 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 760 | .needs_file = 1, |
| 761 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 762 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 763 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 764 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 765 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 766 | .needs_file = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 767 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 768 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 769 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 770 | .needs_mm = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 771 | .file_table = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 772 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 773 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 774 | .needs_mm = 1, |
| 775 | .needs_file = 1, |
| 776 | .fd_non_neg = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 777 | .needs_fs = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 778 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 779 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 780 | .needs_mm = 1, |
| 781 | .needs_file = 1, |
| 782 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 783 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 784 | .buffer_select = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 785 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 786 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 787 | .needs_mm = 1, |
| 788 | .needs_file = 1, |
| 789 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 790 | .pollout = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 791 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 792 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 793 | .needs_file = 1, |
| 794 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 795 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 796 | .needs_mm = 1, |
| 797 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 798 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 799 | .needs_mm = 1, |
| 800 | .needs_file = 1, |
| 801 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 802 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 803 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 804 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 805 | .needs_mm = 1, |
| 806 | .needs_file = 1, |
| 807 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 808 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 809 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 810 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 811 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 812 | .needs_file = 1, |
| 813 | .fd_non_neg = 1, |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 814 | .file_table = 1, |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 815 | .needs_fs = 1, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 816 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 817 | [IORING_OP_EPOLL_CTL] = { |
| 818 | .unbound_nonreg_file = 1, |
| 819 | .file_table = 1, |
| 820 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 821 | [IORING_OP_SPLICE] = { |
| 822 | .needs_file = 1, |
| 823 | .hash_reg_file = 1, |
| 824 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 825 | }, |
| 826 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 827 | }; |
| 828 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 829 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 830 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 831 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 832 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 833 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 834 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 835 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 836 | struct io_uring_files_update *ip, |
| 837 | unsigned nr_args); |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 838 | static int io_grab_files(struct io_kiocb *req); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 839 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 840 | static void io_cleanup_req(struct io_kiocb *req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 841 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 842 | int fd, struct file **out_file, bool fixed); |
| 843 | static void __io_queue_sqe(struct io_kiocb *req, |
| 844 | const struct io_uring_sqe *sqe); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 845 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 846 | static struct kmem_cache *req_cachep; |
| 847 | |
| 848 | static const struct file_operations io_uring_fops; |
| 849 | |
| 850 | struct sock *io_uring_get_socket(struct file *file) |
| 851 | { |
| 852 | #if defined(CONFIG_UNIX) |
| 853 | if (file->f_op == &io_uring_fops) { |
| 854 | struct io_ring_ctx *ctx = file->private_data; |
| 855 | |
| 856 | return ctx->ring_sock->sk; |
| 857 | } |
| 858 | #endif |
| 859 | return NULL; |
| 860 | } |
| 861 | EXPORT_SYMBOL(io_uring_get_socket); |
| 862 | |
| 863 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 864 | { |
| 865 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 866 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 867 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 871 | { |
| 872 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 873 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 874 | |
| 875 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 876 | if (!ctx) |
| 877 | return NULL; |
| 878 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 879 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 880 | if (!ctx->fallback_req) |
| 881 | goto err; |
| 882 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 883 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 884 | if (!ctx->completions) |
| 885 | goto err; |
| 886 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 887 | /* |
| 888 | * Use 5 bits less than the max cq entries, that should give us around |
| 889 | * 32 entries per hash list if totally full and uniformly spread. |
| 890 | */ |
| 891 | hash_bits = ilog2(p->cq_entries); |
| 892 | hash_bits -= 5; |
| 893 | if (hash_bits <= 0) |
| 894 | hash_bits = 1; |
| 895 | ctx->cancel_hash_bits = hash_bits; |
| 896 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 897 | GFP_KERNEL); |
| 898 | if (!ctx->cancel_hash) |
| 899 | goto err; |
| 900 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 901 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 902 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 903 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 904 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 905 | |
| 906 | ctx->flags = p->flags; |
| 907 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 908 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 909 | init_completion(&ctx->completions[0]); |
| 910 | init_completion(&ctx->completions[1]); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 911 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 912 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 913 | mutex_init(&ctx->uring_lock); |
| 914 | init_waitqueue_head(&ctx->wait); |
| 915 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 916 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 917 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 918 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 919 | init_waitqueue_head(&ctx->inflight_wait); |
| 920 | spin_lock_init(&ctx->inflight_lock); |
| 921 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 922 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 923 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 924 | if (ctx->fallback_req) |
| 925 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 926 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 927 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 928 | kfree(ctx); |
| 929 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 932 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 933 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 934 | struct io_ring_ctx *ctx = req->ctx; |
| 935 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 936 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 937 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 938 | } |
| 939 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 940 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 941 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 942 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 943 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 944 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 945 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | 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] | 949 | { |
| 950 | struct io_kiocb *req; |
| 951 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 952 | 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] | 953 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 954 | list_del_init(&req->list); |
| 955 | return req; |
| 956 | } |
| 957 | |
| 958 | return NULL; |
| 959 | } |
| 960 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 961 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 962 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 963 | struct io_kiocb *req; |
| 964 | |
| 965 | 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] | 966 | if (req) { |
| 967 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 968 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 969 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 970 | list_del_init(&req->list); |
| 971 | return req; |
| 972 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 976 | } |
| 977 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 978 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 979 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 980 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 981 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 982 | /* order cqe stores with ring update */ |
| 983 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 984 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 985 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 986 | wake_up_interruptible(&ctx->cq_wait); |
| 987 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 991 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 992 | const struct io_op_def *def) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 993 | { |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 994 | if (!req->work.mm && def->needs_mm) { |
| 995 | mmgrab(current->mm); |
| 996 | req->work.mm = current->mm; |
| 997 | } |
| 998 | if (!req->work.creds) |
| 999 | req->work.creds = get_current_cred(); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1000 | if (!req->work.fs && def->needs_fs) { |
| 1001 | spin_lock(¤t->fs->lock); |
| 1002 | if (!current->fs->in_exec) { |
| 1003 | req->work.fs = current->fs; |
| 1004 | req->work.fs->users++; |
| 1005 | } else { |
| 1006 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1007 | } |
| 1008 | spin_unlock(¤t->fs->lock); |
| 1009 | } |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 1010 | if (!req->work.task_pid) |
| 1011 | req->work.task_pid = task_pid_vnr(current); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 1015 | { |
| 1016 | if (req->work.mm) { |
| 1017 | mmdrop(req->work.mm); |
| 1018 | req->work.mm = NULL; |
| 1019 | } |
| 1020 | if (req->work.creds) { |
| 1021 | put_cred(req->work.creds); |
| 1022 | req->work.creds = NULL; |
| 1023 | } |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1024 | if (req->work.fs) { |
| 1025 | struct fs_struct *fs = req->work.fs; |
| 1026 | |
| 1027 | spin_lock(&req->work.fs->lock); |
| 1028 | if (--fs->users) |
| 1029 | fs = NULL; |
| 1030 | spin_unlock(&req->work.fs->lock); |
| 1031 | if (fs) |
| 1032 | free_fs_struct(fs); |
| 1033 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1034 | } |
| 1035 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1036 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 1037 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1038 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1039 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1040 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1041 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1042 | if (req->flags & REQ_F_ISREG) { |
| 1043 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1044 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1045 | } else { |
| 1046 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1047 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1048 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1049 | |
| 1050 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1051 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1052 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1053 | return do_hashed; |
| 1054 | } |
| 1055 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1056 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1057 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1058 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1059 | struct io_kiocb *link; |
| 1060 | bool do_hashed; |
| 1061 | |
| 1062 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1063 | |
| 1064 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 1065 | req->flags); |
| 1066 | if (!do_hashed) { |
| 1067 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 1068 | } else { |
| 1069 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 1070 | file_inode(req->file)); |
| 1071 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1072 | |
| 1073 | if (link) |
| 1074 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1075 | } |
| 1076 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1077 | static void io_kill_timeout(struct io_kiocb *req) |
| 1078 | { |
| 1079 | int ret; |
| 1080 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1081 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1082 | if (ret != -1) { |
| 1083 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 1084 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1085 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1086 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 1091 | { |
| 1092 | struct io_kiocb *req, *tmp; |
| 1093 | |
| 1094 | spin_lock_irq(&ctx->completion_lock); |
| 1095 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 1096 | io_kill_timeout(req); |
| 1097 | spin_unlock_irq(&ctx->completion_lock); |
| 1098 | } |
| 1099 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1100 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1101 | { |
| 1102 | struct io_kiocb *req; |
| 1103 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1104 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 1105 | io_kill_timeout(req); |
| 1106 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1107 | __io_commit_cqring(ctx); |
| 1108 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 1109 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1110 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1111 | } |
| 1112 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1113 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1114 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1115 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1116 | unsigned tail; |
| 1117 | |
| 1118 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1119 | /* |
| 1120 | * writes to the cq entry need to come after reading head; the |
| 1121 | * control dependency is enough as we're using WRITE_ONCE to |
| 1122 | * fill the cq entry |
| 1123 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1124 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1125 | return NULL; |
| 1126 | |
| 1127 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1128 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1131 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1132 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1133 | if (!ctx->cq_ev_fd) |
| 1134 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1135 | if (!ctx->eventfd_async) |
| 1136 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1137 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1140 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1141 | { |
| 1142 | if (waitqueue_active(&ctx->wait)) |
| 1143 | wake_up(&ctx->wait); |
| 1144 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1145 | wake_up(&ctx->sqo_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1146 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1147 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1148 | } |
| 1149 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1150 | /* Returns true if there are no backlogged entries after the flush */ |
| 1151 | 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] | 1152 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1153 | struct io_rings *rings = ctx->rings; |
| 1154 | struct io_uring_cqe *cqe; |
| 1155 | struct io_kiocb *req; |
| 1156 | unsigned long flags; |
| 1157 | LIST_HEAD(list); |
| 1158 | |
| 1159 | if (!force) { |
| 1160 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1161 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1162 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1163 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1164 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1168 | |
| 1169 | /* if force is set, the ring is going away. always drop after that */ |
| 1170 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1171 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1172 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1173 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1174 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1175 | cqe = io_get_cqring(ctx); |
| 1176 | if (!cqe && !force) |
| 1177 | break; |
| 1178 | |
| 1179 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1180 | list); |
| 1181 | list_move(&req->list, &list); |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1182 | req->flags &= ~REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1183 | if (cqe) { |
| 1184 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1185 | WRITE_ONCE(cqe->res, req->result); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1186 | WRITE_ONCE(cqe->flags, req->cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1189 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1194 | if (cqe) { |
| 1195 | clear_bit(0, &ctx->sq_check_overflow); |
| 1196 | clear_bit(0, &ctx->cq_check_overflow); |
| 1197 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1198 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1199 | io_cqring_ev_posted(ctx); |
| 1200 | |
| 1201 | while (!list_empty(&list)) { |
| 1202 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1203 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1204 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1205 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1206 | |
| 1207 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1210 | 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] | 1211 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1212 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1213 | struct io_uring_cqe *cqe; |
| 1214 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1215 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1216 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1217 | /* |
| 1218 | * If we can't get a cq entry, userspace overflowed the |
| 1219 | * submission (by quite a lot). Increment the overflow count in |
| 1220 | * the ring. |
| 1221 | */ |
| 1222 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1223 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1224 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1225 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1226 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1227 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1228 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1229 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1230 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1231 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1232 | set_bit(0, &ctx->sq_check_overflow); |
| 1233 | set_bit(0, &ctx->cq_check_overflow); |
| 1234 | } |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 1235 | req->flags |= REQ_F_OVERFLOW; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1236 | refcount_inc(&req->refs); |
| 1237 | req->result = res; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1238 | req->cflags = cflags; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1239 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1243 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1244 | { |
| 1245 | __io_cqring_fill_event(req, res, 0); |
| 1246 | } |
| 1247 | |
| 1248 | 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] | 1249 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1250 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1251 | unsigned long flags; |
| 1252 | |
| 1253 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1254 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1255 | io_commit_cqring(ctx); |
| 1256 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1257 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1258 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1261 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
| 1262 | { |
| 1263 | __io_cqring_add_event(req, res, 0); |
| 1264 | } |
| 1265 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1266 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1267 | { |
| 1268 | return req == (struct io_kiocb *) |
| 1269 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1270 | } |
| 1271 | |
| 1272 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1273 | { |
| 1274 | struct io_kiocb *req; |
| 1275 | |
| 1276 | req = ctx->fallback_req; |
| 1277 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1278 | return req; |
| 1279 | |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1283 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1284 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1285 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1286 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1287 | struct io_kiocb *req; |
| 1288 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1289 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1290 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1291 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1292 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1293 | } else if (!state->free_reqs) { |
| 1294 | size_t sz; |
| 1295 | int ret; |
| 1296 | |
| 1297 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1298 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1299 | |
| 1300 | /* |
| 1301 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1302 | * retry single alloc to be on the safe side. |
| 1303 | */ |
| 1304 | if (unlikely(ret <= 0)) { |
| 1305 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1306 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1307 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1308 | ret = 1; |
| 1309 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1310 | state->free_reqs = ret - 1; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1311 | req = state->reqs[ret - 1]; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1312 | } else { |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1313 | state->free_reqs--; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 1314 | req = state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1317 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1318 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1319 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1320 | req->ctx = ctx; |
| 1321 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1322 | /* one is dropped after submission, the other at completion */ |
| 1323 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1324 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1325 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1326 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1327 | fallback: |
| 1328 | req = io_get_fallback_req(ctx); |
| 1329 | if (req) |
| 1330 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1331 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1332 | return NULL; |
| 1333 | } |
| 1334 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1335 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1336 | bool fixed) |
| 1337 | { |
| 1338 | if (fixed) |
| 1339 | percpu_ref_put(&req->ctx->file_data->refs); |
| 1340 | else |
| 1341 | fput(file); |
| 1342 | } |
| 1343 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1344 | static void __io_req_do_free(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1345 | { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1346 | if (likely(!io_is_fallback_req(req))) |
| 1347 | kmem_cache_free(req_cachep, req); |
| 1348 | else |
| 1349 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1350 | } |
| 1351 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1352 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1353 | { |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1354 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 1355 | io_cleanup_req(req); |
| 1356 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1357 | kfree(req->io); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1358 | if (req->file) |
| 1359 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1360 | |
| 1361 | io_req_work_drop_env(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | static void __io_free_req(struct io_kiocb *req) |
| 1365 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1366 | __io_req_aux_free(req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1367 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1368 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1369 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1370 | unsigned long flags; |
| 1371 | |
| 1372 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1373 | list_del(&req->inflight_entry); |
| 1374 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1375 | wake_up(&ctx->inflight_wait); |
| 1376 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1377 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1378 | |
| 1379 | percpu_ref_put(&req->ctx->refs); |
| 1380 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1381 | } |
| 1382 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1383 | struct req_batch { |
| 1384 | void *reqs[IO_IOPOLL_BATCH]; |
| 1385 | int to_free; |
| 1386 | int need_iter; |
| 1387 | }; |
| 1388 | |
| 1389 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1390 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1391 | int fixed_refs = rb->to_free; |
| 1392 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1393 | if (!rb->to_free) |
| 1394 | return; |
| 1395 | if (rb->need_iter) { |
| 1396 | int i, inflight = 0; |
| 1397 | unsigned long flags; |
| 1398 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1399 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1400 | for (i = 0; i < rb->to_free; i++) { |
| 1401 | struct io_kiocb *req = rb->reqs[i]; |
| 1402 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1403 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1404 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1405 | fixed_refs++; |
| 1406 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1407 | if (req->flags & REQ_F_INFLIGHT) |
| 1408 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1409 | __io_req_aux_free(req); |
| 1410 | } |
| 1411 | if (!inflight) |
| 1412 | goto do_free; |
| 1413 | |
| 1414 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1415 | for (i = 0; i < rb->to_free; i++) { |
| 1416 | struct io_kiocb *req = rb->reqs[i]; |
| 1417 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1418 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1419 | list_del(&req->inflight_entry); |
| 1420 | if (!--inflight) |
| 1421 | break; |
| 1422 | } |
| 1423 | } |
| 1424 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1425 | |
| 1426 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1427 | wake_up(&ctx->inflight_wait); |
| 1428 | } |
| 1429 | do_free: |
| 1430 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1431 | if (fixed_refs) |
| 1432 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1433 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1434 | rb->to_free = rb->need_iter = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1435 | } |
| 1436 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1437 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1438 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1439 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1440 | int ret; |
| 1441 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1442 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1443 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1444 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1445 | io_commit_cqring(ctx); |
| 1446 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1447 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1448 | return true; |
| 1449 | } |
| 1450 | |
| 1451 | return false; |
| 1452 | } |
| 1453 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1454 | 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] | 1455 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1456 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1457 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1458 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1459 | /* Already got next link */ |
| 1460 | if (req->flags & REQ_F_LINK_NEXT) |
| 1461 | return; |
| 1462 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1463 | /* |
| 1464 | * The list should never be empty when we are called here. But could |
| 1465 | * potentially happen if the chain is messed up, check to be on the |
| 1466 | * safe side. |
| 1467 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1468 | while (!list_empty(&req->link_list)) { |
| 1469 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1470 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1471 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1472 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1473 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1474 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1475 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1476 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1477 | continue; |
| 1478 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1479 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1480 | list_del_init(&req->link_list); |
| 1481 | if (!list_empty(&nxt->link_list)) |
| 1482 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1483 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1484 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1485 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1486 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1487 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1488 | if (wake_ev) |
| 1489 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1494 | */ |
| 1495 | static void io_fail_links(struct io_kiocb *req) |
| 1496 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1497 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1498 | unsigned long flags; |
| 1499 | |
| 1500 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1501 | |
| 1502 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1503 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1504 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1505 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1506 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1507 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1508 | |
| 1509 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1510 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1511 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1512 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1513 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1514 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1515 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1516 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1517 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1518 | |
| 1519 | io_commit_cqring(ctx); |
| 1520 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1521 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1522 | } |
| 1523 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1524 | 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] | 1525 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1526 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1527 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1528 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1529 | /* |
| 1530 | * If LINK is set, we have dependent requests in this chain. If we |
| 1531 | * didn't fail this request, queue the first one up, moving any other |
| 1532 | * dependencies to the next request. In case of failure, fail the rest |
| 1533 | * of the chain. |
| 1534 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1535 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1536 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1537 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1538 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1539 | struct io_ring_ctx *ctx = req->ctx; |
| 1540 | unsigned long flags; |
| 1541 | |
| 1542 | /* |
| 1543 | * If this is a timeout link, we could be racing with the |
| 1544 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1545 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1546 | */ |
| 1547 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1548 | io_req_link_next(req, nxt); |
| 1549 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1550 | } else { |
| 1551 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1552 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1553 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1554 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1555 | static void io_free_req(struct io_kiocb *req) |
| 1556 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1557 | struct io_kiocb *nxt = NULL; |
| 1558 | |
| 1559 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1560 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1561 | |
| 1562 | if (nxt) |
| 1563 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1564 | } |
| 1565 | |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1566 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 1567 | { |
| 1568 | struct io_wq_work *work = *workptr; |
| 1569 | struct io_kiocb *link = work->data; |
| 1570 | |
| 1571 | io_queue_linked_timeout(link); |
| 1572 | io_wq_submit_work(workptr); |
| 1573 | } |
| 1574 | |
| 1575 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 1576 | { |
| 1577 | struct io_kiocb *link; |
| 1578 | |
| 1579 | *workptr = &nxt->work; |
| 1580 | link = io_prep_linked_timeout(nxt); |
| 1581 | if (link) { |
| 1582 | nxt->work.func = io_link_work_cb; |
| 1583 | nxt->work.data = link; |
| 1584 | } |
| 1585 | } |
| 1586 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1587 | /* |
| 1588 | * Drop reference to request, return next in chain (if there is one) if this |
| 1589 | * was the last reference to this request. |
| 1590 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1591 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1592 | 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] | 1593 | { |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1594 | if (refcount_dec_and_test(&req->refs)) { |
| 1595 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1596 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 1597 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1600 | static void io_put_req(struct io_kiocb *req) |
| 1601 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1602 | if (refcount_dec_and_test(&req->refs)) |
| 1603 | io_free_req(req); |
| 1604 | } |
| 1605 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1606 | static void io_steal_work(struct io_kiocb *req, |
| 1607 | struct io_wq_work **workptr) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1608 | { |
| 1609 | /* |
| 1610 | * It's in an io-wq worker, so there always should be at least |
| 1611 | * one reference, which will be dropped in io_put_work() just |
| 1612 | * after the current handler returns. |
| 1613 | * |
| 1614 | * It also means, that if the counter dropped to 1, then there is |
| 1615 | * no asynchronous users left, so it's safe to steal the next work. |
| 1616 | */ |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 1617 | if (refcount_read(&req->refs) == 1) { |
| 1618 | struct io_kiocb *nxt = NULL; |
| 1619 | |
| 1620 | io_req_find_next(req, &nxt); |
| 1621 | if (nxt) |
| 1622 | io_wq_assign_next(workptr, nxt); |
| 1623 | } |
| 1624 | } |
| 1625 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1626 | /* |
| 1627 | * Must only be used if we don't need to care about links, usually from |
| 1628 | * within the completion handling itself. |
| 1629 | */ |
| 1630 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1631 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1632 | /* drop both submit and complete references */ |
| 1633 | if (refcount_sub_and_test(2, &req->refs)) |
| 1634 | __io_free_req(req); |
| 1635 | } |
| 1636 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1637 | static void io_double_put_req(struct io_kiocb *req) |
| 1638 | { |
| 1639 | /* drop both submit and complete references */ |
| 1640 | if (refcount_sub_and_test(2, &req->refs)) |
| 1641 | io_free_req(req); |
| 1642 | } |
| 1643 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1644 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1645 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1646 | struct io_rings *rings = ctx->rings; |
| 1647 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1648 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1649 | /* |
| 1650 | * noflush == true is from the waitqueue handler, just ensure |
| 1651 | * we wake up the task, and the next invocation will flush the |
| 1652 | * entries. We cannot safely to it from here. |
| 1653 | */ |
| 1654 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1655 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1656 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1657 | io_cqring_overflow_flush(ctx, false); |
| 1658 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1659 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1660 | /* See comment at the top of this file */ |
| 1661 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1662 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1663 | } |
| 1664 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1665 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1666 | { |
| 1667 | struct io_rings *rings = ctx->rings; |
| 1668 | |
| 1669 | /* make sure SQ entry isn't read before tail */ |
| 1670 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1671 | } |
| 1672 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1673 | 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] | 1674 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1675 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1676 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1677 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1678 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1679 | rb->need_iter++; |
| 1680 | |
| 1681 | rb->reqs[rb->to_free++] = req; |
| 1682 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1683 | io_free_req_many(req->ctx, rb); |
| 1684 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1687 | static int io_put_kbuf(struct io_kiocb *req) |
| 1688 | { |
| 1689 | struct io_buffer *kbuf = (struct io_buffer *) req->rw.addr; |
| 1690 | int cflags; |
| 1691 | |
| 1692 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1693 | cflags |= IORING_CQE_F_BUFFER; |
| 1694 | req->rw.addr = 0; |
| 1695 | kfree(kbuf); |
| 1696 | return cflags; |
| 1697 | } |
| 1698 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1699 | /* |
| 1700 | * Find and free completed poll iocbs |
| 1701 | */ |
| 1702 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1703 | struct list_head *done) |
| 1704 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1705 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1706 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1707 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1708 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1709 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1710 | int cflags = 0; |
| 1711 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1712 | req = list_first_entry(done, struct io_kiocb, list); |
| 1713 | list_del(&req->list); |
| 1714 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1715 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1716 | cflags = io_put_kbuf(req); |
| 1717 | |
| 1718 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1719 | (*nr_events)++; |
| 1720 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1721 | if (refcount_dec_and_test(&req->refs) && |
| 1722 | !io_req_multi_free(&rb, req)) |
| 1723 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1724 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1725 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1726 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1727 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1731 | long min) |
| 1732 | { |
| 1733 | struct io_kiocb *req, *tmp; |
| 1734 | LIST_HEAD(done); |
| 1735 | bool spin; |
| 1736 | int ret; |
| 1737 | |
| 1738 | /* |
| 1739 | * Only spin for completions if we don't have multiple devices hanging |
| 1740 | * off our complete list, and we're under the requested amount. |
| 1741 | */ |
| 1742 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1743 | |
| 1744 | ret = 0; |
| 1745 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1746 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1747 | |
| 1748 | /* |
| 1749 | * Move completed entries to our local list. If we find a |
| 1750 | * request that requires polling, break out and complete |
| 1751 | * the done list first, if we have entries there. |
| 1752 | */ |
| 1753 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1754 | list_move_tail(&req->list, &done); |
| 1755 | continue; |
| 1756 | } |
| 1757 | if (!list_empty(&done)) |
| 1758 | break; |
| 1759 | |
| 1760 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1761 | if (ret < 0) |
| 1762 | break; |
| 1763 | |
| 1764 | if (ret && spin) |
| 1765 | spin = false; |
| 1766 | ret = 0; |
| 1767 | } |
| 1768 | |
| 1769 | if (!list_empty(&done)) |
| 1770 | io_iopoll_complete(ctx, nr_events, &done); |
| 1771 | |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
| 1775 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1776 | * 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] | 1777 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1778 | * as a non-spinning completion check. |
| 1779 | */ |
| 1780 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1781 | long min) |
| 1782 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1783 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1784 | int ret; |
| 1785 | |
| 1786 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1787 | if (ret < 0) |
| 1788 | return ret; |
| 1789 | if (!min || *nr_events >= min) |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | return 1; |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * We can't just wait for polled events to come to us, we have to actively |
| 1798 | * find and complete them. |
| 1799 | */ |
| 1800 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1801 | { |
| 1802 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1803 | return; |
| 1804 | |
| 1805 | mutex_lock(&ctx->uring_lock); |
| 1806 | while (!list_empty(&ctx->poll_list)) { |
| 1807 | unsigned int nr_events = 0; |
| 1808 | |
| 1809 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1810 | |
| 1811 | /* |
| 1812 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1813 | * in this case we need to ensure that we reap all events. |
| 1814 | */ |
| 1815 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1816 | } |
| 1817 | mutex_unlock(&ctx->uring_lock); |
| 1818 | } |
| 1819 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1820 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1821 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1822 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1823 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1824 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 1825 | /* |
| 1826 | * We disallow the app entering submit/complete with polling, but we |
| 1827 | * still need to lock the ring to prevent racing with polled issue |
| 1828 | * that got punted to a workqueue. |
| 1829 | */ |
| 1830 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1831 | do { |
| 1832 | int tmin = 0; |
| 1833 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1834 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1835 | * Don't enter poll loop if we already have events pending. |
| 1836 | * If we do, we can potentially be spinning for commands that |
| 1837 | * already triggered a CQE (eg in error). |
| 1838 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1839 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1840 | break; |
| 1841 | |
| 1842 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1843 | * If a submit got punted to a workqueue, we can have the |
| 1844 | * application entering polling for a command before it gets |
| 1845 | * issued. That app will hold the uring_lock for the duration |
| 1846 | * of the poll right here, so we need to take a breather every |
| 1847 | * now and then to ensure that the issue has a chance to add |
| 1848 | * the poll to the issued list. Otherwise we can spin here |
| 1849 | * forever, while the workqueue is stuck trying to acquire the |
| 1850 | * very same mutex. |
| 1851 | */ |
| 1852 | if (!(++iters & 7)) { |
| 1853 | mutex_unlock(&ctx->uring_lock); |
| 1854 | mutex_lock(&ctx->uring_lock); |
| 1855 | } |
| 1856 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1857 | if (*nr_events < min) |
| 1858 | tmin = min - *nr_events; |
| 1859 | |
| 1860 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1861 | if (ret <= 0) |
| 1862 | break; |
| 1863 | ret = 0; |
| 1864 | } while (min && !*nr_events && !need_resched()); |
| 1865 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1866 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1867 | return ret; |
| 1868 | } |
| 1869 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1870 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1871 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1872 | /* |
| 1873 | * Tell lockdep we inherited freeze protection from submission |
| 1874 | * thread. |
| 1875 | */ |
| 1876 | if (req->flags & REQ_F_ISREG) { |
| 1877 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1878 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1879 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1880 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1881 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1884 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1885 | { |
| 1886 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1887 | req->flags |= REQ_F_FAIL_LINK; |
| 1888 | } |
| 1889 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1890 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1891 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1892 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1893 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1894 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1895 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1896 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1897 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1898 | if (res != req->result) |
| 1899 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 1900 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 1901 | cflags = io_put_kbuf(req); |
| 1902 | __io_cqring_add_event(req, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1906 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1907 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1908 | |
| 1909 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1910 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1913 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1914 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1915 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1916 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1917 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1918 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1919 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1920 | if (res != req->result) |
| 1921 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1922 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1923 | if (res != -EAGAIN) |
| 1924 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1929 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1930 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1931 | * accessing the kiocb cookie. |
| 1932 | */ |
| 1933 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1934 | { |
| 1935 | struct io_ring_ctx *ctx = req->ctx; |
| 1936 | |
| 1937 | /* |
| 1938 | * Track whether we have multiple files in our lists. This will impact |
| 1939 | * how we do polling eventually, not spinning if we're on potentially |
| 1940 | * different devices. |
| 1941 | */ |
| 1942 | if (list_empty(&ctx->poll_list)) { |
| 1943 | ctx->poll_multi_file = false; |
| 1944 | } else if (!ctx->poll_multi_file) { |
| 1945 | struct io_kiocb *list_req; |
| 1946 | |
| 1947 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1948 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1949 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1950 | ctx->poll_multi_file = true; |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | * For fast devices, IO may have already completed. If it has, add |
| 1955 | * it to the front so we find it first. |
| 1956 | */ |
| 1957 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1958 | list_add(&req->list, &ctx->poll_list); |
| 1959 | else |
| 1960 | list_add_tail(&req->list, &ctx->poll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 1961 | |
| 1962 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 1963 | wq_has_sleeper(&ctx->sqo_wait)) |
| 1964 | wake_up(&ctx->sqo_wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1967 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1968 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1969 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1970 | int diff = state->has_refs - state->used_refs; |
| 1971 | |
| 1972 | if (diff) |
| 1973 | fput_many(state->file, diff); |
| 1974 | state->file = NULL; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | /* |
| 1979 | * Get as many references to a file as we have IOs left in this submission, |
| 1980 | * assuming most submissions are for one file, or at least that each file |
| 1981 | * has more than one submission. |
| 1982 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1983 | 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] | 1984 | { |
| 1985 | if (!state) |
| 1986 | return fget(fd); |
| 1987 | |
| 1988 | if (state->file) { |
| 1989 | if (state->fd == fd) { |
| 1990 | state->used_refs++; |
| 1991 | state->ios_left--; |
| 1992 | return state->file; |
| 1993 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1994 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1995 | } |
| 1996 | state->file = fget_many(fd, state->ios_left); |
| 1997 | if (!state->file) |
| 1998 | return NULL; |
| 1999 | |
| 2000 | state->fd = fd; |
| 2001 | state->has_refs = state->ios_left; |
| 2002 | state->used_refs = 1; |
| 2003 | state->ios_left--; |
| 2004 | return state->file; |
| 2005 | } |
| 2006 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2007 | /* |
| 2008 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2009 | * any file. For now, just ensure that anything potentially problematic is done |
| 2010 | * inline. |
| 2011 | */ |
| 2012 | static bool io_file_supports_async(struct file *file) |
| 2013 | { |
| 2014 | umode_t mode = file_inode(file)->i_mode; |
| 2015 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2016 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2017 | return true; |
| 2018 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 2019 | return true; |
| 2020 | |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2024 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2025 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2026 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2027 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2028 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2029 | unsigned ioprio; |
| 2030 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2031 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2032 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2033 | req->flags |= REQ_F_ISREG; |
| 2034 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2035 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2036 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2037 | req->flags |= REQ_F_CUR_POS; |
| 2038 | kiocb->ki_pos = req->file->f_pos; |
| 2039 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2040 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2041 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2042 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2043 | if (unlikely(ret)) |
| 2044 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2045 | |
| 2046 | ioprio = READ_ONCE(sqe->ioprio); |
| 2047 | if (ioprio) { |
| 2048 | ret = ioprio_check_cap(ioprio); |
| 2049 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2050 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2051 | |
| 2052 | kiocb->ki_ioprio = ioprio; |
| 2053 | } else |
| 2054 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2055 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2056 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2057 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 2058 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2059 | req->flags |= REQ_F_NOWAIT; |
| 2060 | |
| 2061 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2062 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2063 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2064 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2065 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2066 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2067 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2068 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2069 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2070 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 2071 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2072 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2073 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2074 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2075 | kiocb->ki_complete = io_complete_rw; |
| 2076 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2077 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2078 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2079 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2080 | /* we own ->private, reuse it for the buffer index / buffer ID */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2081 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2082 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2083 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2087 | { |
| 2088 | switch (ret) { |
| 2089 | case -EIOCBQUEUED: |
| 2090 | break; |
| 2091 | case -ERESTARTSYS: |
| 2092 | case -ERESTARTNOINTR: |
| 2093 | case -ERESTARTNOHAND: |
| 2094 | case -ERESTART_RESTARTBLOCK: |
| 2095 | /* |
| 2096 | * We can't just restart the syscall, since previously |
| 2097 | * submitted sqes may already be in progress. Just fail this |
| 2098 | * IO with EINTR. |
| 2099 | */ |
| 2100 | ret = -EINTR; |
| 2101 | /* fall through */ |
| 2102 | default: |
| 2103 | kiocb->ki_complete(kiocb, ret, 0); |
| 2104 | } |
| 2105 | } |
| 2106 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2107 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2108 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2109 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 2110 | |
| 2111 | if (req->flags & REQ_F_CUR_POS) |
| 2112 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2113 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2114 | io_complete_rw(kiocb, ret, 0); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2115 | else |
| 2116 | io_rw_done(kiocb, ret); |
| 2117 | } |
| 2118 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2119 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2120 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2121 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2122 | struct io_ring_ctx *ctx = req->ctx; |
| 2123 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2124 | struct io_mapped_ubuf *imu; |
| 2125 | unsigned index, buf_index; |
| 2126 | size_t offset; |
| 2127 | u64 buf_addr; |
| 2128 | |
| 2129 | /* attempt to use fixed buffers without having provided iovecs */ |
| 2130 | if (unlikely(!ctx->user_bufs)) |
| 2131 | return -EFAULT; |
| 2132 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2133 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2134 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2135 | return -EFAULT; |
| 2136 | |
| 2137 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2138 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2139 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2140 | |
| 2141 | /* overflow */ |
| 2142 | if (buf_addr + len < buf_addr) |
| 2143 | return -EFAULT; |
| 2144 | /* not inside the mapped region */ |
| 2145 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2146 | return -EFAULT; |
| 2147 | |
| 2148 | /* |
| 2149 | * May not be a start of buffer, set size appropriately |
| 2150 | * and advance us to the beginning. |
| 2151 | */ |
| 2152 | offset = buf_addr - imu->ubuf; |
| 2153 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2154 | |
| 2155 | if (offset) { |
| 2156 | /* |
| 2157 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2158 | * using the latter parts of a big fixed buffer - it iterates |
| 2159 | * over each segment manually. We can cheat a bit here, because |
| 2160 | * we know that: |
| 2161 | * |
| 2162 | * 1) it's a BVEC iter, we set it up |
| 2163 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2164 | * first and last bvec |
| 2165 | * |
| 2166 | * So just find our index, and adjust the iterator afterwards. |
| 2167 | * If the offset is within the first bvec (or the whole first |
| 2168 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2169 | * since we can just skip the first segment, which may not |
| 2170 | * be PAGE_SIZE aligned. |
| 2171 | */ |
| 2172 | const struct bio_vec *bvec = imu->bvec; |
| 2173 | |
| 2174 | if (offset <= bvec->bv_len) { |
| 2175 | iov_iter_advance(iter, offset); |
| 2176 | } else { |
| 2177 | unsigned long seg_skip; |
| 2178 | |
| 2179 | /* skip first vec */ |
| 2180 | offset -= bvec->bv_len; |
| 2181 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2182 | |
| 2183 | iter->bvec = bvec + seg_skip; |
| 2184 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2185 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2186 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 2190 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2193 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2194 | { |
| 2195 | if (needs_lock) |
| 2196 | mutex_unlock(&ctx->uring_lock); |
| 2197 | } |
| 2198 | |
| 2199 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2200 | { |
| 2201 | /* |
| 2202 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2203 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2204 | * The only exception is when we've detached the request and issue it |
| 2205 | * from an async worker thread, grab the lock for that case. |
| 2206 | */ |
| 2207 | if (needs_lock) |
| 2208 | mutex_lock(&ctx->uring_lock); |
| 2209 | } |
| 2210 | |
| 2211 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2212 | int bgid, struct io_buffer *kbuf, |
| 2213 | bool needs_lock) |
| 2214 | { |
| 2215 | struct io_buffer *head; |
| 2216 | |
| 2217 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2218 | return kbuf; |
| 2219 | |
| 2220 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2221 | |
| 2222 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2223 | |
| 2224 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2225 | if (head) { |
| 2226 | if (!list_empty(&head->list)) { |
| 2227 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2228 | list); |
| 2229 | list_del(&kbuf->list); |
| 2230 | } else { |
| 2231 | kbuf = head; |
| 2232 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2233 | } |
| 2234 | if (*len > kbuf->len) |
| 2235 | *len = kbuf->len; |
| 2236 | } else { |
| 2237 | kbuf = ERR_PTR(-ENOBUFS); |
| 2238 | } |
| 2239 | |
| 2240 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2241 | |
| 2242 | return kbuf; |
| 2243 | } |
| 2244 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2245 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2246 | struct iovec **iovec, struct iov_iter *iter, |
| 2247 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2248 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2249 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2250 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2251 | u8 opcode; |
| 2252 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2253 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2254 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2255 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2256 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2257 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2258 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2259 | /* buffer index only valid with fixed read/write, or buffer select */ |
| 2260 | if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2261 | return -EINVAL; |
| 2262 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2263 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2264 | ssize_t ret; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2265 | |
| 2266 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2267 | struct io_buffer *kbuf = (struct io_buffer *) req->rw.addr; |
| 2268 | int bgid; |
| 2269 | |
| 2270 | bgid = (int) (unsigned long) req->rw.kiocb.private; |
| 2271 | kbuf = io_buffer_select(req, &sqe_len, bgid, kbuf, |
| 2272 | needs_lock); |
| 2273 | if (IS_ERR(kbuf)) { |
| 2274 | *iovec = NULL; |
| 2275 | return PTR_ERR(kbuf); |
| 2276 | } |
| 2277 | req->rw.addr = (u64) kbuf; |
| 2278 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2279 | buf = u64_to_user_ptr(kbuf->addr); |
| 2280 | } |
| 2281 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2282 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2283 | *iovec = NULL; |
Jens Axboe | 3a90159 | 2020-02-25 17:48:55 -0700 | [diff] [blame] | 2284 | return ret < 0 ? ret : sqe_len; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2287 | if (req->io) { |
| 2288 | struct io_async_rw *iorw = &req->io->rw; |
| 2289 | |
| 2290 | *iovec = iorw->iov; |
| 2291 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2292 | if (iorw->iov == iorw->fast_iov) |
| 2293 | *iovec = NULL; |
| 2294 | return iorw->size; |
| 2295 | } |
| 2296 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2297 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2298 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2299 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2300 | iovec, iter); |
| 2301 | #endif |
| 2302 | |
| 2303 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2304 | } |
| 2305 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2306 | /* |
| 2307 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2308 | * by looping over ->read() or ->write() manually. |
| 2309 | */ |
| 2310 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2311 | struct iov_iter *iter) |
| 2312 | { |
| 2313 | ssize_t ret = 0; |
| 2314 | |
| 2315 | /* |
| 2316 | * Don't support polled IO through this interface, and we can't |
| 2317 | * support non-blocking either. For the latter, this just causes |
| 2318 | * the kiocb to be handled from an async context. |
| 2319 | */ |
| 2320 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2321 | return -EOPNOTSUPP; |
| 2322 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2323 | return -EAGAIN; |
| 2324 | |
| 2325 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2326 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2327 | ssize_t nr; |
| 2328 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2329 | if (!iov_iter_is_bvec(iter)) { |
| 2330 | iovec = iov_iter_iovec(iter); |
| 2331 | } else { |
| 2332 | /* fixed buffers import bvec */ |
| 2333 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2334 | + iter->iov_offset; |
| 2335 | iovec.iov_len = min(iter->count, |
| 2336 | iter->bvec->bv_len - iter->iov_offset); |
| 2337 | } |
| 2338 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2339 | if (rw == READ) { |
| 2340 | nr = file->f_op->read(file, iovec.iov_base, |
| 2341 | iovec.iov_len, &kiocb->ki_pos); |
| 2342 | } else { |
| 2343 | nr = file->f_op->write(file, iovec.iov_base, |
| 2344 | iovec.iov_len, &kiocb->ki_pos); |
| 2345 | } |
| 2346 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2347 | if (iov_iter_is_bvec(iter)) |
| 2348 | kunmap(iter->bvec->bv_page); |
| 2349 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2350 | if (nr < 0) { |
| 2351 | if (!ret) |
| 2352 | ret = nr; |
| 2353 | break; |
| 2354 | } |
| 2355 | ret += nr; |
| 2356 | if (nr != iovec.iov_len) |
| 2357 | break; |
| 2358 | iov_iter_advance(iter, nr); |
| 2359 | } |
| 2360 | |
| 2361 | return ret; |
| 2362 | } |
| 2363 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2364 | 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] | 2365 | struct iovec *iovec, struct iovec *fast_iov, |
| 2366 | struct iov_iter *iter) |
| 2367 | { |
| 2368 | req->io->rw.nr_segs = iter->nr_segs; |
| 2369 | req->io->rw.size = io_size; |
| 2370 | req->io->rw.iov = iovec; |
| 2371 | if (!req->io->rw.iov) { |
| 2372 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2373 | memcpy(req->io->rw.iov, fast_iov, |
| 2374 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2375 | } else { |
| 2376 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2377 | } |
| 2378 | } |
| 2379 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2380 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2381 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2382 | if (!io_op_defs[req->opcode].async_ctx) |
| 2383 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2384 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2385 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2388 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2389 | struct iovec *iovec, struct iovec *fast_iov, |
| 2390 | struct iov_iter *iter) |
| 2391 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2392 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2393 | return 0; |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2394 | if (!req->io) { |
| 2395 | if (io_alloc_async_ctx(req)) |
| 2396 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2397 | |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 2398 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2399 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2400 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2401 | } |
| 2402 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2403 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2404 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2405 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2406 | struct io_async_ctx *io; |
| 2407 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2408 | ssize_t ret; |
| 2409 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2410 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2411 | if (ret) |
| 2412 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2413 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2414 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2415 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2416 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2417 | /* either don't need iovec imported or already have it */ |
| 2418 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2419 | return 0; |
| 2420 | |
| 2421 | io = req->io; |
| 2422 | io->rw.iov = io->rw.fast_iov; |
| 2423 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2424 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2425 | req->io = io; |
| 2426 | if (ret < 0) |
| 2427 | return ret; |
| 2428 | |
| 2429 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2430 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2433 | static int io_read(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2434 | { |
| 2435 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2436 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2437 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2438 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2439 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2440 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2441 | ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2442 | if (ret < 0) |
| 2443 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2444 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2445 | /* Ensure we clear previously set non-block flag */ |
| 2446 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2447 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2448 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2449 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2450 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2451 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2452 | req->result = io_size; |
| 2453 | |
| 2454 | /* |
| 2455 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2456 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2457 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2458 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2459 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2460 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2461 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2462 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2463 | if (!ret) { |
| 2464 | ssize_t ret2; |
| 2465 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2466 | if (req->file->f_op->read_iter) |
| 2467 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2468 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2469 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2470 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2471 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2472 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2473 | kiocb_done(kiocb, ret2); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2474 | } else { |
| 2475 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2476 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2477 | inline_vecs, &iter); |
| 2478 | if (ret) |
| 2479 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2480 | /* any defer here is final, must blocking retry */ |
| 2481 | if (!(req->flags & REQ_F_NOWAIT)) |
| 2482 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2483 | return -EAGAIN; |
| 2484 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2485 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2486 | out_free: |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2487 | kfree(iovec); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2488 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2489 | return ret; |
| 2490 | } |
| 2491 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2492 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2493 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2494 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2495 | struct io_async_ctx *io; |
| 2496 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2497 | ssize_t ret; |
| 2498 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2499 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2500 | if (ret) |
| 2501 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2502 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2503 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2504 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2505 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 2506 | /* either don't need iovec imported or already have it */ |
| 2507 | if (!req->io || req->flags & REQ_F_NEED_CLEANUP) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2508 | return 0; |
| 2509 | |
| 2510 | io = req->io; |
| 2511 | io->rw.iov = io->rw.fast_iov; |
| 2512 | req->io = NULL; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2513 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2514 | req->io = io; |
| 2515 | if (ret < 0) |
| 2516 | return ret; |
| 2517 | |
| 2518 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2519 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2522 | static int io_write(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2523 | { |
| 2524 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2525 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2526 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2527 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2528 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2529 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 2530 | ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2531 | if (ret < 0) |
| 2532 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2533 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2534 | /* Ensure we clear previously set non-block flag */ |
| 2535 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2536 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2537 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2538 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2539 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2540 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2541 | req->result = io_size; |
| 2542 | |
| 2543 | /* |
| 2544 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2545 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2546 | */ |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2547 | if (force_nonblock && !io_file_supports_async(req->file)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2548 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2549 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2550 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2551 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2552 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2553 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2554 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2555 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2556 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2557 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2558 | ssize_t ret2; |
| 2559 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2560 | /* |
| 2561 | * Open-code file_start_write here to grab freeze protection, |
| 2562 | * which will be released by another thread in |
| 2563 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2564 | * released so that it doesn't complain about the held lock when |
| 2565 | * we return to userspace. |
| 2566 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2567 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2568 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2569 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2570 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2571 | SB_FREEZE_WRITE); |
| 2572 | } |
| 2573 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2574 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2575 | if (req->file->f_op->write_iter) |
| 2576 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2577 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2578 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | faac996 | 2020-02-07 15:45:22 -0700 | [diff] [blame] | 2579 | /* |
| 2580 | * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 2581 | * retry them without IOCB_NOWAIT. |
| 2582 | */ |
| 2583 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 2584 | ret2 = -EAGAIN; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2585 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2586 | kiocb_done(kiocb, ret2); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2587 | } else { |
| 2588 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2589 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2590 | inline_vecs, &iter); |
| 2591 | if (ret) |
| 2592 | goto out_free; |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 2593 | /* any defer here is final, must blocking retry */ |
| 2594 | req->flags |= REQ_F_MUST_PUNT; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2595 | return -EAGAIN; |
| 2596 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2597 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2598 | out_free: |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 2599 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 2600 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2601 | return ret; |
| 2602 | } |
| 2603 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2604 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2605 | { |
| 2606 | struct io_splice* sp = &req->splice; |
| 2607 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
| 2608 | int ret; |
| 2609 | |
| 2610 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2611 | return 0; |
| 2612 | |
| 2613 | sp->file_in = NULL; |
| 2614 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 2615 | sp->off_out = READ_ONCE(sqe->off); |
| 2616 | sp->len = READ_ONCE(sqe->len); |
| 2617 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 2618 | |
| 2619 | if (unlikely(sp->flags & ~valid_flags)) |
| 2620 | return -EINVAL; |
| 2621 | |
| 2622 | ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in, |
| 2623 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2624 | if (ret) |
| 2625 | return ret; |
| 2626 | req->flags |= REQ_F_NEED_CLEANUP; |
| 2627 | |
| 2628 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) |
| 2629 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 2630 | |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | static bool io_splice_punt(struct file *file) |
| 2635 | { |
| 2636 | if (get_pipe_info(file)) |
| 2637 | return false; |
| 2638 | if (!io_file_supports_async(file)) |
| 2639 | return true; |
| 2640 | return !(file->f_mode & O_NONBLOCK); |
| 2641 | } |
| 2642 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2643 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2644 | { |
| 2645 | struct io_splice *sp = &req->splice; |
| 2646 | struct file *in = sp->file_in; |
| 2647 | struct file *out = sp->file_out; |
| 2648 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 2649 | loff_t *poff_in, *poff_out; |
| 2650 | long ret; |
| 2651 | |
| 2652 | if (force_nonblock) { |
| 2653 | if (io_splice_punt(in) || io_splice_punt(out)) |
| 2654 | return -EAGAIN; |
| 2655 | flags |= SPLICE_F_NONBLOCK; |
| 2656 | } |
| 2657 | |
| 2658 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 2659 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
| 2660 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
| 2661 | if (force_nonblock && ret == -EAGAIN) |
| 2662 | return -EAGAIN; |
| 2663 | |
| 2664 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 2665 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 2666 | |
| 2667 | io_cqring_add_event(req, ret); |
| 2668 | if (ret != sp->len) |
| 2669 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2670 | io_put_req(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 2671 | return 0; |
| 2672 | } |
| 2673 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2674 | /* |
| 2675 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2676 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2677 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2678 | { |
| 2679 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2680 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2681 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2682 | return -EINVAL; |
| 2683 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2684 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2685 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2686 | return 0; |
| 2687 | } |
| 2688 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2689 | 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] | 2690 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2691 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2692 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2693 | if (!req->file) |
| 2694 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2695 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2696 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2697 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2698 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2699 | return -EINVAL; |
| 2700 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2701 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2702 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2703 | return -EINVAL; |
| 2704 | |
| 2705 | req->sync.off = READ_ONCE(sqe->off); |
| 2706 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2707 | return 0; |
| 2708 | } |
| 2709 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2710 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2711 | { |
| 2712 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2713 | req_set_fail_links(req); |
| 2714 | io_cqring_add_event(req, -ECANCELED); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 2715 | io_put_req(req); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2716 | return true; |
| 2717 | } |
| 2718 | |
| 2719 | return false; |
| 2720 | } |
| 2721 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2722 | static void __io_fsync(struct io_kiocb *req) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2723 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2724 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2725 | int ret; |
| 2726 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2727 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2728 | end > 0 ? end : LLONG_MAX, |
| 2729 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2730 | if (ret < 0) |
| 2731 | req_set_fail_links(req); |
| 2732 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2733 | io_put_req(req); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2737 | { |
| 2738 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2739 | |
| 2740 | if (io_req_cancelled(req)) |
| 2741 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2742 | __io_fsync(req); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 2743 | io_steal_work(req, workptr); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2746 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2747 | { |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2748 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2749 | if (force_nonblock) { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2750 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2751 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2752 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2753 | __io_fsync(req); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2754 | return 0; |
| 2755 | } |
| 2756 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2757 | static void __io_fallocate(struct io_kiocb *req) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2758 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2759 | int ret; |
| 2760 | |
| 2761 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2762 | req->sync.len); |
| 2763 | if (ret < 0) |
| 2764 | req_set_fail_links(req); |
| 2765 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2766 | io_put_req(req); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2770 | { |
| 2771 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 2772 | |
Pavel Begunkov | 594506f | 2020-03-03 21:33:11 +0300 | [diff] [blame] | 2773 | if (io_req_cancelled(req)) |
| 2774 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2775 | __io_fallocate(req); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 2776 | io_steal_work(req, workptr); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2780 | const struct io_uring_sqe *sqe) |
| 2781 | { |
| 2782 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2783 | return -EINVAL; |
| 2784 | |
| 2785 | req->sync.off = READ_ONCE(sqe->off); |
| 2786 | req->sync.len = READ_ONCE(sqe->addr); |
| 2787 | req->sync.mode = READ_ONCE(sqe->len); |
| 2788 | return 0; |
| 2789 | } |
| 2790 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2791 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2792 | { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2793 | /* fallocate always requiring blocking context */ |
| 2794 | if (force_nonblock) { |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2795 | req->work.func = io_fallocate_finish; |
| 2796 | return -EAGAIN; |
| 2797 | } |
| 2798 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2799 | __io_fallocate(req); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2800 | return 0; |
| 2801 | } |
| 2802 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2803 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2804 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2805 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2806 | int ret; |
| 2807 | |
| 2808 | if (sqe->ioprio || sqe->buf_index) |
| 2809 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2810 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2811 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2812 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2813 | return 0; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2814 | |
| 2815 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2816 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2817 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2818 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2819 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2820 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2821 | if (IS_ERR(req->open.filename)) { |
| 2822 | ret = PTR_ERR(req->open.filename); |
| 2823 | req->open.filename = NULL; |
| 2824 | return ret; |
| 2825 | } |
| 2826 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2827 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2828 | return 0; |
| 2829 | } |
| 2830 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2831 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2832 | { |
| 2833 | struct open_how __user *how; |
| 2834 | const char __user *fname; |
| 2835 | size_t len; |
| 2836 | int ret; |
| 2837 | |
| 2838 | if (sqe->ioprio || sqe->buf_index) |
| 2839 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 2840 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2841 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 2842 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 2843 | return 0; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2844 | |
| 2845 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2846 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2847 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2848 | len = READ_ONCE(sqe->len); |
| 2849 | |
| 2850 | if (len < OPEN_HOW_SIZE_VER0) |
| 2851 | return -EINVAL; |
| 2852 | |
| 2853 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2854 | len); |
| 2855 | if (ret) |
| 2856 | return ret; |
| 2857 | |
| 2858 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2859 | req->open.how.flags |= O_LARGEFILE; |
| 2860 | |
| 2861 | req->open.filename = getname(fname); |
| 2862 | if (IS_ERR(req->open.filename)) { |
| 2863 | ret = PTR_ERR(req->open.filename); |
| 2864 | req->open.filename = NULL; |
| 2865 | return ret; |
| 2866 | } |
| 2867 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2868 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2869 | return 0; |
| 2870 | } |
| 2871 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2872 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2873 | { |
| 2874 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2875 | struct file *file; |
| 2876 | int ret; |
| 2877 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 2878 | if (force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2879 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2880 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2881 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2882 | if (ret) |
| 2883 | goto err; |
| 2884 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2885 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2886 | if (ret < 0) |
| 2887 | goto err; |
| 2888 | |
| 2889 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2890 | if (IS_ERR(file)) { |
| 2891 | put_unused_fd(ret); |
| 2892 | ret = PTR_ERR(file); |
| 2893 | } else { |
| 2894 | fsnotify_open(file); |
| 2895 | fd_install(ret, file); |
| 2896 | } |
| 2897 | err: |
| 2898 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 2899 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2900 | if (ret < 0) |
| 2901 | req_set_fail_links(req); |
| 2902 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2903 | io_put_req(req); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2904 | return 0; |
| 2905 | } |
| 2906 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 2907 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2908 | { |
| 2909 | 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] | 2910 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 2913 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 2914 | const struct io_uring_sqe *sqe) |
| 2915 | { |
| 2916 | struct io_provide_buf *p = &req->pbuf; |
| 2917 | u64 tmp; |
| 2918 | |
| 2919 | if (sqe->ioprio || sqe->rw_flags) |
| 2920 | return -EINVAL; |
| 2921 | |
| 2922 | tmp = READ_ONCE(sqe->fd); |
| 2923 | if (!tmp || tmp > USHRT_MAX) |
| 2924 | return -E2BIG; |
| 2925 | p->nbufs = tmp; |
| 2926 | p->addr = READ_ONCE(sqe->addr); |
| 2927 | p->len = READ_ONCE(sqe->len); |
| 2928 | |
| 2929 | if (!access_ok(u64_to_user_ptr(p->addr), p->len)) |
| 2930 | return -EFAULT; |
| 2931 | |
| 2932 | p->bgid = READ_ONCE(sqe->buf_group); |
| 2933 | tmp = READ_ONCE(sqe->off); |
| 2934 | if (tmp > USHRT_MAX) |
| 2935 | return -E2BIG; |
| 2936 | p->bid = tmp; |
| 2937 | return 0; |
| 2938 | } |
| 2939 | |
| 2940 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 2941 | { |
| 2942 | struct io_buffer *buf; |
| 2943 | u64 addr = pbuf->addr; |
| 2944 | int i, bid = pbuf->bid; |
| 2945 | |
| 2946 | for (i = 0; i < pbuf->nbufs; i++) { |
| 2947 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 2948 | if (!buf) |
| 2949 | break; |
| 2950 | |
| 2951 | buf->addr = addr; |
| 2952 | buf->len = pbuf->len; |
| 2953 | buf->bid = bid; |
| 2954 | addr += pbuf->len; |
| 2955 | bid++; |
| 2956 | if (!*head) { |
| 2957 | INIT_LIST_HEAD(&buf->list); |
| 2958 | *head = buf; |
| 2959 | } else { |
| 2960 | list_add_tail(&buf->list, &(*head)->list); |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | return i ? i : -ENOMEM; |
| 2965 | } |
| 2966 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 2967 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock) |
| 2968 | { |
| 2969 | struct io_provide_buf *p = &req->pbuf; |
| 2970 | struct io_ring_ctx *ctx = req->ctx; |
| 2971 | struct io_buffer *head, *list; |
| 2972 | int ret = 0; |
| 2973 | |
| 2974 | io_ring_submit_lock(ctx, !force_nonblock); |
| 2975 | |
| 2976 | lockdep_assert_held(&ctx->uring_lock); |
| 2977 | |
| 2978 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 2979 | |
| 2980 | ret = io_add_buffers(p, &head); |
| 2981 | if (ret < 0) |
| 2982 | goto out; |
| 2983 | |
| 2984 | if (!list) { |
| 2985 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 2986 | GFP_KERNEL); |
| 2987 | if (ret < 0) { |
| 2988 | while (!list_empty(&head->list)) { |
| 2989 | struct io_buffer *buf; |
| 2990 | |
| 2991 | buf = list_first_entry(&head->list, |
| 2992 | struct io_buffer, list); |
| 2993 | list_del(&buf->list); |
| 2994 | kfree(buf); |
| 2995 | } |
| 2996 | kfree(head); |
| 2997 | goto out; |
| 2998 | } |
| 2999 | } |
| 3000 | out: |
| 3001 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3002 | if (ret < 0) |
| 3003 | req_set_fail_links(req); |
| 3004 | io_cqring_add_event(req, ret); |
| 3005 | io_put_req(req); |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3009 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3010 | const struct io_uring_sqe *sqe) |
| 3011 | { |
| 3012 | #if defined(CONFIG_EPOLL) |
| 3013 | if (sqe->ioprio || sqe->buf_index) |
| 3014 | return -EINVAL; |
| 3015 | |
| 3016 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 3017 | req->epoll.op = READ_ONCE(sqe->len); |
| 3018 | req->epoll.fd = READ_ONCE(sqe->off); |
| 3019 | |
| 3020 | if (ep_op_has_event(req->epoll.op)) { |
| 3021 | struct epoll_event __user *ev; |
| 3022 | |
| 3023 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3024 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 3025 | return -EFAULT; |
| 3026 | } |
| 3027 | |
| 3028 | return 0; |
| 3029 | #else |
| 3030 | return -EOPNOTSUPP; |
| 3031 | #endif |
| 3032 | } |
| 3033 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3034 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3035 | { |
| 3036 | #if defined(CONFIG_EPOLL) |
| 3037 | struct io_epoll *ie = &req->epoll; |
| 3038 | int ret; |
| 3039 | |
| 3040 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 3041 | if (force_nonblock && ret == -EAGAIN) |
| 3042 | return -EAGAIN; |
| 3043 | |
| 3044 | if (ret < 0) |
| 3045 | req_set_fail_links(req); |
| 3046 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3047 | io_put_req(req); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3048 | return 0; |
| 3049 | #else |
| 3050 | return -EOPNOTSUPP; |
| 3051 | #endif |
| 3052 | } |
| 3053 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3054 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3055 | { |
| 3056 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3057 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 3058 | return -EINVAL; |
| 3059 | |
| 3060 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 3061 | req->madvise.len = READ_ONCE(sqe->len); |
| 3062 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3063 | return 0; |
| 3064 | #else |
| 3065 | return -EOPNOTSUPP; |
| 3066 | #endif |
| 3067 | } |
| 3068 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3069 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3070 | { |
| 3071 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 3072 | struct io_madvise *ma = &req->madvise; |
| 3073 | int ret; |
| 3074 | |
| 3075 | if (force_nonblock) |
| 3076 | return -EAGAIN; |
| 3077 | |
| 3078 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 3079 | if (ret < 0) |
| 3080 | req_set_fail_links(req); |
| 3081 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3082 | io_put_req(req); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3083 | return 0; |
| 3084 | #else |
| 3085 | return -EOPNOTSUPP; |
| 3086 | #endif |
| 3087 | } |
| 3088 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3089 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3090 | { |
| 3091 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 3092 | return -EINVAL; |
| 3093 | |
| 3094 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 3095 | req->fadvise.len = READ_ONCE(sqe->len); |
| 3096 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3100 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3101 | { |
| 3102 | struct io_fadvise *fa = &req->fadvise; |
| 3103 | int ret; |
| 3104 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 3105 | if (force_nonblock) { |
| 3106 | switch (fa->advice) { |
| 3107 | case POSIX_FADV_NORMAL: |
| 3108 | case POSIX_FADV_RANDOM: |
| 3109 | case POSIX_FADV_SEQUENTIAL: |
| 3110 | break; |
| 3111 | default: |
| 3112 | return -EAGAIN; |
| 3113 | } |
| 3114 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3115 | |
| 3116 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 3117 | if (ret < 0) |
| 3118 | req_set_fail_links(req); |
| 3119 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3120 | io_put_req(req); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3121 | return 0; |
| 3122 | } |
| 3123 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3124 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3125 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3126 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3127 | unsigned lookup_flags; |
| 3128 | int ret; |
| 3129 | |
| 3130 | if (sqe->ioprio || sqe->buf_index) |
| 3131 | return -EINVAL; |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3132 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 3133 | return -EBADF; |
Pavel Begunkov | 0bdbdd0 | 2020-02-08 13:28:03 +0300 | [diff] [blame] | 3134 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3135 | return 0; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3136 | |
| 3137 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3138 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3139 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3140 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3141 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3142 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3143 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3144 | return -EINVAL; |
| 3145 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3146 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3147 | if (IS_ERR(req->open.filename)) { |
| 3148 | ret = PTR_ERR(req->open.filename); |
| 3149 | req->open.filename = NULL; |
| 3150 | return ret; |
| 3151 | } |
| 3152 | |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3153 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3154 | return 0; |
| 3155 | } |
| 3156 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3157 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3158 | { |
| 3159 | struct io_open *ctx = &req->open; |
| 3160 | unsigned lookup_flags; |
| 3161 | struct path path; |
| 3162 | struct kstat stat; |
| 3163 | int ret; |
| 3164 | |
| 3165 | if (force_nonblock) |
| 3166 | return -EAGAIN; |
| 3167 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3168 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3169 | return -EINVAL; |
| 3170 | |
| 3171 | retry: |
| 3172 | /* filename_lookup() drops it, keep a reference */ |
| 3173 | ctx->filename->refcnt++; |
| 3174 | |
| 3175 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 3176 | NULL); |
| 3177 | if (ret) |
| 3178 | goto err; |
| 3179 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 3180 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3181 | path_put(&path); |
| 3182 | if (retry_estale(ret, lookup_flags)) { |
| 3183 | lookup_flags |= LOOKUP_REVAL; |
| 3184 | goto retry; |
| 3185 | } |
| 3186 | if (!ret) |
| 3187 | ret = cp_statx(&stat, ctx->buffer); |
| 3188 | err: |
| 3189 | putname(ctx->filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3190 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 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 | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3195 | return 0; |
| 3196 | } |
| 3197 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3198 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3199 | { |
| 3200 | /* |
| 3201 | * If we queue this for async, it must not be cancellable. That would |
| 3202 | * leave the 'file' in an undeterminate state. |
| 3203 | */ |
| 3204 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 3205 | |
| 3206 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 3207 | sqe->rw_flags || sqe->buf_index) |
| 3208 | return -EINVAL; |
| 3209 | if (sqe->flags & IOSQE_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3210 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3211 | |
| 3212 | req->close.fd = READ_ONCE(sqe->fd); |
| 3213 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 3214 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3215 | return -EBADF; |
| 3216 | |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3220 | /* only called when __close_fd_get_file() is done */ |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3221 | static void __io_close_finish(struct io_kiocb *req) |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3222 | { |
| 3223 | int ret; |
| 3224 | |
| 3225 | ret = filp_close(req->close.put_file, req->work.files); |
| 3226 | if (ret < 0) |
| 3227 | req_set_fail_links(req); |
| 3228 | io_cqring_add_event(req, ret); |
| 3229 | fput(req->close.put_file); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3230 | io_put_req(req); |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3231 | } |
| 3232 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3233 | static void io_close_finish(struct io_wq_work **workptr) |
| 3234 | { |
| 3235 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3236 | |
Pavel Begunkov | 7fbeb95 | 2020-02-16 01:01:18 +0300 | [diff] [blame] | 3237 | /* not cancellable, don't do io_req_cancelled() */ |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3238 | __io_close_finish(req); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 3239 | io_steal_work(req, workptr); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3242 | static int io_close(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3243 | { |
| 3244 | int ret; |
| 3245 | |
| 3246 | req->close.put_file = NULL; |
| 3247 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 3248 | if (ret < 0) |
| 3249 | return ret; |
| 3250 | |
| 3251 | /* 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] | 3252 | if (req->close.put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 594506f | 2020-03-03 21:33:11 +0300 | [diff] [blame] | 3253 | /* submission ref will be dropped, take it for async */ |
| 3254 | refcount_inc(&req->refs); |
| 3255 | |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 3256 | req->work.func = io_close_finish; |
| 3257 | /* |
| 3258 | * Do manual async queue here to avoid grabbing files - we don't |
| 3259 | * need the files, and it'll cause io_close_finish() to close |
| 3260 | * the file again and cause a double CQE entry for this request |
| 3261 | */ |
| 3262 | io_queue_async_work(req); |
| 3263 | return 0; |
| 3264 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3265 | |
| 3266 | /* |
| 3267 | * No ->flush(), safely close from here and just punt the |
| 3268 | * fput() to async context. |
| 3269 | */ |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3270 | __io_close_finish(req); |
Pavel Begunkov | a93b333 | 2020-02-08 14:04:34 +0300 | [diff] [blame] | 3271 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3274 | 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] | 3275 | { |
| 3276 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3277 | |
| 3278 | if (!req->file) |
| 3279 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3280 | |
| 3281 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3282 | return -EINVAL; |
| 3283 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 3284 | return -EINVAL; |
| 3285 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3286 | req->sync.off = READ_ONCE(sqe->off); |
| 3287 | req->sync.len = READ_ONCE(sqe->len); |
| 3288 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3289 | return 0; |
| 3290 | } |
| 3291 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3292 | static void __io_sync_file_range(struct io_kiocb *req) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3293 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3294 | int ret; |
| 3295 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3296 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3297 | req->sync.flags); |
| 3298 | if (ret < 0) |
| 3299 | req_set_fail_links(req); |
| 3300 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3301 | io_put_req(req); |
Pavel Begunkov | 5ea6216 | 2020-02-24 11:30:16 +0300 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | |
| 3305 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 3306 | { |
| 3307 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3308 | struct io_kiocb *nxt = NULL; |
| 3309 | |
| 3310 | if (io_req_cancelled(req)) |
| 3311 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3312 | __io_sync_file_range(req); |
Pavel Begunkov | 594506f | 2020-03-03 21:33:11 +0300 | [diff] [blame] | 3313 | io_put_req(req); /* put submission ref */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3314 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3315 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3316 | } |
| 3317 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3318 | 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] | 3319 | { |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3320 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3321 | if (force_nonblock) { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3322 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3323 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3324 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3325 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3326 | __io_sync_file_range(req); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3327 | return 0; |
| 3328 | } |
| 3329 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3330 | static int io_setup_async_msg(struct io_kiocb *req, |
| 3331 | struct io_async_msghdr *kmsg) |
| 3332 | { |
| 3333 | if (req->io) |
| 3334 | return -EAGAIN; |
| 3335 | if (io_alloc_async_ctx(req)) { |
| 3336 | if (kmsg->iov != kmsg->fast_iov) |
| 3337 | kfree(kmsg->iov); |
| 3338 | return -ENOMEM; |
| 3339 | } |
| 3340 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3341 | memcpy(&req->io->msg, kmsg, sizeof(*kmsg)); |
| 3342 | return -EAGAIN; |
| 3343 | } |
| 3344 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3345 | 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] | 3346 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3347 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3348 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3349 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3350 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3351 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3352 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3353 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3354 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3355 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3356 | #ifdef CONFIG_COMPAT |
| 3357 | if (req->ctx->compat) |
| 3358 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3359 | #endif |
| 3360 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3361 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3362 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3363 | /* iovec is already imported */ |
| 3364 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3365 | return 0; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3366 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3367 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3368 | ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3369 | &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3370 | if (!ret) |
| 3371 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3372 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3373 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3374 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3375 | #endif |
| 3376 | } |
| 3377 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3378 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3379 | { |
| 3380 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3381 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3382 | struct socket *sock; |
| 3383 | int ret; |
| 3384 | |
| 3385 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3386 | return -EINVAL; |
| 3387 | |
| 3388 | sock = sock_from_file(req->file, &ret); |
| 3389 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3390 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3391 | unsigned flags; |
| 3392 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3393 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3394 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3395 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3396 | /* if iov is set, it's allocated already */ |
| 3397 | if (!kmsg->iov) |
| 3398 | kmsg->iov = kmsg->fast_iov; |
| 3399 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3400 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3401 | struct io_sr_msg *sr = &req->sr_msg; |
| 3402 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3403 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3404 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3405 | |
| 3406 | io.msg.iov = io.msg.fast_iov; |
| 3407 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3408 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3409 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3410 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3413 | flags = req->sr_msg.msg_flags; |
| 3414 | if (flags & MSG_DONTWAIT) |
| 3415 | req->flags |= REQ_F_NOWAIT; |
| 3416 | else if (force_nonblock) |
| 3417 | flags |= MSG_DONTWAIT; |
| 3418 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3419 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3420 | if (force_nonblock && ret == -EAGAIN) |
| 3421 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3422 | if (ret == -ERESTARTSYS) |
| 3423 | ret = -EINTR; |
| 3424 | } |
| 3425 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3426 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3427 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3428 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3429 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3430 | if (ret < 0) |
| 3431 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3432 | io_put_req(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3433 | return 0; |
| 3434 | #else |
| 3435 | return -EOPNOTSUPP; |
| 3436 | #endif |
| 3437 | } |
| 3438 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3439 | static int io_send(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3440 | { |
| 3441 | #if defined(CONFIG_NET) |
| 3442 | struct socket *sock; |
| 3443 | int ret; |
| 3444 | |
| 3445 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3446 | return -EINVAL; |
| 3447 | |
| 3448 | sock = sock_from_file(req->file, &ret); |
| 3449 | if (sock) { |
| 3450 | struct io_sr_msg *sr = &req->sr_msg; |
| 3451 | struct msghdr msg; |
| 3452 | struct iovec iov; |
| 3453 | unsigned flags; |
| 3454 | |
| 3455 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3456 | &msg.msg_iter); |
| 3457 | if (ret) |
| 3458 | return ret; |
| 3459 | |
| 3460 | msg.msg_name = NULL; |
| 3461 | msg.msg_control = NULL; |
| 3462 | msg.msg_controllen = 0; |
| 3463 | msg.msg_namelen = 0; |
| 3464 | |
| 3465 | flags = req->sr_msg.msg_flags; |
| 3466 | if (flags & MSG_DONTWAIT) |
| 3467 | req->flags |= REQ_F_NOWAIT; |
| 3468 | else if (force_nonblock) |
| 3469 | flags |= MSG_DONTWAIT; |
| 3470 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3471 | msg.msg_flags = flags; |
| 3472 | ret = sock_sendmsg(sock, &msg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3473 | if (force_nonblock && ret == -EAGAIN) |
| 3474 | return -EAGAIN; |
| 3475 | if (ret == -ERESTARTSYS) |
| 3476 | ret = -EINTR; |
| 3477 | } |
| 3478 | |
| 3479 | io_cqring_add_event(req, ret); |
| 3480 | if (ret < 0) |
| 3481 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3482 | io_put_req(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3483 | return 0; |
| 3484 | #else |
| 3485 | return -EOPNOTSUPP; |
| 3486 | #endif |
| 3487 | } |
| 3488 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3489 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
| 3490 | int *cflags, bool needs_lock) |
| 3491 | { |
| 3492 | struct io_sr_msg *sr = &req->sr_msg; |
| 3493 | struct io_buffer *kbuf; |
| 3494 | |
| 3495 | if (!(req->flags & REQ_F_BUFFER_SELECT)) |
| 3496 | return NULL; |
| 3497 | |
| 3498 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 3499 | if (IS_ERR(kbuf)) |
| 3500 | return kbuf; |
| 3501 | |
| 3502 | sr->kbuf = kbuf; |
| 3503 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3504 | |
| 3505 | *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 3506 | *cflags |= IORING_CQE_F_BUFFER; |
| 3507 | return kbuf; |
| 3508 | } |
| 3509 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3510 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3511 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3512 | { |
| 3513 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3514 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3515 | struct io_async_ctx *io = req->io; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3516 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3517 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3518 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3519 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3520 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3521 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3522 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 3523 | #ifdef CONFIG_COMPAT |
| 3524 | if (req->ctx->compat) |
| 3525 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 3526 | #endif |
| 3527 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3528 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3529 | return 0; |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3530 | /* iovec is already imported */ |
| 3531 | if (req->flags & REQ_F_NEED_CLEANUP) |
| 3532 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3533 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3534 | io->msg.iov = io->msg.fast_iov; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3535 | ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3536 | &io->msg.uaddr, &io->msg.iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3537 | if (!ret) |
| 3538 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3539 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3540 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3541 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3542 | #endif |
| 3543 | } |
| 3544 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3545 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3546 | { |
| 3547 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3548 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3549 | struct socket *sock; |
| 3550 | int ret; |
| 3551 | |
| 3552 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3553 | return -EINVAL; |
| 3554 | |
| 3555 | sock = sock_from_file(req->file, &ret); |
| 3556 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3557 | struct io_async_ctx io; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3558 | unsigned flags; |
| 3559 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3560 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3561 | kmsg = &req->io->msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3562 | kmsg->msg.msg_name = &req->io->msg.addr; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3563 | /* if iov is set, it's allocated already */ |
| 3564 | if (!kmsg->iov) |
| 3565 | kmsg->iov = kmsg->fast_iov; |
| 3566 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3567 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3568 | struct io_sr_msg *sr = &req->sr_msg; |
| 3569 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3570 | kmsg = &io.msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 3571 | kmsg->msg.msg_name = &io.msg.addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3572 | |
| 3573 | io.msg.iov = io.msg.fast_iov; |
| 3574 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3575 | sr->msg_flags, &io.msg.uaddr, |
| 3576 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3577 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3578 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3579 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3580 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3581 | flags = req->sr_msg.msg_flags; |
| 3582 | if (flags & MSG_DONTWAIT) |
| 3583 | req->flags |= REQ_F_NOWAIT; |
| 3584 | else if (force_nonblock) |
| 3585 | flags |= MSG_DONTWAIT; |
| 3586 | |
| 3587 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3588 | kmsg->uaddr, flags); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 3589 | if (force_nonblock && ret == -EAGAIN) |
| 3590 | return io_setup_async_msg(req, kmsg); |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3591 | if (ret == -ERESTARTSYS) |
| 3592 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3593 | } |
| 3594 | |
Pavel Begunkov | 1e95081 | 2020-02-06 19:51:16 +0300 | [diff] [blame] | 3595 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3596 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3597 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3598 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3599 | if (ret < 0) |
| 3600 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3601 | io_put_req(req); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3602 | return 0; |
| 3603 | #else |
| 3604 | return -EOPNOTSUPP; |
| 3605 | #endif |
| 3606 | } |
| 3607 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3608 | static int io_recv(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3609 | { |
| 3610 | #if defined(CONFIG_NET) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3611 | struct io_buffer *kbuf = NULL; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3612 | struct socket *sock; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3613 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3614 | |
| 3615 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3616 | return -EINVAL; |
| 3617 | |
| 3618 | sock = sock_from_file(req->file, &ret); |
| 3619 | if (sock) { |
| 3620 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3621 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3622 | struct msghdr msg; |
| 3623 | struct iovec iov; |
| 3624 | unsigned flags; |
| 3625 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3626 | kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock); |
| 3627 | if (IS_ERR(kbuf)) |
| 3628 | return PTR_ERR(kbuf); |
| 3629 | else if (kbuf) |
| 3630 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3631 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3632 | ret = import_single_range(READ, buf, sr->len, &iov, |
| 3633 | &msg.msg_iter); |
| 3634 | if (ret) { |
| 3635 | kfree(kbuf); |
| 3636 | return ret; |
| 3637 | } |
| 3638 | |
| 3639 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3640 | msg.msg_name = NULL; |
| 3641 | msg.msg_control = NULL; |
| 3642 | msg.msg_controllen = 0; |
| 3643 | msg.msg_namelen = 0; |
| 3644 | msg.msg_iocb = NULL; |
| 3645 | msg.msg_flags = 0; |
| 3646 | |
| 3647 | flags = req->sr_msg.msg_flags; |
| 3648 | if (flags & MSG_DONTWAIT) |
| 3649 | req->flags |= REQ_F_NOWAIT; |
| 3650 | else if (force_nonblock) |
| 3651 | flags |= MSG_DONTWAIT; |
| 3652 | |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 3653 | ret = sock_recvmsg(sock, &msg, flags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3654 | if (force_nonblock && ret == -EAGAIN) |
| 3655 | return -EAGAIN; |
| 3656 | if (ret == -ERESTARTSYS) |
| 3657 | ret = -EINTR; |
| 3658 | } |
| 3659 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 3660 | kfree(kbuf); |
| 3661 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3662 | __io_cqring_add_event(req, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3663 | if (ret < 0) |
| 3664 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3665 | io_put_req(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3666 | return 0; |
| 3667 | #else |
| 3668 | return -EOPNOTSUPP; |
| 3669 | #endif |
| 3670 | } |
| 3671 | |
| 3672 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3673 | 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] | 3674 | { |
| 3675 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3676 | struct io_accept *accept = &req->accept; |
| 3677 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3678 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3679 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3680 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3681 | return -EINVAL; |
| 3682 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3683 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3684 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3685 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3686 | return 0; |
| 3687 | #else |
| 3688 | return -EOPNOTSUPP; |
| 3689 | #endif |
| 3690 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3691 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3692 | #if defined(CONFIG_NET) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3693 | static int __io_accept(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3694 | { |
| 3695 | struct io_accept *accept = &req->accept; |
| 3696 | unsigned file_flags; |
| 3697 | int ret; |
| 3698 | |
| 3699 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3700 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3701 | accept->addr_len, accept->flags); |
| 3702 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3703 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3704 | if (ret == -ERESTARTSYS) |
| 3705 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3706 | if (ret < 0) |
| 3707 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3708 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3709 | io_put_req(req); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3710 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3714 | { |
| 3715 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3716 | |
| 3717 | if (io_req_cancelled(req)) |
| 3718 | return; |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3719 | __io_accept(req, false); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 3720 | io_steal_work(req, workptr); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3721 | } |
| 3722 | #endif |
| 3723 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3724 | static int io_accept(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3725 | { |
| 3726 | #if defined(CONFIG_NET) |
| 3727 | int ret; |
| 3728 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3729 | ret = __io_accept(req, force_nonblock); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3730 | if (ret == -EAGAIN && force_nonblock) { |
| 3731 | req->work.func = io_accept_finish; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3732 | return -EAGAIN; |
| 3733 | } |
| 3734 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3735 | #else |
| 3736 | return -EOPNOTSUPP; |
| 3737 | #endif |
| 3738 | } |
| 3739 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3740 | 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] | 3741 | { |
| 3742 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3743 | struct io_connect *conn = &req->connect; |
| 3744 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3745 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3746 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3747 | return -EINVAL; |
| 3748 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3749 | return -EINVAL; |
| 3750 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3751 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3752 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3753 | |
| 3754 | if (!io) |
| 3755 | return 0; |
| 3756 | |
| 3757 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3758 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3759 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3760 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3761 | #endif |
| 3762 | } |
| 3763 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3764 | static int io_connect(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3765 | { |
| 3766 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3767 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3768 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3769 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3770 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3771 | if (req->io) { |
| 3772 | io = req->io; |
| 3773 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3774 | ret = move_addr_to_kernel(req->connect.addr, |
| 3775 | req->connect.addr_len, |
| 3776 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3777 | if (ret) |
| 3778 | goto out; |
| 3779 | io = &__io; |
| 3780 | } |
| 3781 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3782 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3783 | |
| 3784 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3785 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3786 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3787 | if (req->io) |
| 3788 | return -EAGAIN; |
| 3789 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3790 | ret = -ENOMEM; |
| 3791 | goto out; |
| 3792 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3793 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3794 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3795 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3796 | if (ret == -ERESTARTSYS) |
| 3797 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3798 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3799 | if (ret < 0) |
| 3800 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3801 | io_cqring_add_event(req, ret); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3802 | io_put_req(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3803 | return 0; |
| 3804 | #else |
| 3805 | return -EOPNOTSUPP; |
| 3806 | #endif |
| 3807 | } |
| 3808 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3809 | struct io_poll_table { |
| 3810 | struct poll_table_struct pt; |
| 3811 | struct io_kiocb *req; |
| 3812 | int error; |
| 3813 | }; |
| 3814 | |
| 3815 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
| 3816 | struct wait_queue_head *head) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3817 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3818 | if (unlikely(poll->head)) { |
| 3819 | pt->error = -EINVAL; |
| 3820 | return; |
| 3821 | } |
| 3822 | |
| 3823 | pt->error = 0; |
| 3824 | poll->head = head; |
| 3825 | add_wait_queue(head, &poll->wait); |
| 3826 | } |
| 3827 | |
| 3828 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3829 | struct poll_table_struct *p) |
| 3830 | { |
| 3831 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3832 | |
| 3833 | __io_queue_proc(&pt->req->apoll->poll, pt, head); |
| 3834 | } |
| 3835 | |
| 3836 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 3837 | __poll_t mask, task_work_func_t func) |
| 3838 | { |
| 3839 | struct task_struct *tsk; |
| 3840 | |
| 3841 | /* for instances that support it check for an event match first: */ |
| 3842 | if (mask && !(mask & poll->events)) |
| 3843 | return 0; |
| 3844 | |
| 3845 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 3846 | |
| 3847 | list_del_init(&poll->wait.entry); |
| 3848 | |
| 3849 | tsk = req->task; |
| 3850 | req->result = mask; |
| 3851 | init_task_work(&req->task_work, func); |
| 3852 | /* |
| 3853 | * If this fails, then the task is exiting. If that is the case, then |
| 3854 | * the exit check will ultimately cancel these work items. Hence we |
| 3855 | * don't need to check here and handle it specifically. |
| 3856 | */ |
| 3857 | task_work_add(tsk, &req->task_work, true); |
| 3858 | wake_up_process(tsk); |
| 3859 | return 1; |
| 3860 | } |
| 3861 | |
| 3862 | static void io_async_task_func(struct callback_head *cb) |
| 3863 | { |
| 3864 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 3865 | struct async_poll *apoll = req->apoll; |
| 3866 | struct io_ring_ctx *ctx = req->ctx; |
| 3867 | |
| 3868 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 3869 | |
| 3870 | WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry)); |
| 3871 | |
| 3872 | if (hash_hashed(&req->hash_node)) { |
| 3873 | spin_lock_irq(&ctx->completion_lock); |
| 3874 | hash_del(&req->hash_node); |
| 3875 | spin_unlock_irq(&ctx->completion_lock); |
| 3876 | } |
| 3877 | |
| 3878 | /* restore ->work in case we need to retry again */ |
| 3879 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
| 3880 | |
| 3881 | __set_current_state(TASK_RUNNING); |
| 3882 | mutex_lock(&ctx->uring_lock); |
| 3883 | __io_queue_sqe(req, NULL); |
| 3884 | mutex_unlock(&ctx->uring_lock); |
| 3885 | |
| 3886 | kfree(apoll); |
| 3887 | } |
| 3888 | |
| 3889 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3890 | void *key) |
| 3891 | { |
| 3892 | struct io_kiocb *req = wait->private; |
| 3893 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 3894 | |
| 3895 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 3896 | key_to_poll(key)); |
| 3897 | |
| 3898 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 3899 | } |
| 3900 | |
| 3901 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3902 | { |
| 3903 | struct io_ring_ctx *ctx = req->ctx; |
| 3904 | struct hlist_head *list; |
| 3905 | |
| 3906 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3907 | hlist_add_head(&req->hash_node, list); |
| 3908 | } |
| 3909 | |
| 3910 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 3911 | struct io_poll_iocb *poll, |
| 3912 | struct io_poll_table *ipt, __poll_t mask, |
| 3913 | wait_queue_func_t wake_func) |
| 3914 | __acquires(&ctx->completion_lock) |
| 3915 | { |
| 3916 | struct io_ring_ctx *ctx = req->ctx; |
| 3917 | bool cancel = false; |
| 3918 | |
| 3919 | poll->file = req->file; |
| 3920 | poll->head = NULL; |
| 3921 | poll->done = poll->canceled = false; |
| 3922 | poll->events = mask; |
| 3923 | |
| 3924 | ipt->pt._key = mask; |
| 3925 | ipt->req = req; |
| 3926 | ipt->error = -EINVAL; |
| 3927 | |
| 3928 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3929 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 3930 | poll->wait.private = req; |
| 3931 | |
| 3932 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 3933 | |
| 3934 | spin_lock_irq(&ctx->completion_lock); |
| 3935 | if (likely(poll->head)) { |
| 3936 | spin_lock(&poll->head->lock); |
| 3937 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 3938 | if (ipt->error) |
| 3939 | cancel = true; |
| 3940 | ipt->error = 0; |
| 3941 | mask = 0; |
| 3942 | } |
| 3943 | if (mask || ipt->error) |
| 3944 | list_del_init(&poll->wait.entry); |
| 3945 | else if (cancel) |
| 3946 | WRITE_ONCE(poll->canceled, true); |
| 3947 | else if (!poll->done) /* actually waiting for an event */ |
| 3948 | io_poll_req_insert(req); |
| 3949 | spin_unlock(&poll->head->lock); |
| 3950 | } |
| 3951 | |
| 3952 | return mask; |
| 3953 | } |
| 3954 | |
| 3955 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 3956 | { |
| 3957 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 3958 | struct io_ring_ctx *ctx = req->ctx; |
| 3959 | struct async_poll *apoll; |
| 3960 | struct io_poll_table ipt; |
| 3961 | __poll_t mask, ret; |
| 3962 | |
| 3963 | if (!req->file || !file_can_poll(req->file)) |
| 3964 | return false; |
| 3965 | if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED)) |
| 3966 | return false; |
| 3967 | if (!def->pollin && !def->pollout) |
| 3968 | return false; |
| 3969 | |
| 3970 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 3971 | if (unlikely(!apoll)) |
| 3972 | return false; |
| 3973 | |
| 3974 | req->flags |= REQ_F_POLLED; |
| 3975 | memcpy(&apoll->work, &req->work, sizeof(req->work)); |
| 3976 | |
| 3977 | /* |
| 3978 | * Don't need a reference here, as we're adding it to the task |
| 3979 | * task_works list. If the task exits, the list is pruned. |
| 3980 | */ |
| 3981 | req->task = current; |
| 3982 | req->apoll = apoll; |
| 3983 | INIT_HLIST_NODE(&req->hash_node); |
| 3984 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 3985 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3986 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 3987 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 3988 | if (def->pollout) |
| 3989 | mask |= POLLOUT | POLLWRNORM; |
| 3990 | mask |= POLLERR | POLLPRI; |
| 3991 | |
| 3992 | ipt.pt._qproc = io_async_queue_proc; |
| 3993 | |
| 3994 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 3995 | io_async_wake); |
| 3996 | if (ret) { |
| 3997 | ipt.error = 0; |
| 3998 | apoll->poll.done = true; |
| 3999 | spin_unlock_irq(&ctx->completion_lock); |
| 4000 | memcpy(&req->work, &apoll->work, sizeof(req->work)); |
| 4001 | kfree(apoll); |
| 4002 | return false; |
| 4003 | } |
| 4004 | spin_unlock_irq(&ctx->completion_lock); |
| 4005 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 4006 | apoll->poll.events); |
| 4007 | return true; |
| 4008 | } |
| 4009 | |
| 4010 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 4011 | struct io_poll_iocb *poll) |
| 4012 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4013 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4014 | |
| 4015 | spin_lock(&poll->head->lock); |
| 4016 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 4017 | if (!list_empty(&poll->wait.entry)) { |
| 4018 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4019 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4020 | } |
| 4021 | spin_unlock(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4022 | return do_complete; |
| 4023 | } |
| 4024 | |
| 4025 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 4026 | { |
| 4027 | bool do_complete; |
| 4028 | |
| 4029 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 4030 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 4031 | } else { |
| 4032 | /* non-poll requests have submit ref still */ |
| 4033 | do_complete = __io_poll_remove_one(req, &req->apoll->poll); |
| 4034 | if (do_complete) |
| 4035 | io_put_req(req); |
| 4036 | } |
| 4037 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4038 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4039 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4040 | if (do_complete) { |
| 4041 | io_cqring_fill_event(req, -ECANCELED); |
| 4042 | io_commit_cqring(req->ctx); |
| 4043 | req->flags |= REQ_F_COMP_LOCKED; |
| 4044 | io_put_req(req); |
| 4045 | } |
| 4046 | |
| 4047 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 4051 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4052 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4053 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4054 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4055 | |
| 4056 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4057 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 4058 | struct hlist_head *list; |
| 4059 | |
| 4060 | list = &ctx->cancel_hash[i]; |
| 4061 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 4062 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4063 | } |
| 4064 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4065 | |
| 4066 | io_cqring_ev_posted(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4067 | } |
| 4068 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4069 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 4070 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4071 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4072 | struct io_kiocb *req; |
| 4073 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4074 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 4075 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4076 | if (sqe_addr != req->user_data) |
| 4077 | continue; |
| 4078 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4079 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4080 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | return -ENOENT; |
| 4084 | } |
| 4085 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4086 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 4087 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4088 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4089 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4090 | return -EINVAL; |
| 4091 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 4092 | sqe->poll_events) |
| 4093 | return -EINVAL; |
| 4094 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4095 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4096 | return 0; |
| 4097 | } |
| 4098 | |
| 4099 | /* |
| 4100 | * Find a running poll command that matches one specified in sqe->addr, |
| 4101 | * and remove it if found. |
| 4102 | */ |
| 4103 | static int io_poll_remove(struct io_kiocb *req) |
| 4104 | { |
| 4105 | struct io_ring_ctx *ctx = req->ctx; |
| 4106 | u64 addr; |
| 4107 | int ret; |
| 4108 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4109 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4110 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4111 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4112 | spin_unlock_irq(&ctx->completion_lock); |
| 4113 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4114 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4115 | if (ret < 0) |
| 4116 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4117 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4118 | return 0; |
| 4119 | } |
| 4120 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4121 | 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] | 4122 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4123 | struct io_ring_ctx *ctx = req->ctx; |
| 4124 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4125 | req->poll.done = true; |
Pavel Begunkov | b0a2034 | 2020-02-28 10:36:35 +0300 | [diff] [blame] | 4126 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4127 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4130 | 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] | 4131 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4132 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4133 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4134 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4135 | hash_del(&req->hash_node); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4136 | io_poll_complete(req, req->result, 0); |
| 4137 | req->flags |= REQ_F_COMP_LOCKED; |
| 4138 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4139 | spin_unlock_irq(&ctx->completion_lock); |
| 4140 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4141 | io_cqring_ev_posted(ctx); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4142 | } |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 4143 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4144 | static void io_poll_task_func(struct callback_head *cb) |
| 4145 | { |
| 4146 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4147 | struct io_kiocb *nxt = NULL; |
| 4148 | |
| 4149 | io_poll_task_handler(req, &nxt); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4150 | if (nxt) { |
| 4151 | struct io_ring_ctx *ctx = nxt->ctx; |
| 4152 | |
| 4153 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4154 | __io_queue_sqe(nxt, NULL); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4155 | mutex_unlock(&ctx->uring_lock); |
| 4156 | } |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 4157 | } |
| 4158 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4159 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 4160 | void *key) |
| 4161 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 4162 | struct io_kiocb *req = wait->private; |
| 4163 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4164 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4165 | 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] | 4166 | } |
| 4167 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4168 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4169 | struct poll_table_struct *p) |
| 4170 | { |
| 4171 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 4172 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4173 | __io_queue_proc(&pt->req->poll, pt, head); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 4174 | } |
| 4175 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4176 | 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] | 4177 | { |
| 4178 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4179 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4180 | |
| 4181 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4182 | return -EINVAL; |
| 4183 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 4184 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4185 | if (!poll->file) |
| 4186 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4187 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4188 | events = READ_ONCE(sqe->poll_events); |
| 4189 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4190 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4191 | /* |
| 4192 | * Don't need a reference here, as we're adding it to the task |
| 4193 | * task_works list. If the task exits, the list is pruned. |
| 4194 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 4195 | req->task = current; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4196 | return 0; |
| 4197 | } |
| 4198 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4199 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4200 | { |
| 4201 | struct io_poll_iocb *poll = &req->poll; |
| 4202 | struct io_ring_ctx *ctx = req->ctx; |
| 4203 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4204 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4205 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 4206 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4207 | INIT_LIST_HEAD(&req->list); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4208 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 4209 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4210 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 4211 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4212 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4213 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4214 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4215 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4216 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4217 | spin_unlock_irq(&ctx->completion_lock); |
| 4218 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4219 | if (mask) { |
| 4220 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4221 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4222 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 4223 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4224 | } |
| 4225 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4226 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 4227 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4228 | struct io_timeout_data *data = container_of(timer, |
| 4229 | struct io_timeout_data, timer); |
| 4230 | struct io_kiocb *req = data->req; |
| 4231 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4232 | unsigned long flags; |
| 4233 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4234 | atomic_inc(&ctx->cq_timeouts); |
| 4235 | |
| 4236 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4237 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4238 | * We could be racing with timeout deletion. If the list is empty, |
| 4239 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 4240 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4241 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4242 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4243 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4244 | /* |
| 4245 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4246 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4247 | * pointer will be increased, otherwise other timeout reqs may |
| 4248 | * return in advance without waiting for enough wait_nr. |
| 4249 | */ |
| 4250 | prev = req; |
| 4251 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 4252 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4253 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4254 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4255 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4256 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4257 | io_commit_cqring(ctx); |
| 4258 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4259 | |
| 4260 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4261 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4262 | io_put_req(req); |
| 4263 | return HRTIMER_NORESTART; |
| 4264 | } |
| 4265 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4266 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 4267 | { |
| 4268 | struct io_kiocb *req; |
| 4269 | int ret = -ENOENT; |
| 4270 | |
| 4271 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 4272 | if (user_data == req->user_data) { |
| 4273 | list_del_init(&req->list); |
| 4274 | ret = 0; |
| 4275 | break; |
| 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | if (ret == -ENOENT) |
| 4280 | return ret; |
| 4281 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4282 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4283 | if (ret == -1) |
| 4284 | return -EALREADY; |
| 4285 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4286 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4287 | io_cqring_fill_event(req, -ECANCELED); |
| 4288 | io_put_req(req); |
| 4289 | return 0; |
| 4290 | } |
| 4291 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4292 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 4293 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4294 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4295 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4296 | return -EINVAL; |
| 4297 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 4298 | return -EINVAL; |
| 4299 | |
| 4300 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 4301 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 4302 | if (req->timeout.flags) |
| 4303 | return -EINVAL; |
| 4304 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4305 | return 0; |
| 4306 | } |
| 4307 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4308 | /* |
| 4309 | * Remove or update an existing timeout command |
| 4310 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4311 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4312 | { |
| 4313 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4314 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4315 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4316 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4317 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4318 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4319 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4320 | io_commit_cqring(ctx); |
| 4321 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4322 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4323 | if (ret < 0) |
| 4324 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4325 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4326 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4327 | } |
| 4328 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4329 | 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] | 4330 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4331 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4332 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4333 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4334 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4335 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4336 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4337 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4338 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4339 | if (sqe->off && is_timeout_link) |
| 4340 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 4341 | flags = READ_ONCE(sqe->timeout_flags); |
| 4342 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4343 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 4344 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4345 | req->timeout.count = READ_ONCE(sqe->off); |
| 4346 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4347 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4348 | return -ENOMEM; |
| 4349 | |
| 4350 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4351 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4352 | req->flags |= REQ_F_TIMEOUT; |
| 4353 | |
| 4354 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4355 | return -EFAULT; |
| 4356 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4357 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4358 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4359 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4360 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4361 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4362 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 4363 | return 0; |
| 4364 | } |
| 4365 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4366 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4367 | { |
| 4368 | unsigned count; |
| 4369 | struct io_ring_ctx *ctx = req->ctx; |
| 4370 | struct io_timeout_data *data; |
| 4371 | struct list_head *entry; |
| 4372 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4373 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4374 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4375 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4376 | /* |
| 4377 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4378 | * timeout event to be satisfied. If it isn't set, then this is |
| 4379 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4380 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 4381 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4382 | if (!count) { |
| 4383 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 4384 | spin_lock_irq(&ctx->completion_lock); |
| 4385 | entry = ctx->timeout_list.prev; |
| 4386 | goto add; |
| 4387 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4388 | |
| 4389 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4390 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4391 | |
| 4392 | /* |
| 4393 | * Insertion sort, ensuring the first entry in the list is always |
| 4394 | * the one we need first. |
| 4395 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4396 | spin_lock_irq(&ctx->completion_lock); |
| 4397 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 4398 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4399 | unsigned nxt_sq_head; |
| 4400 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4401 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4402 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4403 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 4404 | continue; |
| 4405 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4406 | /* |
| 4407 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 4408 | * long to store it. |
| 4409 | */ |
| 4410 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 4411 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 4412 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4413 | |
| 4414 | /* |
| 4415 | * cached_sq_head may overflow, and it will never overflow twice |
| 4416 | * once there is some timeout req still be valid. |
| 4417 | */ |
| 4418 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 4419 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 4420 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4421 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4422 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4423 | |
| 4424 | /* |
| 4425 | * Sequence of reqs after the insert one and itself should |
| 4426 | * be adjusted because each timeout req consumes a slot. |
| 4427 | */ |
| 4428 | span++; |
| 4429 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4430 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 4431 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 4432 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4433 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4434 | data->timer.function = io_timeout_fn; |
| 4435 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 4436 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4440 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4441 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4442 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4443 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4444 | return req->user_data == (unsigned long) data; |
| 4445 | } |
| 4446 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4447 | 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] | 4448 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4449 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4450 | int ret = 0; |
| 4451 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4452 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 4453 | switch (cancel_ret) { |
| 4454 | case IO_WQ_CANCEL_OK: |
| 4455 | ret = 0; |
| 4456 | break; |
| 4457 | case IO_WQ_CANCEL_RUNNING: |
| 4458 | ret = -EALREADY; |
| 4459 | break; |
| 4460 | case IO_WQ_CANCEL_NOTFOUND: |
| 4461 | ret = -ENOENT; |
| 4462 | break; |
| 4463 | } |
| 4464 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4465 | return ret; |
| 4466 | } |
| 4467 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4468 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 4469 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4470 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4471 | { |
| 4472 | unsigned long flags; |
| 4473 | int ret; |
| 4474 | |
| 4475 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 4476 | if (ret != -ENOENT) { |
| 4477 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4478 | goto done; |
| 4479 | } |
| 4480 | |
| 4481 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4482 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 4483 | if (ret != -ENOENT) |
| 4484 | goto done; |
| 4485 | ret = io_poll_cancel(ctx, sqe_addr); |
| 4486 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4487 | if (!ret) |
| 4488 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4489 | io_cqring_fill_event(req, ret); |
| 4490 | io_commit_cqring(ctx); |
| 4491 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4492 | io_cqring_ev_posted(ctx); |
| 4493 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4494 | if (ret < 0) |
| 4495 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4496 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4499 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 4500 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4501 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4502 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 4503 | return -EINVAL; |
| 4504 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 4505 | sqe->cancel_flags) |
| 4506 | return -EINVAL; |
| 4507 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4508 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 4509 | return 0; |
| 4510 | } |
| 4511 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4512 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4513 | { |
| 4514 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4515 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4516 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4517 | return 0; |
| 4518 | } |
| 4519 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4520 | static int io_files_update_prep(struct io_kiocb *req, |
| 4521 | const struct io_uring_sqe *sqe) |
| 4522 | { |
| 4523 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 4524 | return -EINVAL; |
| 4525 | |
| 4526 | req->files_update.offset = READ_ONCE(sqe->off); |
| 4527 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 4528 | if (!req->files_update.nr_args) |
| 4529 | return -EINVAL; |
| 4530 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 4531 | return 0; |
| 4532 | } |
| 4533 | |
| 4534 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 4535 | { |
| 4536 | struct io_ring_ctx *ctx = req->ctx; |
| 4537 | struct io_uring_files_update up; |
| 4538 | int ret; |
| 4539 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4540 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4541 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4542 | |
| 4543 | up.offset = req->files_update.offset; |
| 4544 | up.fds = req->files_update.arg; |
| 4545 | |
| 4546 | mutex_lock(&ctx->uring_lock); |
| 4547 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 4548 | mutex_unlock(&ctx->uring_lock); |
| 4549 | |
| 4550 | if (ret < 0) |
| 4551 | req_set_fail_links(req); |
| 4552 | io_cqring_add_event(req, ret); |
| 4553 | io_put_req(req); |
| 4554 | return 0; |
| 4555 | } |
| 4556 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4557 | static int io_req_defer_prep(struct io_kiocb *req, |
| 4558 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4559 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4560 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4561 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 4562 | if (io_op_defs[req->opcode].file_table) { |
| 4563 | ret = io_grab_files(req); |
| 4564 | if (unlikely(ret)) |
| 4565 | return ret; |
| 4566 | } |
| 4567 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 4568 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 4569 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4570 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4571 | case IORING_OP_NOP: |
| 4572 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4573 | case IORING_OP_READV: |
| 4574 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4575 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4576 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4577 | break; |
| 4578 | case IORING_OP_WRITEV: |
| 4579 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4580 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4581 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4582 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4583 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4584 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4585 | break; |
| 4586 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4587 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4588 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4589 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4590 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4591 | break; |
| 4592 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4593 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4594 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4595 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4596 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4597 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4598 | break; |
| 4599 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4600 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4601 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4602 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4603 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4604 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4605 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4606 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4607 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4608 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4609 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4610 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4611 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4612 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4613 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4614 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4615 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4616 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4617 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4618 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4619 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4620 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4621 | case IORING_OP_FALLOCATE: |
| 4622 | ret = io_fallocate_prep(req, sqe); |
| 4623 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4624 | case IORING_OP_OPENAT: |
| 4625 | ret = io_openat_prep(req, sqe); |
| 4626 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4627 | case IORING_OP_CLOSE: |
| 4628 | ret = io_close_prep(req, sqe); |
| 4629 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4630 | case IORING_OP_FILES_UPDATE: |
| 4631 | ret = io_files_update_prep(req, sqe); |
| 4632 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4633 | case IORING_OP_STATX: |
| 4634 | ret = io_statx_prep(req, sqe); |
| 4635 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4636 | case IORING_OP_FADVISE: |
| 4637 | ret = io_fadvise_prep(req, sqe); |
| 4638 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4639 | case IORING_OP_MADVISE: |
| 4640 | ret = io_madvise_prep(req, sqe); |
| 4641 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4642 | case IORING_OP_OPENAT2: |
| 4643 | ret = io_openat2_prep(req, sqe); |
| 4644 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4645 | case IORING_OP_EPOLL_CTL: |
| 4646 | ret = io_epoll_ctl_prep(req, sqe); |
| 4647 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4648 | case IORING_OP_SPLICE: |
| 4649 | ret = io_splice_prep(req, sqe); |
| 4650 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4651 | case IORING_OP_PROVIDE_BUFFERS: |
| 4652 | ret = io_provide_buffers_prep(req, sqe); |
| 4653 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4654 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4655 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4656 | req->opcode); |
| 4657 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4658 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4661 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4664 | 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] | 4665 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4666 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4667 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4668 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4669 | /* Still need defer if there is pending req in defer list. */ |
| 4670 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4671 | return 0; |
| 4672 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4673 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4674 | return -EAGAIN; |
| 4675 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4676 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4677 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4678 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4679 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4680 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4681 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4682 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4683 | return 0; |
| 4684 | } |
| 4685 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4686 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4687 | list_add_tail(&req->list, &ctx->defer_list); |
| 4688 | spin_unlock_irq(&ctx->completion_lock); |
| 4689 | return -EIOCBQUEUED; |
| 4690 | } |
| 4691 | |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4692 | static void io_cleanup_req(struct io_kiocb *req) |
| 4693 | { |
| 4694 | struct io_async_ctx *io = req->io; |
| 4695 | |
| 4696 | switch (req->opcode) { |
| 4697 | case IORING_OP_READV: |
| 4698 | case IORING_OP_READ_FIXED: |
| 4699 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 4700 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4701 | kfree((void *)(unsigned long)req->rw.addr); |
| 4702 | /* fallthrough */ |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4703 | case IORING_OP_WRITEV: |
| 4704 | case IORING_OP_WRITE_FIXED: |
| 4705 | case IORING_OP_WRITE: |
| 4706 | if (io->rw.iov != io->rw.fast_iov) |
| 4707 | kfree(io->rw.iov); |
| 4708 | break; |
| 4709 | case IORING_OP_SENDMSG: |
| 4710 | case IORING_OP_RECVMSG: |
| 4711 | if (io->msg.iov != io->msg.fast_iov) |
| 4712 | kfree(io->msg.iov); |
| 4713 | break; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 4714 | case IORING_OP_RECV: |
| 4715 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4716 | kfree(req->sr_msg.kbuf); |
| 4717 | break; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4718 | case IORING_OP_OPENAT: |
| 4719 | case IORING_OP_OPENAT2: |
| 4720 | case IORING_OP_STATX: |
| 4721 | putname(req->open.filename); |
| 4722 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4723 | case IORING_OP_SPLICE: |
| 4724 | io_put_file(req, req->splice.file_in, |
| 4725 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 4726 | break; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4730 | } |
| 4731 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4732 | 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] | 4733 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4734 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4735 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4736 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4737 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4738 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4739 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4740 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4741 | break; |
| 4742 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4743 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4744 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4745 | if (sqe) { |
| 4746 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4747 | if (ret < 0) |
| 4748 | break; |
| 4749 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4750 | ret = io_read(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4751 | break; |
| 4752 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4753 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4754 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4755 | if (sqe) { |
| 4756 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4757 | if (ret < 0) |
| 4758 | break; |
| 4759 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4760 | ret = io_write(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4761 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4762 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4763 | if (sqe) { |
| 4764 | ret = io_prep_fsync(req, sqe); |
| 4765 | if (ret < 0) |
| 4766 | break; |
| 4767 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4768 | ret = io_fsync(req, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4769 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4770 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4771 | if (sqe) { |
| 4772 | ret = io_poll_add_prep(req, sqe); |
| 4773 | if (ret) |
| 4774 | break; |
| 4775 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4776 | ret = io_poll_add(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4777 | break; |
| 4778 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4779 | if (sqe) { |
| 4780 | ret = io_poll_remove_prep(req, sqe); |
| 4781 | if (ret < 0) |
| 4782 | break; |
| 4783 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4784 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4785 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4786 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4787 | if (sqe) { |
| 4788 | ret = io_prep_sfr(req, sqe); |
| 4789 | if (ret < 0) |
| 4790 | break; |
| 4791 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4792 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4793 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4794 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4795 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4796 | if (sqe) { |
| 4797 | ret = io_sendmsg_prep(req, sqe); |
| 4798 | if (ret < 0) |
| 4799 | break; |
| 4800 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4801 | if (req->opcode == IORING_OP_SENDMSG) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4802 | ret = io_sendmsg(req, force_nonblock); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4803 | else |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4804 | ret = io_send(req, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4805 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4806 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4807 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4808 | if (sqe) { |
| 4809 | ret = io_recvmsg_prep(req, sqe); |
| 4810 | if (ret) |
| 4811 | break; |
| 4812 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4813 | if (req->opcode == IORING_OP_RECVMSG) |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4814 | ret = io_recvmsg(req, force_nonblock); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4815 | else |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4816 | ret = io_recv(req, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4817 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4818 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4819 | if (sqe) { |
| 4820 | ret = io_timeout_prep(req, sqe, false); |
| 4821 | if (ret) |
| 4822 | break; |
| 4823 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4824 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4825 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4826 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4827 | if (sqe) { |
| 4828 | ret = io_timeout_remove_prep(req, sqe); |
| 4829 | if (ret) |
| 4830 | break; |
| 4831 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4832 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4833 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4834 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4835 | if (sqe) { |
| 4836 | ret = io_accept_prep(req, sqe); |
| 4837 | if (ret) |
| 4838 | break; |
| 4839 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4840 | ret = io_accept(req, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4841 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4842 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4843 | if (sqe) { |
| 4844 | ret = io_connect_prep(req, sqe); |
| 4845 | if (ret) |
| 4846 | break; |
| 4847 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4848 | ret = io_connect(req, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4849 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4850 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4851 | if (sqe) { |
| 4852 | ret = io_async_cancel_prep(req, sqe); |
| 4853 | if (ret) |
| 4854 | break; |
| 4855 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4856 | ret = io_async_cancel(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4857 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4858 | case IORING_OP_FALLOCATE: |
| 4859 | if (sqe) { |
| 4860 | ret = io_fallocate_prep(req, sqe); |
| 4861 | if (ret) |
| 4862 | break; |
| 4863 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4864 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4865 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4866 | case IORING_OP_OPENAT: |
| 4867 | if (sqe) { |
| 4868 | ret = io_openat_prep(req, sqe); |
| 4869 | if (ret) |
| 4870 | break; |
| 4871 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4872 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4873 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4874 | case IORING_OP_CLOSE: |
| 4875 | if (sqe) { |
| 4876 | ret = io_close_prep(req, sqe); |
| 4877 | if (ret) |
| 4878 | break; |
| 4879 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4880 | ret = io_close(req, force_nonblock); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4881 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4882 | case IORING_OP_FILES_UPDATE: |
| 4883 | if (sqe) { |
| 4884 | ret = io_files_update_prep(req, sqe); |
| 4885 | if (ret) |
| 4886 | break; |
| 4887 | } |
| 4888 | ret = io_files_update(req, force_nonblock); |
| 4889 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4890 | case IORING_OP_STATX: |
| 4891 | if (sqe) { |
| 4892 | ret = io_statx_prep(req, sqe); |
| 4893 | if (ret) |
| 4894 | break; |
| 4895 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4896 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4897 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4898 | case IORING_OP_FADVISE: |
| 4899 | if (sqe) { |
| 4900 | ret = io_fadvise_prep(req, sqe); |
| 4901 | if (ret) |
| 4902 | break; |
| 4903 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4904 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4905 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4906 | case IORING_OP_MADVISE: |
| 4907 | if (sqe) { |
| 4908 | ret = io_madvise_prep(req, sqe); |
| 4909 | if (ret) |
| 4910 | break; |
| 4911 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4912 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4913 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4914 | case IORING_OP_OPENAT2: |
| 4915 | if (sqe) { |
| 4916 | ret = io_openat2_prep(req, sqe); |
| 4917 | if (ret) |
| 4918 | break; |
| 4919 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4920 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4921 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4922 | case IORING_OP_EPOLL_CTL: |
| 4923 | if (sqe) { |
| 4924 | ret = io_epoll_ctl_prep(req, sqe); |
| 4925 | if (ret) |
| 4926 | break; |
| 4927 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4928 | ret = io_epoll_ctl(req, force_nonblock); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4929 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4930 | case IORING_OP_SPLICE: |
| 4931 | if (sqe) { |
| 4932 | ret = io_splice_prep(req, sqe); |
| 4933 | if (ret < 0) |
| 4934 | break; |
| 4935 | } |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4936 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4937 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4938 | case IORING_OP_PROVIDE_BUFFERS: |
| 4939 | if (sqe) { |
| 4940 | ret = io_provide_buffers_prep(req, sqe); |
| 4941 | if (ret) |
| 4942 | break; |
| 4943 | } |
| 4944 | ret = io_provide_buffers(req, force_nonblock); |
| 4945 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4946 | default: |
| 4947 | ret = -EINVAL; |
| 4948 | break; |
| 4949 | } |
| 4950 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4951 | if (ret) |
| 4952 | return ret; |
| 4953 | |
| 4954 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4955 | const bool in_async = io_wq_current_is_worker(); |
| 4956 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4957 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4958 | return -EAGAIN; |
| 4959 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4960 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4961 | if (in_async) |
| 4962 | mutex_lock(&ctx->uring_lock); |
| 4963 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4964 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4965 | |
| 4966 | if (in_async) |
| 4967 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4973 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4974 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4975 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4976 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4977 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4978 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4979 | /* if NO_CANCEL is set, we must still run the work */ |
| 4980 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4981 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4982 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4983 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4984 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4985 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4986 | do { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4987 | ret = io_issue_sqe(req, NULL, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4988 | /* |
| 4989 | * We can get EAGAIN for polled IO even though we're |
| 4990 | * forcing a sync submission from here, since we can't |
| 4991 | * wait for request slots on the block side. |
| 4992 | */ |
| 4993 | if (ret != -EAGAIN) |
| 4994 | break; |
| 4995 | cond_resched(); |
| 4996 | } while (1); |
| 4997 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4998 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4999 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5000 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5001 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 5002 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5003 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5004 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 5005 | io_steal_work(req, workptr); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5006 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5007 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5008 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 5009 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5010 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 5011 | return 0; |
Jens Axboe | 0b5faf6 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 5012 | 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] | 5013 | return 0; |
| 5014 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5015 | } |
| 5016 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5017 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 5018 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5019 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5020 | struct fixed_file_table *table; |
| 5021 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5022 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 5023 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5024 | } |
| 5025 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5026 | static int io_file_get(struct io_submit_state *state, struct io_kiocb *req, |
| 5027 | int fd, struct file **out_file, bool fixed) |
| 5028 | { |
| 5029 | struct io_ring_ctx *ctx = req->ctx; |
| 5030 | struct file *file; |
| 5031 | |
| 5032 | if (fixed) { |
| 5033 | if (unlikely(!ctx->file_data || |
| 5034 | (unsigned) fd >= ctx->nr_user_files)) |
| 5035 | return -EBADF; |
| 5036 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 5037 | file = io_file_from_index(ctx, fd); |
| 5038 | if (!file) |
| 5039 | return -EBADF; |
| 5040 | percpu_ref_get(&ctx->file_data->refs); |
| 5041 | } else { |
| 5042 | trace_io_uring_file_get(ctx, fd); |
| 5043 | file = __io_file_get(state, fd); |
| 5044 | if (unlikely(!file)) |
| 5045 | return -EBADF; |
| 5046 | } |
| 5047 | |
| 5048 | *out_file = file; |
| 5049 | return 0; |
| 5050 | } |
| 5051 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5052 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 5053 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5054 | { |
| 5055 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5056 | int fd; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5057 | bool fixed; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5058 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5059 | flags = READ_ONCE(sqe->flags); |
| 5060 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5061 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5062 | if (!io_req_needs_file(req, fd)) |
| 5063 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5064 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5065 | fixed = (flags & IOSQE_FIXED_FILE); |
| 5066 | if (unlikely(!fixed && req->needs_fixed_file)) |
| 5067 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5068 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5069 | return io_file_get(state, req, fd, &req->file, fixed); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 5070 | } |
| 5071 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5072 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5073 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5074 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5075 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5076 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5077 | if (req->work.files) |
| 5078 | return 0; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5079 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5080 | return -EBADF; |
| 5081 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5082 | rcu_read_lock(); |
| 5083 | spin_lock_irq(&ctx->inflight_lock); |
| 5084 | /* |
| 5085 | * We use the f_ops->flush() handler to ensure that we can flush |
| 5086 | * out work accessing these files if the fd is closed. Check if |
| 5087 | * the fd has changed since we started down this path, and disallow |
| 5088 | * this operation if it has. |
| 5089 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5090 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5091 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 5092 | req->flags |= REQ_F_INFLIGHT; |
| 5093 | req->work.files = current->files; |
| 5094 | ret = 0; |
| 5095 | } |
| 5096 | spin_unlock_irq(&ctx->inflight_lock); |
| 5097 | rcu_read_unlock(); |
| 5098 | |
| 5099 | return ret; |
| 5100 | } |
| 5101 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5102 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 5103 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5104 | struct io_timeout_data *data = container_of(timer, |
| 5105 | struct io_timeout_data, timer); |
| 5106 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5107 | struct io_ring_ctx *ctx = req->ctx; |
| 5108 | struct io_kiocb *prev = NULL; |
| 5109 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5110 | |
| 5111 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5112 | |
| 5113 | /* |
| 5114 | * We don't expect the list to be empty, that will only happen if we |
| 5115 | * race with the completion of the linked work. |
| 5116 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5117 | if (!list_empty(&req->link_list)) { |
| 5118 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 5119 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5120 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5121 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 5122 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 5123 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5124 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5128 | |
| 5129 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5130 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5131 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5132 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5133 | } else { |
| 5134 | io_cqring_add_event(req, -ETIME); |
| 5135 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5136 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5137 | return HRTIMER_NORESTART; |
| 5138 | } |
| 5139 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5140 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5141 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5142 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5143 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5144 | /* |
| 5145 | * If the list is now empty, then our linked request finished before |
| 5146 | * we got a chance to setup the timer |
| 5147 | */ |
| 5148 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5149 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5150 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5151 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5152 | data->timer.function = io_link_timeout_fn; |
| 5153 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 5154 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5155 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5156 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5157 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5158 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5159 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5160 | } |
| 5161 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5162 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5163 | { |
| 5164 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5165 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5166 | if (!(req->flags & REQ_F_LINK)) |
| 5167 | return NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5168 | /* for polled retry, if flag is set, we already went through here */ |
| 5169 | if (req->flags & REQ_F_POLLED) |
| 5170 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5171 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 5172 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 5173 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5174 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5175 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5176 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5177 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5178 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 5179 | } |
| 5180 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5181 | 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] | 5182 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5183 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5184 | struct io_kiocb *nxt; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5185 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5186 | int ret; |
| 5187 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5188 | again: |
| 5189 | linked_timeout = io_prep_linked_timeout(req); |
| 5190 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5191 | if (req->work.creds && req->work.creds != current_cred()) { |
| 5192 | if (old_creds) |
| 5193 | revert_creds(old_creds); |
| 5194 | if (old_creds == req->work.creds) |
| 5195 | old_creds = NULL; /* restored original creds */ |
| 5196 | else |
| 5197 | old_creds = override_creds(req->work.creds); |
| 5198 | } |
| 5199 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5200 | ret = io_issue_sqe(req, sqe, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 5201 | |
| 5202 | /* |
| 5203 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 5204 | * doesn't support non-blocking read/write attempts |
| 5205 | */ |
| 5206 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 5207 | (req->flags & REQ_F_MUST_PUNT))) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5208 | if (io_arm_poll_handler(req)) { |
| 5209 | if (linked_timeout) |
| 5210 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5211 | goto exit; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5212 | } |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5213 | punt: |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5214 | if (io_op_defs[req->opcode].file_table) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5215 | ret = io_grab_files(req); |
| 5216 | if (ret) |
| 5217 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5218 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 5219 | |
| 5220 | /* |
| 5221 | * Queued up for async execution, worker will release |
| 5222 | * submit reference when the iocb is actually submitted. |
| 5223 | */ |
| 5224 | io_queue_async_work(req); |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5225 | goto exit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5226 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5227 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5228 | err: |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5229 | nxt = NULL; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5230 | /* drop submission reference */ |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 5231 | io_put_req_find_next(req, &nxt); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5232 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5233 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5234 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5235 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5236 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 5237 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 5238 | } |
| 5239 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5240 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5241 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5242 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5243 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 5244 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5245 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5246 | if (nxt) { |
| 5247 | req = nxt; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 5248 | |
| 5249 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 5250 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 5251 | goto again; |
| 5252 | } |
Pavel Begunkov | 4bc4494 | 2020-02-29 22:48:24 +0300 | [diff] [blame] | 5253 | exit: |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5254 | if (old_creds) |
| 5255 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5258 | 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] | 5259 | { |
| 5260 | int ret; |
| 5261 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5262 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5263 | if (ret) { |
| 5264 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5265 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5266 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5267 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5268 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5269 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 5270 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 5271 | ret = io_req_defer_prep(req, sqe); |
| 5272 | if (unlikely(ret < 0)) |
| 5273 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5274 | /* |
| 5275 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 5276 | * to async execution. |
| 5277 | */ |
| 5278 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 5279 | io_queue_async_work(req); |
| 5280 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5281 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 5282 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5283 | } |
| 5284 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5285 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5286 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 5287 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5288 | io_cqring_add_event(req, -ECANCELED); |
| 5289 | io_double_put_req(req); |
| 5290 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5291 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 5292 | } |
| 5293 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5294 | #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^] | 5295 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 5296 | IOSQE_BUFFER_SELECT) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5297 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5298 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 5299 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5300 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 5301 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5302 | unsigned int sqe_flags; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5303 | int ret, id; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5304 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5305 | sqe_flags = READ_ONCE(sqe->flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5306 | |
| 5307 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5308 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5309 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5310 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5311 | } |
| 5312 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 5313 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 5314 | !io_op_defs[req->opcode].buffer_select) { |
| 5315 | ret = -EOPNOTSUPP; |
| 5316 | goto err_req; |
| 5317 | } |
| 5318 | |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5319 | id = READ_ONCE(sqe->personality); |
| 5320 | if (id) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5321 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 5322 | if (unlikely(!req->work.creds)) { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5323 | ret = -EINVAL; |
| 5324 | goto err_req; |
| 5325 | } |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 5326 | get_cred(req->work.creds); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 5327 | } |
| 5328 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 5329 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 5330 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame^] | 5331 | IOSQE_ASYNC | IOSQE_FIXED_FILE | |
| 5332 | IOSQE_BUFFER_SELECT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5333 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5334 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5335 | if (unlikely(ret)) { |
| 5336 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5337 | io_cqring_add_event(req, ret); |
| 5338 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5339 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5340 | } |
| 5341 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5342 | /* |
| 5343 | * If we already have a head request, queue this one for async |
| 5344 | * submittal once the head completes. If we don't have a head but |
| 5345 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 5346 | * submitted sync once the chain is complete. If none of those |
| 5347 | * conditions are true (normal request), then just queue it. |
| 5348 | */ |
| 5349 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5350 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5351 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 5352 | /* |
| 5353 | * Taking sequential execution of a link, draining both sides |
| 5354 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 5355 | * requests in the link. So, it drains the head and the |
| 5356 | * next after the link request. The last one is done via |
| 5357 | * drain_next flag to persist the effect across calls. |
| 5358 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5359 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 5360 | head->flags |= REQ_F_IO_DRAIN; |
| 5361 | ctx->drain_next = 1; |
| 5362 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5363 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5364 | ret = -EAGAIN; |
| 5365 | goto err_req; |
| 5366 | } |
| 5367 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5368 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5369 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5370 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5371 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5372 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5373 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 5374 | trace_io_uring_link(ctx, req, head); |
| 5375 | list_add_tail(&req->link_list, &head->link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5376 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 5377 | /* last request of a link, enqueue the link */ |
| 5378 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 5379 | io_queue_link_head(head); |
| 5380 | *link = NULL; |
| 5381 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5382 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5383 | if (unlikely(ctx->drain_next)) { |
| 5384 | req->flags |= REQ_F_IO_DRAIN; |
| 5385 | req->ctx->drain_next = 0; |
| 5386 | } |
| 5387 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 5388 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 5389 | INIT_LIST_HEAD(&req->link_list); |
| 5390 | ret = io_req_defer_prep(req, sqe); |
| 5391 | if (ret) |
| 5392 | req->flags |= REQ_F_FAIL_LINK; |
| 5393 | *link = req; |
| 5394 | } else { |
| 5395 | io_queue_sqe(req, sqe); |
| 5396 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5397 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5398 | |
| 5399 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5400 | } |
| 5401 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5402 | /* |
| 5403 | * Batched submission is done, ensure local IO is flushed out. |
| 5404 | */ |
| 5405 | static void io_submit_state_end(struct io_submit_state *state) |
| 5406 | { |
| 5407 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 5408 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5409 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 5410 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | /* |
| 5414 | * Start submission side cache. |
| 5415 | */ |
| 5416 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5417 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5418 | { |
| 5419 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 5420 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 5421 | state->file = NULL; |
| 5422 | state->ios_left = max_ios; |
| 5423 | } |
| 5424 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5425 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 5426 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5427 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5428 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 5429 | /* |
| 5430 | * Ensure any loads from the SQEs are done at this point, |
| 5431 | * since once we write the new head, the application could |
| 5432 | * write new data to them. |
| 5433 | */ |
| 5434 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5438 | * 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] | 5439 | * that is mapped by userspace. This means that care needs to be taken to |
| 5440 | * ensure that reads are stable, as we cannot rely on userspace always |
| 5441 | * being a good citizen. If members of the sqe are validated and then later |
| 5442 | * used, it's important that those reads are done through READ_ONCE() to |
| 5443 | * prevent a re-load down the line. |
| 5444 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5445 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 5446 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5447 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5448 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5449 | unsigned head; |
| 5450 | |
| 5451 | /* |
| 5452 | * The cached sq head (or cq tail) serves two purposes: |
| 5453 | * |
| 5454 | * 1) allows us to batch the cost of updating the user visible |
| 5455 | * head updates. |
| 5456 | * 2) allows the kernel side to track the head on its own, even |
| 5457 | * though the application is the one updating it. |
| 5458 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5459 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 5460 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5461 | /* |
| 5462 | * All io need record the previous position, if LINK vs DARIN, |
| 5463 | * it can be used to mark the position of the first IO in the |
| 5464 | * link list. |
| 5465 | */ |
| 5466 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5467 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 5468 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 5469 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5470 | ctx->cached_sq_head++; |
| 5471 | return true; |
| 5472 | } |
| 5473 | |
| 5474 | /* drop invalid entries */ |
| 5475 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 5476 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5477 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5478 | return false; |
| 5479 | } |
| 5480 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5481 | 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] | 5482 | struct file *ring_file, int ring_fd, |
| 5483 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5484 | { |
| 5485 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5486 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5487 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5488 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5489 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 5490 | /* 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] | 5491 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 5492 | if (!list_empty(&ctx->cq_overflow_list) && |
| 5493 | !io_cqring_overflow_flush(ctx, false)) |
| 5494 | return -EBUSY; |
| 5495 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5496 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 5497 | /* make sure SQ entry isn't read before tail */ |
| 5498 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 5499 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5500 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 5501 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5502 | |
| 5503 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 5504 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5505 | statep = &state; |
| 5506 | } |
| 5507 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 5508 | ctx->ring_fd = ring_fd; |
| 5509 | ctx->ring_file = ring_file; |
| 5510 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5511 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5512 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5513 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5514 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5515 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5516 | req = io_get_req(ctx, statep); |
| 5517 | if (unlikely(!req)) { |
| 5518 | if (!submitted) |
| 5519 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5520 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5521 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5522 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 5523 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 5524 | break; |
| 5525 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5526 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5527 | /* will complete beyond this point, count as submitted */ |
| 5528 | submitted++; |
| 5529 | |
| 5530 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5531 | err = -EINVAL; |
| 5532 | fail_req: |
| 5533 | io_cqring_add_event(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 5534 | io_double_put_req(req); |
| 5535 | break; |
| 5536 | } |
| 5537 | |
| 5538 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5539 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5540 | if (unlikely(mm_fault)) { |
| 5541 | err = -EFAULT; |
| 5542 | goto fail_req; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5543 | } |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 5544 | use_mm(ctx->sqo_mm); |
| 5545 | *mm = ctx->sqo_mm; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 5546 | } |
| 5547 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 5548 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 5549 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 5550 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5551 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 5552 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5553 | } |
| 5554 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 5555 | if (unlikely(submitted != nr)) { |
| 5556 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 5557 | |
| 5558 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 5559 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 5560 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 5561 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5562 | if (statep) |
| 5563 | io_submit_state_end(&state); |
| 5564 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5565 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 5566 | io_commit_sqring(ctx); |
| 5567 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5568 | return submitted; |
| 5569 | } |
| 5570 | |
| 5571 | static int io_sq_thread(void *data) |
| 5572 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5573 | struct io_ring_ctx *ctx = data; |
| 5574 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5575 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5576 | mm_segment_t old_fs; |
| 5577 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5578 | unsigned long timeout; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5579 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5580 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5581 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 5582 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5583 | old_fs = get_fs(); |
| 5584 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5585 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5586 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5587 | timeout = jiffies + ctx->sq_thread_idle; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5588 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5589 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5590 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5591 | if (!list_empty(&ctx->poll_list)) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5592 | unsigned nr_events = 0; |
| 5593 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5594 | mutex_lock(&ctx->uring_lock); |
| 5595 | if (!list_empty(&ctx->poll_list)) |
| 5596 | io_iopoll_getevents(ctx, &nr_events, 0); |
| 5597 | else |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5598 | timeout = jiffies + ctx->sq_thread_idle; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5599 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5600 | } |
| 5601 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5602 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5603 | |
| 5604 | /* |
| 5605 | * If submit got -EBUSY, flag us as needing the application |
| 5606 | * to enter the kernel to reap and flush events. |
| 5607 | */ |
| 5608 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5609 | /* |
Stefano Garzarella | 7143b5a | 2020-02-21 16:42:16 +0100 | [diff] [blame] | 5610 | * Drop cur_mm before scheduling, we can't hold it for |
| 5611 | * long periods (or over schedule()). Do this before |
| 5612 | * adding ourselves to the waitqueue, as the unuse/drop |
| 5613 | * may sleep. |
| 5614 | */ |
| 5615 | if (cur_mm) { |
| 5616 | unuse_mm(cur_mm); |
| 5617 | mmput(cur_mm); |
| 5618 | cur_mm = NULL; |
| 5619 | } |
| 5620 | |
| 5621 | /* |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5622 | * We're polling. If we're within the defined idle |
| 5623 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5624 | * to sleep. The exception is if we got EBUSY doing |
| 5625 | * more IO, we should wait for the application to |
| 5626 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5627 | */ |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5628 | if (!list_empty(&ctx->poll_list) || |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 5629 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
| 5630 | !percpu_ref_is_dying(&ctx->refs))) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5631 | if (current->task_works) |
| 5632 | task_work_run(); |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 5633 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5634 | continue; |
| 5635 | } |
| 5636 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5637 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 5638 | TASK_INTERRUPTIBLE); |
| 5639 | |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5640 | /* |
| 5641 | * While doing polled IO, before going to sleep, we need |
| 5642 | * to check if there are new reqs added to poll_list, it |
| 5643 | * is because reqs may have been punted to io worker and |
| 5644 | * will be added to poll_list later, hence check the |
| 5645 | * poll_list again. |
| 5646 | */ |
| 5647 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 5648 | !list_empty_careful(&ctx->poll_list)) { |
| 5649 | finish_wait(&ctx->sqo_wait, &wait); |
| 5650 | continue; |
| 5651 | } |
| 5652 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5653 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5654 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 5655 | /* make sure to read SQ tail after writing flags */ |
| 5656 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5657 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 5658 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5659 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5660 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5661 | finish_wait(&ctx->sqo_wait, &wait); |
| 5662 | break; |
| 5663 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5664 | if (current->task_works) { |
| 5665 | task_work_run(); |
| 5666 | continue; |
| 5667 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5668 | if (signal_pending(current)) |
| 5669 | flush_signals(current); |
| 5670 | schedule(); |
| 5671 | finish_wait(&ctx->sqo_wait, &wait); |
| 5672 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5673 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5674 | continue; |
| 5675 | } |
| 5676 | finish_wait(&ctx->sqo_wait, &wait); |
| 5677 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5678 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5681 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5682 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 5683 | mutex_unlock(&ctx->uring_lock); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 5684 | timeout = jiffies + ctx->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5687 | if (current->task_works) |
| 5688 | task_work_run(); |
| 5689 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5690 | set_fs(old_fs); |
| 5691 | if (cur_mm) { |
| 5692 | unuse_mm(cur_mm); |
| 5693 | mmput(cur_mm); |
| 5694 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5695 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5696 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5697 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5698 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5699 | return 0; |
| 5700 | } |
| 5701 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5702 | struct io_wait_queue { |
| 5703 | struct wait_queue_entry wq; |
| 5704 | struct io_ring_ctx *ctx; |
| 5705 | unsigned to_wait; |
| 5706 | unsigned nr_timeouts; |
| 5707 | }; |
| 5708 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5709 | 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] | 5710 | { |
| 5711 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5712 | |
| 5713 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5714 | * 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] | 5715 | * started waiting. For timeouts, we always want to return to userspace, |
| 5716 | * regardless of event count. |
| 5717 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5718 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5719 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5720 | } |
| 5721 | |
| 5722 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5723 | int wake_flags, void *key) |
| 5724 | { |
| 5725 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5726 | wq); |
| 5727 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5728 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5729 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5730 | return -1; |
| 5731 | |
| 5732 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5733 | } |
| 5734 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5735 | /* |
| 5736 | * Wait until events become available, if we don't already have some. The |
| 5737 | * application must reap them itself, as they reside on the shared cq ring. |
| 5738 | */ |
| 5739 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5740 | const sigset_t __user *sig, size_t sigsz) |
| 5741 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5742 | struct io_wait_queue iowq = { |
| 5743 | .wq = { |
| 5744 | .private = current, |
| 5745 | .func = io_wake_function, |
| 5746 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5747 | }, |
| 5748 | .ctx = ctx, |
| 5749 | .to_wait = min_events, |
| 5750 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5751 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5752 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5753 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5754 | do { |
| 5755 | if (io_cqring_events(ctx, false) >= min_events) |
| 5756 | return 0; |
| 5757 | if (!current->task_works) |
| 5758 | break; |
| 5759 | task_work_run(); |
| 5760 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5761 | |
| 5762 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5763 | #ifdef CONFIG_COMPAT |
| 5764 | if (in_compat_syscall()) |
| 5765 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5766 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5767 | else |
| 5768 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5769 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5770 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5771 | if (ret) |
| 5772 | return ret; |
| 5773 | } |
| 5774 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5775 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5776 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5777 | do { |
| 5778 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5779 | TASK_INTERRUPTIBLE); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5780 | if (current->task_works) |
| 5781 | task_work_run(); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5782 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5783 | break; |
| 5784 | schedule(); |
| 5785 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5786 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5787 | break; |
| 5788 | } |
| 5789 | } while (1); |
| 5790 | finish_wait(&ctx->wait, &iowq.wq); |
| 5791 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5792 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5793 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5794 | 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] | 5795 | } |
| 5796 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5797 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5798 | { |
| 5799 | #if defined(CONFIG_UNIX) |
| 5800 | if (ctx->ring_sock) { |
| 5801 | struct sock *sock = ctx->ring_sock->sk; |
| 5802 | struct sk_buff *skb; |
| 5803 | |
| 5804 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5805 | kfree_skb(skb); |
| 5806 | } |
| 5807 | #else |
| 5808 | int i; |
| 5809 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5810 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5811 | struct file *file; |
| 5812 | |
| 5813 | file = io_file_from_index(ctx, i); |
| 5814 | if (file) |
| 5815 | fput(file); |
| 5816 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5817 | #endif |
| 5818 | } |
| 5819 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5820 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5821 | { |
| 5822 | struct fixed_file_data *data; |
| 5823 | |
| 5824 | data = container_of(ref, struct fixed_file_data, refs); |
| 5825 | complete(&data->done); |
| 5826 | } |
| 5827 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5828 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5829 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5830 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5831 | unsigned nr_tables, i; |
| 5832 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5833 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5834 | return -ENXIO; |
| 5835 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5836 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5837 | flush_work(&data->ref_work); |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 5838 | wait_for_completion(&data->done); |
| 5839 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5840 | percpu_ref_exit(&data->refs); |
| 5841 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5842 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5843 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5844 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5845 | kfree(data->table[i].files); |
| 5846 | kfree(data->table); |
| 5847 | kfree(data); |
| 5848 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5849 | ctx->nr_user_files = 0; |
| 5850 | return 0; |
| 5851 | } |
| 5852 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5853 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5854 | { |
| 5855 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5856 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5857 | /* |
| 5858 | * The park is a bit of a work-around, without it we get |
| 5859 | * warning spews on shutdown with SQPOLL set and affinity |
| 5860 | * set to a single CPU. |
| 5861 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5862 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5863 | kthread_stop(ctx->sqo_thread); |
| 5864 | ctx->sqo_thread = NULL; |
| 5865 | } |
| 5866 | } |
| 5867 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5868 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5869 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5870 | io_sq_thread_stop(ctx); |
| 5871 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5872 | if (ctx->io_wq) { |
| 5873 | io_wq_destroy(ctx->io_wq); |
| 5874 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5879 | /* |
| 5880 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5881 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5882 | * loops in the file referencing. |
| 5883 | */ |
| 5884 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5885 | { |
| 5886 | struct sock *sk = ctx->ring_sock->sk; |
| 5887 | struct scm_fp_list *fpl; |
| 5888 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5889 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5890 | |
| 5891 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5892 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5893 | |
| 5894 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5895 | return -EMFILE; |
| 5896 | } |
| 5897 | |
| 5898 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5899 | if (!fpl) |
| 5900 | return -ENOMEM; |
| 5901 | |
| 5902 | skb = alloc_skb(0, GFP_KERNEL); |
| 5903 | if (!skb) { |
| 5904 | kfree(fpl); |
| 5905 | return -ENOMEM; |
| 5906 | } |
| 5907 | |
| 5908 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5909 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5910 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5911 | fpl->user = get_uid(ctx->user); |
| 5912 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5913 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5914 | |
| 5915 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5916 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5917 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5918 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5919 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5920 | } |
| 5921 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5922 | if (nr_files) { |
| 5923 | fpl->max = SCM_MAX_FD; |
| 5924 | fpl->count = nr_files; |
| 5925 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5926 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5927 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5928 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5929 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5930 | for (i = 0; i < nr_files; i++) |
| 5931 | fput(fpl->fp[i]); |
| 5932 | } else { |
| 5933 | kfree_skb(skb); |
| 5934 | kfree(fpl); |
| 5935 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5936 | |
| 5937 | return 0; |
| 5938 | } |
| 5939 | |
| 5940 | /* |
| 5941 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5942 | * causes regular reference counting to break down. We rely on the UNIX |
| 5943 | * garbage collection to take care of this problem for us. |
| 5944 | */ |
| 5945 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5946 | { |
| 5947 | unsigned left, total; |
| 5948 | int ret = 0; |
| 5949 | |
| 5950 | total = 0; |
| 5951 | left = ctx->nr_user_files; |
| 5952 | while (left) { |
| 5953 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5954 | |
| 5955 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5956 | if (ret) |
| 5957 | break; |
| 5958 | left -= this_files; |
| 5959 | total += this_files; |
| 5960 | } |
| 5961 | |
| 5962 | if (!ret) |
| 5963 | return 0; |
| 5964 | |
| 5965 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5966 | struct file *file = io_file_from_index(ctx, total); |
| 5967 | |
| 5968 | if (file) |
| 5969 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5970 | total++; |
| 5971 | } |
| 5972 | |
| 5973 | return ret; |
| 5974 | } |
| 5975 | #else |
| 5976 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5977 | { |
| 5978 | return 0; |
| 5979 | } |
| 5980 | #endif |
| 5981 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5982 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5983 | unsigned nr_files) |
| 5984 | { |
| 5985 | int i; |
| 5986 | |
| 5987 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5988 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5989 | unsigned this_files; |
| 5990 | |
| 5991 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5992 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5993 | GFP_KERNEL); |
| 5994 | if (!table->files) |
| 5995 | break; |
| 5996 | nr_files -= this_files; |
| 5997 | } |
| 5998 | |
| 5999 | if (i == nr_tables) |
| 6000 | return 0; |
| 6001 | |
| 6002 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6003 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6004 | kfree(table->files); |
| 6005 | } |
| 6006 | return 1; |
| 6007 | } |
| 6008 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6009 | 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] | 6010 | { |
| 6011 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6012 | struct sock *sock = ctx->ring_sock->sk; |
| 6013 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 6014 | struct sk_buff *skb; |
| 6015 | int i; |
| 6016 | |
| 6017 | __skb_queue_head_init(&list); |
| 6018 | |
| 6019 | /* |
| 6020 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 6021 | * remove this entry and rearrange the file array. |
| 6022 | */ |
| 6023 | skb = skb_dequeue(head); |
| 6024 | while (skb) { |
| 6025 | struct scm_fp_list *fp; |
| 6026 | |
| 6027 | fp = UNIXCB(skb).fp; |
| 6028 | for (i = 0; i < fp->count; i++) { |
| 6029 | int left; |
| 6030 | |
| 6031 | if (fp->fp[i] != file) |
| 6032 | continue; |
| 6033 | |
| 6034 | unix_notinflight(fp->user, fp->fp[i]); |
| 6035 | left = fp->count - 1 - i; |
| 6036 | if (left) { |
| 6037 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 6038 | left * sizeof(struct file *)); |
| 6039 | } |
| 6040 | fp->count--; |
| 6041 | if (!fp->count) { |
| 6042 | kfree_skb(skb); |
| 6043 | skb = NULL; |
| 6044 | } else { |
| 6045 | __skb_queue_tail(&list, skb); |
| 6046 | } |
| 6047 | fput(file); |
| 6048 | file = NULL; |
| 6049 | break; |
| 6050 | } |
| 6051 | |
| 6052 | if (!file) |
| 6053 | break; |
| 6054 | |
| 6055 | __skb_queue_tail(&list, skb); |
| 6056 | |
| 6057 | skb = skb_dequeue(head); |
| 6058 | } |
| 6059 | |
| 6060 | if (skb_peek(&list)) { |
| 6061 | spin_lock_irq(&head->lock); |
| 6062 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 6063 | __skb_queue_tail(head, skb); |
| 6064 | spin_unlock_irq(&head->lock); |
| 6065 | } |
| 6066 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6067 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6068 | #endif |
| 6069 | } |
| 6070 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6071 | struct io_file_put { |
| 6072 | struct llist_node llist; |
| 6073 | struct file *file; |
| 6074 | struct completion *done; |
| 6075 | }; |
| 6076 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6077 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6078 | { |
| 6079 | struct io_file_put *pfile, *tmp; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6080 | struct llist_node *node; |
| 6081 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6082 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 6083 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 6084 | io_ring_file_put(data->ctx, pfile->file); |
| 6085 | if (pfile->done) |
| 6086 | complete(pfile->done); |
| 6087 | else |
| 6088 | kfree(pfile); |
| 6089 | } |
| 6090 | } |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6091 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6092 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6093 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 6094 | { |
| 6095 | struct fixed_file_data *data; |
| 6096 | |
| 6097 | data = container_of(work, struct fixed_file_data, ref_work); |
| 6098 | io_ring_file_ref_flush(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6099 | percpu_ref_switch_to_percpu(&data->refs); |
| 6100 | } |
| 6101 | |
| 6102 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 6103 | { |
| 6104 | struct fixed_file_data *data; |
| 6105 | |
| 6106 | data = container_of(ref, struct fixed_file_data, refs); |
| 6107 | |
Jens Axboe | 2faf852 | 2020-02-04 19:54:55 -0700 | [diff] [blame] | 6108 | /* |
| 6109 | * We can't safely switch from inside this context, punt to wq. If |
| 6110 | * the table ref is going away, the table is being unregistered. |
| 6111 | * Don't queue up the async work for that case, the caller will |
| 6112 | * handle it. |
| 6113 | */ |
| 6114 | if (!percpu_ref_is_dying(&data->refs)) |
| 6115 | queue_work(system_wq, &data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6116 | } |
| 6117 | |
| 6118 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6119 | unsigned nr_args) |
| 6120 | { |
| 6121 | __s32 __user *fds = (__s32 __user *) arg; |
| 6122 | unsigned nr_tables; |
| 6123 | struct file *file; |
| 6124 | int fd, ret = 0; |
| 6125 | unsigned i; |
| 6126 | |
| 6127 | if (ctx->file_data) |
| 6128 | return -EBUSY; |
| 6129 | if (!nr_args) |
| 6130 | return -EINVAL; |
| 6131 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 6132 | return -EMFILE; |
| 6133 | |
| 6134 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 6135 | if (!ctx->file_data) |
| 6136 | return -ENOMEM; |
| 6137 | ctx->file_data->ctx = ctx; |
| 6138 | init_completion(&ctx->file_data->done); |
| 6139 | |
| 6140 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 6141 | ctx->file_data->table = kcalloc(nr_tables, |
| 6142 | sizeof(struct fixed_file_table), |
| 6143 | GFP_KERNEL); |
| 6144 | if (!ctx->file_data->table) { |
| 6145 | kfree(ctx->file_data); |
| 6146 | ctx->file_data = NULL; |
| 6147 | return -ENOMEM; |
| 6148 | } |
| 6149 | |
| 6150 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 6151 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 6152 | kfree(ctx->file_data->table); |
| 6153 | kfree(ctx->file_data); |
| 6154 | ctx->file_data = NULL; |
| 6155 | return -ENOMEM; |
| 6156 | } |
| 6157 | ctx->file_data->put_llist.first = NULL; |
| 6158 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 6159 | |
| 6160 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 6161 | percpu_ref_exit(&ctx->file_data->refs); |
| 6162 | kfree(ctx->file_data->table); |
| 6163 | kfree(ctx->file_data); |
| 6164 | ctx->file_data = NULL; |
| 6165 | return -ENOMEM; |
| 6166 | } |
| 6167 | |
| 6168 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 6169 | struct fixed_file_table *table; |
| 6170 | unsigned index; |
| 6171 | |
| 6172 | ret = -EFAULT; |
| 6173 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 6174 | break; |
| 6175 | /* allow sparse sets */ |
| 6176 | if (fd == -1) { |
| 6177 | ret = 0; |
| 6178 | continue; |
| 6179 | } |
| 6180 | |
| 6181 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 6182 | index = i & IORING_FILE_TABLE_MASK; |
| 6183 | file = fget(fd); |
| 6184 | |
| 6185 | ret = -EBADF; |
| 6186 | if (!file) |
| 6187 | break; |
| 6188 | |
| 6189 | /* |
| 6190 | * Don't allow io_uring instances to be registered. If UNIX |
| 6191 | * isn't enabled, then this causes a reference cycle and this |
| 6192 | * instance can never get freed. If UNIX is enabled we'll |
| 6193 | * handle it just fine, but there's still no point in allowing |
| 6194 | * a ring fd as it doesn't support regular read/write anyway. |
| 6195 | */ |
| 6196 | if (file->f_op == &io_uring_fops) { |
| 6197 | fput(file); |
| 6198 | break; |
| 6199 | } |
| 6200 | ret = 0; |
| 6201 | table->files[index] = file; |
| 6202 | } |
| 6203 | |
| 6204 | if (ret) { |
| 6205 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6206 | file = io_file_from_index(ctx, i); |
| 6207 | if (file) |
| 6208 | fput(file); |
| 6209 | } |
| 6210 | for (i = 0; i < nr_tables; i++) |
| 6211 | kfree(ctx->file_data->table[i].files); |
| 6212 | |
| 6213 | kfree(ctx->file_data->table); |
| 6214 | kfree(ctx->file_data); |
| 6215 | ctx->file_data = NULL; |
| 6216 | ctx->nr_user_files = 0; |
| 6217 | return ret; |
| 6218 | } |
| 6219 | |
| 6220 | ret = io_sqe_files_scm(ctx); |
| 6221 | if (ret) |
| 6222 | io_sqe_files_unregister(ctx); |
| 6223 | |
| 6224 | return ret; |
| 6225 | } |
| 6226 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6227 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 6228 | int index) |
| 6229 | { |
| 6230 | #if defined(CONFIG_UNIX) |
| 6231 | struct sock *sock = ctx->ring_sock->sk; |
| 6232 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 6233 | struct sk_buff *skb; |
| 6234 | |
| 6235 | /* |
| 6236 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 6237 | * file set. If there's no room, fall back to allocating a new skb |
| 6238 | * and filling it in. |
| 6239 | */ |
| 6240 | spin_lock_irq(&head->lock); |
| 6241 | skb = skb_peek(head); |
| 6242 | if (skb) { |
| 6243 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 6244 | |
| 6245 | if (fpl->count < SCM_MAX_FD) { |
| 6246 | __skb_unlink(skb, head); |
| 6247 | spin_unlock_irq(&head->lock); |
| 6248 | fpl->fp[fpl->count] = get_file(file); |
| 6249 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 6250 | fpl->count++; |
| 6251 | spin_lock_irq(&head->lock); |
| 6252 | __skb_queue_head(head, skb); |
| 6253 | } else { |
| 6254 | skb = NULL; |
| 6255 | } |
| 6256 | } |
| 6257 | spin_unlock_irq(&head->lock); |
| 6258 | |
| 6259 | if (skb) { |
| 6260 | fput(file); |
| 6261 | return 0; |
| 6262 | } |
| 6263 | |
| 6264 | return __io_sqe_files_scm(ctx, 1, index); |
| 6265 | #else |
| 6266 | return 0; |
| 6267 | #endif |
| 6268 | } |
| 6269 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6270 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6271 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6272 | struct fixed_file_data *data; |
| 6273 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6274 | /* |
| 6275 | * Juggle reference to ensure we hit zero, if needed, so we can |
| 6276 | * switch back to percpu mode |
| 6277 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6278 | data = container_of(ref, struct fixed_file_data, refs); |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6279 | percpu_ref_put(&data->refs); |
| 6280 | percpu_ref_get(&data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 6284 | struct file *file) |
| 6285 | { |
| 6286 | struct io_file_put *pfile, pfile_stack; |
| 6287 | DECLARE_COMPLETION_ONSTACK(done); |
| 6288 | |
| 6289 | /* |
| 6290 | * If we fail allocating the struct we need for doing async reomval |
| 6291 | * of this file, just punt to sync and wait for it. |
| 6292 | */ |
| 6293 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 6294 | if (!pfile) { |
| 6295 | pfile = &pfile_stack; |
| 6296 | pfile->done = &done; |
| 6297 | } |
| 6298 | |
| 6299 | pfile->file = file; |
| 6300 | llist_add(&pfile->llist, &data->put_llist); |
| 6301 | |
| 6302 | if (pfile == &pfile_stack) { |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6303 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6304 | wait_for_completion(&done); |
| 6305 | flush_work(&data->ref_work); |
| 6306 | return false; |
| 6307 | } |
| 6308 | |
| 6309 | return true; |
| 6310 | } |
| 6311 | |
| 6312 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 6313 | struct io_uring_files_update *up, |
| 6314 | unsigned nr_args) |
| 6315 | { |
| 6316 | struct fixed_file_data *data = ctx->file_data; |
| 6317 | bool ref_switch = false; |
| 6318 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6319 | __s32 __user *fds; |
| 6320 | int fd, i, err; |
| 6321 | __u32 done; |
| 6322 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6323 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6324 | return -EOVERFLOW; |
| 6325 | if (done > ctx->nr_user_files) |
| 6326 | return -EINVAL; |
| 6327 | |
| 6328 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6329 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6330 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6331 | struct fixed_file_table *table; |
| 6332 | unsigned index; |
| 6333 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6334 | err = 0; |
| 6335 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 6336 | err = -EFAULT; |
| 6337 | break; |
| 6338 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6339 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 6340 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6341 | index = i & IORING_FILE_TABLE_MASK; |
| 6342 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6343 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6344 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6345 | if (io_queue_file_removal(data, file)) |
| 6346 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6347 | } |
| 6348 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6349 | file = fget(fd); |
| 6350 | if (!file) { |
| 6351 | err = -EBADF; |
| 6352 | break; |
| 6353 | } |
| 6354 | /* |
| 6355 | * Don't allow io_uring instances to be registered. If |
| 6356 | * UNIX isn't enabled, then this causes a reference |
| 6357 | * cycle and this instance can never get freed. If UNIX |
| 6358 | * is enabled we'll handle it just fine, but there's |
| 6359 | * still no point in allowing a ring fd as it doesn't |
| 6360 | * support regular read/write anyway. |
| 6361 | */ |
| 6362 | if (file->f_op == &io_uring_fops) { |
| 6363 | fput(file); |
| 6364 | err = -EBADF; |
| 6365 | break; |
| 6366 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6367 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6368 | err = io_sqe_file_register(ctx, file, i); |
| 6369 | if (err) |
| 6370 | break; |
| 6371 | } |
| 6372 | nr_args--; |
| 6373 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6374 | up->offset++; |
| 6375 | } |
| 6376 | |
Jens Axboe | dd3db2a | 2020-02-26 10:23:43 -0700 | [diff] [blame] | 6377 | if (ref_switch) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6378 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6379 | |
| 6380 | return done ? done : err; |
| 6381 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6382 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 6383 | unsigned nr_args) |
| 6384 | { |
| 6385 | struct io_uring_files_update up; |
| 6386 | |
| 6387 | if (!ctx->file_data) |
| 6388 | return -ENXIO; |
| 6389 | if (!nr_args) |
| 6390 | return -EINVAL; |
| 6391 | if (copy_from_user(&up, arg, sizeof(up))) |
| 6392 | return -EFAULT; |
| 6393 | if (up.resv) |
| 6394 | return -EINVAL; |
| 6395 | |
| 6396 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 6397 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6398 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 6399 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 6400 | { |
| 6401 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6402 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 6403 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 6404 | io_put_req(req); |
| 6405 | } |
| 6406 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6407 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 6408 | struct io_uring_params *p) |
| 6409 | { |
| 6410 | struct io_wq_data data; |
| 6411 | struct fd f; |
| 6412 | struct io_ring_ctx *ctx_attach; |
| 6413 | unsigned int concurrency; |
| 6414 | int ret = 0; |
| 6415 | |
| 6416 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 6417 | data.free_work = io_free_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6418 | |
| 6419 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 6420 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 6421 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 6422 | |
| 6423 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 6424 | if (IS_ERR(ctx->io_wq)) { |
| 6425 | ret = PTR_ERR(ctx->io_wq); |
| 6426 | ctx->io_wq = NULL; |
| 6427 | } |
| 6428 | return ret; |
| 6429 | } |
| 6430 | |
| 6431 | f = fdget(p->wq_fd); |
| 6432 | if (!f.file) |
| 6433 | return -EBADF; |
| 6434 | |
| 6435 | if (f.file->f_op != &io_uring_fops) { |
| 6436 | ret = -EINVAL; |
| 6437 | goto out_fput; |
| 6438 | } |
| 6439 | |
| 6440 | ctx_attach = f.file->private_data; |
| 6441 | /* @io_wq is protected by holding the fd */ |
| 6442 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 6443 | ret = -EINVAL; |
| 6444 | goto out_fput; |
| 6445 | } |
| 6446 | |
| 6447 | ctx->io_wq = ctx_attach->io_wq; |
| 6448 | out_fput: |
| 6449 | fdput(f); |
| 6450 | return ret; |
| 6451 | } |
| 6452 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6453 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 6454 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6455 | { |
| 6456 | int ret; |
| 6457 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6458 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6459 | mmgrab(current->mm); |
| 6460 | ctx->sqo_mm = current->mm; |
| 6461 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6462 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 6463 | ret = -EPERM; |
| 6464 | if (!capable(CAP_SYS_ADMIN)) |
| 6465 | goto err; |
| 6466 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6467 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 6468 | if (!ctx->sq_thread_idle) |
| 6469 | ctx->sq_thread_idle = HZ; |
| 6470 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6471 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6472 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6473 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6474 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 6475 | if (cpu >= nr_cpu_ids) |
| 6476 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 6477 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 6478 | goto err; |
| 6479 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6480 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 6481 | ctx, cpu, |
| 6482 | "io_uring-sq"); |
| 6483 | } else { |
| 6484 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 6485 | "io_uring-sq"); |
| 6486 | } |
| 6487 | if (IS_ERR(ctx->sqo_thread)) { |
| 6488 | ret = PTR_ERR(ctx->sqo_thread); |
| 6489 | ctx->sqo_thread = NULL; |
| 6490 | goto err; |
| 6491 | } |
| 6492 | wake_up_process(ctx->sqo_thread); |
| 6493 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 6494 | /* Can't have SQ_AFF without SQPOLL */ |
| 6495 | ret = -EINVAL; |
| 6496 | goto err; |
| 6497 | } |
| 6498 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6499 | ret = io_init_wq_offload(ctx, p); |
| 6500 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6501 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6502 | |
| 6503 | return 0; |
| 6504 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 6505 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6506 | mmdrop(ctx->sqo_mm); |
| 6507 | ctx->sqo_mm = NULL; |
| 6508 | return ret; |
| 6509 | } |
| 6510 | |
| 6511 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 6512 | { |
| 6513 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 6514 | } |
| 6515 | |
| 6516 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 6517 | { |
| 6518 | unsigned long page_limit, cur_pages, new_pages; |
| 6519 | |
| 6520 | /* Don't allow more pages than we can safely lock */ |
| 6521 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 6522 | |
| 6523 | do { |
| 6524 | cur_pages = atomic_long_read(&user->locked_vm); |
| 6525 | new_pages = cur_pages + nr_pages; |
| 6526 | if (new_pages > page_limit) |
| 6527 | return -ENOMEM; |
| 6528 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 6529 | new_pages) != cur_pages); |
| 6530 | |
| 6531 | return 0; |
| 6532 | } |
| 6533 | |
| 6534 | static void io_mem_free(void *ptr) |
| 6535 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6536 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6537 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 6538 | if (!ptr) |
| 6539 | return; |
| 6540 | |
| 6541 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6542 | if (put_page_testzero(page)) |
| 6543 | free_compound_page(page); |
| 6544 | } |
| 6545 | |
| 6546 | static void *io_mem_alloc(size_t size) |
| 6547 | { |
| 6548 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 6549 | __GFP_NORETRY; |
| 6550 | |
| 6551 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 6552 | } |
| 6553 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6554 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 6555 | size_t *sq_offset) |
| 6556 | { |
| 6557 | struct io_rings *rings; |
| 6558 | size_t off, sq_array_size; |
| 6559 | |
| 6560 | off = struct_size(rings, cqes, cq_entries); |
| 6561 | if (off == SIZE_MAX) |
| 6562 | return SIZE_MAX; |
| 6563 | |
| 6564 | #ifdef CONFIG_SMP |
| 6565 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 6566 | if (off == 0) |
| 6567 | return SIZE_MAX; |
| 6568 | #endif |
| 6569 | |
| 6570 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 6571 | if (sq_array_size == SIZE_MAX) |
| 6572 | return SIZE_MAX; |
| 6573 | |
| 6574 | if (check_add_overflow(off, sq_array_size, &off)) |
| 6575 | return SIZE_MAX; |
| 6576 | |
| 6577 | if (sq_offset) |
| 6578 | *sq_offset = off; |
| 6579 | |
| 6580 | return off; |
| 6581 | } |
| 6582 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6583 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 6584 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6585 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6586 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6587 | pages = (size_t)1 << get_order( |
| 6588 | rings_size(sq_entries, cq_entries, NULL)); |
| 6589 | pages += (size_t)1 << get_order( |
| 6590 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6591 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6592 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6593 | } |
| 6594 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6595 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 6596 | { |
| 6597 | int i, j; |
| 6598 | |
| 6599 | if (!ctx->user_bufs) |
| 6600 | return -ENXIO; |
| 6601 | |
| 6602 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 6603 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6604 | |
| 6605 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6606 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6607 | |
| 6608 | if (ctx->account_mem) |
| 6609 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6610 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6611 | imu->nr_bvecs = 0; |
| 6612 | } |
| 6613 | |
| 6614 | kfree(ctx->user_bufs); |
| 6615 | ctx->user_bufs = NULL; |
| 6616 | ctx->nr_user_bufs = 0; |
| 6617 | return 0; |
| 6618 | } |
| 6619 | |
| 6620 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 6621 | void __user *arg, unsigned index) |
| 6622 | { |
| 6623 | struct iovec __user *src; |
| 6624 | |
| 6625 | #ifdef CONFIG_COMPAT |
| 6626 | if (ctx->compat) { |
| 6627 | struct compat_iovec __user *ciovs; |
| 6628 | struct compat_iovec ciov; |
| 6629 | |
| 6630 | ciovs = (struct compat_iovec __user *) arg; |
| 6631 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 6632 | return -EFAULT; |
| 6633 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 6634 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6635 | dst->iov_len = ciov.iov_len; |
| 6636 | return 0; |
| 6637 | } |
| 6638 | #endif |
| 6639 | src = (struct iovec __user *) arg; |
| 6640 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 6641 | return -EFAULT; |
| 6642 | return 0; |
| 6643 | } |
| 6644 | |
| 6645 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 6646 | unsigned nr_args) |
| 6647 | { |
| 6648 | struct vm_area_struct **vmas = NULL; |
| 6649 | struct page **pages = NULL; |
| 6650 | int i, j, got_pages = 0; |
| 6651 | int ret = -EINVAL; |
| 6652 | |
| 6653 | if (ctx->user_bufs) |
| 6654 | return -EBUSY; |
| 6655 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 6656 | return -EINVAL; |
| 6657 | |
| 6658 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 6659 | GFP_KERNEL); |
| 6660 | if (!ctx->user_bufs) |
| 6661 | return -ENOMEM; |
| 6662 | |
| 6663 | for (i = 0; i < nr_args; i++) { |
| 6664 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 6665 | unsigned long off, start, end, ubuf; |
| 6666 | int pret, nr_pages; |
| 6667 | struct iovec iov; |
| 6668 | size_t size; |
| 6669 | |
| 6670 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 6671 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 6672 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6673 | |
| 6674 | /* |
| 6675 | * Don't impose further limits on the size and buffer |
| 6676 | * constraints here, we'll -EINVAL later when IO is |
| 6677 | * submitted if they are wrong. |
| 6678 | */ |
| 6679 | ret = -EFAULT; |
| 6680 | if (!iov.iov_base || !iov.iov_len) |
| 6681 | goto err; |
| 6682 | |
| 6683 | /* arbitrary limit, but we need something */ |
| 6684 | if (iov.iov_len > SZ_1G) |
| 6685 | goto err; |
| 6686 | |
| 6687 | ubuf = (unsigned long) iov.iov_base; |
| 6688 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 6689 | start = ubuf >> PAGE_SHIFT; |
| 6690 | nr_pages = end - start; |
| 6691 | |
| 6692 | if (ctx->account_mem) { |
| 6693 | ret = io_account_mem(ctx->user, nr_pages); |
| 6694 | if (ret) |
| 6695 | goto err; |
| 6696 | } |
| 6697 | |
| 6698 | ret = 0; |
| 6699 | if (!pages || nr_pages > got_pages) { |
| 6700 | kfree(vmas); |
| 6701 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6702 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6703 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6704 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6705 | sizeof(struct vm_area_struct *), |
| 6706 | GFP_KERNEL); |
| 6707 | if (!pages || !vmas) { |
| 6708 | ret = -ENOMEM; |
| 6709 | if (ctx->account_mem) |
| 6710 | io_unaccount_mem(ctx->user, nr_pages); |
| 6711 | goto err; |
| 6712 | } |
| 6713 | got_pages = nr_pages; |
| 6714 | } |
| 6715 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6716 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6717 | GFP_KERNEL); |
| 6718 | ret = -ENOMEM; |
| 6719 | if (!imu->bvec) { |
| 6720 | if (ctx->account_mem) |
| 6721 | io_unaccount_mem(ctx->user, nr_pages); |
| 6722 | goto err; |
| 6723 | } |
| 6724 | |
| 6725 | ret = 0; |
| 6726 | down_read(¤t->mm->mmap_sem); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 6727 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6728 | FOLL_WRITE | FOLL_LONGTERM, |
| 6729 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6730 | if (pret == nr_pages) { |
| 6731 | /* don't support file backed memory */ |
| 6732 | for (j = 0; j < nr_pages; j++) { |
| 6733 | struct vm_area_struct *vma = vmas[j]; |
| 6734 | |
| 6735 | if (vma->vm_file && |
| 6736 | !is_file_hugepages(vma->vm_file)) { |
| 6737 | ret = -EOPNOTSUPP; |
| 6738 | break; |
| 6739 | } |
| 6740 | } |
| 6741 | } else { |
| 6742 | ret = pret < 0 ? pret : -EFAULT; |
| 6743 | } |
| 6744 | up_read(¤t->mm->mmap_sem); |
| 6745 | if (ret) { |
| 6746 | /* |
| 6747 | * if we did partial map, or found file backed vmas, |
| 6748 | * release any pages we did get |
| 6749 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6750 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 6751 | unpin_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6752 | if (ctx->account_mem) |
| 6753 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6754 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6755 | goto err; |
| 6756 | } |
| 6757 | |
| 6758 | off = ubuf & ~PAGE_MASK; |
| 6759 | size = iov.iov_len; |
| 6760 | for (j = 0; j < nr_pages; j++) { |
| 6761 | size_t vec_len; |
| 6762 | |
| 6763 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6764 | imu->bvec[j].bv_page = pages[j]; |
| 6765 | imu->bvec[j].bv_len = vec_len; |
| 6766 | imu->bvec[j].bv_offset = off; |
| 6767 | off = 0; |
| 6768 | size -= vec_len; |
| 6769 | } |
| 6770 | /* store original address for later verification */ |
| 6771 | imu->ubuf = ubuf; |
| 6772 | imu->len = iov.iov_len; |
| 6773 | imu->nr_bvecs = nr_pages; |
| 6774 | |
| 6775 | ctx->nr_user_bufs++; |
| 6776 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6777 | kvfree(pages); |
| 6778 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6779 | return 0; |
| 6780 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6781 | kvfree(pages); |
| 6782 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6783 | io_sqe_buffer_unregister(ctx); |
| 6784 | return ret; |
| 6785 | } |
| 6786 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6787 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6788 | { |
| 6789 | __s32 __user *fds = arg; |
| 6790 | int fd; |
| 6791 | |
| 6792 | if (ctx->cq_ev_fd) |
| 6793 | return -EBUSY; |
| 6794 | |
| 6795 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6796 | return -EFAULT; |
| 6797 | |
| 6798 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6799 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6800 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6801 | ctx->cq_ev_fd = NULL; |
| 6802 | return ret; |
| 6803 | } |
| 6804 | |
| 6805 | return 0; |
| 6806 | } |
| 6807 | |
| 6808 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6809 | { |
| 6810 | if (ctx->cq_ev_fd) { |
| 6811 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6812 | ctx->cq_ev_fd = NULL; |
| 6813 | return 0; |
| 6814 | } |
| 6815 | |
| 6816 | return -ENXIO; |
| 6817 | } |
| 6818 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 6819 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 6820 | { |
| 6821 | struct io_ring_ctx *ctx = data; |
| 6822 | struct io_buffer *buf = p; |
| 6823 | |
| 6824 | /* the head kbuf is the list itself */ |
| 6825 | while (!list_empty(&buf->list)) { |
| 6826 | struct io_buffer *nxt; |
| 6827 | |
| 6828 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 6829 | list_del(&nxt->list); |
| 6830 | kfree(nxt); |
| 6831 | } |
| 6832 | kfree(buf); |
| 6833 | idr_remove(&ctx->io_buffer_idr, id); |
| 6834 | return 0; |
| 6835 | } |
| 6836 | |
| 6837 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 6838 | { |
| 6839 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 6840 | idr_destroy(&ctx->io_buffer_idr); |
| 6841 | } |
| 6842 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6843 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6844 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6845 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6846 | if (ctx->sqo_mm) |
| 6847 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6848 | |
| 6849 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6850 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6851 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6852 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 6853 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 6854 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6855 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6856 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6857 | if (ctx->ring_sock) { |
| 6858 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6859 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6860 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6861 | #endif |
| 6862 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6863 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6864 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6865 | |
| 6866 | percpu_ref_exit(&ctx->refs); |
| 6867 | if (ctx->account_mem) |
| 6868 | io_unaccount_mem(ctx->user, |
| 6869 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6870 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6871 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6872 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6873 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6874 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6875 | kfree(ctx); |
| 6876 | } |
| 6877 | |
| 6878 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6879 | { |
| 6880 | struct io_ring_ctx *ctx = file->private_data; |
| 6881 | __poll_t mask = 0; |
| 6882 | |
| 6883 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6884 | /* |
| 6885 | * synchronizes with barrier from wq_has_sleeper call in |
| 6886 | * io_commit_cqring |
| 6887 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6888 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6889 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6890 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6891 | mask |= EPOLLOUT | EPOLLWRNORM; |
Stefano Garzarella | 63e5d81 | 2020-02-07 13:18:28 +0100 | [diff] [blame] | 6892 | if (io_cqring_events(ctx, false)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6893 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6894 | |
| 6895 | return mask; |
| 6896 | } |
| 6897 | |
| 6898 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6899 | { |
| 6900 | struct io_ring_ctx *ctx = file->private_data; |
| 6901 | |
| 6902 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6903 | } |
| 6904 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6905 | static int io_remove_personalities(int id, void *p, void *data) |
| 6906 | { |
| 6907 | struct io_ring_ctx *ctx = data; |
| 6908 | const struct cred *cred; |
| 6909 | |
| 6910 | cred = idr_remove(&ctx->personality_idr, id); |
| 6911 | if (cred) |
| 6912 | put_cred(cred); |
| 6913 | return 0; |
| 6914 | } |
| 6915 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6916 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6917 | { |
| 6918 | mutex_lock(&ctx->uring_lock); |
| 6919 | percpu_ref_kill(&ctx->refs); |
| 6920 | mutex_unlock(&ctx->uring_lock); |
| 6921 | |
Jens Axboe | df069d8 | 2020-02-04 16:48:34 -0700 | [diff] [blame] | 6922 | /* |
| 6923 | * Wait for sq thread to idle, if we have one. It won't spin on new |
| 6924 | * work after we've killed the ctx ref above. This is important to do |
| 6925 | * before we cancel existing commands, as the thread could otherwise |
| 6926 | * be queueing new work post that. If that's work we need to cancel, |
| 6927 | * it could cause shutdown to hang. |
| 6928 | */ |
| 6929 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) |
| 6930 | cpu_relax(); |
| 6931 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6932 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6933 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6934 | |
| 6935 | if (ctx->io_wq) |
| 6936 | io_wq_cancel_all(ctx->io_wq); |
| 6937 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6938 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6939 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6940 | if (ctx->rings) |
| 6941 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 6942 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6943 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6944 | io_ring_ctx_free(ctx); |
| 6945 | } |
| 6946 | |
| 6947 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6948 | { |
| 6949 | struct io_ring_ctx *ctx = file->private_data; |
| 6950 | |
| 6951 | file->private_data = NULL; |
| 6952 | io_ring_ctx_wait_and_kill(ctx); |
| 6953 | return 0; |
| 6954 | } |
| 6955 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6956 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6957 | struct files_struct *files) |
| 6958 | { |
| 6959 | struct io_kiocb *req; |
| 6960 | DEFINE_WAIT(wait); |
| 6961 | |
| 6962 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6963 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6964 | |
| 6965 | spin_lock_irq(&ctx->inflight_lock); |
| 6966 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6967 | if (req->work.files != files) |
| 6968 | continue; |
| 6969 | /* req is being completed, ignore */ |
| 6970 | if (!refcount_inc_not_zero(&req->refs)) |
| 6971 | continue; |
| 6972 | cancel_req = req; |
| 6973 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6974 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6975 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6976 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6977 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6978 | spin_unlock_irq(&ctx->inflight_lock); |
| 6979 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6980 | /* We need to keep going until we don't find a matching req */ |
| 6981 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6982 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6983 | |
Jens Axboe | 2ca1025 | 2020-02-13 17:17:35 -0700 | [diff] [blame] | 6984 | if (cancel_req->flags & REQ_F_OVERFLOW) { |
| 6985 | spin_lock_irq(&ctx->completion_lock); |
| 6986 | list_del(&cancel_req->list); |
| 6987 | cancel_req->flags &= ~REQ_F_OVERFLOW; |
| 6988 | if (list_empty(&ctx->cq_overflow_list)) { |
| 6989 | clear_bit(0, &ctx->sq_check_overflow); |
| 6990 | clear_bit(0, &ctx->cq_check_overflow); |
| 6991 | } |
| 6992 | spin_unlock_irq(&ctx->completion_lock); |
| 6993 | |
| 6994 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 6995 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 6996 | |
| 6997 | /* |
| 6998 | * Put inflight ref and overflow ref. If that's |
| 6999 | * all we had, then we're done with this request. |
| 7000 | */ |
| 7001 | if (refcount_sub_and_test(2, &cancel_req->refs)) { |
| 7002 | io_put_req(cancel_req); |
| 7003 | continue; |
| 7004 | } |
| 7005 | } |
| 7006 | |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 7007 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 7008 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7009 | schedule(); |
| 7010 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 7011 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7012 | } |
| 7013 | |
| 7014 | static int io_uring_flush(struct file *file, void *data) |
| 7015 | { |
| 7016 | struct io_ring_ctx *ctx = file->private_data; |
| 7017 | |
| 7018 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 6ab2314 | 2020-02-08 20:23:59 -0700 | [diff] [blame] | 7019 | |
| 7020 | /* |
| 7021 | * If the task is going away, cancel work it may have pending |
| 7022 | */ |
| 7023 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 7024 | io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current)); |
| 7025 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7026 | return 0; |
| 7027 | } |
| 7028 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7029 | static void *io_uring_validate_mmap_request(struct file *file, |
| 7030 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7031 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7032 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7033 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7034 | struct page *page; |
| 7035 | void *ptr; |
| 7036 | |
| 7037 | switch (offset) { |
| 7038 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7039 | case IORING_OFF_CQ_RING: |
| 7040 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7041 | break; |
| 7042 | case IORING_OFF_SQES: |
| 7043 | ptr = ctx->sq_sqes; |
| 7044 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7045 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7046 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7047 | } |
| 7048 | |
| 7049 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 7050 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7051 | return ERR_PTR(-EINVAL); |
| 7052 | |
| 7053 | return ptr; |
| 7054 | } |
| 7055 | |
| 7056 | #ifdef CONFIG_MMU |
| 7057 | |
| 7058 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7059 | { |
| 7060 | size_t sz = vma->vm_end - vma->vm_start; |
| 7061 | unsigned long pfn; |
| 7062 | void *ptr; |
| 7063 | |
| 7064 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 7065 | if (IS_ERR(ptr)) |
| 7066 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7067 | |
| 7068 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 7069 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 7070 | } |
| 7071 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7072 | #else /* !CONFIG_MMU */ |
| 7073 | |
| 7074 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 7075 | { |
| 7076 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 7077 | } |
| 7078 | |
| 7079 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 7080 | { |
| 7081 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 7082 | } |
| 7083 | |
| 7084 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 7085 | unsigned long addr, unsigned long len, |
| 7086 | unsigned long pgoff, unsigned long flags) |
| 7087 | { |
| 7088 | void *ptr; |
| 7089 | |
| 7090 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 7091 | if (IS_ERR(ptr)) |
| 7092 | return PTR_ERR(ptr); |
| 7093 | |
| 7094 | return (unsigned long) ptr; |
| 7095 | } |
| 7096 | |
| 7097 | #endif /* !CONFIG_MMU */ |
| 7098 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7099 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 7100 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 7101 | size_t, sigsz) |
| 7102 | { |
| 7103 | struct io_ring_ctx *ctx; |
| 7104 | long ret = -EBADF; |
| 7105 | int submitted = 0; |
| 7106 | struct fd f; |
| 7107 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7108 | if (current->task_works) |
| 7109 | task_work_run(); |
| 7110 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7111 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7112 | return -EINVAL; |
| 7113 | |
| 7114 | f = fdget(fd); |
| 7115 | if (!f.file) |
| 7116 | return -EBADF; |
| 7117 | |
| 7118 | ret = -EOPNOTSUPP; |
| 7119 | if (f.file->f_op != &io_uring_fops) |
| 7120 | goto out_fput; |
| 7121 | |
| 7122 | ret = -ENXIO; |
| 7123 | ctx = f.file->private_data; |
| 7124 | if (!percpu_ref_tryget(&ctx->refs)) |
| 7125 | goto out_fput; |
| 7126 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7127 | /* |
| 7128 | * For SQ polling, the thread will do all submissions and completions. |
| 7129 | * Just return the requested submit count, and wake the thread if |
| 7130 | * we were asked to. |
| 7131 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7132 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7133 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7134 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 7135 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7136 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 7137 | wake_up(&ctx->sqo_wait); |
| 7138 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 7139 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7140 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7141 | |
| 7142 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7143 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 7144 | cur_mm = ctx->sqo_mm; |
| 7145 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 7146 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7147 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7148 | |
| 7149 | if (submitted != to_submit) |
| 7150 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7151 | } |
| 7152 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7153 | unsigned nr_events = 0; |
| 7154 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7155 | min_complete = min(min_complete, ctx->cq_entries); |
| 7156 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7157 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7158 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 7159 | } else { |
| 7160 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 7161 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7162 | } |
| 7163 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 7164 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 7165 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7166 | out_fput: |
| 7167 | fdput(f); |
| 7168 | return submitted ? submitted : ret; |
| 7169 | } |
| 7170 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7171 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7172 | static int io_uring_show_cred(int id, void *p, void *data) |
| 7173 | { |
| 7174 | const struct cred *cred = p; |
| 7175 | struct seq_file *m = data; |
| 7176 | struct user_namespace *uns = seq_user_ns(m); |
| 7177 | struct group_info *gi; |
| 7178 | kernel_cap_t cap; |
| 7179 | unsigned __capi; |
| 7180 | int g; |
| 7181 | |
| 7182 | seq_printf(m, "%5d\n", id); |
| 7183 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 7184 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 7185 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 7186 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 7187 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 7188 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 7189 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 7190 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 7191 | seq_puts(m, "\n\tGroups:\t"); |
| 7192 | gi = cred->group_info; |
| 7193 | for (g = 0; g < gi->ngroups; g++) { |
| 7194 | seq_put_decimal_ull(m, g ? " " : "", |
| 7195 | from_kgid_munged(uns, gi->gid[g])); |
| 7196 | } |
| 7197 | seq_puts(m, "\n\tCapEff:\t"); |
| 7198 | cap = cred->cap_effective; |
| 7199 | CAP_FOR_EACH_U32(__capi) |
| 7200 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 7201 | seq_putc(m, '\n'); |
| 7202 | return 0; |
| 7203 | } |
| 7204 | |
| 7205 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 7206 | { |
| 7207 | int i; |
| 7208 | |
| 7209 | mutex_lock(&ctx->uring_lock); |
| 7210 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
| 7211 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7212 | struct fixed_file_table *table; |
| 7213 | struct file *f; |
| 7214 | |
| 7215 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7216 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 7217 | if (f) |
| 7218 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 7219 | else |
| 7220 | seq_printf(m, "%5u: <none>\n", i); |
| 7221 | } |
| 7222 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
| 7223 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 7224 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 7225 | |
| 7226 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 7227 | (unsigned int) buf->len); |
| 7228 | } |
| 7229 | if (!idr_is_empty(&ctx->personality_idr)) { |
| 7230 | seq_printf(m, "Personalities:\n"); |
| 7231 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 7232 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 7233 | seq_printf(m, "PollList:\n"); |
| 7234 | spin_lock_irq(&ctx->completion_lock); |
| 7235 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 7236 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 7237 | struct io_kiocb *req; |
| 7238 | |
| 7239 | hlist_for_each_entry(req, list, hash_node) |
| 7240 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 7241 | req->task->task_works != NULL); |
| 7242 | } |
| 7243 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7244 | mutex_unlock(&ctx->uring_lock); |
| 7245 | } |
| 7246 | |
| 7247 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 7248 | { |
| 7249 | struct io_ring_ctx *ctx = f->private_data; |
| 7250 | |
| 7251 | if (percpu_ref_tryget(&ctx->refs)) { |
| 7252 | __io_uring_show_fdinfo(ctx, m); |
| 7253 | percpu_ref_put(&ctx->refs); |
| 7254 | } |
| 7255 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7256 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7257 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7258 | static const struct file_operations io_uring_fops = { |
| 7259 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 7260 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7261 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 7262 | #ifndef CONFIG_MMU |
| 7263 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 7264 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 7265 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7266 | .poll = io_uring_poll, |
| 7267 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7268 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 7269 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 7270 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7271 | }; |
| 7272 | |
| 7273 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 7274 | struct io_uring_params *p) |
| 7275 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7276 | struct io_rings *rings; |
| 7277 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7278 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7279 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 7280 | if (size == SIZE_MAX) |
| 7281 | return -EOVERFLOW; |
| 7282 | |
| 7283 | rings = io_mem_alloc(size); |
| 7284 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7285 | return -ENOMEM; |
| 7286 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7287 | ctx->rings = rings; |
| 7288 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 7289 | rings->sq_ring_mask = p->sq_entries - 1; |
| 7290 | rings->cq_ring_mask = p->cq_entries - 1; |
| 7291 | rings->sq_ring_entries = p->sq_entries; |
| 7292 | rings->cq_ring_entries = p->cq_entries; |
| 7293 | ctx->sq_mask = rings->sq_ring_mask; |
| 7294 | ctx->cq_mask = rings->cq_ring_mask; |
| 7295 | ctx->sq_entries = rings->sq_ring_entries; |
| 7296 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7297 | |
| 7298 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7299 | if (size == SIZE_MAX) { |
| 7300 | io_mem_free(ctx->rings); |
| 7301 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7302 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7303 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7304 | |
| 7305 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7306 | if (!ctx->sq_sqes) { |
| 7307 | io_mem_free(ctx->rings); |
| 7308 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7309 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 7310 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7311 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7312 | return 0; |
| 7313 | } |
| 7314 | |
| 7315 | /* |
| 7316 | * Allocate an anonymous fd, this is what constitutes the application |
| 7317 | * visible backing of an io_uring instance. The application mmaps this |
| 7318 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 7319 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 7320 | */ |
| 7321 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 7322 | { |
| 7323 | struct file *file; |
| 7324 | int ret; |
| 7325 | |
| 7326 | #if defined(CONFIG_UNIX) |
| 7327 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 7328 | &ctx->ring_sock); |
| 7329 | if (ret) |
| 7330 | return ret; |
| 7331 | #endif |
| 7332 | |
| 7333 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 7334 | if (ret < 0) |
| 7335 | goto err; |
| 7336 | |
| 7337 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 7338 | O_RDWR | O_CLOEXEC); |
| 7339 | if (IS_ERR(file)) { |
| 7340 | put_unused_fd(ret); |
| 7341 | ret = PTR_ERR(file); |
| 7342 | goto err; |
| 7343 | } |
| 7344 | |
| 7345 | #if defined(CONFIG_UNIX) |
| 7346 | ctx->ring_sock->file = file; |
| 7347 | #endif |
| 7348 | fd_install(ret, file); |
| 7349 | return ret; |
| 7350 | err: |
| 7351 | #if defined(CONFIG_UNIX) |
| 7352 | sock_release(ctx->ring_sock); |
| 7353 | ctx->ring_sock = NULL; |
| 7354 | #endif |
| 7355 | return ret; |
| 7356 | } |
| 7357 | |
| 7358 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 7359 | { |
| 7360 | struct user_struct *user = NULL; |
| 7361 | struct io_ring_ctx *ctx; |
| 7362 | bool account_mem; |
| 7363 | int ret; |
| 7364 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7365 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7366 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7367 | if (entries > IORING_MAX_ENTRIES) { |
| 7368 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 7369 | return -EINVAL; |
| 7370 | entries = IORING_MAX_ENTRIES; |
| 7371 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7372 | |
| 7373 | /* |
| 7374 | * Use twice as many entries for the CQ ring. It's possible for the |
| 7375 | * application to drive a higher depth than the size of the SQ ring, |
| 7376 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7377 | * some flexibility in overcommitting a bit. If the application has |
| 7378 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 7379 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7380 | */ |
| 7381 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7382 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 7383 | /* |
| 7384 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 7385 | * to a power-of-two, if it isn't already. We do NOT impose |
| 7386 | * any cq vs sq ring sizing. |
| 7387 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7388 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7389 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7390 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 7391 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 7392 | return -EINVAL; |
| 7393 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 7394 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 7395 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 7396 | } else { |
| 7397 | p->cq_entries = 2 * p->sq_entries; |
| 7398 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7399 | |
| 7400 | user = get_uid(current_user()); |
| 7401 | account_mem = !capable(CAP_IPC_LOCK); |
| 7402 | |
| 7403 | if (account_mem) { |
| 7404 | ret = io_account_mem(user, |
| 7405 | ring_pages(p->sq_entries, p->cq_entries)); |
| 7406 | if (ret) { |
| 7407 | free_uid(user); |
| 7408 | return ret; |
| 7409 | } |
| 7410 | } |
| 7411 | |
| 7412 | ctx = io_ring_ctx_alloc(p); |
| 7413 | if (!ctx) { |
| 7414 | if (account_mem) |
| 7415 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 7416 | p->cq_entries)); |
| 7417 | free_uid(user); |
| 7418 | return -ENOMEM; |
| 7419 | } |
| 7420 | ctx->compat = in_compat_syscall(); |
| 7421 | ctx->account_mem = account_mem; |
| 7422 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 7423 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7424 | |
| 7425 | ret = io_allocate_scq_urings(ctx, p); |
| 7426 | if (ret) |
| 7427 | goto err; |
| 7428 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7429 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7430 | if (ret) |
| 7431 | goto err; |
| 7432 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7433 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7434 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 7435 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 7436 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 7437 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 7438 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 7439 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 7440 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7441 | |
| 7442 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7443 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 7444 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 7445 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 7446 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 7447 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 7448 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 7449 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 7450 | /* |
| 7451 | * Install ring fd as the very last thing, so we don't risk someone |
| 7452 | * having closed it before we finish setup |
| 7453 | */ |
| 7454 | ret = io_uring_get_fd(ctx); |
| 7455 | if (ret < 0) |
| 7456 | goto err; |
| 7457 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 7458 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 7459 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 7460 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7461 | 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] | 7462 | return ret; |
| 7463 | err: |
| 7464 | io_ring_ctx_wait_and_kill(ctx); |
| 7465 | return ret; |
| 7466 | } |
| 7467 | |
| 7468 | /* |
| 7469 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 7470 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 7471 | * params structure passed in. |
| 7472 | */ |
| 7473 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 7474 | { |
| 7475 | struct io_uring_params p; |
| 7476 | long ret; |
| 7477 | int i; |
| 7478 | |
| 7479 | if (copy_from_user(&p, params, sizeof(p))) |
| 7480 | return -EFAULT; |
| 7481 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 7482 | if (p.resv[i]) |
| 7483 | return -EINVAL; |
| 7484 | } |
| 7485 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7486 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 7487 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7488 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7489 | return -EINVAL; |
| 7490 | |
| 7491 | ret = io_uring_create(entries, &p); |
| 7492 | if (ret < 0) |
| 7493 | return ret; |
| 7494 | |
| 7495 | if (copy_to_user(params, &p, sizeof(p))) |
| 7496 | return -EFAULT; |
| 7497 | |
| 7498 | return ret; |
| 7499 | } |
| 7500 | |
| 7501 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 7502 | struct io_uring_params __user *, params) |
| 7503 | { |
| 7504 | return io_uring_setup(entries, params); |
| 7505 | } |
| 7506 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7507 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 7508 | { |
| 7509 | struct io_uring_probe *p; |
| 7510 | size_t size; |
| 7511 | int i, ret; |
| 7512 | |
| 7513 | size = struct_size(p, ops, nr_args); |
| 7514 | if (size == SIZE_MAX) |
| 7515 | return -EOVERFLOW; |
| 7516 | p = kzalloc(size, GFP_KERNEL); |
| 7517 | if (!p) |
| 7518 | return -ENOMEM; |
| 7519 | |
| 7520 | ret = -EFAULT; |
| 7521 | if (copy_from_user(p, arg, size)) |
| 7522 | goto out; |
| 7523 | ret = -EINVAL; |
| 7524 | if (memchr_inv(p, 0, size)) |
| 7525 | goto out; |
| 7526 | |
| 7527 | p->last_op = IORING_OP_LAST - 1; |
| 7528 | if (nr_args > IORING_OP_LAST) |
| 7529 | nr_args = IORING_OP_LAST; |
| 7530 | |
| 7531 | for (i = 0; i < nr_args; i++) { |
| 7532 | p->ops[i].op = i; |
| 7533 | if (!io_op_defs[i].not_supported) |
| 7534 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 7535 | } |
| 7536 | p->ops_len = i; |
| 7537 | |
| 7538 | ret = 0; |
| 7539 | if (copy_to_user(arg, p, size)) |
| 7540 | ret = -EFAULT; |
| 7541 | out: |
| 7542 | kfree(p); |
| 7543 | return ret; |
| 7544 | } |
| 7545 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7546 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 7547 | { |
| 7548 | const struct cred *creds = get_current_cred(); |
| 7549 | int id; |
| 7550 | |
| 7551 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 7552 | USHRT_MAX, GFP_KERNEL); |
| 7553 | if (id < 0) |
| 7554 | put_cred(creds); |
| 7555 | return id; |
| 7556 | } |
| 7557 | |
| 7558 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 7559 | { |
| 7560 | const struct cred *old_creds; |
| 7561 | |
| 7562 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 7563 | if (old_creds) { |
| 7564 | put_cred(old_creds); |
| 7565 | return 0; |
| 7566 | } |
| 7567 | |
| 7568 | return -EINVAL; |
| 7569 | } |
| 7570 | |
| 7571 | static bool io_register_op_must_quiesce(int op) |
| 7572 | { |
| 7573 | switch (op) { |
| 7574 | case IORING_UNREGISTER_FILES: |
| 7575 | case IORING_REGISTER_FILES_UPDATE: |
| 7576 | case IORING_REGISTER_PROBE: |
| 7577 | case IORING_REGISTER_PERSONALITY: |
| 7578 | case IORING_UNREGISTER_PERSONALITY: |
| 7579 | return false; |
| 7580 | default: |
| 7581 | return true; |
| 7582 | } |
| 7583 | } |
| 7584 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7585 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 7586 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7587 | __releases(ctx->uring_lock) |
| 7588 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7589 | { |
| 7590 | int ret; |
| 7591 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 7592 | /* |
| 7593 | * We're inside the ring mutex, if the ref is already dying, then |
| 7594 | * someone else killed the ctx or is already going through |
| 7595 | * io_uring_register(). |
| 7596 | */ |
| 7597 | if (percpu_ref_is_dying(&ctx->refs)) |
| 7598 | return -ENXIO; |
| 7599 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7600 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7601 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 7602 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7603 | /* |
| 7604 | * Drop uring mutex before waiting for references to exit. If |
| 7605 | * another thread is currently inside io_uring_enter() it might |
| 7606 | * need to grab the uring_lock to make progress. If we hold it |
| 7607 | * here across the drain wait, then we can deadlock. It's safe |
| 7608 | * to drop the mutex here, since no new references will come in |
| 7609 | * after we've killed the percpu ref. |
| 7610 | */ |
| 7611 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7612 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7613 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7614 | if (ret) { |
| 7615 | percpu_ref_resurrect(&ctx->refs); |
| 7616 | ret = -EINTR; |
| 7617 | goto out; |
| 7618 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7619 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7620 | |
| 7621 | switch (opcode) { |
| 7622 | case IORING_REGISTER_BUFFERS: |
| 7623 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 7624 | break; |
| 7625 | case IORING_UNREGISTER_BUFFERS: |
| 7626 | ret = -EINVAL; |
| 7627 | if (arg || nr_args) |
| 7628 | break; |
| 7629 | ret = io_sqe_buffer_unregister(ctx); |
| 7630 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7631 | case IORING_REGISTER_FILES: |
| 7632 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 7633 | break; |
| 7634 | case IORING_UNREGISTER_FILES: |
| 7635 | ret = -EINVAL; |
| 7636 | if (arg || nr_args) |
| 7637 | break; |
| 7638 | ret = io_sqe_files_unregister(ctx); |
| 7639 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7640 | case IORING_REGISTER_FILES_UPDATE: |
| 7641 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 7642 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7643 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7644 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7645 | ret = -EINVAL; |
| 7646 | if (nr_args != 1) |
| 7647 | break; |
| 7648 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 7649 | if (ret) |
| 7650 | break; |
| 7651 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 7652 | ctx->eventfd_async = 1; |
| 7653 | else |
| 7654 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 7655 | break; |
| 7656 | case IORING_UNREGISTER_EVENTFD: |
| 7657 | ret = -EINVAL; |
| 7658 | if (arg || nr_args) |
| 7659 | break; |
| 7660 | ret = io_eventfd_unregister(ctx); |
| 7661 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 7662 | case IORING_REGISTER_PROBE: |
| 7663 | ret = -EINVAL; |
| 7664 | if (!arg || nr_args > 256) |
| 7665 | break; |
| 7666 | ret = io_probe(ctx, arg, nr_args); |
| 7667 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7668 | case IORING_REGISTER_PERSONALITY: |
| 7669 | ret = -EINVAL; |
| 7670 | if (arg || nr_args) |
| 7671 | break; |
| 7672 | ret = io_register_personality(ctx); |
| 7673 | break; |
| 7674 | case IORING_UNREGISTER_PERSONALITY: |
| 7675 | ret = -EINVAL; |
| 7676 | if (arg) |
| 7677 | break; |
| 7678 | ret = io_unregister_personality(ctx, nr_args); |
| 7679 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7680 | default: |
| 7681 | ret = -EINVAL; |
| 7682 | break; |
| 7683 | } |
| 7684 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 7685 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7686 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7687 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 7688 | out: |
| 7689 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7690 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7691 | return ret; |
| 7692 | } |
| 7693 | |
| 7694 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 7695 | void __user *, arg, unsigned int, nr_args) |
| 7696 | { |
| 7697 | struct io_ring_ctx *ctx; |
| 7698 | long ret = -EBADF; |
| 7699 | struct fd f; |
| 7700 | |
| 7701 | f = fdget(fd); |
| 7702 | if (!f.file) |
| 7703 | return -EBADF; |
| 7704 | |
| 7705 | ret = -EOPNOTSUPP; |
| 7706 | if (f.file->f_op != &io_uring_fops) |
| 7707 | goto out_fput; |
| 7708 | |
| 7709 | ctx = f.file->private_data; |
| 7710 | |
| 7711 | mutex_lock(&ctx->uring_lock); |
| 7712 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 7713 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7714 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 7715 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 7716 | out_fput: |
| 7717 | fdput(f); |
| 7718 | return ret; |
| 7719 | } |
| 7720 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7721 | static int __init io_uring_init(void) |
| 7722 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7723 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 7724 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 7725 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 7726 | } while (0) |
| 7727 | |
| 7728 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 7729 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 7730 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 7731 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 7732 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 7733 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 7734 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 7735 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 7736 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 7737 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7738 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7739 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 7740 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 7741 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 7742 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 7743 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
| 7744 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); |
| 7745 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 7746 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 7747 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 7748 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 7749 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 7750 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 7751 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 7752 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7753 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7754 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 7755 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 7756 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 7757 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 7758 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7759 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7760 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 7761 | return 0; |
| 7762 | }; |
| 7763 | __initcall(io_uring_init); |