Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 47 | #include <net/compat.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 48 | #include <linux/refcount.h> |
| 49 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 50 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/sched/signal.h> |
| 53 | #include <linux/fs.h> |
| 54 | #include <linux/file.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/mm.h> |
| 57 | #include <linux/mman.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 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 | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 81 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 82 | #include <linux/io_uring.h> |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 83 | #include <linux/blk-cgroup.h> |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 84 | #include <linux/audit.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 86 | #define CREATE_TRACE_POINTS |
| 87 | #include <trace/events/io_uring.h> |
| 88 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 89 | #include <uapi/linux/io_uring.h> |
| 90 | |
| 91 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 92 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 93 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 94 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 95 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 99 | */ |
| 100 | #define IORING_FILE_TABLE_SHIFT 9 |
| 101 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 102 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 103 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 104 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 105 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 106 | |
| 107 | struct io_uring { |
| 108 | u32 head ____cacheline_aligned_in_smp; |
| 109 | u32 tail ____cacheline_aligned_in_smp; |
| 110 | }; |
| 111 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 112 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | * This data is shared with the application through the mmap at offsets |
| 114 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | * |
| 116 | * The offsets to the member fields are published through struct |
| 117 | * io_sqring_offsets when calling io_uring_setup. |
| 118 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Head and tail offsets into the ring; the offsets need to be |
| 122 | * masked to get valid indices. |
| 123 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 124 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 125 | * and the application controls tail of the sq ring and the head of the |
| 126 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 127 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 128 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 129 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 130 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | * ring_entries - 1) |
| 132 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 133 | u32 sq_ring_mask, cq_ring_mask; |
| 134 | /* Ring sizes (constant, power of 2) */ |
| 135 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Number of invalid entries dropped by the kernel due to |
| 138 | * invalid index stored in array |
| 139 | * |
| 140 | * Written by the kernel, shouldn't be modified by the |
| 141 | * application (i.e. get number of "new events" by comparing to |
| 142 | * cached value). |
| 143 | * |
| 144 | * After a new SQ head value was read by the application this |
| 145 | * counter includes all submissions that were dropped reaching |
| 146 | * the new SQ head (and possibly more). |
| 147 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 148 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 149 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 150 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 151 | * |
| 152 | * Written by the kernel, shouldn't be modified by the |
| 153 | * application. |
| 154 | * |
| 155 | * The application needs a full memory barrier before checking |
| 156 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 157 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 158 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 159 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 160 | * Runtime CQ flags |
| 161 | * |
| 162 | * Written by the application, shouldn't be modified by the |
| 163 | * kernel. |
| 164 | */ |
| 165 | u32 cq_flags; |
| 166 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 167 | * Number of completion events lost because the queue was full; |
| 168 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 169 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 170 | * the completion queue. |
| 171 | * |
| 172 | * Written by the kernel, shouldn't be modified by the |
| 173 | * application (i.e. get number of "new events" by comparing to |
| 174 | * cached value). |
| 175 | * |
| 176 | * As completion events come in out of order this counter is not |
| 177 | * ordered with any other data. |
| 178 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 179 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Ring buffer of completion events. |
| 182 | * |
| 183 | * The kernel writes completion events fresh every time they are |
| 184 | * produced, so the application is allowed to modify pending |
| 185 | * entries. |
| 186 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 187 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 190 | struct io_mapped_ubuf { |
| 191 | u64 ubuf; |
| 192 | size_t len; |
| 193 | struct bio_vec *bvec; |
| 194 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 195 | unsigned long acct_pages; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 198 | struct fixed_file_table { |
| 199 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 202 | struct fixed_file_ref_node { |
| 203 | struct percpu_ref refs; |
| 204 | struct list_head node; |
| 205 | struct list_head file_list; |
| 206 | struct fixed_file_data *file_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 207 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 208 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 209 | }; |
| 210 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 211 | struct fixed_file_data { |
| 212 | struct fixed_file_table *table; |
| 213 | struct io_ring_ctx *ctx; |
| 214 | |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 215 | struct fixed_file_ref_node *node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 216 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 217 | struct completion done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 218 | struct list_head ref_list; |
| 219 | spinlock_t lock; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 222 | struct io_buffer { |
| 223 | struct list_head list; |
| 224 | __u64 addr; |
| 225 | __s32 len; |
| 226 | __u16 bid; |
| 227 | }; |
| 228 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 229 | struct io_restriction { |
| 230 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 231 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 232 | u8 sqe_flags_allowed; |
| 233 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 234 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 235 | }; |
| 236 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 237 | struct io_sq_data { |
| 238 | refcount_t refs; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 239 | struct mutex lock; |
| 240 | |
| 241 | /* ctx's that are using this sqd */ |
| 242 | struct list_head ctx_list; |
| 243 | struct list_head ctx_new_list; |
| 244 | struct mutex ctx_lock; |
| 245 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 246 | struct task_struct *thread; |
| 247 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 248 | |
| 249 | unsigned sq_thread_idle; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 250 | }; |
| 251 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 252 | struct io_ring_ctx { |
| 253 | struct { |
| 254 | struct percpu_ref refs; |
| 255 | } ____cacheline_aligned_in_smp; |
| 256 | |
| 257 | struct { |
| 258 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 259 | unsigned int compat: 1; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 260 | unsigned int limit_mem: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 261 | unsigned int cq_overflow_flushed: 1; |
| 262 | unsigned int drain_next: 1; |
| 263 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 264 | unsigned int restricted: 1; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 265 | unsigned int sqo_dead: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 266 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 267 | /* |
| 268 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 269 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 270 | * |
| 271 | * This indirection could e.g. be used to assign fixed |
| 272 | * io_uring_sqe entries to operations and only submit them to |
| 273 | * the queue when needed. |
| 274 | * |
| 275 | * The kernel modifies neither the indices array nor the entries |
| 276 | * array. |
| 277 | */ |
| 278 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 279 | unsigned cached_sq_head; |
| 280 | unsigned sq_entries; |
| 281 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 282 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 283 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 284 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 285 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 286 | |
| 287 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 288 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 289 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 290 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 291 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 292 | } ____cacheline_aligned_in_smp; |
| 293 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 294 | struct io_rings *rings; |
| 295 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 296 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 297 | struct io_wq *io_wq; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * For SQPOLL usage - we hold a reference to the parent task, so we |
| 301 | * have access to the ->files |
| 302 | */ |
| 303 | struct task_struct *sqo_task; |
| 304 | |
| 305 | /* Only used for accounting purposes */ |
| 306 | struct mm_struct *mm_account; |
| 307 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 308 | #ifdef CONFIG_BLK_CGROUP |
| 309 | struct cgroup_subsys_state *sqo_blkcg_css; |
| 310 | #endif |
| 311 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 312 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 313 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 314 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 315 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 316 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 317 | /* |
| 318 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 319 | * readers must ensure that ->refs is alive as long as the file* is |
| 320 | * used. Only updated through io_uring_register(2). |
| 321 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 322 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 323 | unsigned nr_user_files; |
| 324 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 325 | /* if used, fixed mapped user buffers */ |
| 326 | unsigned nr_user_bufs; |
| 327 | struct io_mapped_ubuf *user_bufs; |
| 328 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 329 | struct user_struct *user; |
| 330 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 331 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 332 | |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 333 | #ifdef CONFIG_AUDIT |
| 334 | kuid_t loginuid; |
| 335 | unsigned int sessionid; |
| 336 | #endif |
| 337 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 338 | struct completion ref_comp; |
| 339 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 340 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 341 | /* if all else fails... */ |
| 342 | struct io_kiocb *fallback_req; |
| 343 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 344 | #if defined(CONFIG_UNIX) |
| 345 | struct socket *ring_sock; |
| 346 | #endif |
| 347 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 348 | struct idr io_buffer_idr; |
| 349 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 350 | struct idr personality_idr; |
| 351 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 352 | struct { |
| 353 | unsigned cached_cq_tail; |
| 354 | unsigned cq_entries; |
| 355 | unsigned cq_mask; |
| 356 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 357 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 358 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 359 | struct wait_queue_head cq_wait; |
| 360 | struct fasync_struct *cq_fasync; |
| 361 | struct eventfd_ctx *cq_ev_fd; |
| 362 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 363 | |
| 364 | struct { |
| 365 | struct mutex uring_lock; |
| 366 | wait_queue_head_t wait; |
| 367 | } ____cacheline_aligned_in_smp; |
| 368 | |
| 369 | struct { |
| 370 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 371 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 372 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 373 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 374 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 375 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 376 | * manipulate the list, hence no extra locking is needed there. |
| 377 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 378 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 379 | struct hlist_head *cancel_hash; |
| 380 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 381 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 382 | |
| 383 | spinlock_t inflight_lock; |
| 384 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 385 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 386 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 387 | struct delayed_work file_put_work; |
| 388 | struct llist_head file_put_llist; |
| 389 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 390 | struct work_struct exit_work; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 391 | struct io_restriction restrictions; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 394 | /* |
| 395 | * First field must be the file pointer in all the |
| 396 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 397 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 398 | struct io_poll_iocb { |
| 399 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 400 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 401 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 402 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 403 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 404 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 407 | struct io_poll_remove { |
| 408 | struct file *file; |
| 409 | u64 addr; |
| 410 | }; |
| 411 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 412 | struct io_close { |
| 413 | struct file *file; |
| 414 | struct file *put_file; |
| 415 | int fd; |
| 416 | }; |
| 417 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 418 | struct io_timeout_data { |
| 419 | struct io_kiocb *req; |
| 420 | struct hrtimer timer; |
| 421 | struct timespec64 ts; |
| 422 | enum hrtimer_mode mode; |
| 423 | }; |
| 424 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 425 | struct io_accept { |
| 426 | struct file *file; |
| 427 | struct sockaddr __user *addr; |
| 428 | int __user *addr_len; |
| 429 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 430 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | struct io_sync { |
| 434 | struct file *file; |
| 435 | loff_t len; |
| 436 | loff_t off; |
| 437 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 438 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 441 | struct io_cancel { |
| 442 | struct file *file; |
| 443 | u64 addr; |
| 444 | }; |
| 445 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 446 | struct io_timeout { |
| 447 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 448 | u32 off; |
| 449 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 450 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 451 | /* head of the link, used by linked timeouts only */ |
| 452 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 455 | struct io_timeout_rem { |
| 456 | struct file *file; |
| 457 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 458 | |
| 459 | /* timeout update */ |
| 460 | struct timespec64 ts; |
| 461 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 462 | }; |
| 463 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 464 | struct io_rw { |
| 465 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 466 | struct kiocb kiocb; |
| 467 | u64 addr; |
| 468 | u64 len; |
| 469 | }; |
| 470 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 471 | struct io_connect { |
| 472 | struct file *file; |
| 473 | struct sockaddr __user *addr; |
| 474 | int addr_len; |
| 475 | }; |
| 476 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 477 | struct io_sr_msg { |
| 478 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 479 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 480 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 481 | void __user *buf; |
| 482 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 483 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 484 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 485 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 486 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 489 | struct io_open { |
| 490 | struct file *file; |
| 491 | int dfd; |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 492 | bool ignore_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 493 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 494 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 495 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 496 | }; |
| 497 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 498 | struct io_files_update { |
| 499 | struct file *file; |
| 500 | u64 arg; |
| 501 | u32 nr_args; |
| 502 | u32 offset; |
| 503 | }; |
| 504 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 505 | struct io_fadvise { |
| 506 | struct file *file; |
| 507 | u64 offset; |
| 508 | u32 len; |
| 509 | u32 advice; |
| 510 | }; |
| 511 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 512 | struct io_madvise { |
| 513 | struct file *file; |
| 514 | u64 addr; |
| 515 | u32 len; |
| 516 | u32 advice; |
| 517 | }; |
| 518 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 519 | struct io_epoll { |
| 520 | struct file *file; |
| 521 | int epfd; |
| 522 | int op; |
| 523 | int fd; |
| 524 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 525 | }; |
| 526 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 527 | struct io_splice { |
| 528 | struct file *file_out; |
| 529 | struct file *file_in; |
| 530 | loff_t off_out; |
| 531 | loff_t off_in; |
| 532 | u64 len; |
| 533 | unsigned int flags; |
| 534 | }; |
| 535 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 536 | struct io_provide_buf { |
| 537 | struct file *file; |
| 538 | __u64 addr; |
| 539 | __s32 len; |
| 540 | __u32 bgid; |
| 541 | __u16 nbufs; |
| 542 | __u16 bid; |
| 543 | }; |
| 544 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 545 | struct io_statx { |
| 546 | struct file *file; |
| 547 | int dfd; |
| 548 | unsigned int mask; |
| 549 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 550 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 551 | struct statx __user *buffer; |
| 552 | }; |
| 553 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 554 | struct io_shutdown { |
| 555 | struct file *file; |
| 556 | int how; |
| 557 | }; |
| 558 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 559 | struct io_rename { |
| 560 | struct file *file; |
| 561 | int old_dfd; |
| 562 | int new_dfd; |
| 563 | struct filename *oldpath; |
| 564 | struct filename *newpath; |
| 565 | int flags; |
| 566 | }; |
| 567 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 568 | struct io_unlink { |
| 569 | struct file *file; |
| 570 | int dfd; |
| 571 | int flags; |
| 572 | struct filename *filename; |
| 573 | }; |
| 574 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 575 | struct io_completion { |
| 576 | struct file *file; |
| 577 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 578 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 579 | }; |
| 580 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 581 | struct io_async_connect { |
| 582 | struct sockaddr_storage address; |
| 583 | }; |
| 584 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 585 | struct io_async_msghdr { |
| 586 | struct iovec fast_iov[UIO_FASTIOV]; |
| 587 | struct iovec *iov; |
| 588 | struct sockaddr __user *uaddr; |
| 589 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 590 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 591 | }; |
| 592 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 593 | struct io_async_rw { |
| 594 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 595 | const struct iovec *free_iovec; |
| 596 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 597 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 598 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 599 | }; |
| 600 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 601 | enum { |
| 602 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 603 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 604 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 605 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 606 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 607 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 608 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 609 | REQ_F_FAIL_LINK_BIT, |
| 610 | REQ_F_INFLIGHT_BIT, |
| 611 | REQ_F_CUR_POS_BIT, |
| 612 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 613 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 614 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 615 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 616 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 617 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 618 | REQ_F_NO_FILE_TABLE_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 619 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 620 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 621 | |
| 622 | /* not a real bit, just to check we're not overflowing the space */ |
| 623 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | enum { |
| 627 | /* ctx owns file */ |
| 628 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 629 | /* drain existing IO first */ |
| 630 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 631 | /* linked sqes */ |
| 632 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 633 | /* doesn't sever on completion < 0 */ |
| 634 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 635 | /* IOSQE_ASYNC */ |
| 636 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 637 | /* IOSQE_BUFFER_SELECT */ |
| 638 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 639 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 640 | /* fail rest of links */ |
| 641 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 642 | /* on inflight list */ |
| 643 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 644 | /* read/write uses file position */ |
| 645 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 646 | /* must not punt to workers */ |
| 647 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 648 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 649 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 650 | /* regular file */ |
| 651 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 652 | /* needs cleanup */ |
| 653 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 654 | /* already went through poll handler */ |
| 655 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 656 | /* buffer already selected */ |
| 657 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 658 | /* doesn't need file table for this request */ |
| 659 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 660 | /* io_wq_work is initialized */ |
| 661 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 662 | /* linked timeout is active, i.e. prepared by link's head */ |
| 663 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | struct async_poll { |
| 667 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 668 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 669 | }; |
| 670 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 671 | /* |
| 672 | * NOTE! Each of the iocb union members has the file pointer |
| 673 | * as the first entry in their struct definition. So you can |
| 674 | * access the file pointer through any of the sub-structs, |
| 675 | * or directly as just 'ki_filp' in this struct. |
| 676 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 677 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 678 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 679 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 680 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 681 | struct io_poll_iocb poll; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 682 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 683 | struct io_accept accept; |
| 684 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 685 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 686 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 687 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 688 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 689 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 690 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 691 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 692 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 693 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 694 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 695 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 696 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 697 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 698 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 699 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 700 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 701 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 702 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 703 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 704 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 705 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 706 | /* opcode allocated if it needs to store data for async defer */ |
| 707 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 708 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 709 | /* polled IO has completed */ |
| 710 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 711 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 712 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 713 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 714 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 715 | struct io_ring_ctx *ctx; |
| 716 | unsigned int flags; |
| 717 | refcount_t refs; |
| 718 | struct task_struct *task; |
| 719 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 720 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 721 | struct io_kiocb *link; |
Pavel Begunkov | 0415767 | 2020-10-27 23:25:38 +0000 | [diff] [blame] | 722 | struct percpu_ref *fixed_file_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 723 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 724 | /* |
| 725 | * 1. used with ctx->iopoll_list with reads/writes |
| 726 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 727 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 728 | struct list_head inflight_entry; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 729 | struct callback_head task_work; |
| 730 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 731 | struct hlist_node hash_node; |
| 732 | struct async_poll *apoll; |
| 733 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 734 | }; |
| 735 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 736 | struct io_defer_entry { |
| 737 | struct list_head list; |
| 738 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 739 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 740 | }; |
| 741 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 742 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 743 | |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 744 | struct io_comp_state { |
| 745 | unsigned int nr; |
| 746 | struct list_head list; |
| 747 | struct io_ring_ctx *ctx; |
| 748 | }; |
| 749 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 750 | struct io_submit_state { |
| 751 | struct blk_plug plug; |
| 752 | |
| 753 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 754 | * io_kiocb alloc cache |
| 755 | */ |
| 756 | void *reqs[IO_IOPOLL_BATCH]; |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 757 | unsigned int free_reqs; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 758 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 759 | bool plug_started; |
| 760 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 761 | /* |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 762 | * Batch completion logic |
| 763 | */ |
| 764 | struct io_comp_state comp; |
| 765 | |
| 766 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 767 | * File reference cache |
| 768 | */ |
| 769 | struct file *file; |
| 770 | unsigned int fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 771 | unsigned int file_refs; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 772 | unsigned int ios_left; |
| 773 | }; |
| 774 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 775 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 776 | /* needs req->file assigned */ |
| 777 | unsigned needs_file : 1; |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 778 | /* don't fail if file grab fails */ |
| 779 | unsigned needs_file_no_error : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 780 | /* hash wq insertion if file is a regular file */ |
| 781 | unsigned hash_reg_file : 1; |
| 782 | /* unbound wq insertion if file is a non-regular file */ |
| 783 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 784 | /* opcode is not supported by this kernel */ |
| 785 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 786 | /* set if opcode supports polled "wait" */ |
| 787 | unsigned pollin : 1; |
| 788 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 789 | /* op supports buffer selection */ |
| 790 | unsigned buffer_select : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 791 | /* must always have async data allocated */ |
| 792 | unsigned needs_async_data : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 793 | /* should block plug */ |
| 794 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 795 | /* size of async data needed, if any */ |
| 796 | unsigned short async_size; |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 797 | unsigned work_flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 800 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 801 | [IORING_OP_NOP] = {}, |
| 802 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 803 | .needs_file = 1, |
| 804 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 805 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 806 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 807 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 808 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 809 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 810 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 811 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 812 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 813 | .needs_file = 1, |
| 814 | .hash_reg_file = 1, |
| 815 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 816 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 817 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 818 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 819 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 820 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 821 | IO_WQ_WORK_FSIZE, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 823 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 824 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 825 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 826 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 827 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 828 | .needs_file = 1, |
| 829 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 830 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 831 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 832 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 833 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 834 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 835 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 836 | .needs_file = 1, |
| 837 | .hash_reg_file = 1, |
| 838 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 839 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 840 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 841 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 842 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
| 843 | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 844 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 845 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 846 | .needs_file = 1, |
| 847 | .unbound_nonreg_file = 1, |
| 848 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 849 | [IORING_OP_POLL_REMOVE] = {}, |
| 850 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 851 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 852 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 853 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 854 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 855 | .needs_file = 1, |
| 856 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 857 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 858 | .needs_async_data = 1, |
| 859 | .async_size = sizeof(struct io_async_msghdr), |
Pavel Begunkov | 10cad2c | 2020-11-07 13:20:39 +0000 | [diff] [blame] | 860 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 861 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 862 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 863 | .needs_file = 1, |
| 864 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 865 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 866 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 867 | .needs_async_data = 1, |
| 868 | .async_size = sizeof(struct io_async_msghdr), |
Pavel Begunkov | 10cad2c | 2020-11-07 13:20:39 +0000 | [diff] [blame] | 869 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 870 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 871 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 872 | .needs_async_data = 1, |
| 873 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 874 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 875 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 876 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 877 | /* used by timeout updates' prep() */ |
| 878 | .work_flags = IO_WQ_WORK_MM, |
| 879 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 880 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 881 | .needs_file = 1, |
| 882 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 883 | .pollin = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 884 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 885 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 886 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 887 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 888 | .needs_async_data = 1, |
| 889 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 890 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 891 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 892 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 893 | .needs_file = 1, |
| 894 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 895 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 896 | .needs_async_data = 1, |
| 897 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 898 | .work_flags = IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 899 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 900 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 901 | .needs_file = 1, |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 902 | .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 903 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 904 | [IORING_OP_OPENAT] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 905 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 906 | IO_WQ_WORK_FS | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 907 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 908 | [IORING_OP_CLOSE] = { |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 909 | .needs_file = 1, |
| 910 | .needs_file_no_error = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 911 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 912 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 913 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 914 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 915 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 916 | [IORING_OP_STATX] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 917 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
| 918 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 919 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 920 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 921 | .needs_file = 1, |
| 922 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 923 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 924 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 925 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 926 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 927 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 928 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 929 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 930 | .needs_file = 1, |
| 931 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 932 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 933 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 934 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 935 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| 936 | IO_WQ_WORK_FSIZE, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 937 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 938 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 939 | .needs_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 940 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 941 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 942 | [IORING_OP_MADVISE] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 943 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 944 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 945 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 946 | .needs_file = 1, |
| 947 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 948 | .pollout = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 949 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 950 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 951 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 952 | .needs_file = 1, |
| 953 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 954 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 955 | .buffer_select = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 956 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 957 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 958 | [IORING_OP_OPENAT2] = { |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 959 | .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 960 | IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 961 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 962 | [IORING_OP_EPOLL_CTL] = { |
| 963 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 964 | .work_flags = IO_WQ_WORK_FILES, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 965 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 966 | [IORING_OP_SPLICE] = { |
| 967 | .needs_file = 1, |
| 968 | .hash_reg_file = 1, |
| 969 | .unbound_nonreg_file = 1, |
Jens Axboe | 0f20376 | 2020-10-14 09:23:55 -0600 | [diff] [blame] | 970 | .work_flags = IO_WQ_WORK_BLKCG, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 971 | }, |
| 972 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 973 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 974 | [IORING_OP_TEE] = { |
| 975 | .needs_file = 1, |
| 976 | .hash_reg_file = 1, |
| 977 | .unbound_nonreg_file = 1, |
| 978 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 979 | [IORING_OP_SHUTDOWN] = { |
| 980 | .needs_file = 1, |
| 981 | }, |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 982 | [IORING_OP_RENAMEAT] = { |
| 983 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 984 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 985 | }, |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 986 | [IORING_OP_UNLINKAT] = { |
| 987 | .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | |
| 988 | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| 989 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 990 | }; |
| 991 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 992 | enum io_mem_account { |
| 993 | ACCT_LOCKED, |
| 994 | ACCT_PINNED, |
| 995 | }; |
| 996 | |
Pavel Begunkov | 90df085 | 2021-01-04 20:43:30 +0000 | [diff] [blame] | 997 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 998 | struct task_struct *task); |
| 999 | |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1000 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node); |
| 1001 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 1002 | struct io_ring_ctx *ctx); |
| 1003 | |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 1004 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 1005 | struct io_comp_state *cs); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1006 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1007 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1008 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1009 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1010 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1011 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1012 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1013 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 1014 | struct io_uring_files_update *ip, |
| 1015 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1016 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1017 | static struct file *io_file_get(struct io_submit_state *state, |
| 1018 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 1019 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
Jens Axboe | 4349f30 | 2020-07-09 15:07:01 -0600 | [diff] [blame] | 1020 | static void io_file_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1021 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 1022 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1023 | struct iovec **iovec, struct iov_iter *iter, |
| 1024 | bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1025 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1026 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1027 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1028 | static void io_req_drop_files(struct io_kiocb *req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1029 | |
| 1030 | static struct kmem_cache *req_cachep; |
| 1031 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1032 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1033 | |
| 1034 | struct sock *io_uring_get_socket(struct file *file) |
| 1035 | { |
| 1036 | #if defined(CONFIG_UNIX) |
| 1037 | if (file->f_op == &io_uring_fops) { |
| 1038 | struct io_ring_ctx *ctx = file->private_data; |
| 1039 | |
| 1040 | return ctx->ring_sock->sk; |
| 1041 | } |
| 1042 | #endif |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1046 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1047 | #define io_for_each_link(pos, head) \ |
| 1048 | for (pos = (head); pos; pos = pos->link) |
| 1049 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1050 | static inline void io_clean_op(struct io_kiocb *req) |
| 1051 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1052 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1053 | __io_clean_op(req); |
| 1054 | } |
| 1055 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1056 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1057 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1058 | struct io_ring_ctx *ctx = req->ctx; |
| 1059 | |
| 1060 | if (!req->fixed_file_refs) { |
| 1061 | req->fixed_file_refs = &ctx->file_data->node->refs; |
| 1062 | percpu_ref_get(req->fixed_file_refs); |
| 1063 | } |
| 1064 | } |
| 1065 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1066 | static bool io_match_task(struct io_kiocb *head, |
| 1067 | struct task_struct *task, |
| 1068 | struct files_struct *files) |
| 1069 | { |
| 1070 | struct io_kiocb *req; |
| 1071 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame^] | 1072 | if (task && head->task != task) { |
| 1073 | /* in terms of cancelation, always match if req task is dead */ |
| 1074 | if (head->task->flags & PF_EXITING) |
| 1075 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1076 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame^] | 1077 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1078 | if (!files) |
| 1079 | return true; |
| 1080 | |
| 1081 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1082 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1083 | continue; |
| 1084 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1085 | return true; |
| 1086 | if ((req->work.flags & IO_WQ_WORK_FILES) && |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1087 | req->work.identity->files == files) |
| 1088 | return true; |
| 1089 | } |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1093 | static void io_sq_thread_drop_mm_files(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1094 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1095 | struct files_struct *files = current->files; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1096 | struct mm_struct *mm = current->mm; |
| 1097 | |
| 1098 | if (mm) { |
| 1099 | kthread_unuse_mm(mm); |
| 1100 | mmput(mm); |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1101 | current->mm = NULL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1102 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1103 | if (files) { |
| 1104 | struct nsproxy *nsproxy = current->nsproxy; |
| 1105 | |
| 1106 | task_lock(current); |
| 1107 | current->files = NULL; |
| 1108 | current->nsproxy = NULL; |
| 1109 | task_unlock(current); |
| 1110 | put_files_struct(files); |
| 1111 | put_nsproxy(nsproxy); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1115 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1116 | { |
Pavel Begunkov | 621fadc | 2021-01-11 04:00:31 +0000 | [diff] [blame] | 1117 | if (current->flags & PF_EXITING) |
| 1118 | return -EFAULT; |
| 1119 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1120 | if (!current->files) { |
| 1121 | struct files_struct *files; |
| 1122 | struct nsproxy *nsproxy; |
| 1123 | |
| 1124 | task_lock(ctx->sqo_task); |
| 1125 | files = ctx->sqo_task->files; |
| 1126 | if (!files) { |
| 1127 | task_unlock(ctx->sqo_task); |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1128 | return -EOWNERDEAD; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1129 | } |
| 1130 | atomic_inc(&files->count); |
| 1131 | get_nsproxy(ctx->sqo_task->nsproxy); |
| 1132 | nsproxy = ctx->sqo_task->nsproxy; |
| 1133 | task_unlock(ctx->sqo_task); |
| 1134 | |
| 1135 | task_lock(current); |
| 1136 | current->files = files; |
| 1137 | current->nsproxy = nsproxy; |
| 1138 | task_unlock(current); |
| 1139 | } |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1140 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 1144 | { |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1145 | struct mm_struct *mm; |
| 1146 | |
Pavel Begunkov | 621fadc | 2021-01-11 04:00:31 +0000 | [diff] [blame] | 1147 | if (current->flags & PF_EXITING) |
| 1148 | return -EFAULT; |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1149 | if (current->mm) |
| 1150 | return 0; |
| 1151 | |
| 1152 | /* Should never happen */ |
| 1153 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) |
| 1154 | return -EFAULT; |
| 1155 | |
| 1156 | task_lock(ctx->sqo_task); |
| 1157 | mm = ctx->sqo_task->mm; |
| 1158 | if (unlikely(!mm || !mmget_not_zero(mm))) |
| 1159 | mm = NULL; |
| 1160 | task_unlock(ctx->sqo_task); |
| 1161 | |
| 1162 | if (mm) { |
| 1163 | kthread_use_mm(mm); |
| 1164 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1165 | } |
| 1166 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1167 | return -EFAULT; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1168 | } |
| 1169 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1170 | static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1171 | struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1172 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1173 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1174 | int ret; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1175 | |
| 1176 | if (def->work_flags & IO_WQ_WORK_MM) { |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1177 | ret = __io_sq_thread_acquire_mm(ctx); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1178 | if (unlikely(ret)) |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1182 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
| 1183 | ret = __io_sq_thread_acquire_files(ctx); |
| 1184 | if (unlikely(ret)) |
| 1185 | return ret; |
| 1186 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1187 | |
| 1188 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1189 | } |
| 1190 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1191 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
| 1192 | struct cgroup_subsys_state **cur_css) |
| 1193 | |
| 1194 | { |
| 1195 | #ifdef CONFIG_BLK_CGROUP |
| 1196 | /* puts the old one when swapping */ |
| 1197 | if (*cur_css != ctx->sqo_blkcg_css) { |
| 1198 | kthread_associate_blkcg(ctx->sqo_blkcg_css); |
| 1199 | *cur_css = ctx->sqo_blkcg_css; |
| 1200 | } |
| 1201 | #endif |
| 1202 | } |
| 1203 | |
| 1204 | static void io_sq_thread_unassociate_blkcg(void) |
| 1205 | { |
| 1206 | #ifdef CONFIG_BLK_CGROUP |
| 1207 | kthread_associate_blkcg(NULL); |
| 1208 | #endif |
| 1209 | } |
| 1210 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1211 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1212 | { |
| 1213 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1214 | req->flags |= REQ_F_FAIL_LINK; |
| 1215 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1216 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1217 | /* |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1218 | * None of these are dereferenced, they are simply used to check if any of |
| 1219 | * them have changed. If we're under current and check they are still the |
| 1220 | * same, we're fine to grab references to them for actual out-of-line use. |
| 1221 | */ |
| 1222 | static void io_init_identity(struct io_identity *id) |
| 1223 | { |
| 1224 | id->files = current->files; |
| 1225 | id->mm = current->mm; |
| 1226 | #ifdef CONFIG_BLK_CGROUP |
| 1227 | rcu_read_lock(); |
| 1228 | id->blkcg_css = blkcg_css(); |
| 1229 | rcu_read_unlock(); |
| 1230 | #endif |
| 1231 | id->creds = current_cred(); |
| 1232 | id->nsproxy = current->nsproxy; |
| 1233 | id->fs = current->fs; |
| 1234 | id->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1235 | #ifdef CONFIG_AUDIT |
| 1236 | id->loginuid = current->loginuid; |
| 1237 | id->sessionid = current->sessionid; |
| 1238 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1239 | refcount_set(&id->count, 1); |
| 1240 | } |
| 1241 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1242 | static inline void __io_req_init_async(struct io_kiocb *req) |
| 1243 | { |
| 1244 | memset(&req->work, 0, sizeof(req->work)); |
| 1245 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1246 | } |
| 1247 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1248 | /* |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1249 | * Note: must call io_req_init_async() for the first time you |
| 1250 | * touch any members of io_wq_work. |
| 1251 | */ |
| 1252 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1253 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1254 | struct io_uring_task *tctx = current->io_uring; |
| 1255 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1256 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1257 | return; |
| 1258 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1259 | __io_req_init_async(req); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1260 | |
| 1261 | /* Grab a ref if this isn't our static identity */ |
| 1262 | req->work.identity = tctx->identity; |
| 1263 | if (tctx->identity != &tctx->__identity) |
| 1264 | refcount_inc(&req->work.identity->count); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 1267 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| 1268 | { |
| 1269 | return ctx->flags & IORING_SETUP_SQPOLL; |
| 1270 | } |
| 1271 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1272 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1273 | { |
| 1274 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1275 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1276 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1279 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1280 | { |
| 1281 | return !req->timeout.off; |
| 1282 | } |
| 1283 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1284 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1285 | { |
| 1286 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1287 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1288 | |
| 1289 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1290 | if (!ctx) |
| 1291 | return NULL; |
| 1292 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1293 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 1294 | if (!ctx->fallback_req) |
| 1295 | goto err; |
| 1296 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1297 | /* |
| 1298 | * Use 5 bits less than the max cq entries, that should give us around |
| 1299 | * 32 entries per hash list if totally full and uniformly spread. |
| 1300 | */ |
| 1301 | hash_bits = ilog2(p->cq_entries); |
| 1302 | hash_bits -= 5; |
| 1303 | if (hash_bits <= 0) |
| 1304 | hash_bits = 1; |
| 1305 | ctx->cancel_hash_bits = hash_bits; |
| 1306 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1307 | GFP_KERNEL); |
| 1308 | if (!ctx->cancel_hash) |
| 1309 | goto err; |
| 1310 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1311 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1312 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1313 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1314 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1315 | |
| 1316 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1317 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1318 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1319 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1320 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1321 | init_completion(&ctx->ref_comp); |
| 1322 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1323 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1324 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1325 | mutex_init(&ctx->uring_lock); |
| 1326 | init_waitqueue_head(&ctx->wait); |
| 1327 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1328 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1329 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1330 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1331 | spin_lock_init(&ctx->inflight_lock); |
| 1332 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1333 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| 1334 | init_llist_head(&ctx->file_put_llist); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1335 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1336 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1337 | if (ctx->fallback_req) |
| 1338 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1339 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1340 | kfree(ctx); |
| 1341 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1344 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1345 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1346 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1347 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1348 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1349 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1350 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1351 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1352 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1353 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1354 | } |
| 1355 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1356 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1357 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1358 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1359 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1360 | /* order cqe stores with ring update */ |
| 1361 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1364 | static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1365 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1366 | if (req->work.identity == &tctx->__identity) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1367 | return; |
| 1368 | if (refcount_dec_and_test(&req->work.identity->count)) |
| 1369 | kfree(req->work.identity); |
| 1370 | } |
| 1371 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1372 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1373 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1374 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1375 | return; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1376 | |
| 1377 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1378 | |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1379 | if (req->work.flags & IO_WQ_WORK_MM) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1380 | mmdrop(req->work.identity->mm); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1381 | req->work.flags &= ~IO_WQ_WORK_MM; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1382 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1383 | #ifdef CONFIG_BLK_CGROUP |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1384 | if (req->work.flags & IO_WQ_WORK_BLKCG) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1385 | css_put(req->work.identity->blkcg_css); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1386 | req->work.flags &= ~IO_WQ_WORK_BLKCG; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1387 | } |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1388 | #endif |
| 1389 | if (req->work.flags & IO_WQ_WORK_CREDS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1390 | put_cred(req->work.identity->creds); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1391 | req->work.flags &= ~IO_WQ_WORK_CREDS; |
| 1392 | } |
| 1393 | if (req->work.flags & IO_WQ_WORK_FS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1394 | struct fs_struct *fs = req->work.identity->fs; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1395 | |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1396 | spin_lock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1397 | if (--fs->users) |
| 1398 | fs = NULL; |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1399 | spin_unlock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1400 | if (fs) |
| 1401 | free_fs_struct(fs); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1402 | req->work.flags &= ~IO_WQ_WORK_FS; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1403 | } |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1404 | if (req->flags & REQ_F_INFLIGHT) |
| 1405 | io_req_drop_files(req); |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1406 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1407 | io_put_identity(req->task->io_uring, req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * Create a private copy of io_identity, since some fields don't match |
| 1412 | * the current context. |
| 1413 | */ |
| 1414 | static bool io_identity_cow(struct io_kiocb *req) |
| 1415 | { |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1416 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1417 | const struct cred *creds = NULL; |
| 1418 | struct io_identity *id; |
| 1419 | |
| 1420 | if (req->work.flags & IO_WQ_WORK_CREDS) |
| 1421 | creds = req->work.identity->creds; |
| 1422 | |
| 1423 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); |
| 1424 | if (unlikely(!id)) { |
| 1425 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * We can safely just re-init the creds we copied Either the field |
| 1431 | * matches the current one, or we haven't grabbed it yet. The only |
| 1432 | * exception is ->creds, through registered personalities, so handle |
| 1433 | * that one separately. |
| 1434 | */ |
| 1435 | io_init_identity(id); |
| 1436 | if (creds) |
Pavel Begunkov | e8c954d | 2020-12-06 22:22:46 +0000 | [diff] [blame] | 1437 | id->creds = creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1438 | |
| 1439 | /* add one for this request */ |
| 1440 | refcount_inc(&id->count); |
| 1441 | |
Jens Axboe | cb8a8ae | 2020-11-03 12:19:07 -0700 | [diff] [blame] | 1442 | /* drop tctx and req identity references, if needed */ |
| 1443 | if (tctx->identity != &tctx->__identity && |
| 1444 | refcount_dec_and_test(&tctx->identity->count)) |
| 1445 | kfree(tctx->identity); |
| 1446 | if (req->work.identity != &tctx->__identity && |
| 1447 | refcount_dec_and_test(&req->work.identity->count)) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1448 | kfree(req->work.identity); |
| 1449 | |
| 1450 | req->work.identity = id; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1451 | tctx->identity = id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1452 | return true; |
| 1453 | } |
| 1454 | |
| 1455 | static bool io_grab_identity(struct io_kiocb *req) |
| 1456 | { |
| 1457 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1458 | struct io_identity *id = req->work.identity; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1459 | struct io_ring_ctx *ctx = req->ctx; |
| 1460 | |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 1461 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
| 1462 | if (id->fsize != rlimit(RLIMIT_FSIZE)) |
| 1463 | return false; |
| 1464 | req->work.flags |= IO_WQ_WORK_FSIZE; |
| 1465 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1466 | #ifdef CONFIG_BLK_CGROUP |
| 1467 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
| 1468 | (def->work_flags & IO_WQ_WORK_BLKCG)) { |
| 1469 | rcu_read_lock(); |
| 1470 | if (id->blkcg_css != blkcg_css()) { |
| 1471 | rcu_read_unlock(); |
| 1472 | return false; |
| 1473 | } |
| 1474 | /* |
| 1475 | * This should be rare, either the cgroup is dying or the task |
| 1476 | * is moving cgroups. Just punt to root for the handful of ios. |
| 1477 | */ |
| 1478 | if (css_tryget_online(id->blkcg_css)) |
| 1479 | req->work.flags |= IO_WQ_WORK_BLKCG; |
| 1480 | rcu_read_unlock(); |
| 1481 | } |
| 1482 | #endif |
| 1483 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
| 1484 | if (id->creds != current_cred()) |
| 1485 | return false; |
| 1486 | get_cred(id->creds); |
| 1487 | req->work.flags |= IO_WQ_WORK_CREDS; |
| 1488 | } |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1489 | #ifdef CONFIG_AUDIT |
| 1490 | if (!uid_eq(current->loginuid, id->loginuid) || |
| 1491 | current->sessionid != id->sessionid) |
| 1492 | return false; |
| 1493 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1494 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
| 1495 | (def->work_flags & IO_WQ_WORK_FS)) { |
| 1496 | if (current->fs != id->fs) |
| 1497 | return false; |
| 1498 | spin_lock(&id->fs->lock); |
| 1499 | if (!id->fs->in_exec) { |
| 1500 | id->fs->users++; |
| 1501 | req->work.flags |= IO_WQ_WORK_FS; |
| 1502 | } else { |
| 1503 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1504 | } |
| 1505 | spin_unlock(¤t->fs->lock); |
| 1506 | } |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1507 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
| 1508 | (def->work_flags & IO_WQ_WORK_FILES) && |
| 1509 | !(req->flags & REQ_F_NO_FILE_TABLE)) { |
| 1510 | if (id->files != current->files || |
| 1511 | id->nsproxy != current->nsproxy) |
| 1512 | return false; |
| 1513 | atomic_inc(&id->files->count); |
| 1514 | get_nsproxy(id->nsproxy); |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1515 | |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1516 | if (!(req->flags & REQ_F_INFLIGHT)) { |
| 1517 | req->flags |= REQ_F_INFLIGHT; |
| 1518 | |
| 1519 | spin_lock_irq(&ctx->inflight_lock); |
| 1520 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1521 | spin_unlock_irq(&ctx->inflight_lock); |
| 1522 | } |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1523 | req->work.flags |= IO_WQ_WORK_FILES; |
| 1524 | } |
Jens Axboe | 7778877 | 2020-12-29 10:50:46 -0700 | [diff] [blame] | 1525 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
| 1526 | (def->work_flags & IO_WQ_WORK_MM)) { |
| 1527 | if (id->mm != current->mm) |
| 1528 | return false; |
| 1529 | mmgrab(id->mm); |
| 1530 | req->work.flags |= IO_WQ_WORK_MM; |
| 1531 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1532 | |
| 1533 | return true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1534 | } |
| 1535 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1536 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1537 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1538 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1539 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1540 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1541 | io_req_init_async(req); |
| 1542 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1543 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1544 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1545 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1546 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1547 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1548 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1549 | } else { |
| 1550 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1551 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1552 | } |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1553 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1554 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
| 1555 | if (io_grab_identity(req)) |
| 1556 | return; |
| 1557 | |
| 1558 | if (!io_identity_cow(req)) |
| 1559 | return; |
| 1560 | |
| 1561 | /* can't fail at this point */ |
| 1562 | if (!io_grab_identity(req)) |
| 1563 | WARN_ON(1); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1564 | } |
| 1565 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1566 | static void io_prep_async_link(struct io_kiocb *req) |
| 1567 | { |
| 1568 | struct io_kiocb *cur; |
| 1569 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1570 | io_for_each_link(cur, req) |
| 1571 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1572 | } |
| 1573 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1574 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1575 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1576 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1577 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1578 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1579 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1580 | &req->work, req->flags); |
| 1581 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1582 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1583 | } |
| 1584 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1585 | static void io_queue_async_work(struct io_kiocb *req) |
| 1586 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1587 | struct io_kiocb *link; |
| 1588 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1589 | /* init ->work of the whole link before punting */ |
| 1590 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1591 | link = __io_queue_async_work(req); |
| 1592 | |
| 1593 | if (link) |
| 1594 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1595 | } |
| 1596 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1597 | static void io_kill_timeout(struct io_kiocb *req) |
| 1598 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1599 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1600 | int ret; |
| 1601 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1602 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1603 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1604 | atomic_set(&req->ctx->cq_timeouts, |
| 1605 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1606 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1607 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1608 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1612 | /* |
| 1613 | * Returns true if we found and killed one or more timeouts |
| 1614 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1615 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1616 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1617 | { |
| 1618 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1619 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1620 | |
| 1621 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1622 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1623 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1624 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1625 | canceled++; |
| 1626 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1627 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1628 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1629 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1630 | } |
| 1631 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1632 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1633 | { |
| 1634 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1635 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1636 | struct io_defer_entry, list); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1637 | struct io_kiocb *link; |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1638 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1639 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1640 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1641 | list_del_init(&de->list); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1642 | /* punt-init is done before queueing for defer */ |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1643 | link = __io_queue_async_work(de->req); |
| 1644 | if (link) { |
| 1645 | __io_queue_linked_timeout(link); |
| 1646 | /* drop submission reference */ |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1647 | io_put_req_deferred(link, 1); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1648 | } |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1649 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1650 | } while (!list_empty(&ctx->defer_list)); |
| 1651 | } |
| 1652 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1653 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1654 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1655 | u32 seq; |
| 1656 | |
| 1657 | if (list_empty(&ctx->timeout_list)) |
| 1658 | return; |
| 1659 | |
| 1660 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1661 | |
| 1662 | do { |
| 1663 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1664 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1665 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1666 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1667 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1668 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1669 | |
| 1670 | /* |
| 1671 | * Since seq can easily wrap around over time, subtract |
| 1672 | * the last seq at which timeouts were flushed before comparing. |
| 1673 | * Assuming not more than 2^31-1 events have happened since, |
| 1674 | * these subtractions won't have wrapped, so we can check if |
| 1675 | * target is in [last_seq, current_seq] by comparing the two. |
| 1676 | */ |
| 1677 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1678 | events_got = seq - ctx->cq_last_tm_flush; |
| 1679 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1680 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1681 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1682 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1683 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1684 | } while (!list_empty(&ctx->timeout_list)); |
| 1685 | |
| 1686 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1687 | } |
| 1688 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1689 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1690 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1691 | io_flush_timeouts(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1692 | __io_commit_cqring(ctx); |
| 1693 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1694 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1695 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1696 | } |
| 1697 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1698 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1699 | { |
| 1700 | struct io_rings *r = ctx->rings; |
| 1701 | |
| 1702 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1703 | } |
| 1704 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1705 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1706 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1707 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1708 | unsigned tail; |
| 1709 | |
| 1710 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1711 | /* |
| 1712 | * writes to the cq entry need to come after reading head; the |
| 1713 | * control dependency is enough as we're using WRITE_ONCE to |
| 1714 | * fill the cq entry |
| 1715 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1716 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1717 | return NULL; |
| 1718 | |
| 1719 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1720 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1723 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1724 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1725 | if (!ctx->cq_ev_fd) |
| 1726 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1727 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1728 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1729 | if (!ctx->eventfd_async) |
| 1730 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1731 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1734 | static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx) |
| 1735 | { |
| 1736 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1737 | } |
| 1738 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1739 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1740 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1741 | /* see waitqueue_active() comment */ |
| 1742 | smp_mb(); |
| 1743 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1744 | if (waitqueue_active(&ctx->wait)) |
| 1745 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1746 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1747 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1748 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1749 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1750 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1751 | wake_up_interruptible(&ctx->cq_wait); |
| 1752 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1753 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1754 | } |
| 1755 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1756 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1757 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1758 | /* see waitqueue_active() comment */ |
| 1759 | smp_mb(); |
| 1760 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1761 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1762 | if (waitqueue_active(&ctx->wait)) |
| 1763 | wake_up(&ctx->wait); |
| 1764 | } |
| 1765 | if (io_should_trigger_evfd(ctx)) |
| 1766 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1767 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1768 | wake_up_interruptible(&ctx->cq_wait); |
| 1769 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1770 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1773 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1774 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1775 | struct task_struct *tsk, |
| 1776 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1777 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1778 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1779 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1780 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1781 | unsigned long flags; |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1782 | bool all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1783 | LIST_HEAD(list); |
| 1784 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1785 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1786 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1787 | |
| 1788 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1789 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1790 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1791 | continue; |
| 1792 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1793 | cqe = io_get_cqring(ctx); |
| 1794 | if (!cqe && !force) |
| 1795 | break; |
| 1796 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1797 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1798 | if (cqe) { |
| 1799 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1800 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1801 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1802 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1803 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1804 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1805 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1809 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1810 | if (all_flushed) { |
| 1811 | clear_bit(0, &ctx->sq_check_overflow); |
| 1812 | clear_bit(0, &ctx->cq_check_overflow); |
| 1813 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1814 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1815 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1816 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1817 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1818 | io_cqring_ev_posted(ctx); |
| 1819 | |
| 1820 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1821 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1822 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1823 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1824 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1825 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1826 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1829 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1830 | struct task_struct *tsk, |
| 1831 | struct files_struct *files) |
| 1832 | { |
| 1833 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1834 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1835 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1836 | mutex_lock(&ctx->uring_lock); |
| 1837 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1838 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1839 | mutex_unlock(&ctx->uring_lock); |
| 1840 | } |
| 1841 | } |
| 1842 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1843 | 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] | 1844 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1845 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1846 | struct io_uring_cqe *cqe; |
| 1847 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1848 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1849 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1850 | /* |
| 1851 | * If we can't get a cq entry, userspace overflowed the |
| 1852 | * submission (by quite a lot). Increment the overflow count in |
| 1853 | * the ring. |
| 1854 | */ |
| 1855 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1856 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1857 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1858 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1859 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1860 | } else if (ctx->cq_overflow_flushed || |
| 1861 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1862 | /* |
| 1863 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1864 | * then we cannot store the request for later flushing, we need |
| 1865 | * to drop it on the floor. |
| 1866 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1867 | ctx->cached_cq_overflow++; |
| 1868 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1869 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1870 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1871 | set_bit(0, &ctx->sq_check_overflow); |
| 1872 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1873 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1874 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1875 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1876 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1877 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1878 | refcount_inc(&req->refs); |
| 1879 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1883 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1884 | { |
| 1885 | __io_cqring_fill_event(req, res, 0); |
| 1886 | } |
| 1887 | |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1888 | 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] | 1889 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1890 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1891 | unsigned long flags; |
| 1892 | |
| 1893 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1894 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1895 | io_commit_cqring(ctx); |
| 1896 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1897 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1898 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1901 | static void io_submit_flush_completions(struct io_comp_state *cs) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1902 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1903 | struct io_ring_ctx *ctx = cs->ctx; |
| 1904 | |
| 1905 | spin_lock_irq(&ctx->completion_lock); |
| 1906 | while (!list_empty(&cs->list)) { |
| 1907 | struct io_kiocb *req; |
| 1908 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1909 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
| 1910 | list_del(&req->compl.list); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1911 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1912 | |
| 1913 | /* |
| 1914 | * io_free_req() doesn't care about completion_lock unless one |
| 1915 | * of these flags is set. REQ_F_WORK_INITIALIZED is in the list |
| 1916 | * because of a potential deadlock with req->work.fs->lock |
| 1917 | */ |
| 1918 | if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT |
| 1919 | |REQ_F_WORK_INITIALIZED)) { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1920 | spin_unlock_irq(&ctx->completion_lock); |
| 1921 | io_put_req(req); |
| 1922 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1923 | } else { |
| 1924 | io_put_req(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | io_commit_cqring(ctx); |
| 1928 | spin_unlock_irq(&ctx->completion_lock); |
| 1929 | |
| 1930 | io_cqring_ev_posted(ctx); |
| 1931 | cs->nr = 0; |
| 1932 | } |
| 1933 | |
| 1934 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, |
| 1935 | struct io_comp_state *cs) |
| 1936 | { |
| 1937 | if (!cs) { |
| 1938 | io_cqring_add_event(req, res, cflags); |
| 1939 | io_put_req(req); |
| 1940 | } else { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1941 | io_clean_op(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1942 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1943 | req->compl.cflags = cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1944 | list_add_tail(&req->compl.list, &cs->list); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1945 | if (++cs->nr >= 32) |
| 1946 | io_submit_flush_completions(cs); |
| 1947 | } |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | static void io_req_complete(struct io_kiocb *req, long res) |
| 1951 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1952 | __io_req_complete(req, res, 0, NULL); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1955 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1956 | { |
| 1957 | return req == (struct io_kiocb *) |
| 1958 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1959 | } |
| 1960 | |
| 1961 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1962 | { |
| 1963 | struct io_kiocb *req; |
| 1964 | |
| 1965 | req = ctx->fallback_req; |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1966 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1967 | return req; |
| 1968 | |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1972 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| 1973 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1974 | { |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1975 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1976 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1977 | size_t sz; |
| 1978 | int ret; |
| 1979 | |
| 1980 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1981 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1982 | |
| 1983 | /* |
| 1984 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1985 | * retry single alloc to be on the safe side. |
| 1986 | */ |
| 1987 | if (unlikely(ret <= 0)) { |
| 1988 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1989 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1990 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1991 | ret = 1; |
| 1992 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1993 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1996 | state->free_reqs--; |
| 1997 | return state->reqs[state->free_reqs]; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1998 | fallback: |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1999 | return io_get_fallback_req(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2002 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 2003 | bool fixed) |
| 2004 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 2005 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2006 | fput(file); |
| 2007 | } |
| 2008 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2009 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2010 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 2011 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2012 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2013 | if (req->async_data) |
| 2014 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2015 | if (req->file) |
| 2016 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 2017 | if (req->fixed_file_refs) |
| 2018 | percpu_ref_put(req->fixed_file_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2019 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2020 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2021 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2022 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2023 | { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 2024 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2025 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2026 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2027 | io_dismantle_req(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2028 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 2029 | percpu_counter_dec(&tctx->inflight); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 2030 | if (atomic_read(&tctx->in_idle)) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 2031 | wake_up(&tctx->wait); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2032 | put_task_struct(req->task); |
| 2033 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 2034 | if (likely(!io_is_fallback_req(req))) |
| 2035 | kmem_cache_free(req_cachep, req); |
| 2036 | else |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2037 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
| 2038 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2039 | } |
| 2040 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2041 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2042 | { |
| 2043 | struct io_kiocb *nxt = req->link; |
| 2044 | |
| 2045 | req->link = nxt->link; |
| 2046 | nxt->link = NULL; |
| 2047 | } |
| 2048 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2049 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2050 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2051 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2052 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2053 | bool cancelled = false; |
| 2054 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2055 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2056 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2057 | link = req->link; |
| 2058 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 2059 | /* |
| 2060 | * Can happen if a linked timeout fired and link had been like |
| 2061 | * req -> link t-out -> link t-out [-> ...] |
| 2062 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2063 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 2064 | struct io_timeout_data *io = link->async_data; |
| 2065 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2066 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2067 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2068 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2069 | ret = hrtimer_try_to_cancel(&io->timer); |
| 2070 | if (ret != -1) { |
| 2071 | io_cqring_fill_event(link, -ECANCELED); |
| 2072 | io_commit_cqring(ctx); |
| 2073 | cancelled = true; |
| 2074 | } |
| 2075 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2076 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2077 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 2078 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2079 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2080 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2081 | io_put_req(link); |
| 2082 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2083 | } |
| 2084 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2085 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2086 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2087 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2088 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2089 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2090 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2091 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2092 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2093 | link = req->link; |
| 2094 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2095 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2096 | while (link) { |
| 2097 | nxt = link->link; |
| 2098 | link->link = NULL; |
| 2099 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2100 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2101 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2102 | |
| 2103 | /* |
| 2104 | * It's ok to free under spinlock as they're not linked anymore, |
| 2105 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| 2106 | * work.fs->lock. |
| 2107 | */ |
| 2108 | if (link->flags & REQ_F_WORK_INITIALIZED) |
| 2109 | io_put_req_deferred(link, 2); |
| 2110 | else |
| 2111 | io_double_put_req(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2112 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2113 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2114 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2115 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2116 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2117 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2118 | } |
| 2119 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2120 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2121 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2122 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 2123 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2124 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2125 | /* |
| 2126 | * If LINK is set, we have dependent requests in this chain. If we |
| 2127 | * didn't fail this request, queue the first one up, moving any other |
| 2128 | * dependencies to the next request. In case of failure, fail the rest |
| 2129 | * of the chain. |
| 2130 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2131 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 2132 | struct io_kiocb *nxt = req->link; |
| 2133 | |
| 2134 | req->link = NULL; |
| 2135 | return nxt; |
| 2136 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2137 | io_fail_links(req); |
| 2138 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2139 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2140 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2141 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2142 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2143 | if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2144 | return NULL; |
| 2145 | return __io_req_find_next(req); |
| 2146 | } |
| 2147 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2148 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2149 | { |
| 2150 | struct task_struct *tsk = req->task; |
| 2151 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2152 | enum task_work_notify_mode notify; |
| 2153 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2154 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2155 | if (tsk->flags & PF_EXITING) |
| 2156 | return -ESRCH; |
| 2157 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2158 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2159 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2160 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2161 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2162 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2163 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2164 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2165 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2166 | notify = TWA_SIGNAL; |
| 2167 | |
Jens Axboe | 87c4311 | 2020-09-30 21:00:14 -0600 | [diff] [blame] | 2168 | ret = task_work_add(tsk, &req->task_work, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2169 | if (!ret) |
| 2170 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2171 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2172 | return ret; |
| 2173 | } |
| 2174 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2175 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 2176 | { |
| 2177 | struct io_ring_ctx *ctx = req->ctx; |
| 2178 | |
| 2179 | spin_lock_irq(&ctx->completion_lock); |
| 2180 | io_cqring_fill_event(req, error); |
| 2181 | io_commit_cqring(ctx); |
| 2182 | spin_unlock_irq(&ctx->completion_lock); |
| 2183 | |
| 2184 | io_cqring_ev_posted(ctx); |
| 2185 | req_set_fail_links(req); |
| 2186 | io_double_put_req(req); |
| 2187 | } |
| 2188 | |
| 2189 | static void io_req_task_cancel(struct callback_head *cb) |
| 2190 | { |
| 2191 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2192 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2193 | |
| 2194 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2195 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2199 | { |
| 2200 | struct io_ring_ctx *ctx = req->ctx; |
| 2201 | |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2202 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 2203 | if (!ctx->sqo_dead && |
| 2204 | !__io_sq_thread_acquire_mm(ctx) && |
| 2205 | !__io_sq_thread_acquire_files(ctx)) |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 2206 | __io_queue_sqe(req, NULL); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2207 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2208 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2209 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2210 | } |
| 2211 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2212 | static void io_req_task_submit(struct callback_head *cb) |
| 2213 | { |
| 2214 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2215 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2216 | |
| 2217 | __io_req_task_submit(req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2218 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | static void io_req_task_queue(struct io_kiocb *req) |
| 2222 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2223 | int ret; |
| 2224 | |
| 2225 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2226 | percpu_ref_get(&req->ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2227 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2228 | ret = io_req_task_work_add(req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2229 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2230 | struct task_struct *tsk; |
| 2231 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2232 | init_task_work(&req->task_work, io_req_task_cancel); |
| 2233 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2234 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2235 | wake_up_process(tsk); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2236 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2237 | } |
| 2238 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2239 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2240 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2241 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2242 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2243 | if (nxt) |
| 2244 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2245 | } |
| 2246 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2247 | static void io_free_req(struct io_kiocb *req) |
| 2248 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2249 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2250 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2251 | } |
| 2252 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2253 | struct req_batch { |
| 2254 | void *reqs[IO_IOPOLL_BATCH]; |
| 2255 | int to_free; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2256 | |
| 2257 | struct task_struct *task; |
| 2258 | int task_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2259 | }; |
| 2260 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2261 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2262 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2263 | rb->to_free = 0; |
| 2264 | rb->task_refs = 0; |
| 2265 | rb->task = NULL; |
| 2266 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2267 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2268 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
| 2269 | struct req_batch *rb) |
| 2270 | { |
| 2271 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 2272 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
| 2273 | rb->to_free = 0; |
| 2274 | } |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2275 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2276 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2277 | struct req_batch *rb) |
| 2278 | { |
| 2279 | if (rb->to_free) |
| 2280 | __io_req_free_batch_flush(ctx, rb); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2281 | if (rb->task) { |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 2282 | struct io_uring_task *tctx = rb->task->io_uring; |
| 2283 | |
| 2284 | percpu_counter_sub(&tctx->inflight, rb->task_refs); |
Jens Axboe | c93cc9e | 2021-01-16 11:52:11 -0700 | [diff] [blame] | 2285 | if (atomic_read(&tctx->in_idle)) |
| 2286 | wake_up(&tctx->wait); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2287 | put_task_struct_many(rb->task, rb->task_refs); |
| 2288 | rb->task = NULL; |
| 2289 | } |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) |
| 2293 | { |
| 2294 | if (unlikely(io_is_fallback_req(req))) { |
| 2295 | io_free_req(req); |
| 2296 | return; |
| 2297 | } |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2298 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2299 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2300 | if (req->task != rb->task) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 2301 | if (rb->task) { |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 2302 | struct io_uring_task *tctx = rb->task->io_uring; |
| 2303 | |
| 2304 | percpu_counter_sub(&tctx->inflight, rb->task_refs); |
Jens Axboe | c93cc9e | 2021-01-16 11:52:11 -0700 | [diff] [blame] | 2305 | if (atomic_read(&tctx->in_idle)) |
| 2306 | wake_up(&tctx->wait); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2307 | put_task_struct_many(rb->task, rb->task_refs); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2308 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2309 | rb->task = req->task; |
| 2310 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2311 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2312 | rb->task_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2313 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2314 | io_dismantle_req(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2315 | rb->reqs[rb->to_free++] = req; |
| 2316 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 2317 | __io_req_free_batch_flush(req->ctx, rb); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2318 | } |
| 2319 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2320 | /* |
| 2321 | * Drop reference to request, return next in chain (if there is one) if this |
| 2322 | * was the last reference to this request. |
| 2323 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2324 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2325 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2326 | struct io_kiocb *nxt = NULL; |
| 2327 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2328 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2329 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2330 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2331 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2332 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2335 | static void io_put_req(struct io_kiocb *req) |
| 2336 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2337 | if (refcount_dec_and_test(&req->refs)) |
| 2338 | io_free_req(req); |
| 2339 | } |
| 2340 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2341 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2342 | { |
| 2343 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2344 | |
| 2345 | io_free_req(req); |
| 2346 | } |
| 2347 | |
| 2348 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2349 | { |
| 2350 | int ret; |
| 2351 | |
| 2352 | init_task_work(&req->task_work, io_put_req_deferred_cb); |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2353 | ret = io_req_task_work_add(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2354 | if (unlikely(ret)) { |
| 2355 | struct task_struct *tsk; |
| 2356 | |
| 2357 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2358 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2359 | wake_up_process(tsk); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2364 | { |
| 2365 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2366 | io_free_req_deferred(req); |
| 2367 | } |
| 2368 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 2369 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2370 | { |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 2371 | struct io_kiocb *nxt; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2372 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 2373 | /* |
| 2374 | * A ref is owned by io-wq in which context we're. So, if that's the |
| 2375 | * last one, it's safe to steal next work. False negatives are Ok, |
| 2376 | * it just will be re-punted async in io_put_work() |
| 2377 | */ |
| 2378 | if (refcount_read(&req->refs) != 1) |
| 2379 | return NULL; |
| 2380 | |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2381 | nxt = io_req_find_next(req); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 2382 | return nxt ? &nxt->work : NULL; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2383 | } |
| 2384 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2385 | static void io_double_put_req(struct io_kiocb *req) |
| 2386 | { |
| 2387 | /* drop both submit and complete references */ |
| 2388 | if (refcount_sub_and_test(2, &req->refs)) |
| 2389 | io_free_req(req); |
| 2390 | } |
| 2391 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2392 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2393 | { |
| 2394 | /* See comment at the top of this file */ |
| 2395 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2396 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2397 | } |
| 2398 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2399 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2400 | { |
| 2401 | struct io_rings *rings = ctx->rings; |
| 2402 | |
| 2403 | /* make sure SQ entry isn't read before tail */ |
| 2404 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2405 | } |
| 2406 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2407 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2408 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2409 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2410 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2411 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2412 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2413 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2414 | kfree(kbuf); |
| 2415 | return cflags; |
| 2416 | } |
| 2417 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2418 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2419 | { |
| 2420 | struct io_buffer *kbuf; |
| 2421 | |
| 2422 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2423 | return io_put_kbuf(req, kbuf); |
| 2424 | } |
| 2425 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2426 | static inline bool io_run_task_work(void) |
| 2427 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2428 | /* |
| 2429 | * Not safe to run on exiting task, and the task_work handling will |
| 2430 | * not add work to such a task. |
| 2431 | */ |
| 2432 | if (unlikely(current->flags & PF_EXITING)) |
| 2433 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2434 | if (current->task_works) { |
| 2435 | __set_current_state(TASK_RUNNING); |
| 2436 | task_work_run(); |
| 2437 | return true; |
| 2438 | } |
| 2439 | |
| 2440 | return false; |
| 2441 | } |
| 2442 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2443 | static void io_iopoll_queue(struct list_head *again) |
| 2444 | { |
| 2445 | struct io_kiocb *req; |
| 2446 | |
| 2447 | do { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2448 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
| 2449 | list_del(&req->inflight_entry); |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 2450 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2451 | } while (!list_empty(again)); |
| 2452 | } |
| 2453 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2454 | /* |
| 2455 | * Find and free completed poll iocbs |
| 2456 | */ |
| 2457 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2458 | struct list_head *done) |
| 2459 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2460 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2461 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2462 | LIST_HEAD(again); |
| 2463 | |
| 2464 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2465 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2466 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2467 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2468 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2469 | int cflags = 0; |
| 2470 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2471 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2472 | if (READ_ONCE(req->result) == -EAGAIN) { |
Jens Axboe | 56450c2 | 2020-08-26 18:58:26 -0600 | [diff] [blame] | 2473 | req->result = 0; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2474 | req->iopoll_completed = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2475 | list_move_tail(&req->inflight_entry, &again); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2476 | continue; |
| 2477 | } |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2478 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2479 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2480 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2481 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2482 | |
| 2483 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2484 | (*nr_events)++; |
| 2485 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2486 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2487 | io_req_free_batch(&rb, req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2488 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2489 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2490 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2491 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2492 | io_req_free_batch_finish(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2493 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2494 | if (!list_empty(&again)) |
| 2495 | io_iopoll_queue(&again); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2496 | } |
| 2497 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2498 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2499 | long min) |
| 2500 | { |
| 2501 | struct io_kiocb *req, *tmp; |
| 2502 | LIST_HEAD(done); |
| 2503 | bool spin; |
| 2504 | int ret; |
| 2505 | |
| 2506 | /* |
| 2507 | * Only spin for completions if we don't have multiple devices hanging |
| 2508 | * off our complete list, and we're under the requested amount. |
| 2509 | */ |
| 2510 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2511 | |
| 2512 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2513 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2514 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2515 | |
| 2516 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2517 | * Move completed and retryable entries to our local lists. |
| 2518 | * If we find a request that requires polling, break out |
| 2519 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2520 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2521 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2522 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2523 | continue; |
| 2524 | } |
| 2525 | if (!list_empty(&done)) |
| 2526 | break; |
| 2527 | |
| 2528 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2529 | if (ret < 0) |
| 2530 | break; |
| 2531 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2532 | /* iopoll may have completed current req */ |
| 2533 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2534 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2535 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2536 | if (ret && spin) |
| 2537 | spin = false; |
| 2538 | ret = 0; |
| 2539 | } |
| 2540 | |
| 2541 | if (!list_empty(&done)) |
| 2542 | io_iopoll_complete(ctx, nr_events, &done); |
| 2543 | |
| 2544 | return ret; |
| 2545 | } |
| 2546 | |
| 2547 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2548 | * 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] | 2549 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2550 | * as a non-spinning completion check. |
| 2551 | */ |
| 2552 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2553 | long min) |
| 2554 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2555 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2556 | int ret; |
| 2557 | |
| 2558 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2559 | if (ret < 0) |
| 2560 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2561 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2562 | return 0; |
| 2563 | } |
| 2564 | |
| 2565 | return 1; |
| 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * We can't just wait for polled events to come to us, we have to actively |
| 2570 | * find and complete them. |
| 2571 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2572 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2573 | { |
| 2574 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2575 | return; |
| 2576 | |
| 2577 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2578 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2579 | unsigned int nr_events = 0; |
| 2580 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2581 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2582 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2583 | /* let it sleep and repeat later if can't complete a request */ |
| 2584 | if (nr_events == 0) |
| 2585 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2586 | /* |
| 2587 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2588 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2589 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2590 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2591 | if (need_resched()) { |
| 2592 | mutex_unlock(&ctx->uring_lock); |
| 2593 | cond_resched(); |
| 2594 | mutex_lock(&ctx->uring_lock); |
| 2595 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2596 | } |
| 2597 | mutex_unlock(&ctx->uring_lock); |
| 2598 | } |
| 2599 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2600 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2601 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2602 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2603 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2604 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2605 | /* |
| 2606 | * We disallow the app entering submit/complete with polling, but we |
| 2607 | * still need to lock the ring to prevent racing with polled issue |
| 2608 | * that got punted to a workqueue. |
| 2609 | */ |
| 2610 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2611 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2612 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2613 | * Don't enter poll loop if we already have events pending. |
| 2614 | * If we do, we can potentially be spinning for commands that |
| 2615 | * already triggered a CQE (eg in error). |
| 2616 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2617 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2618 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2619 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2620 | break; |
| 2621 | |
| 2622 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2623 | * If a submit got punted to a workqueue, we can have the |
| 2624 | * application entering polling for a command before it gets |
| 2625 | * issued. That app will hold the uring_lock for the duration |
| 2626 | * of the poll right here, so we need to take a breather every |
| 2627 | * now and then to ensure that the issue has a chance to add |
| 2628 | * the poll to the issued list. Otherwise we can spin here |
| 2629 | * forever, while the workqueue is stuck trying to acquire the |
| 2630 | * very same mutex. |
| 2631 | */ |
| 2632 | if (!(++iters & 7)) { |
| 2633 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2634 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2635 | mutex_lock(&ctx->uring_lock); |
| 2636 | } |
| 2637 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2638 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2639 | if (ret <= 0) |
| 2640 | break; |
| 2641 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2642 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2643 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2644 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2645 | return ret; |
| 2646 | } |
| 2647 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2648 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2649 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2650 | /* |
| 2651 | * Tell lockdep we inherited freeze protection from submission |
| 2652 | * thread. |
| 2653 | */ |
| 2654 | if (req->flags & REQ_F_ISREG) { |
| 2655 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2656 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2657 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2658 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2659 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2660 | } |
| 2661 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2662 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| 2663 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2664 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2665 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2666 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2667 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2668 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2669 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2670 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2671 | if (res != req->result) |
| 2672 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2673 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2674 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2675 | __io_req_complete(req, res, cflags, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2676 | } |
| 2677 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2678 | #ifdef CONFIG_BLOCK |
| 2679 | static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| 2680 | { |
| 2681 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 2682 | ssize_t ret = -ECANCELED; |
| 2683 | struct iov_iter iter; |
| 2684 | int rw; |
| 2685 | |
| 2686 | if (error) { |
| 2687 | ret = error; |
| 2688 | goto end_req; |
| 2689 | } |
| 2690 | |
| 2691 | switch (req->opcode) { |
| 2692 | case IORING_OP_READV: |
| 2693 | case IORING_OP_READ_FIXED: |
| 2694 | case IORING_OP_READ: |
| 2695 | rw = READ; |
| 2696 | break; |
| 2697 | case IORING_OP_WRITEV: |
| 2698 | case IORING_OP_WRITE_FIXED: |
| 2699 | case IORING_OP_WRITE: |
| 2700 | rw = WRITE; |
| 2701 | break; |
| 2702 | default: |
| 2703 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2704 | req->opcode); |
| 2705 | goto end_req; |
| 2706 | } |
| 2707 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2708 | if (!req->async_data) { |
Jens Axboe | 8f3d749 | 2020-09-14 09:28:14 -0600 | [diff] [blame] | 2709 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2710 | if (ret < 0) |
| 2711 | goto end_req; |
| 2712 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
| 2713 | if (!ret) |
| 2714 | return true; |
| 2715 | kfree(iovec); |
| 2716 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2717 | return true; |
Jens Axboe | 8f3d749 | 2020-09-14 09:28:14 -0600 | [diff] [blame] | 2718 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2719 | end_req: |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2720 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2721 | return false; |
| 2722 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2723 | #endif |
| 2724 | |
| 2725 | static bool io_rw_reissue(struct io_kiocb *req, long res) |
| 2726 | { |
| 2727 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2728 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2729 | int ret; |
| 2730 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2731 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2732 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2733 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| 2734 | return false; |
| 2735 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2736 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2737 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 2738 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2739 | |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2740 | if (io_resubmit_prep(req, ret)) { |
| 2741 | refcount_inc(&req->refs); |
| 2742 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2743 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2744 | } |
| 2745 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2746 | #endif |
| 2747 | return false; |
| 2748 | } |
| 2749 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2750 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2751 | struct io_comp_state *cs) |
| 2752 | { |
| 2753 | if (!io_rw_reissue(req, res)) |
| 2754 | io_complete_rw_common(&req->rw.kiocb, res, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2758 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2759 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2760 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2761 | __io_complete_rw(req, res, res2, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2764 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2765 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2766 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2767 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2768 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2769 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2770 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2771 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2772 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2773 | |
| 2774 | WRITE_ONCE(req->result, res); |
| 2775 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2776 | smp_wmb(); |
| 2777 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /* |
| 2781 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2782 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2783 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2784 | * accessing the kiocb cookie. |
| 2785 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2786 | static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2787 | { |
| 2788 | struct io_ring_ctx *ctx = req->ctx; |
| 2789 | |
| 2790 | /* |
| 2791 | * Track whether we have multiple files in our lists. This will impact |
| 2792 | * how we do polling eventually, not spinning if we're on potentially |
| 2793 | * different devices. |
| 2794 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2795 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2796 | ctx->poll_multi_file = false; |
| 2797 | } else if (!ctx->poll_multi_file) { |
| 2798 | struct io_kiocb *list_req; |
| 2799 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2800 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2801 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2802 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2803 | ctx->poll_multi_file = true; |
| 2804 | } |
| 2805 | |
| 2806 | /* |
| 2807 | * For fast devices, IO may have already completed. If it has, add |
| 2808 | * it to the front so we find it first. |
| 2809 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2810 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2811 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2812 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2813 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2814 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2815 | /* |
| 2816 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2817 | * task context or in io worker task context. If current task context is |
| 2818 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2819 | */ |
| 2820 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2821 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2822 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2825 | static inline void __io_state_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2826 | { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2827 | fput_many(state->file, state->file_refs); |
| 2828 | state->file_refs = 0; |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2832 | { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2833 | if (state->file_refs) |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2834 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /* |
| 2838 | * Get as many references to a file as we have IOs left in this submission, |
| 2839 | * assuming most submissions are for one file, or at least that each file |
| 2840 | * has more than one submission. |
| 2841 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2842 | 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] | 2843 | { |
| 2844 | if (!state) |
| 2845 | return fget(fd); |
| 2846 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2847 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2848 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2849 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2850 | return state->file; |
| 2851 | } |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2852 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2853 | } |
| 2854 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2855 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2856 | return NULL; |
| 2857 | |
| 2858 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2859 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2860 | return state->file; |
| 2861 | } |
| 2862 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2863 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2864 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2865 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2866 | } |
| 2867 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2868 | /* |
| 2869 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2870 | * any file. For now, just ensure that anything potentially problematic is done |
| 2871 | * inline. |
| 2872 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2873 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2874 | { |
| 2875 | umode_t mode = file_inode(file)->i_mode; |
| 2876 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2877 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2878 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2879 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2880 | return true; |
| 2881 | return false; |
| 2882 | } |
| 2883 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2884 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2885 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2886 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2887 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2888 | file->f_op != &io_uring_fops) |
| 2889 | return true; |
| 2890 | return false; |
| 2891 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2892 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2893 | /* any ->read/write should understand O_NONBLOCK */ |
| 2894 | if (file->f_flags & O_NONBLOCK) |
| 2895 | return true; |
| 2896 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2897 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2898 | return false; |
| 2899 | |
| 2900 | if (rw == READ) |
| 2901 | return file->f_op->read_iter != NULL; |
| 2902 | |
| 2903 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2906 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2907 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2908 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2909 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2910 | unsigned ioprio; |
| 2911 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2912 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2913 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2914 | req->flags |= REQ_F_ISREG; |
| 2915 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2916 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2917 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2918 | req->flags |= REQ_F_CUR_POS; |
| 2919 | kiocb->ki_pos = req->file->f_pos; |
| 2920 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2921 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2922 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2923 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2924 | if (unlikely(ret)) |
| 2925 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2926 | |
| 2927 | ioprio = READ_ONCE(sqe->ioprio); |
| 2928 | if (ioprio) { |
| 2929 | ret = ioprio_check_cap(ioprio); |
| 2930 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2931 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2932 | |
| 2933 | kiocb->ki_ioprio = ioprio; |
| 2934 | } else |
| 2935 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2936 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2937 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2938 | if (kiocb->ki_flags & IOCB_NOWAIT) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2939 | req->flags |= REQ_F_NOWAIT; |
| 2940 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2941 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2942 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2943 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2944 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2945 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2946 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2947 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2948 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2949 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2950 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2951 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2952 | kiocb->ki_complete = io_complete_rw; |
| 2953 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2954 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2955 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2956 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2957 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2958 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2962 | { |
| 2963 | switch (ret) { |
| 2964 | case -EIOCBQUEUED: |
| 2965 | break; |
| 2966 | case -ERESTARTSYS: |
| 2967 | case -ERESTARTNOINTR: |
| 2968 | case -ERESTARTNOHAND: |
| 2969 | case -ERESTART_RESTARTBLOCK: |
| 2970 | /* |
| 2971 | * We can't just restart the syscall, since previously |
| 2972 | * submitted sqes may already be in progress. Just fail this |
| 2973 | * IO with EINTR. |
| 2974 | */ |
| 2975 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2976 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2977 | default: |
| 2978 | kiocb->ki_complete(kiocb, ret, 0); |
| 2979 | } |
| 2980 | } |
| 2981 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2982 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
| 2983 | struct io_comp_state *cs) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2984 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2985 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2986 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2987 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2988 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2989 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2990 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2991 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2992 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2993 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2994 | } |
| 2995 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2996 | if (req->flags & REQ_F_CUR_POS) |
| 2997 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2998 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2999 | __io_complete_rw(req, ret, 0, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3000 | else |
| 3001 | io_rw_done(kiocb, ret); |
| 3002 | } |
| 3003 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3004 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3005 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3006 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3007 | struct io_ring_ctx *ctx = req->ctx; |
| 3008 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3009 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 3010 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3011 | size_t offset; |
| 3012 | u64 buf_addr; |
| 3013 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3014 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3015 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3016 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3017 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3018 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3019 | |
| 3020 | /* overflow */ |
| 3021 | if (buf_addr + len < buf_addr) |
| 3022 | return -EFAULT; |
| 3023 | /* not inside the mapped region */ |
| 3024 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 3025 | return -EFAULT; |
| 3026 | |
| 3027 | /* |
| 3028 | * May not be a start of buffer, set size appropriately |
| 3029 | * and advance us to the beginning. |
| 3030 | */ |
| 3031 | offset = buf_addr - imu->ubuf; |
| 3032 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3033 | |
| 3034 | if (offset) { |
| 3035 | /* |
| 3036 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3037 | * using the latter parts of a big fixed buffer - it iterates |
| 3038 | * over each segment manually. We can cheat a bit here, because |
| 3039 | * we know that: |
| 3040 | * |
| 3041 | * 1) it's a BVEC iter, we set it up |
| 3042 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3043 | * first and last bvec |
| 3044 | * |
| 3045 | * So just find our index, and adjust the iterator afterwards. |
| 3046 | * If the offset is within the first bvec (or the whole first |
| 3047 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3048 | * since we can just skip the first segment, which may not |
| 3049 | * be PAGE_SIZE aligned. |
| 3050 | */ |
| 3051 | const struct bio_vec *bvec = imu->bvec; |
| 3052 | |
| 3053 | if (offset <= bvec->bv_len) { |
| 3054 | iov_iter_advance(iter, offset); |
| 3055 | } else { |
| 3056 | unsigned long seg_skip; |
| 3057 | |
| 3058 | /* skip first vec */ |
| 3059 | offset -= bvec->bv_len; |
| 3060 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3061 | |
| 3062 | iter->bvec = bvec + seg_skip; |
| 3063 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3064 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3065 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3066 | } |
| 3067 | } |
| 3068 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 3069 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3072 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3073 | { |
| 3074 | if (needs_lock) |
| 3075 | mutex_unlock(&ctx->uring_lock); |
| 3076 | } |
| 3077 | |
| 3078 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3079 | { |
| 3080 | /* |
| 3081 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3082 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3083 | * The only exception is when we've detached the request and issue it |
| 3084 | * from an async worker thread, grab the lock for that case. |
| 3085 | */ |
| 3086 | if (needs_lock) |
| 3087 | mutex_lock(&ctx->uring_lock); |
| 3088 | } |
| 3089 | |
| 3090 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3091 | int bgid, struct io_buffer *kbuf, |
| 3092 | bool needs_lock) |
| 3093 | { |
| 3094 | struct io_buffer *head; |
| 3095 | |
| 3096 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3097 | return kbuf; |
| 3098 | |
| 3099 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3100 | |
| 3101 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3102 | |
| 3103 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 3104 | if (head) { |
| 3105 | if (!list_empty(&head->list)) { |
| 3106 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3107 | list); |
| 3108 | list_del(&kbuf->list); |
| 3109 | } else { |
| 3110 | kbuf = head; |
| 3111 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 3112 | } |
| 3113 | if (*len > kbuf->len) |
| 3114 | *len = kbuf->len; |
| 3115 | } else { |
| 3116 | kbuf = ERR_PTR(-ENOBUFS); |
| 3117 | } |
| 3118 | |
| 3119 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3120 | |
| 3121 | return kbuf; |
| 3122 | } |
| 3123 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3124 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3125 | bool needs_lock) |
| 3126 | { |
| 3127 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3128 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3129 | |
| 3130 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3131 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3132 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3133 | if (IS_ERR(kbuf)) |
| 3134 | return kbuf; |
| 3135 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3136 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3137 | return u64_to_user_ptr(kbuf->addr); |
| 3138 | } |
| 3139 | |
| 3140 | #ifdef CONFIG_COMPAT |
| 3141 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3142 | bool needs_lock) |
| 3143 | { |
| 3144 | struct compat_iovec __user *uiov; |
| 3145 | compat_ssize_t clen; |
| 3146 | void __user *buf; |
| 3147 | ssize_t len; |
| 3148 | |
| 3149 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3150 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3151 | return -EFAULT; |
| 3152 | if (__get_user(clen, &uiov->iov_len)) |
| 3153 | return -EFAULT; |
| 3154 | if (clen < 0) |
| 3155 | return -EINVAL; |
| 3156 | |
| 3157 | len = clen; |
| 3158 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3159 | if (IS_ERR(buf)) |
| 3160 | return PTR_ERR(buf); |
| 3161 | iov[0].iov_base = buf; |
| 3162 | iov[0].iov_len = (compat_size_t) len; |
| 3163 | return 0; |
| 3164 | } |
| 3165 | #endif |
| 3166 | |
| 3167 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3168 | bool needs_lock) |
| 3169 | { |
| 3170 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3171 | void __user *buf; |
| 3172 | ssize_t len; |
| 3173 | |
| 3174 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3175 | return -EFAULT; |
| 3176 | |
| 3177 | len = iov[0].iov_len; |
| 3178 | if (len < 0) |
| 3179 | return -EINVAL; |
| 3180 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3181 | if (IS_ERR(buf)) |
| 3182 | return PTR_ERR(buf); |
| 3183 | iov[0].iov_base = buf; |
| 3184 | iov[0].iov_len = len; |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
| 3188 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3189 | bool needs_lock) |
| 3190 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3191 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3192 | struct io_buffer *kbuf; |
| 3193 | |
| 3194 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3195 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3196 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3197 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3198 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3199 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3200 | return -EINVAL; |
| 3201 | |
| 3202 | #ifdef CONFIG_COMPAT |
| 3203 | if (req->ctx->compat) |
| 3204 | return io_compat_import(req, iov, needs_lock); |
| 3205 | #endif |
| 3206 | |
| 3207 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3208 | } |
| 3209 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3210 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | 8452fd0 | 2020-08-18 13:58:33 -0700 | [diff] [blame] | 3211 | struct iovec **iovec, struct iov_iter *iter, |
| 3212 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3213 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3214 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3215 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3216 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3217 | u8 opcode; |
| 3218 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3219 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3220 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3221 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3222 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3223 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3224 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3225 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3226 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3227 | return -EINVAL; |
| 3228 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3229 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3230 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3231 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3232 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3233 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3234 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3235 | } |
| 3236 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3237 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3238 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3239 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3242 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3243 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3244 | if (!ret) { |
| 3245 | ret = (*iovec)->iov_len; |
| 3246 | iov_iter_init(iter, rw, *iovec, 1, ret); |
| 3247 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3248 | *iovec = NULL; |
| 3249 | return ret; |
| 3250 | } |
| 3251 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3252 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3253 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3254 | } |
| 3255 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3256 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3257 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3258 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3259 | } |
| 3260 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3261 | /* |
| 3262 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3263 | * by looping over ->read() or ->write() manually. |
| 3264 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3265 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3266 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3267 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3268 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3269 | ssize_t ret = 0; |
| 3270 | |
| 3271 | /* |
| 3272 | * Don't support polled IO through this interface, and we can't |
| 3273 | * support non-blocking either. For the latter, this just causes |
| 3274 | * the kiocb to be handled from an async context. |
| 3275 | */ |
| 3276 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3277 | return -EOPNOTSUPP; |
| 3278 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3279 | return -EAGAIN; |
| 3280 | |
| 3281 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3282 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3283 | ssize_t nr; |
| 3284 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3285 | if (!iov_iter_is_bvec(iter)) { |
| 3286 | iovec = iov_iter_iovec(iter); |
| 3287 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3288 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3289 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3290 | } |
| 3291 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3292 | if (rw == READ) { |
| 3293 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3294 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3295 | } else { |
| 3296 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3297 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | if (nr < 0) { |
| 3301 | if (!ret) |
| 3302 | ret = nr; |
| 3303 | break; |
| 3304 | } |
| 3305 | ret += nr; |
| 3306 | if (nr != iovec.iov_len) |
| 3307 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3308 | req->rw.len -= nr; |
| 3309 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3310 | iov_iter_advance(iter, nr); |
| 3311 | } |
| 3312 | |
| 3313 | return ret; |
| 3314 | } |
| 3315 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3316 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3317 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3318 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3319 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3320 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3321 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3322 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3323 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3324 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3325 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3326 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3327 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3328 | unsigned iov_off = 0; |
| 3329 | |
| 3330 | rw->iter.iov = rw->fast_iov; |
| 3331 | if (iter->iov != fast_iov) { |
| 3332 | iov_off = iter->iov - fast_iov; |
| 3333 | rw->iter.iov += iov_off; |
| 3334 | } |
| 3335 | if (rw->fast_iov != fast_iov) |
| 3336 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3337 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3338 | } else { |
| 3339 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3340 | } |
| 3341 | } |
| 3342 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3343 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3344 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3345 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3346 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3347 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3348 | } |
| 3349 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3350 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3351 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3352 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3353 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3354 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3355 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3358 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3359 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3360 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3361 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3362 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3363 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3364 | if (!req->async_data) { |
| 3365 | if (__io_alloc_async_data(req)) |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3366 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3367 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3368 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3369 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3370 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3373 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3374 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3375 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3376 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3377 | ssize_t ret; |
| 3378 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3379 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3380 | if (unlikely(ret < 0)) |
| 3381 | return ret; |
| 3382 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3383 | iorw->bytes_done = 0; |
| 3384 | iorw->free_iovec = iov; |
| 3385 | if (iov) |
| 3386 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3387 | return 0; |
| 3388 | } |
| 3389 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3390 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3391 | { |
| 3392 | ssize_t ret; |
| 3393 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3394 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3395 | if (ret) |
| 3396 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3397 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3398 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3399 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3400 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3401 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3402 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3403 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3404 | return io_rw_prep_async(req, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3407 | /* |
| 3408 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3409 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3410 | * This gets called when the page is unlocked, and we generally expect that to |
| 3411 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3412 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3413 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3414 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3415 | * slow path. |
| 3416 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3417 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3418 | int sync, void *arg) |
| 3419 | { |
| 3420 | struct wait_page_queue *wpq; |
| 3421 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3422 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3423 | int ret; |
| 3424 | |
| 3425 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3426 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3427 | if (!wake_page_match(wpq, key)) |
| 3428 | return 0; |
| 3429 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3430 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3431 | list_del_init(&wait->entry); |
| 3432 | |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3433 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 3434 | percpu_ref_get(&req->ctx->refs); |
| 3435 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3436 | /* submit ref gets dropped, acquire a new one */ |
| 3437 | refcount_inc(&req->refs); |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 3438 | ret = io_req_task_work_add(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3439 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3440 | struct task_struct *tsk; |
| 3441 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3442 | /* queue just for cancelation */ |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3443 | init_task_work(&req->task_work, io_req_task_cancel); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3444 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 3445 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3446 | wake_up_process(tsk); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3447 | } |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3448 | return 1; |
| 3449 | } |
| 3450 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3451 | /* |
| 3452 | * This controls whether a given IO request should be armed for async page |
| 3453 | * based retry. If we return false here, the request is handed to the async |
| 3454 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3455 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3456 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3457 | * will register a callback when the page is unlocked at IO completion. Through |
| 3458 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3459 | * That retry will attempt the buffered read again. The retry will generally |
| 3460 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3461 | * async worker threads for a blocking retry. |
| 3462 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3463 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3464 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3465 | struct io_async_rw *rw = req->async_data; |
| 3466 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3467 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3468 | |
| 3469 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3470 | if (req->flags & REQ_F_NOWAIT) |
| 3471 | return false; |
| 3472 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3473 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3474 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3475 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3476 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3477 | /* |
| 3478 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3479 | * support callback based unlocks |
| 3480 | */ |
| 3481 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3482 | return false; |
| 3483 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3484 | wait->wait.func = io_async_buf_func; |
| 3485 | wait->wait.private = req; |
| 3486 | wait->wait.flags = 0; |
| 3487 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3488 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3489 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3490 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3491 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3495 | { |
| 3496 | if (req->file->f_op->read_iter) |
| 3497 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3498 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3499 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3500 | else |
| 3501 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3502 | } |
| 3503 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3504 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
| 3505 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3506 | { |
| 3507 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3508 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3509 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3510 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3511 | ssize_t io_size, ret, ret2; |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3512 | bool no_async; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3513 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3514 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3515 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3516 | iovec = NULL; |
| 3517 | } else { |
| 3518 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3519 | if (ret < 0) |
| 3520 | return ret; |
| 3521 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3522 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3523 | req->result = io_size; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3524 | ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3525 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3526 | /* Ensure we clear previously set non-block flag */ |
| 3527 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3528 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3529 | else |
| 3530 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3531 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3532 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3533 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3534 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
| 3535 | if (no_async) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3536 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3537 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3538 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3539 | if (unlikely(ret)) |
| 3540 | goto out_free; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3541 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3542 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3543 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3544 | if (!ret) { |
| 3545 | goto done; |
| 3546 | } else if (ret == -EIOCBQUEUED) { |
| 3547 | ret = 0; |
| 3548 | goto out_free; |
| 3549 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3550 | /* IOPOLL retry should happen for io-wq threads */ |
| 3551 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3552 | goto done; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3553 | /* no retry on NONBLOCK marked file */ |
| 3554 | if (req->file->f_flags & O_NONBLOCK) |
| 3555 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3556 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3557 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3558 | ret = 0; |
| 3559 | goto copy_iov; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3560 | } else if (ret < 0) { |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3561 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
| 3562 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /* read it all, or we did blocking attempt. no retry. */ |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3566 | if (!iov_iter_count(iter) || !force_nonblock || |
Pavel Begunkov | 9a17334 | 2021-01-21 12:01:08 +0000 | [diff] [blame] | 3567 | (req->file->f_flags & O_NONBLOCK) || !(req->flags & REQ_F_ISREG)) |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3568 | goto done; |
| 3569 | |
| 3570 | io_size -= ret; |
| 3571 | copy_iov: |
| 3572 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
| 3573 | if (ret2) { |
| 3574 | ret = ret2; |
| 3575 | goto out_free; |
| 3576 | } |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3577 | if (no_async) |
| 3578 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3579 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3580 | /* it's copied and will be cleaned with ->io */ |
| 3581 | iovec = NULL; |
| 3582 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3583 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3584 | retry: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3585 | rw->bytes_done += ret; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3586 | /* if we can retry, do so with the callbacks armed */ |
| 3587 | if (!io_rw_should_retry(req)) { |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3588 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3589 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3590 | } |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3591 | |
| 3592 | /* |
| 3593 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we |
| 3594 | * get -EIOCBQUEUED, then we'll get a notification when the desired |
| 3595 | * page gets unlocked. We can also get a partial read here, and if we |
| 3596 | * do, then just retry at the new offset. |
| 3597 | */ |
| 3598 | ret = io_iter_do_read(req, iter); |
| 3599 | if (ret == -EIOCBQUEUED) { |
| 3600 | ret = 0; |
| 3601 | goto out_free; |
| 3602 | } else if (ret > 0 && ret < io_size) { |
| 3603 | /* we got some bytes, but not all. retry. */ |
| 3604 | goto retry; |
| 3605 | } |
| 3606 | done: |
| 3607 | kiocb_done(kiocb, ret, cs); |
| 3608 | ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3609 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3610 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3611 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3612 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3613 | return ret; |
| 3614 | } |
| 3615 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3616 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3617 | { |
| 3618 | ssize_t ret; |
| 3619 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3620 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3621 | if (ret) |
| 3622 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3623 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3624 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3625 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3626 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3627 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3628 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3629 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3630 | return io_rw_prep_async(req, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3633 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
| 3634 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3635 | { |
| 3636 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3637 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3638 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3639 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3640 | ssize_t ret, ret2, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3641 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3642 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3643 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3644 | iovec = NULL; |
| 3645 | } else { |
| 3646 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3647 | if (ret < 0) |
| 3648 | return ret; |
| 3649 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3650 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3651 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3652 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3653 | /* Ensure we clear previously set non-block flag */ |
| 3654 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3655 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3656 | else |
| 3657 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3658 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3659 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3660 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3661 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3662 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3663 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3664 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3665 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3666 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3667 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3668 | ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3669 | if (unlikely(ret)) |
| 3670 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3671 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3672 | /* |
| 3673 | * Open-code file_start_write here to grab freeze protection, |
| 3674 | * which will be released by another thread in |
| 3675 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3676 | * released so that it doesn't complain about the held lock when |
| 3677 | * we return to userspace. |
| 3678 | */ |
| 3679 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3680 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3681 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3682 | SB_FREEZE_WRITE); |
| 3683 | } |
| 3684 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3685 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3686 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3687 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3688 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3689 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3690 | else |
| 3691 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3692 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3693 | /* |
| 3694 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3695 | * retry them without IOCB_NOWAIT. |
| 3696 | */ |
| 3697 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3698 | ret2 = -EAGAIN; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3699 | /* no retry on NONBLOCK marked file */ |
| 3700 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) |
| 3701 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3702 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3703 | /* IOPOLL retry should happen for io-wq threads */ |
| 3704 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3705 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3706 | done: |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3707 | kiocb_done(kiocb, ret2, cs); |
| 3708 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3709 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3710 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3711 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3712 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3713 | if (!ret) |
| 3714 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3715 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3716 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3717 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3718 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3719 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3720 | return ret; |
| 3721 | } |
| 3722 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3723 | static int io_renameat_prep(struct io_kiocb *req, |
| 3724 | const struct io_uring_sqe *sqe) |
| 3725 | { |
| 3726 | struct io_rename *ren = &req->rename; |
| 3727 | const char __user *oldf, *newf; |
| 3728 | |
| 3729 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3730 | return -EBADF; |
| 3731 | |
| 3732 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3733 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3734 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3735 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3736 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3737 | |
| 3738 | ren->oldpath = getname(oldf); |
| 3739 | if (IS_ERR(ren->oldpath)) |
| 3740 | return PTR_ERR(ren->oldpath); |
| 3741 | |
| 3742 | ren->newpath = getname(newf); |
| 3743 | if (IS_ERR(ren->newpath)) { |
| 3744 | putname(ren->oldpath); |
| 3745 | return PTR_ERR(ren->newpath); |
| 3746 | } |
| 3747 | |
| 3748 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
| 3752 | static int io_renameat(struct io_kiocb *req, bool force_nonblock) |
| 3753 | { |
| 3754 | struct io_rename *ren = &req->rename; |
| 3755 | int ret; |
| 3756 | |
| 3757 | if (force_nonblock) |
| 3758 | return -EAGAIN; |
| 3759 | |
| 3760 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3761 | ren->newpath, ren->flags); |
| 3762 | |
| 3763 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3764 | if (ret < 0) |
| 3765 | req_set_fail_links(req); |
| 3766 | io_req_complete(req, ret); |
| 3767 | return 0; |
| 3768 | } |
| 3769 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3770 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3771 | const struct io_uring_sqe *sqe) |
| 3772 | { |
| 3773 | struct io_unlink *un = &req->unlink; |
| 3774 | const char __user *fname; |
| 3775 | |
| 3776 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3777 | return -EBADF; |
| 3778 | |
| 3779 | un->dfd = READ_ONCE(sqe->fd); |
| 3780 | |
| 3781 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3782 | if (un->flags & ~AT_REMOVEDIR) |
| 3783 | return -EINVAL; |
| 3784 | |
| 3785 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3786 | un->filename = getname(fname); |
| 3787 | if (IS_ERR(un->filename)) |
| 3788 | return PTR_ERR(un->filename); |
| 3789 | |
| 3790 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3791 | return 0; |
| 3792 | } |
| 3793 | |
| 3794 | static int io_unlinkat(struct io_kiocb *req, bool force_nonblock) |
| 3795 | { |
| 3796 | struct io_unlink *un = &req->unlink; |
| 3797 | int ret; |
| 3798 | |
| 3799 | if (force_nonblock) |
| 3800 | return -EAGAIN; |
| 3801 | |
| 3802 | if (un->flags & AT_REMOVEDIR) |
| 3803 | ret = do_rmdir(un->dfd, un->filename); |
| 3804 | else |
| 3805 | ret = do_unlinkat(un->dfd, un->filename); |
| 3806 | |
| 3807 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3808 | if (ret < 0) |
| 3809 | req_set_fail_links(req); |
| 3810 | io_req_complete(req, ret); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3814 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3815 | const struct io_uring_sqe *sqe) |
| 3816 | { |
| 3817 | #if defined(CONFIG_NET) |
| 3818 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3819 | return -EINVAL; |
| 3820 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3821 | sqe->buf_index) |
| 3822 | return -EINVAL; |
| 3823 | |
| 3824 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3825 | return 0; |
| 3826 | #else |
| 3827 | return -EOPNOTSUPP; |
| 3828 | #endif |
| 3829 | } |
| 3830 | |
| 3831 | static int io_shutdown(struct io_kiocb *req, bool force_nonblock) |
| 3832 | { |
| 3833 | #if defined(CONFIG_NET) |
| 3834 | struct socket *sock; |
| 3835 | int ret; |
| 3836 | |
| 3837 | if (force_nonblock) |
| 3838 | return -EAGAIN; |
| 3839 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3840 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3841 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3842 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3843 | |
| 3844 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3845 | if (ret < 0) |
| 3846 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3847 | io_req_complete(req, ret); |
| 3848 | return 0; |
| 3849 | #else |
| 3850 | return -EOPNOTSUPP; |
| 3851 | #endif |
| 3852 | } |
| 3853 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3854 | static int __io_splice_prep(struct io_kiocb *req, |
| 3855 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3856 | { |
| 3857 | struct io_splice* sp = &req->splice; |
| 3858 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3859 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3860 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3861 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3862 | |
| 3863 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3864 | sp->len = READ_ONCE(sqe->len); |
| 3865 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3866 | |
| 3867 | if (unlikely(sp->flags & ~valid_flags)) |
| 3868 | return -EINVAL; |
| 3869 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3870 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3871 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3872 | if (!sp->file_in) |
| 3873 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3874 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3875 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3876 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3877 | /* |
| 3878 | * Splice operation will be punted aync, and here need to |
| 3879 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3880 | */ |
| 3881 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3882 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3883 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3884 | |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3888 | static int io_tee_prep(struct io_kiocb *req, |
| 3889 | const struct io_uring_sqe *sqe) |
| 3890 | { |
| 3891 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3892 | return -EINVAL; |
| 3893 | return __io_splice_prep(req, sqe); |
| 3894 | } |
| 3895 | |
| 3896 | static int io_tee(struct io_kiocb *req, bool force_nonblock) |
| 3897 | { |
| 3898 | struct io_splice *sp = &req->splice; |
| 3899 | struct file *in = sp->file_in; |
| 3900 | struct file *out = sp->file_out; |
| 3901 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3902 | long ret = 0; |
| 3903 | |
| 3904 | if (force_nonblock) |
| 3905 | return -EAGAIN; |
| 3906 | if (sp->len) |
| 3907 | ret = do_tee(in, out, sp->len, flags); |
| 3908 | |
| 3909 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3910 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3911 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3912 | if (ret != sp->len) |
| 3913 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3914 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3915 | return 0; |
| 3916 | } |
| 3917 | |
| 3918 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3919 | { |
| 3920 | struct io_splice* sp = &req->splice; |
| 3921 | |
| 3922 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3923 | sp->off_out = READ_ONCE(sqe->off); |
| 3924 | return __io_splice_prep(req, sqe); |
| 3925 | } |
| 3926 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3927 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3928 | { |
| 3929 | struct io_splice *sp = &req->splice; |
| 3930 | struct file *in = sp->file_in; |
| 3931 | struct file *out = sp->file_out; |
| 3932 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3933 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3934 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3935 | |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3936 | if (force_nonblock) |
| 3937 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3938 | |
| 3939 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3940 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3941 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3942 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3943 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3944 | |
| 3945 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3946 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3947 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3948 | if (ret != sp->len) |
| 3949 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3950 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3951 | return 0; |
| 3952 | } |
| 3953 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3954 | /* |
| 3955 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3956 | */ |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3957 | static int io_nop(struct io_kiocb *req, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3958 | { |
| 3959 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3960 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3961 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3962 | return -EINVAL; |
| 3963 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3964 | __io_req_complete(req, 0, 0, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3965 | return 0; |
| 3966 | } |
| 3967 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3968 | 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] | 3969 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3970 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3971 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3972 | if (!req->file) |
| 3973 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3974 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3975 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3976 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3977 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3978 | return -EINVAL; |
| 3979 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3980 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3981 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3982 | return -EINVAL; |
| 3983 | |
| 3984 | req->sync.off = READ_ONCE(sqe->off); |
| 3985 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3986 | return 0; |
| 3987 | } |
| 3988 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3989 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3990 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3991 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3992 | int ret; |
| 3993 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3994 | /* fsync always requires a blocking context */ |
| 3995 | if (force_nonblock) |
| 3996 | return -EAGAIN; |
| 3997 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3998 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3999 | end > 0 ? end : LLONG_MAX, |
| 4000 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4001 | if (ret < 0) |
| 4002 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4003 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4004 | return 0; |
| 4005 | } |
| 4006 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4007 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4008 | const struct io_uring_sqe *sqe) |
| 4009 | { |
| 4010 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 4011 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4012 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4013 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4014 | |
| 4015 | req->sync.off = READ_ONCE(sqe->off); |
| 4016 | req->sync.len = READ_ONCE(sqe->addr); |
| 4017 | req->sync.mode = READ_ONCE(sqe->len); |
| 4018 | return 0; |
| 4019 | } |
| 4020 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4021 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4022 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4023 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4024 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4025 | /* fallocate always requiring blocking context */ |
| 4026 | if (force_nonblock) |
| 4027 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4028 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4029 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4030 | if (ret < 0) |
| 4031 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4032 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4033 | return 0; |
| 4034 | } |
| 4035 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4036 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4037 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4038 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4039 | int ret; |
| 4040 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4041 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4042 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4043 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4044 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4045 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4046 | /* open.how should be already initialised */ |
| 4047 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4048 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4049 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4050 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4051 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4052 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4053 | if (IS_ERR(req->open.filename)) { |
| 4054 | ret = PTR_ERR(req->open.filename); |
| 4055 | req->open.filename = NULL; |
| 4056 | return ret; |
| 4057 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4058 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 4059 | req->open.ignore_nonblock = false; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4060 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4061 | return 0; |
| 4062 | } |
| 4063 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4064 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4065 | { |
| 4066 | u64 flags, mode; |
| 4067 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4068 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4069 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4070 | mode = READ_ONCE(sqe->len); |
| 4071 | flags = READ_ONCE(sqe->open_flags); |
| 4072 | req->open.how = build_open_how(flags, mode); |
| 4073 | return __io_openat_prep(req, sqe); |
| 4074 | } |
| 4075 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4076 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4077 | { |
| 4078 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4079 | size_t len; |
| 4080 | int ret; |
| 4081 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4082 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4083 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4084 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4085 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4086 | if (len < OPEN_HOW_SIZE_VER0) |
| 4087 | return -EINVAL; |
| 4088 | |
| 4089 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4090 | len); |
| 4091 | if (ret) |
| 4092 | return ret; |
| 4093 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4094 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4095 | } |
| 4096 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4097 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4098 | { |
| 4099 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4100 | struct file *file; |
| 4101 | int ret; |
| 4102 | |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 4103 | if (force_nonblock && !req->open.ignore_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4104 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4105 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4106 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4107 | if (ret) |
| 4108 | goto err; |
| 4109 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4110 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4111 | if (ret < 0) |
| 4112 | goto err; |
| 4113 | |
| 4114 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 4115 | if (IS_ERR(file)) { |
| 4116 | put_unused_fd(ret); |
| 4117 | ret = PTR_ERR(file); |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 4118 | /* |
| 4119 | * A work-around to ensure that /proc/self works that way |
| 4120 | * that it should - if we get -EOPNOTSUPP back, then assume |
| 4121 | * that proc_self_get_link() failed us because we're in async |
| 4122 | * context. We should be safe to retry this from the task |
| 4123 | * itself with force_nonblock == false set, as it should not |
| 4124 | * block on lookup. Would be nice to know this upfront and |
| 4125 | * avoid the async dance, but doesn't seem feasible. |
| 4126 | */ |
| 4127 | if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) { |
| 4128 | req->open.ignore_nonblock = true; |
| 4129 | refcount_inc(&req->refs); |
| 4130 | io_req_task_queue(req); |
| 4131 | return 0; |
| 4132 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4133 | } else { |
| 4134 | fsnotify_open(file); |
| 4135 | fd_install(ret, file); |
| 4136 | } |
| 4137 | err: |
| 4138 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4139 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4140 | if (ret < 0) |
| 4141 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4142 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4143 | return 0; |
| 4144 | } |
| 4145 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4146 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4147 | { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4148 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4149 | } |
| 4150 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4151 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4152 | const struct io_uring_sqe *sqe) |
| 4153 | { |
| 4154 | struct io_provide_buf *p = &req->pbuf; |
| 4155 | u64 tmp; |
| 4156 | |
| 4157 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 4158 | return -EINVAL; |
| 4159 | |
| 4160 | tmp = READ_ONCE(sqe->fd); |
| 4161 | if (!tmp || tmp > USHRT_MAX) |
| 4162 | return -EINVAL; |
| 4163 | |
| 4164 | memset(p, 0, sizeof(*p)); |
| 4165 | p->nbufs = tmp; |
| 4166 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4167 | return 0; |
| 4168 | } |
| 4169 | |
| 4170 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4171 | int bgid, unsigned nbufs) |
| 4172 | { |
| 4173 | unsigned i = 0; |
| 4174 | |
| 4175 | /* shouldn't happen */ |
| 4176 | if (!nbufs) |
| 4177 | return 0; |
| 4178 | |
| 4179 | /* the head kbuf is the list itself */ |
| 4180 | while (!list_empty(&buf->list)) { |
| 4181 | struct io_buffer *nxt; |
| 4182 | |
| 4183 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4184 | list_del(&nxt->list); |
| 4185 | kfree(nxt); |
| 4186 | if (++i == nbufs) |
| 4187 | return i; |
| 4188 | } |
| 4189 | i++; |
| 4190 | kfree(buf); |
| 4191 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 4192 | |
| 4193 | return i; |
| 4194 | } |
| 4195 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4196 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
| 4197 | struct io_comp_state *cs) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4198 | { |
| 4199 | struct io_provide_buf *p = &req->pbuf; |
| 4200 | struct io_ring_ctx *ctx = req->ctx; |
| 4201 | struct io_buffer *head; |
| 4202 | int ret = 0; |
| 4203 | |
| 4204 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4205 | |
| 4206 | lockdep_assert_held(&ctx->uring_lock); |
| 4207 | |
| 4208 | ret = -ENOENT; |
| 4209 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4210 | if (head) |
| 4211 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4212 | if (ret < 0) |
| 4213 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4214 | |
| 4215 | /* need to hold the lock to complete IOPOLL requests */ |
| 4216 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 4217 | __io_req_complete(req, ret, 0, cs); |
| 4218 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4219 | } else { |
| 4220 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4221 | __io_req_complete(req, ret, 0, cs); |
| 4222 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4223 | return 0; |
| 4224 | } |
| 4225 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4226 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4227 | const struct io_uring_sqe *sqe) |
| 4228 | { |
| 4229 | struct io_provide_buf *p = &req->pbuf; |
| 4230 | u64 tmp; |
| 4231 | |
| 4232 | if (sqe->ioprio || sqe->rw_flags) |
| 4233 | return -EINVAL; |
| 4234 | |
| 4235 | tmp = READ_ONCE(sqe->fd); |
| 4236 | if (!tmp || tmp > USHRT_MAX) |
| 4237 | return -E2BIG; |
| 4238 | p->nbufs = tmp; |
| 4239 | p->addr = READ_ONCE(sqe->addr); |
| 4240 | p->len = READ_ONCE(sqe->len); |
| 4241 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 4242 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4243 | return -EFAULT; |
| 4244 | |
| 4245 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4246 | tmp = READ_ONCE(sqe->off); |
| 4247 | if (tmp > USHRT_MAX) |
| 4248 | return -E2BIG; |
| 4249 | p->bid = tmp; |
| 4250 | return 0; |
| 4251 | } |
| 4252 | |
| 4253 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4254 | { |
| 4255 | struct io_buffer *buf; |
| 4256 | u64 addr = pbuf->addr; |
| 4257 | int i, bid = pbuf->bid; |
| 4258 | |
| 4259 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4260 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 4261 | if (!buf) |
| 4262 | break; |
| 4263 | |
| 4264 | buf->addr = addr; |
| 4265 | buf->len = pbuf->len; |
| 4266 | buf->bid = bid; |
| 4267 | addr += pbuf->len; |
| 4268 | bid++; |
| 4269 | if (!*head) { |
| 4270 | INIT_LIST_HEAD(&buf->list); |
| 4271 | *head = buf; |
| 4272 | } else { |
| 4273 | list_add_tail(&buf->list, &(*head)->list); |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | return i ? i : -ENOMEM; |
| 4278 | } |
| 4279 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4280 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
| 4281 | struct io_comp_state *cs) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4282 | { |
| 4283 | struct io_provide_buf *p = &req->pbuf; |
| 4284 | struct io_ring_ctx *ctx = req->ctx; |
| 4285 | struct io_buffer *head, *list; |
| 4286 | int ret = 0; |
| 4287 | |
| 4288 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4289 | |
| 4290 | lockdep_assert_held(&ctx->uring_lock); |
| 4291 | |
| 4292 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4293 | |
| 4294 | ret = io_add_buffers(p, &head); |
| 4295 | if (ret < 0) |
| 4296 | goto out; |
| 4297 | |
| 4298 | if (!list) { |
| 4299 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4300 | GFP_KERNEL); |
| 4301 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4302 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4303 | goto out; |
| 4304 | } |
| 4305 | } |
| 4306 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4307 | if (ret < 0) |
| 4308 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4309 | |
| 4310 | /* need to hold the lock to complete IOPOLL requests */ |
| 4311 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 4312 | __io_req_complete(req, ret, 0, cs); |
| 4313 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4314 | } else { |
| 4315 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4316 | __io_req_complete(req, ret, 0, cs); |
| 4317 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4318 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4321 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4322 | const struct io_uring_sqe *sqe) |
| 4323 | { |
| 4324 | #if defined(CONFIG_EPOLL) |
| 4325 | if (sqe->ioprio || sqe->buf_index) |
| 4326 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4327 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4328 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4329 | |
| 4330 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4331 | req->epoll.op = READ_ONCE(sqe->len); |
| 4332 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4333 | |
| 4334 | if (ep_op_has_event(req->epoll.op)) { |
| 4335 | struct epoll_event __user *ev; |
| 4336 | |
| 4337 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4338 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4339 | return -EFAULT; |
| 4340 | } |
| 4341 | |
| 4342 | return 0; |
| 4343 | #else |
| 4344 | return -EOPNOTSUPP; |
| 4345 | #endif |
| 4346 | } |
| 4347 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4348 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
| 4349 | struct io_comp_state *cs) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4350 | { |
| 4351 | #if defined(CONFIG_EPOLL) |
| 4352 | struct io_epoll *ie = &req->epoll; |
| 4353 | int ret; |
| 4354 | |
| 4355 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4356 | if (force_nonblock && ret == -EAGAIN) |
| 4357 | return -EAGAIN; |
| 4358 | |
| 4359 | if (ret < 0) |
| 4360 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4361 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4362 | return 0; |
| 4363 | #else |
| 4364 | return -EOPNOTSUPP; |
| 4365 | #endif |
| 4366 | } |
| 4367 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4368 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4369 | { |
| 4370 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4371 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4372 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4373 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4374 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4375 | |
| 4376 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4377 | req->madvise.len = READ_ONCE(sqe->len); |
| 4378 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4379 | return 0; |
| 4380 | #else |
| 4381 | return -EOPNOTSUPP; |
| 4382 | #endif |
| 4383 | } |
| 4384 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4385 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4386 | { |
| 4387 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4388 | struct io_madvise *ma = &req->madvise; |
| 4389 | int ret; |
| 4390 | |
| 4391 | if (force_nonblock) |
| 4392 | return -EAGAIN; |
| 4393 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4394 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4395 | if (ret < 0) |
| 4396 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4397 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4398 | return 0; |
| 4399 | #else |
| 4400 | return -EOPNOTSUPP; |
| 4401 | #endif |
| 4402 | } |
| 4403 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4404 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4405 | { |
| 4406 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4407 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4408 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4409 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4410 | |
| 4411 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4412 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4413 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4417 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4418 | { |
| 4419 | struct io_fadvise *fa = &req->fadvise; |
| 4420 | int ret; |
| 4421 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4422 | if (force_nonblock) { |
| 4423 | switch (fa->advice) { |
| 4424 | case POSIX_FADV_NORMAL: |
| 4425 | case POSIX_FADV_RANDOM: |
| 4426 | case POSIX_FADV_SEQUENTIAL: |
| 4427 | break; |
| 4428 | default: |
| 4429 | return -EAGAIN; |
| 4430 | } |
| 4431 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4432 | |
| 4433 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4434 | if (ret < 0) |
| 4435 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4436 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4440 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4441 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4442 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4443 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4444 | if (sqe->ioprio || sqe->buf_index) |
| 4445 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4446 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4447 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4448 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4449 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4450 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4451 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4452 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4453 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4454 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4455 | return 0; |
| 4456 | } |
| 4457 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4458 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4459 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4460 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4461 | int ret; |
| 4462 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4463 | if (force_nonblock) { |
| 4464 | /* only need file table for an actual valid fd */ |
| 4465 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4466 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4467 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4468 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4469 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4470 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4471 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4472 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4473 | if (ret < 0) |
| 4474 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4475 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4476 | return 0; |
| 4477 | } |
| 4478 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4479 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4480 | { |
| 4481 | /* |
| 4482 | * If we queue this for async, it must not be cancellable. That would |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 4483 | * leave the 'file' in an undeterminate state, and here need to modify |
| 4484 | * io_wq_work.flags, so initialize io_wq_work firstly. |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4485 | */ |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 4486 | io_req_init_async(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4487 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4488 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4489 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4490 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4491 | sqe->rw_flags || sqe->buf_index) |
| 4492 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4493 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4494 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4495 | |
| 4496 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 4497 | if ((req->file && req->file->f_op == &io_uring_fops)) |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 4498 | return -EBADF; |
| 4499 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4500 | req->close.put_file = NULL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4504 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
| 4505 | struct io_comp_state *cs) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4506 | { |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4507 | struct io_close *close = &req->close; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4508 | int ret; |
| 4509 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4510 | /* might be already done during nonblock submission */ |
| 4511 | if (!close->put_file) { |
Eric W. Biederman | 9fe83c4 | 2020-11-20 17:14:40 -0600 | [diff] [blame] | 4512 | ret = close_fd_get_file(close->fd, &close->put_file); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4513 | if (ret < 0) |
| 4514 | return (ret == -ENOENT) ? -EBADF : ret; |
| 4515 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4516 | |
| 4517 | /* if the file has a flush method, be safe and punt to async */ |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4518 | if (close->put_file->f_op->flush && force_nonblock) { |
Jens Axboe | 607ec89 | 2021-01-19 10:10:54 -0700 | [diff] [blame] | 4519 | /* not safe to cancel at this point */ |
| 4520 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4521 | /* was never set, but play safe */ |
| 4522 | req->flags &= ~REQ_F_NOWAIT; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4523 | /* avoid grabbing files - we don't need the files */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4524 | req->flags |= REQ_F_NO_FILE_TABLE; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4525 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4526 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4527 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4528 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 4529 | ret = filp_close(close->put_file, req->work.identity->files); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4530 | if (ret < 0) |
| 4531 | req_set_fail_links(req); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4532 | fput(close->put_file); |
| 4533 | close->put_file = NULL; |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4534 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4535 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4536 | } |
| 4537 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4538 | 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] | 4539 | { |
| 4540 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4541 | |
| 4542 | if (!req->file) |
| 4543 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4544 | |
| 4545 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4546 | return -EINVAL; |
| 4547 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4548 | return -EINVAL; |
| 4549 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4550 | req->sync.off = READ_ONCE(sqe->off); |
| 4551 | req->sync.len = READ_ONCE(sqe->len); |
| 4552 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4553 | return 0; |
| 4554 | } |
| 4555 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4556 | static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4557 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4558 | int ret; |
| 4559 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4560 | /* sync_file_range always requires a blocking context */ |
| 4561 | if (force_nonblock) |
| 4562 | return -EAGAIN; |
| 4563 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4564 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4565 | req->sync.flags); |
| 4566 | if (ret < 0) |
| 4567 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4568 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4569 | return 0; |
| 4570 | } |
| 4571 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4572 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4573 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4574 | struct io_async_msghdr *kmsg) |
| 4575 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4576 | struct io_async_msghdr *async_msg = req->async_data; |
| 4577 | |
| 4578 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4579 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4580 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4581 | if (kmsg->iov != kmsg->fast_iov) |
| 4582 | kfree(kmsg->iov); |
| 4583 | return -ENOMEM; |
| 4584 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4585 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4586 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4587 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4588 | return -EAGAIN; |
| 4589 | } |
| 4590 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4591 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4592 | struct io_async_msghdr *iomsg) |
| 4593 | { |
| 4594 | iomsg->iov = iomsg->fast_iov; |
| 4595 | iomsg->msg.msg_name = &iomsg->addr; |
| 4596 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
| 4597 | req->sr_msg.msg_flags, &iomsg->iov); |
| 4598 | } |
| 4599 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4600 | 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] | 4601 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4602 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4603 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4604 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4605 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4606 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4607 | return -EINVAL; |
| 4608 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4609 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4610 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4611 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4612 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4613 | #ifdef CONFIG_COMPAT |
| 4614 | if (req->ctx->compat) |
| 4615 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4616 | #endif |
| 4617 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4618 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4619 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4620 | ret = io_sendmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4621 | if (!ret) |
| 4622 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4623 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4624 | } |
| 4625 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4626 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4627 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4628 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4629 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4630 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4631 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4632 | int ret; |
| 4633 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4634 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4635 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4636 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4637 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4638 | if (req->async_data) { |
| 4639 | kmsg = req->async_data; |
| 4640 | kmsg->msg.msg_name = &kmsg->addr; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4641 | /* if iov is set, it's allocated already */ |
| 4642 | if (!kmsg->iov) |
| 4643 | kmsg->iov = kmsg->fast_iov; |
| 4644 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4645 | } else { |
| 4646 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4647 | if (ret) |
| 4648 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4649 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4652 | flags = req->sr_msg.msg_flags; |
| 4653 | if (flags & MSG_DONTWAIT) |
| 4654 | req->flags |= REQ_F_NOWAIT; |
| 4655 | else if (force_nonblock) |
| 4656 | flags |= MSG_DONTWAIT; |
| 4657 | |
| 4658 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
| 4659 | if (force_nonblock && ret == -EAGAIN) |
| 4660 | return io_setup_async_msg(req, kmsg); |
| 4661 | if (ret == -ERESTARTSYS) |
| 4662 | ret = -EINTR; |
| 4663 | |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4664 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4665 | kfree(kmsg->iov); |
| 4666 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4667 | if (ret < 0) |
| 4668 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4669 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4670 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4671 | } |
| 4672 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4673 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4674 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4675 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4676 | struct io_sr_msg *sr = &req->sr_msg; |
| 4677 | struct msghdr msg; |
| 4678 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4679 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4680 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4681 | int ret; |
| 4682 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4683 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4684 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4685 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4686 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4687 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4688 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4689 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4690 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4691 | msg.msg_name = NULL; |
| 4692 | msg.msg_control = NULL; |
| 4693 | msg.msg_controllen = 0; |
| 4694 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4695 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4696 | flags = req->sr_msg.msg_flags; |
| 4697 | if (flags & MSG_DONTWAIT) |
| 4698 | req->flags |= REQ_F_NOWAIT; |
| 4699 | else if (force_nonblock) |
| 4700 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4701 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4702 | msg.msg_flags = flags; |
| 4703 | ret = sock_sendmsg(sock, &msg); |
| 4704 | if (force_nonblock && ret == -EAGAIN) |
| 4705 | return -EAGAIN; |
| 4706 | if (ret == -ERESTARTSYS) |
| 4707 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4708 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4709 | if (ret < 0) |
| 4710 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4711 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4712 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4715 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4716 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4717 | { |
| 4718 | struct io_sr_msg *sr = &req->sr_msg; |
| 4719 | struct iovec __user *uiov; |
| 4720 | size_t iov_len; |
| 4721 | int ret; |
| 4722 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4723 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4724 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4725 | if (ret) |
| 4726 | return ret; |
| 4727 | |
| 4728 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4729 | if (iov_len > 1) |
| 4730 | return -EINVAL; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4731 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4732 | return -EFAULT; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4733 | sr->len = iomsg->iov[0].iov_len; |
| 4734 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4735 | sr->len); |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4736 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4737 | } else { |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4738 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
| 4739 | &iomsg->iov, &iomsg->msg.msg_iter, |
| 4740 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4741 | if (ret > 0) |
| 4742 | ret = 0; |
| 4743 | } |
| 4744 | |
| 4745 | return ret; |
| 4746 | } |
| 4747 | |
| 4748 | #ifdef CONFIG_COMPAT |
| 4749 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4750 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4751 | { |
| 4752 | struct compat_msghdr __user *msg_compat; |
| 4753 | struct io_sr_msg *sr = &req->sr_msg; |
| 4754 | struct compat_iovec __user *uiov; |
| 4755 | compat_uptr_t ptr; |
| 4756 | compat_size_t len; |
| 4757 | int ret; |
| 4758 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4759 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4760 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4761 | &ptr, &len); |
| 4762 | if (ret) |
| 4763 | return ret; |
| 4764 | |
| 4765 | uiov = compat_ptr(ptr); |
| 4766 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4767 | compat_ssize_t clen; |
| 4768 | |
| 4769 | if (len > 1) |
| 4770 | return -EINVAL; |
| 4771 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4772 | return -EFAULT; |
| 4773 | if (__get_user(clen, &uiov->iov_len)) |
| 4774 | return -EFAULT; |
| 4775 | if (clen < 0) |
| 4776 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4777 | sr->len = clen; |
| 4778 | iomsg->iov[0].iov_len = clen; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4779 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4780 | } else { |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4781 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
| 4782 | UIO_FASTIOV, &iomsg->iov, |
| 4783 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4784 | if (ret < 0) |
| 4785 | return ret; |
| 4786 | } |
| 4787 | |
| 4788 | return 0; |
| 4789 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4790 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4791 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4792 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4793 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4794 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4795 | iomsg->msg.msg_name = &iomsg->addr; |
| 4796 | iomsg->iov = iomsg->fast_iov; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4797 | |
| 4798 | #ifdef CONFIG_COMPAT |
| 4799 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4800 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4801 | #endif |
| 4802 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4803 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4806 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4807 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4808 | { |
| 4809 | struct io_sr_msg *sr = &req->sr_msg; |
| 4810 | struct io_buffer *kbuf; |
| 4811 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4812 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4813 | if (IS_ERR(kbuf)) |
| 4814 | return kbuf; |
| 4815 | |
| 4816 | sr->kbuf = kbuf; |
| 4817 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4818 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4821 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4822 | { |
| 4823 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4824 | } |
| 4825 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4826 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4827 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4828 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4829 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4830 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4831 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4832 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4833 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4834 | return -EINVAL; |
| 4835 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4836 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4837 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4838 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4839 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4840 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4841 | #ifdef CONFIG_COMPAT |
| 4842 | if (req->ctx->compat) |
| 4843 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4844 | #endif |
| 4845 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4846 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4847 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4848 | ret = io_recvmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4849 | if (!ret) |
| 4850 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4851 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4852 | } |
| 4853 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4854 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4855 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4856 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4857 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4858 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4859 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4860 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4861 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4862 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4863 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4864 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4865 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4866 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4867 | if (req->async_data) { |
| 4868 | kmsg = req->async_data; |
| 4869 | kmsg->msg.msg_name = &kmsg->addr; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4870 | /* if iov is set, it's allocated already */ |
| 4871 | if (!kmsg->iov) |
| 4872 | kmsg->iov = kmsg->fast_iov; |
| 4873 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4874 | } else { |
| 4875 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4876 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4877 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4878 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4879 | } |
| 4880 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4881 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4882 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4883 | if (IS_ERR(kbuf)) |
| 4884 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4885 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 4886 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 4887 | 1, req->sr_msg.len); |
| 4888 | } |
| 4889 | |
| 4890 | flags = req->sr_msg.msg_flags; |
| 4891 | if (flags & MSG_DONTWAIT) |
| 4892 | req->flags |= REQ_F_NOWAIT; |
| 4893 | else if (force_nonblock) |
| 4894 | flags |= MSG_DONTWAIT; |
| 4895 | |
| 4896 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4897 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4898 | if (force_nonblock && ret == -EAGAIN) |
| 4899 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4900 | if (ret == -ERESTARTSYS) |
| 4901 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4902 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4903 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4904 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4905 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4906 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4907 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4908 | if (ret < 0) |
| 4909 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4910 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4911 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4912 | } |
| 4913 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4914 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4915 | struct io_comp_state *cs) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4916 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4917 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4918 | struct io_sr_msg *sr = &req->sr_msg; |
| 4919 | struct msghdr msg; |
| 4920 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4921 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4922 | struct iovec iov; |
| 4923 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4924 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4925 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4926 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4927 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4928 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4929 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4930 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4931 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4932 | if (IS_ERR(kbuf)) |
| 4933 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4934 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4935 | } |
| 4936 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4937 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4938 | if (unlikely(ret)) |
| 4939 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4940 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4941 | msg.msg_name = NULL; |
| 4942 | msg.msg_control = NULL; |
| 4943 | msg.msg_controllen = 0; |
| 4944 | msg.msg_namelen = 0; |
| 4945 | msg.msg_iocb = NULL; |
| 4946 | msg.msg_flags = 0; |
| 4947 | |
| 4948 | flags = req->sr_msg.msg_flags; |
| 4949 | if (flags & MSG_DONTWAIT) |
| 4950 | req->flags |= REQ_F_NOWAIT; |
| 4951 | else if (force_nonblock) |
| 4952 | flags |= MSG_DONTWAIT; |
| 4953 | |
| 4954 | ret = sock_recvmsg(sock, &msg, flags); |
| 4955 | if (force_nonblock && ret == -EAGAIN) |
| 4956 | return -EAGAIN; |
| 4957 | if (ret == -ERESTARTSYS) |
| 4958 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4959 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4960 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4961 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4962 | if (ret < 0) |
| 4963 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4964 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4965 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4966 | } |
| 4967 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4968 | 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] | 4969 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4970 | struct io_accept *accept = &req->accept; |
| 4971 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4972 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4973 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4974 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4975 | return -EINVAL; |
| 4976 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4977 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4978 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4979 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4980 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4981 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4982 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4983 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4984 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4985 | struct io_comp_state *cs) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4986 | { |
| 4987 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4988 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4989 | int ret; |
| 4990 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4991 | if (req->file->f_flags & O_NONBLOCK) |
| 4992 | req->flags |= REQ_F_NOWAIT; |
| 4993 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4994 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4995 | accept->addr_len, accept->flags, |
| 4996 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4997 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4998 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4999 | if (ret < 0) { |
| 5000 | if (ret == -ERESTARTSYS) |
| 5001 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5002 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5003 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5004 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5005 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5008 | 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] | 5009 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5010 | struct io_connect *conn = &req->connect; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5011 | struct io_async_connect *io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5012 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5013 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5014 | return -EINVAL; |
| 5015 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 5016 | return -EINVAL; |
| 5017 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5018 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5019 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 5020 | |
| 5021 | if (!io) |
| 5022 | return 0; |
| 5023 | |
| 5024 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5025 | &io->address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5026 | } |
| 5027 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5028 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 5029 | struct io_comp_state *cs) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5030 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5031 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5032 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5033 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5034 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5035 | if (req->async_data) { |
| 5036 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5037 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5038 | ret = move_addr_to_kernel(req->connect.addr, |
| 5039 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5040 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5041 | if (ret) |
| 5042 | goto out; |
| 5043 | io = &__io; |
| 5044 | } |
| 5045 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5046 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5047 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5048 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5049 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5050 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5051 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5052 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5053 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5054 | ret = -ENOMEM; |
| 5055 | goto out; |
| 5056 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5057 | io = req->async_data; |
| 5058 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5059 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5060 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5061 | if (ret == -ERESTARTSYS) |
| 5062 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5063 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5064 | if (ret < 0) |
| 5065 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5066 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5067 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5068 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5069 | #else /* !CONFIG_NET */ |
| 5070 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5071 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5072 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5075 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 5076 | struct io_comp_state *cs) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5077 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5078 | return -EOPNOTSUPP; |
| 5079 | } |
| 5080 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5081 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 5082 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5083 | { |
| 5084 | return -EOPNOTSUPP; |
| 5085 | } |
| 5086 | |
| 5087 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 5088 | const struct io_uring_sqe *sqe) |
| 5089 | { |
| 5090 | return -EOPNOTSUPP; |
| 5091 | } |
| 5092 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5093 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 5094 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5095 | { |
| 5096 | return -EOPNOTSUPP; |
| 5097 | } |
| 5098 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5099 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 5100 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5101 | { |
| 5102 | return -EOPNOTSUPP; |
| 5103 | } |
| 5104 | |
| 5105 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5106 | { |
| 5107 | return -EOPNOTSUPP; |
| 5108 | } |
| 5109 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5110 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 5111 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5112 | { |
| 5113 | return -EOPNOTSUPP; |
| 5114 | } |
| 5115 | |
| 5116 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5117 | { |
| 5118 | return -EOPNOTSUPP; |
| 5119 | } |
| 5120 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5121 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 5122 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5123 | { |
| 5124 | return -EOPNOTSUPP; |
| 5125 | } |
| 5126 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5127 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5128 | struct io_poll_table { |
| 5129 | struct poll_table_struct pt; |
| 5130 | struct io_kiocb *req; |
| 5131 | int error; |
| 5132 | }; |
| 5133 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5134 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 5135 | __poll_t mask, task_work_func_t func) |
| 5136 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5137 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5138 | |
| 5139 | /* for instances that support it check for an event match first: */ |
| 5140 | if (mask && !(mask & poll->events)) |
| 5141 | return 0; |
| 5142 | |
| 5143 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5144 | |
| 5145 | list_del_init(&poll->wait.entry); |
| 5146 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5147 | req->result = mask; |
| 5148 | init_task_work(&req->task_work, func); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5149 | percpu_ref_get(&req->ctx->refs); |
| 5150 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5151 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5152 | * If this fails, then the task is exiting. When a task exits, the |
| 5153 | * work gets canceled, so just cancel this request as well instead |
| 5154 | * of executing it. We can't safely execute it anyway, as we may not |
| 5155 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5156 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 5157 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5158 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 5159 | struct task_struct *tsk; |
| 5160 | |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5161 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5162 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 5163 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 5164 | wake_up_process(tsk); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5165 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5166 | return 1; |
| 5167 | } |
| 5168 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5169 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5170 | __acquires(&req->ctx->completion_lock) |
| 5171 | { |
| 5172 | struct io_ring_ctx *ctx = req->ctx; |
| 5173 | |
| 5174 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5175 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5176 | |
| 5177 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5178 | } |
| 5179 | |
| 5180 | spin_lock_irq(&ctx->completion_lock); |
| 5181 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5182 | add_wait_queue(poll->head, &poll->wait); |
| 5183 | return true; |
| 5184 | } |
| 5185 | |
| 5186 | return false; |
| 5187 | } |
| 5188 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5189 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5190 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5191 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5192 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5193 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5194 | return req->apoll->double_poll; |
| 5195 | } |
| 5196 | |
| 5197 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5198 | { |
| 5199 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5200 | return &req->poll; |
| 5201 | return &req->apoll->poll; |
| 5202 | } |
| 5203 | |
| 5204 | static void io_poll_remove_double(struct io_kiocb *req) |
| 5205 | { |
| 5206 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5207 | |
| 5208 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5209 | |
| 5210 | if (poll && poll->head) { |
| 5211 | struct wait_queue_head *head = poll->head; |
| 5212 | |
| 5213 | spin_lock(&head->lock); |
| 5214 | list_del_init(&poll->wait.entry); |
| 5215 | if (poll->wait.private) |
| 5216 | refcount_dec(&req->refs); |
| 5217 | poll->head = NULL; |
| 5218 | spin_unlock(&head->lock); |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 5223 | { |
| 5224 | struct io_ring_ctx *ctx = req->ctx; |
| 5225 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5226 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5227 | req->poll.done = true; |
| 5228 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 5229 | io_commit_cqring(ctx); |
| 5230 | } |
| 5231 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5232 | static void io_poll_task_func(struct callback_head *cb) |
| 5233 | { |
| 5234 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5235 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5236 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5237 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5238 | if (io_poll_rewait(req, &req->poll)) { |
| 5239 | spin_unlock_irq(&ctx->completion_lock); |
| 5240 | } else { |
| 5241 | hash_del(&req->hash_node); |
| 5242 | io_poll_complete(req, req->result, 0); |
| 5243 | spin_unlock_irq(&ctx->completion_lock); |
| 5244 | |
| 5245 | nxt = io_put_req_find_next(req); |
| 5246 | io_cqring_ev_posted(ctx); |
| 5247 | if (nxt) |
| 5248 | __io_req_task_submit(nxt); |
| 5249 | } |
| 5250 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5251 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5255 | int sync, void *key) |
| 5256 | { |
| 5257 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5258 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5259 | __poll_t mask = key_to_poll(key); |
| 5260 | |
| 5261 | /* for instances that support it check for an event match first: */ |
| 5262 | if (mask && !(mask & poll->events)) |
| 5263 | return 0; |
| 5264 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5265 | list_del_init(&wait->entry); |
| 5266 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5267 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5268 | bool done; |
| 5269 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5270 | spin_lock(&poll->head->lock); |
| 5271 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5272 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5273 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5274 | /* make sure double remove sees this as being gone */ |
| 5275 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5276 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5277 | if (!done) { |
| 5278 | /* use wait func handler, so it matches the rq type */ |
| 5279 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5280 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5281 | } |
| 5282 | refcount_dec(&req->refs); |
| 5283 | return 1; |
| 5284 | } |
| 5285 | |
| 5286 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5287 | wait_queue_func_t wake_func) |
| 5288 | { |
| 5289 | poll->head = NULL; |
| 5290 | poll->done = false; |
| 5291 | poll->canceled = false; |
| 5292 | poll->events = events; |
| 5293 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5294 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5295 | } |
| 5296 | |
| 5297 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5298 | struct wait_queue_head *head, |
| 5299 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5300 | { |
| 5301 | struct io_kiocb *req = pt->req; |
| 5302 | |
| 5303 | /* |
| 5304 | * If poll->head is already set, it's because the file being polled |
| 5305 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5306 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5307 | */ |
| 5308 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5309 | struct io_poll_iocb *poll_one = poll; |
| 5310 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5311 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5312 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5313 | pt->error = -EINVAL; |
| 5314 | return; |
| 5315 | } |
| 5316 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5317 | if (!poll) { |
| 5318 | pt->error = -ENOMEM; |
| 5319 | return; |
| 5320 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5321 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5322 | refcount_inc(&req->refs); |
| 5323 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5324 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | pt->error = 0; |
| 5328 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5329 | |
| 5330 | if (poll->events & EPOLLEXCLUSIVE) |
| 5331 | add_wait_queue_exclusive(head, &poll->wait); |
| 5332 | else |
| 5333 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5337 | struct poll_table_struct *p) |
| 5338 | { |
| 5339 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5340 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5341 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5342 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5343 | } |
| 5344 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5345 | static void io_async_task_func(struct callback_head *cb) |
| 5346 | { |
| 5347 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5348 | struct async_poll *apoll = req->apoll; |
| 5349 | struct io_ring_ctx *ctx = req->ctx; |
| 5350 | |
| 5351 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5352 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5353 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5354 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5355 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5356 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5357 | } |
| 5358 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5359 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5360 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5361 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5362 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5363 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5364 | spin_unlock_irq(&ctx->completion_lock); |
| 5365 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5366 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5367 | __io_req_task_submit(req); |
| 5368 | else |
| 5369 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5370 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5371 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5372 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5373 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5377 | void *key) |
| 5378 | { |
| 5379 | struct io_kiocb *req = wait->private; |
| 5380 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5381 | |
| 5382 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5383 | key_to_poll(key)); |
| 5384 | |
| 5385 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5386 | } |
| 5387 | |
| 5388 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5389 | { |
| 5390 | struct io_ring_ctx *ctx = req->ctx; |
| 5391 | struct hlist_head *list; |
| 5392 | |
| 5393 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5394 | hlist_add_head(&req->hash_node, list); |
| 5395 | } |
| 5396 | |
| 5397 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5398 | struct io_poll_iocb *poll, |
| 5399 | struct io_poll_table *ipt, __poll_t mask, |
| 5400 | wait_queue_func_t wake_func) |
| 5401 | __acquires(&ctx->completion_lock) |
| 5402 | { |
| 5403 | struct io_ring_ctx *ctx = req->ctx; |
| 5404 | bool cancel = false; |
| 5405 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5406 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5407 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5408 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5409 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5410 | |
| 5411 | ipt->pt._key = mask; |
| 5412 | ipt->req = req; |
| 5413 | ipt->error = -EINVAL; |
| 5414 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5415 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5416 | |
| 5417 | spin_lock_irq(&ctx->completion_lock); |
| 5418 | if (likely(poll->head)) { |
| 5419 | spin_lock(&poll->head->lock); |
| 5420 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5421 | if (ipt->error) |
| 5422 | cancel = true; |
| 5423 | ipt->error = 0; |
| 5424 | mask = 0; |
| 5425 | } |
| 5426 | if (mask || ipt->error) |
| 5427 | list_del_init(&poll->wait.entry); |
| 5428 | else if (cancel) |
| 5429 | WRITE_ONCE(poll->canceled, true); |
| 5430 | else if (!poll->done) /* actually waiting for an event */ |
| 5431 | io_poll_req_insert(req); |
| 5432 | spin_unlock(&poll->head->lock); |
| 5433 | } |
| 5434 | |
| 5435 | return mask; |
| 5436 | } |
| 5437 | |
| 5438 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5439 | { |
| 5440 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5441 | struct io_ring_ctx *ctx = req->ctx; |
| 5442 | struct async_poll *apoll; |
| 5443 | struct io_poll_table ipt; |
| 5444 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5445 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5446 | |
| 5447 | if (!req->file || !file_can_poll(req->file)) |
| 5448 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5449 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5450 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5451 | if (def->pollin) |
| 5452 | rw = READ; |
| 5453 | else if (def->pollout) |
| 5454 | rw = WRITE; |
| 5455 | else |
| 5456 | return false; |
| 5457 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5458 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5459 | return false; |
| 5460 | |
| 5461 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5462 | if (unlikely(!apoll)) |
| 5463 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5464 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5465 | |
| 5466 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5467 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5468 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5469 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5470 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5471 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5472 | if (def->pollout) |
| 5473 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5474 | |
| 5475 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5476 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5477 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5478 | mask &= ~POLLIN; |
| 5479 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5480 | mask |= POLLERR | POLLPRI; |
| 5481 | |
| 5482 | ipt.pt._qproc = io_async_queue_proc; |
| 5483 | |
| 5484 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5485 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5486 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5487 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5488 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5489 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5490 | kfree(apoll); |
| 5491 | return false; |
| 5492 | } |
| 5493 | spin_unlock_irq(&ctx->completion_lock); |
| 5494 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5495 | apoll->poll.events); |
| 5496 | return true; |
| 5497 | } |
| 5498 | |
| 5499 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5500 | struct io_poll_iocb *poll) |
| 5501 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5502 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5503 | |
| 5504 | spin_lock(&poll->head->lock); |
| 5505 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5506 | if (!list_empty(&poll->wait.entry)) { |
| 5507 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5508 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5509 | } |
| 5510 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5511 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5512 | return do_complete; |
| 5513 | } |
| 5514 | |
| 5515 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5516 | { |
| 5517 | bool do_complete; |
| 5518 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5519 | io_poll_remove_double(req); |
| 5520 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5521 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5522 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5523 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5524 | struct async_poll *apoll = req->apoll; |
| 5525 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5526 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5527 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5528 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5529 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5530 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5531 | kfree(apoll); |
| 5532 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5533 | } |
| 5534 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5535 | if (do_complete) { |
| 5536 | io_cqring_fill_event(req, -ECANCELED); |
| 5537 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5538 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5539 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5543 | } |
| 5544 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5545 | /* |
| 5546 | * Returns true if we found and killed one or more poll requests |
| 5547 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5548 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5549 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5550 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5551 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5552 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5553 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5554 | |
| 5555 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5556 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5557 | struct hlist_head *list; |
| 5558 | |
| 5559 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5560 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5561 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5562 | posted += io_poll_remove_one(req); |
| 5563 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5564 | } |
| 5565 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5566 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5567 | if (posted) |
| 5568 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5569 | |
| 5570 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5571 | } |
| 5572 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5573 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5574 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5575 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5576 | struct io_kiocb *req; |
| 5577 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5578 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5579 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5580 | if (sqe_addr != req->user_data) |
| 5581 | continue; |
| 5582 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5583 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5584 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | return -ENOENT; |
| 5588 | } |
| 5589 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5590 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5591 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5592 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5593 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5594 | return -EINVAL; |
| 5595 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5596 | sqe->poll_events) |
| 5597 | return -EINVAL; |
| 5598 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5599 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5600 | return 0; |
| 5601 | } |
| 5602 | |
| 5603 | /* |
| 5604 | * Find a running poll command that matches one specified in sqe->addr, |
| 5605 | * and remove it if found. |
| 5606 | */ |
| 5607 | static int io_poll_remove(struct io_kiocb *req) |
| 5608 | { |
| 5609 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5610 | int ret; |
| 5611 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5612 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5613 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5614 | spin_unlock_irq(&ctx->completion_lock); |
| 5615 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5616 | if (ret < 0) |
| 5617 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5618 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5619 | return 0; |
| 5620 | } |
| 5621 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5622 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5623 | void *key) |
| 5624 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5625 | struct io_kiocb *req = wait->private; |
| 5626 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5627 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5628 | 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] | 5629 | } |
| 5630 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5631 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5632 | struct poll_table_struct *p) |
| 5633 | { |
| 5634 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5635 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5636 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5637 | } |
| 5638 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5639 | 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] | 5640 | { |
| 5641 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5642 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5643 | |
| 5644 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5645 | return -EINVAL; |
| 5646 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5647 | return -EINVAL; |
| 5648 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5649 | events = READ_ONCE(sqe->poll32_events); |
| 5650 | #ifdef __BIG_ENDIAN |
| 5651 | events = swahw32(events); |
| 5652 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5653 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5654 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5655 | return 0; |
| 5656 | } |
| 5657 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5658 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5659 | { |
| 5660 | struct io_poll_iocb *poll = &req->poll; |
| 5661 | struct io_ring_ctx *ctx = req->ctx; |
| 5662 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5663 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5664 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5665 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5666 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5667 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5668 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5669 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5670 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5671 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5672 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5673 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5674 | spin_unlock_irq(&ctx->completion_lock); |
| 5675 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5676 | if (mask) { |
| 5677 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5678 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5679 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5680 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5681 | } |
| 5682 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5683 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5684 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5685 | struct io_timeout_data *data = container_of(timer, |
| 5686 | struct io_timeout_data, timer); |
| 5687 | struct io_kiocb *req = data->req; |
| 5688 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5689 | unsigned long flags; |
| 5690 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5691 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5692 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5693 | atomic_set(&req->ctx->cq_timeouts, |
| 5694 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5695 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5696 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5697 | io_commit_cqring(ctx); |
| 5698 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5699 | |
| 5700 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5701 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5702 | io_put_req(req); |
| 5703 | return HRTIMER_NORESTART; |
| 5704 | } |
| 5705 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5706 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5707 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5708 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5709 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5710 | struct io_kiocb *req; |
| 5711 | int ret = -ENOENT; |
| 5712 | |
| 5713 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5714 | if (user_data == req->user_data) { |
| 5715 | ret = 0; |
| 5716 | break; |
| 5717 | } |
| 5718 | } |
| 5719 | |
| 5720 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5721 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5722 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5723 | io = req->async_data; |
| 5724 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5725 | if (ret == -1) |
| 5726 | return ERR_PTR(-EALREADY); |
| 5727 | list_del_init(&req->timeout.list); |
| 5728 | return req; |
| 5729 | } |
| 5730 | |
| 5731 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5732 | { |
| 5733 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5734 | |
| 5735 | if (IS_ERR(req)) |
| 5736 | return PTR_ERR(req); |
| 5737 | |
| 5738 | req_set_fail_links(req); |
| 5739 | io_cqring_fill_event(req, -ECANCELED); |
| 5740 | io_put_req_deferred(req, 1); |
| 5741 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5742 | } |
| 5743 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5744 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5745 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5746 | { |
| 5747 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5748 | struct io_timeout_data *data; |
| 5749 | |
| 5750 | if (IS_ERR(req)) |
| 5751 | return PTR_ERR(req); |
| 5752 | |
| 5753 | req->timeout.off = 0; /* noseq */ |
| 5754 | data = req->async_data; |
| 5755 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5756 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5757 | data->timer.function = io_timeout_fn; |
| 5758 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5759 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5760 | } |
| 5761 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5762 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5763 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5764 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5765 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5766 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5767 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5768 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5769 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5770 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5771 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5772 | return -EINVAL; |
| 5773 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5774 | tr->addr = READ_ONCE(sqe->addr); |
| 5775 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5776 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5777 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5778 | return -EINVAL; |
| 5779 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5780 | return -EFAULT; |
| 5781 | } else if (tr->flags) { |
| 5782 | /* timeout removal doesn't support flags */ |
| 5783 | return -EINVAL; |
| 5784 | } |
| 5785 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5786 | return 0; |
| 5787 | } |
| 5788 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5789 | /* |
| 5790 | * Remove or update an existing timeout command |
| 5791 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5792 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5793 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5794 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5795 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5796 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5797 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5798 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5799 | if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) { |
| 5800 | enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS) |
| 5801 | ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; |
| 5802 | |
| 5803 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 5804 | } else { |
| 5805 | ret = io_timeout_cancel(ctx, tr->addr); |
| 5806 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5807 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5808 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5809 | io_commit_cqring(ctx); |
| 5810 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5811 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5812 | if (ret < 0) |
| 5813 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5814 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5815 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5816 | } |
| 5817 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5818 | 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] | 5819 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5820 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5821 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5822 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5823 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5824 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5825 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5826 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5827 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5828 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5829 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5830 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5831 | flags = READ_ONCE(sqe->timeout_flags); |
| 5832 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5833 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5834 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5835 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5836 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5837 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5838 | return -ENOMEM; |
| 5839 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5840 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5841 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5842 | |
| 5843 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5844 | return -EFAULT; |
| 5845 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5846 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5847 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5848 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5849 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5850 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5851 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5852 | return 0; |
| 5853 | } |
| 5854 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5855 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5856 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5857 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5858 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5859 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5860 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5861 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5862 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5863 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5864 | /* |
| 5865 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5866 | * timeout event to be satisfied. If it isn't set, then this is |
| 5867 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5868 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5869 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5870 | entry = ctx->timeout_list.prev; |
| 5871 | goto add; |
| 5872 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5873 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5874 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5875 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5876 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5877 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5878 | * This is safe because ->completion_lock is held, and submissions |
| 5879 | * and completions are never mixed in the same ->completion_lock section. |
| 5880 | */ |
| 5881 | ctx->cq_last_tm_flush = tail; |
| 5882 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5883 | /* |
| 5884 | * Insertion sort, ensuring the first entry in the list is always |
| 5885 | * the one we need first. |
| 5886 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5887 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5888 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5889 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5890 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5891 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5892 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5893 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5894 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5895 | break; |
| 5896 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5897 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5898 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5899 | data->timer.function = io_timeout_fn; |
| 5900 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5901 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5902 | return 0; |
| 5903 | } |
| 5904 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5905 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5906 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5907 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5908 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5909 | return req->user_data == (unsigned long) data; |
| 5910 | } |
| 5911 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5912 | 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] | 5913 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5914 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5915 | int ret = 0; |
| 5916 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5917 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5918 | switch (cancel_ret) { |
| 5919 | case IO_WQ_CANCEL_OK: |
| 5920 | ret = 0; |
| 5921 | break; |
| 5922 | case IO_WQ_CANCEL_RUNNING: |
| 5923 | ret = -EALREADY; |
| 5924 | break; |
| 5925 | case IO_WQ_CANCEL_NOTFOUND: |
| 5926 | ret = -ENOENT; |
| 5927 | break; |
| 5928 | } |
| 5929 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5930 | return ret; |
| 5931 | } |
| 5932 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5933 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5934 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5935 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5936 | { |
| 5937 | unsigned long flags; |
| 5938 | int ret; |
| 5939 | |
| 5940 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5941 | if (ret != -ENOENT) { |
| 5942 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5943 | goto done; |
| 5944 | } |
| 5945 | |
| 5946 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5947 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5948 | if (ret != -ENOENT) |
| 5949 | goto done; |
| 5950 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5951 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5952 | if (!ret) |
| 5953 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5954 | io_cqring_fill_event(req, ret); |
| 5955 | io_commit_cqring(ctx); |
| 5956 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5957 | io_cqring_ev_posted(ctx); |
| 5958 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5959 | if (ret < 0) |
| 5960 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5961 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5962 | } |
| 5963 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5964 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5965 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5966 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5967 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5968 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5969 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5970 | return -EINVAL; |
| 5971 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5972 | return -EINVAL; |
| 5973 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5974 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5975 | return 0; |
| 5976 | } |
| 5977 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5978 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5979 | { |
| 5980 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5981 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5982 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5983 | return 0; |
| 5984 | } |
| 5985 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5986 | static int io_files_update_prep(struct io_kiocb *req, |
| 5987 | const struct io_uring_sqe *sqe) |
| 5988 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5989 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5990 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5991 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5992 | return -EINVAL; |
| 5993 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5994 | return -EINVAL; |
| 5995 | |
| 5996 | req->files_update.offset = READ_ONCE(sqe->off); |
| 5997 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 5998 | if (!req->files_update.nr_args) |
| 5999 | return -EINVAL; |
| 6000 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 6001 | return 0; |
| 6002 | } |
| 6003 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6004 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
| 6005 | struct io_comp_state *cs) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6006 | { |
| 6007 | struct io_ring_ctx *ctx = req->ctx; |
| 6008 | struct io_uring_files_update up; |
| 6009 | int ret; |
| 6010 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 6011 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6012 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6013 | |
| 6014 | up.offset = req->files_update.offset; |
| 6015 | up.fds = req->files_update.arg; |
| 6016 | |
| 6017 | mutex_lock(&ctx->uring_lock); |
| 6018 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 6019 | mutex_unlock(&ctx->uring_lock); |
| 6020 | |
| 6021 | if (ret < 0) |
| 6022 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6023 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6024 | return 0; |
| 6025 | } |
| 6026 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6027 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6028 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6029 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6030 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6031 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6032 | case IORING_OP_READV: |
| 6033 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6034 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6035 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6036 | case IORING_OP_WRITEV: |
| 6037 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6038 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6039 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6040 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6041 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6042 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6043 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6044 | case IORING_OP_FSYNC: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6045 | return io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6046 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6047 | return io_prep_sfr(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6048 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6049 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6050 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6051 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6052 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6053 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6054 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6055 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6056 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6057 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6058 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6059 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6060 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6061 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6062 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6063 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6064 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6065 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6066 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6067 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6068 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6069 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6070 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6071 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6072 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6073 | return io_files_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6074 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6075 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6076 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6077 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6078 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6079 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6080 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6081 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6082 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6083 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6084 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6085 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6086 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6087 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6088 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6089 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6090 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6091 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6092 | case IORING_OP_SHUTDOWN: |
| 6093 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6094 | case IORING_OP_RENAMEAT: |
| 6095 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6096 | case IORING_OP_UNLINKAT: |
| 6097 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6098 | } |
| 6099 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6100 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6101 | req->opcode); |
| 6102 | return-EINVAL; |
| 6103 | } |
| 6104 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6105 | static int io_req_defer_prep(struct io_kiocb *req, |
| 6106 | const struct io_uring_sqe *sqe) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6107 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6108 | if (!sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6109 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6110 | if (io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6111 | return -EAGAIN; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6112 | return io_req_prep(req, sqe); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6113 | } |
| 6114 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6115 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6116 | { |
| 6117 | struct io_kiocb *pos; |
| 6118 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6119 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6120 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6121 | io_for_each_link(pos, req) |
| 6122 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6123 | |
| 6124 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 6125 | return total_submitted - nr_reqs; |
| 6126 | } |
| 6127 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6128 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6129 | { |
| 6130 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6131 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6132 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6133 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6134 | |
| 6135 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6136 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6137 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6138 | return 0; |
| 6139 | |
| 6140 | seq = io_get_sequence(req); |
| 6141 | /* Still a chance to pass the sequence check */ |
| 6142 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6143 | return 0; |
| 6144 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6145 | if (!req->async_data) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6146 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6147 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6148 | return ret; |
| 6149 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6150 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6151 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6152 | if (!de) |
| 6153 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6154 | |
| 6155 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6156 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6157 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6158 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6159 | io_queue_async_work(req); |
| 6160 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6164 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6165 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6166 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6167 | spin_unlock_irq(&ctx->completion_lock); |
| 6168 | return -EIOCBQUEUED; |
| 6169 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6170 | |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6171 | static void io_req_drop_files(struct io_kiocb *req) |
| 6172 | { |
| 6173 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 6174 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6175 | unsigned long flags; |
| 6176 | |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 6177 | if (req->work.flags & IO_WQ_WORK_FILES) { |
| 6178 | put_files_struct(req->work.identity->files); |
| 6179 | put_nsproxy(req->work.identity->nsproxy); |
| 6180 | } |
Pavel Begunkov | dfea9fc | 2020-12-18 13:12:21 +0000 | [diff] [blame] | 6181 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 6182 | list_del(&req->inflight_entry); |
| 6183 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 6184 | req->flags &= ~REQ_F_INFLIGHT; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6185 | req->work.flags &= ~IO_WQ_WORK_FILES; |
Pavel Begunkov | dfea9fc | 2020-12-18 13:12:21 +0000 | [diff] [blame] | 6186 | if (atomic_read(&tctx->in_idle)) |
| 6187 | wake_up(&tctx->wait); |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6188 | } |
| 6189 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 6190 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6191 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6192 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6193 | switch (req->opcode) { |
| 6194 | case IORING_OP_READV: |
| 6195 | case IORING_OP_READ_FIXED: |
| 6196 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6197 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6198 | break; |
| 6199 | case IORING_OP_RECVMSG: |
| 6200 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6201 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6202 | break; |
| 6203 | } |
| 6204 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6205 | } |
| 6206 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6207 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6208 | switch (req->opcode) { |
| 6209 | case IORING_OP_READV: |
| 6210 | case IORING_OP_READ_FIXED: |
| 6211 | case IORING_OP_READ: |
| 6212 | case IORING_OP_WRITEV: |
| 6213 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6214 | case IORING_OP_WRITE: { |
| 6215 | struct io_async_rw *io = req->async_data; |
| 6216 | if (io->free_iovec) |
| 6217 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6218 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6219 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6220 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6221 | case IORING_OP_SENDMSG: { |
| 6222 | struct io_async_msghdr *io = req->async_data; |
| 6223 | if (io->iov != io->fast_iov) |
| 6224 | kfree(io->iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6225 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6226 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6227 | case IORING_OP_SPLICE: |
| 6228 | case IORING_OP_TEE: |
| 6229 | io_put_file(req, req->splice.file_in, |
| 6230 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 6231 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6232 | case IORING_OP_OPENAT: |
| 6233 | case IORING_OP_OPENAT2: |
| 6234 | if (req->open.filename) |
| 6235 | putname(req->open.filename); |
| 6236 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6237 | case IORING_OP_RENAMEAT: |
| 6238 | putname(req->rename.oldpath); |
| 6239 | putname(req->rename.newpath); |
| 6240 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6241 | case IORING_OP_UNLINKAT: |
| 6242 | putname(req->unlink.filename); |
| 6243 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6244 | } |
| 6245 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6246 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6247 | } |
| 6248 | |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6249 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
| 6250 | struct io_comp_state *cs) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6251 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6252 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6253 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6254 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6255 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6256 | case IORING_OP_NOP: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6257 | ret = io_nop(req, cs); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6258 | break; |
| 6259 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6260 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6261 | case IORING_OP_READ: |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 6262 | ret = io_read(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6263 | break; |
| 6264 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6265 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6266 | case IORING_OP_WRITE: |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 6267 | ret = io_write(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6268 | break; |
| 6269 | case IORING_OP_FSYNC: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6270 | ret = io_fsync(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6271 | break; |
| 6272 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6273 | ret = io_poll_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6274 | break; |
| 6275 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6276 | ret = io_poll_remove(req); |
| 6277 | break; |
| 6278 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6279 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6280 | break; |
| 6281 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6282 | ret = io_sendmsg(req, force_nonblock, cs); |
| 6283 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6284 | case IORING_OP_SEND: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6285 | ret = io_send(req, force_nonblock, cs); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6286 | break; |
| 6287 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6288 | ret = io_recvmsg(req, force_nonblock, cs); |
| 6289 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6290 | case IORING_OP_RECV: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6291 | ret = io_recv(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6292 | break; |
| 6293 | case IORING_OP_TIMEOUT: |
| 6294 | ret = io_timeout(req); |
| 6295 | break; |
| 6296 | case IORING_OP_TIMEOUT_REMOVE: |
| 6297 | ret = io_timeout_remove(req); |
| 6298 | break; |
| 6299 | case IORING_OP_ACCEPT: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6300 | ret = io_accept(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6301 | break; |
| 6302 | case IORING_OP_CONNECT: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6303 | ret = io_connect(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6304 | break; |
| 6305 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6306 | ret = io_async_cancel(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6307 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6308 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6309 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6310 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6311 | case IORING_OP_OPENAT: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6312 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6313 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6314 | case IORING_OP_CLOSE: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6315 | ret = io_close(req, force_nonblock, cs); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6316 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6317 | case IORING_OP_FILES_UPDATE: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6318 | ret = io_files_update(req, force_nonblock, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6319 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6320 | case IORING_OP_STATX: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6321 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6322 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6323 | case IORING_OP_FADVISE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6324 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6325 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6326 | case IORING_OP_MADVISE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6327 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6328 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6329 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6330 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6331 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6332 | case IORING_OP_EPOLL_CTL: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6333 | ret = io_epoll_ctl(req, force_nonblock, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6334 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6335 | case IORING_OP_SPLICE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6336 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6337 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6338 | case IORING_OP_PROVIDE_BUFFERS: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6339 | ret = io_provide_buffers(req, force_nonblock, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6340 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6341 | case IORING_OP_REMOVE_BUFFERS: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6342 | ret = io_remove_buffers(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6343 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6344 | case IORING_OP_TEE: |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6345 | ret = io_tee(req, force_nonblock); |
| 6346 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6347 | case IORING_OP_SHUTDOWN: |
| 6348 | ret = io_shutdown(req, force_nonblock); |
| 6349 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6350 | case IORING_OP_RENAMEAT: |
| 6351 | ret = io_renameat(req, force_nonblock); |
| 6352 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6353 | case IORING_OP_UNLINKAT: |
| 6354 | ret = io_unlinkat(req, force_nonblock); |
| 6355 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6356 | default: |
| 6357 | ret = -EINVAL; |
| 6358 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6359 | } |
| 6360 | |
| 6361 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6362 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6363 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6364 | /* If the op doesn't have a file, we're not polling for it */ |
| 6365 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6366 | const bool in_async = io_wq_current_is_worker(); |
| 6367 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6368 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6369 | if (in_async) |
| 6370 | mutex_lock(&ctx->uring_lock); |
| 6371 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6372 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6373 | |
| 6374 | if (in_async) |
| 6375 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | return 0; |
| 6379 | } |
| 6380 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 6381 | static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6382 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6383 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6384 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6385 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6386 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6387 | timeout = io_prep_linked_timeout(req); |
| 6388 | if (timeout) |
| 6389 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6390 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 6391 | /* if NO_CANCEL is set, we must still run the work */ |
| 6392 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 6393 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6394 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 6395 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6396 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6397 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6398 | do { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6399 | ret = io_issue_sqe(req, false, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6400 | /* |
| 6401 | * We can get EAGAIN for polled IO even though we're |
| 6402 | * forcing a sync submission from here, since we can't |
| 6403 | * wait for request slots on the block side. |
| 6404 | */ |
| 6405 | if (ret != -EAGAIN) |
| 6406 | break; |
| 6407 | cond_resched(); |
| 6408 | } while (1); |
| 6409 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6410 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6411 | if (ret) { |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6412 | struct io_ring_ctx *lock_ctx = NULL; |
Xiaoguang Wang | dad1b12 | 2020-12-06 22:22:42 +0000 | [diff] [blame] | 6413 | |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6414 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 6415 | lock_ctx = req->ctx; |
| 6416 | |
| 6417 | /* |
| 6418 | * io_iopoll_complete() does not hold completion_lock to |
| 6419 | * complete polled io, so here for polled io, we can not call |
| 6420 | * io_req_complete() directly, otherwise there maybe concurrent |
| 6421 | * access to cqring, defer_list, etc, which is not safe. Given |
| 6422 | * that io_iopoll_complete() is always called under uring_lock, |
| 6423 | * so here for polled io, we also get uring_lock to complete |
| 6424 | * it. |
| 6425 | */ |
| 6426 | if (lock_ctx) |
| 6427 | mutex_lock(&lock_ctx->uring_lock); |
| 6428 | |
| 6429 | req_set_fail_links(req); |
| 6430 | io_req_complete(req, ret); |
| 6431 | |
| 6432 | if (lock_ctx) |
| 6433 | mutex_unlock(&lock_ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6434 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6435 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 6436 | return io_steal_work(req); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6437 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6438 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6439 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6440 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6441 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6442 | struct fixed_file_table *table; |
| 6443 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6444 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6445 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6446 | } |
| 6447 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6448 | static struct file *io_file_get(struct io_submit_state *state, |
| 6449 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6450 | { |
| 6451 | struct io_ring_ctx *ctx = req->ctx; |
| 6452 | struct file *file; |
| 6453 | |
| 6454 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6455 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6456 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6457 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6458 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6459 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6460 | } else { |
| 6461 | trace_io_uring_file_get(ctx, fd); |
| 6462 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6463 | } |
| 6464 | |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 6465 | if (file && file->f_op == &io_uring_fops) { |
| 6466 | io_req_init_async(req); |
| 6467 | req->flags |= REQ_F_INFLIGHT; |
| 6468 | |
| 6469 | spin_lock_irq(&ctx->inflight_lock); |
| 6470 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 6471 | spin_unlock_irq(&ctx->inflight_lock); |
| 6472 | } |
| 6473 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6474 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6475 | } |
| 6476 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6477 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6478 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6479 | struct io_timeout_data *data = container_of(timer, |
| 6480 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6481 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6482 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6483 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6484 | |
| 6485 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6486 | prev = req->timeout.head; |
| 6487 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6488 | |
| 6489 | /* |
| 6490 | * We don't expect the list to be empty, that will only happen if we |
| 6491 | * race with the completion of the linked work. |
| 6492 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6493 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6494 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6495 | else |
| 6496 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6497 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6498 | |
| 6499 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6500 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6501 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6502 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6503 | } else { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6504 | io_req_complete(req, -ETIME); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6505 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6506 | return HRTIMER_NORESTART; |
| 6507 | } |
| 6508 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6509 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6510 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6511 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6512 | * If the back reference is NULL, then our linked request finished |
| 6513 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6514 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6515 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6516 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6517 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6518 | data->timer.function = io_link_timeout_fn; |
| 6519 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6520 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6521 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6522 | } |
| 6523 | |
| 6524 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6525 | { |
| 6526 | struct io_ring_ctx *ctx = req->ctx; |
| 6527 | |
| 6528 | spin_lock_irq(&ctx->completion_lock); |
| 6529 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6530 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6531 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6532 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6533 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6534 | } |
| 6535 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6536 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6537 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6538 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6539 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6540 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6541 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6542 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6543 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6544 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6545 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6546 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6547 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6548 | } |
| 6549 | |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6550 | static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6551 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6552 | struct io_kiocb *linked_timeout; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6553 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6554 | int ret; |
| 6555 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6556 | again: |
| 6557 | linked_timeout = io_prep_linked_timeout(req); |
| 6558 | |
Pavel Begunkov | 2e5aa6c | 2020-10-18 10:17:37 +0100 | [diff] [blame] | 6559 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 6560 | (req->work.flags & IO_WQ_WORK_CREDS) && |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6561 | req->work.identity->creds != current_cred()) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6562 | if (old_creds) |
| 6563 | revert_creds(old_creds); |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6564 | if (old_creds == req->work.identity->creds) |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6565 | old_creds = NULL; /* restored original creds */ |
| 6566 | else |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6567 | old_creds = override_creds(req->work.identity->creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6568 | } |
| 6569 | |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6570 | ret = io_issue_sqe(req, true, cs); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6571 | |
| 6572 | /* |
| 6573 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6574 | * doesn't support non-blocking read/write attempts |
| 6575 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6576 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6577 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6578 | /* |
| 6579 | * Queued up for async execution, worker will release |
| 6580 | * submit reference when the iocb is actually submitted. |
| 6581 | */ |
| 6582 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6583 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 6584 | |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6585 | if (linked_timeout) |
| 6586 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6587 | } else if (likely(!ret)) { |
| 6588 | /* drop submission reference */ |
| 6589 | req = io_put_req_find_next(req); |
| 6590 | if (linked_timeout) |
| 6591 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6592 | |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6593 | if (req) { |
| 6594 | if (!(req->flags & REQ_F_FORCE_ASYNC)) |
| 6595 | goto again; |
| 6596 | io_queue_async_work(req); |
| 6597 | } |
| 6598 | } else { |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6599 | /* un-prep timeout, so it'll be killed as any other linked */ |
| 6600 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6601 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6602 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6603 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6604 | } |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6605 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6606 | if (old_creds) |
| 6607 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6608 | } |
| 6609 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6610 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 6611 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6612 | { |
| 6613 | int ret; |
| 6614 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6615 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6616 | if (ret) { |
| 6617 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6618 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6619 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6620 | io_put_req(req); |
| 6621 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6622 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6623 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6624 | if (!req->async_data) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6625 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6626 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6627 | goto fail_req; |
| 6628 | } |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6629 | io_queue_async_work(req); |
| 6630 | } else { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6631 | if (sqe) { |
| 6632 | ret = io_req_prep(req, sqe); |
| 6633 | if (unlikely(ret)) |
| 6634 | goto fail_req; |
| 6635 | } |
| 6636 | __io_queue_sqe(req, cs); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6637 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6638 | } |
| 6639 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6640 | static inline void io_queue_link_head(struct io_kiocb *req, |
| 6641 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6642 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6643 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6644 | io_put_req(req); |
| 6645 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6646 | } else |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6647 | io_queue_sqe(req, NULL, cs); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6648 | } |
| 6649 | |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6650 | struct io_submit_link { |
| 6651 | struct io_kiocb *head; |
| 6652 | struct io_kiocb *last; |
| 6653 | }; |
| 6654 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6655 | static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6656 | struct io_submit_link *link, struct io_comp_state *cs) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6657 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6658 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6659 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6660 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6661 | /* |
| 6662 | * If we already have a head request, queue this one for async |
| 6663 | * submittal once the head completes. If we don't have a head but |
| 6664 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6665 | * submitted sync once the chain is complete. If none of those |
| 6666 | * conditions are true (normal request), then just queue it. |
| 6667 | */ |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6668 | if (link->head) { |
| 6669 | struct io_kiocb *head = link->head; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6670 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6671 | /* |
| 6672 | * Taking sequential execution of a link, draining both sides |
| 6673 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6674 | * requests in the link. So, it drains the head and the |
| 6675 | * next after the link request. The last one is done via |
| 6676 | * drain_next flag to persist the effect across calls. |
| 6677 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6678 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6679 | head->flags |= REQ_F_IO_DRAIN; |
| 6680 | ctx->drain_next = 1; |
| 6681 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6682 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6683 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6684 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6685 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6686 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6687 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6688 | trace_io_uring_link(ctx, req, head); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6689 | link->last->link = req; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6690 | link->last = req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6691 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6692 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6693 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6694 | io_queue_link_head(head, cs); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6695 | link->head = NULL; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6696 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6697 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6698 | if (unlikely(ctx->drain_next)) { |
| 6699 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6700 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6701 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6702 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6703 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6704 | if (unlikely(ret)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6705 | req->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6706 | link->head = req; |
| 6707 | link->last = req; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6708 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6709 | io_queue_sqe(req, sqe, cs); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6710 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6711 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6712 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6713 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6714 | } |
| 6715 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6716 | /* |
| 6717 | * Batched submission is done, ensure local IO is flushed out. |
| 6718 | */ |
| 6719 | static void io_submit_state_end(struct io_submit_state *state) |
| 6720 | { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6721 | if (!list_empty(&state->comp.list)) |
| 6722 | io_submit_flush_completions(&state->comp); |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6723 | if (state->plug_started) |
| 6724 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6725 | io_state_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6726 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 6727 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6728 | } |
| 6729 | |
| 6730 | /* |
| 6731 | * Start submission side cache. |
| 6732 | */ |
| 6733 | static void io_submit_state_start(struct io_submit_state *state, |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6734 | struct io_ring_ctx *ctx, unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6735 | { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6736 | state->plug_started = false; |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6737 | state->comp.nr = 0; |
| 6738 | INIT_LIST_HEAD(&state->comp.list); |
| 6739 | state->comp.ctx = ctx; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6740 | state->free_reqs = 0; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 6741 | state->file_refs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6742 | state->ios_left = max_ios; |
| 6743 | } |
| 6744 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6745 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6746 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6747 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6748 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6749 | /* |
| 6750 | * Ensure any loads from the SQEs are done at this point, |
| 6751 | * since once we write the new head, the application could |
| 6752 | * write new data to them. |
| 6753 | */ |
| 6754 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6755 | } |
| 6756 | |
| 6757 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6758 | * 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] | 6759 | * that is mapped by userspace. This means that care needs to be taken to |
| 6760 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6761 | * being a good citizen. If members of the sqe are validated and then later |
| 6762 | * used, it's important that those reads are done through READ_ONCE() to |
| 6763 | * prevent a re-load down the line. |
| 6764 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6765 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6766 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6767 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6768 | unsigned head; |
| 6769 | |
| 6770 | /* |
| 6771 | * The cached sq head (or cq tail) serves two purposes: |
| 6772 | * |
| 6773 | * 1) allows us to batch the cost of updating the user visible |
| 6774 | * head updates. |
| 6775 | * 2) allows the kernel side to track the head on its own, even |
| 6776 | * though the application is the one updating it. |
| 6777 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6778 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6779 | if (likely(head < ctx->sq_entries)) |
| 6780 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6781 | |
| 6782 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6783 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6784 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6785 | return NULL; |
| 6786 | } |
| 6787 | |
| 6788 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) |
| 6789 | { |
| 6790 | ctx->cached_sq_head++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6791 | } |
| 6792 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6793 | /* |
| 6794 | * Check SQE restrictions (opcode and flags). |
| 6795 | * |
| 6796 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6797 | */ |
| 6798 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6799 | struct io_kiocb *req, |
| 6800 | unsigned int sqe_flags) |
| 6801 | { |
| 6802 | if (!ctx->restricted) |
| 6803 | return true; |
| 6804 | |
| 6805 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6806 | return false; |
| 6807 | |
| 6808 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6809 | ctx->restrictions.sqe_flags_required) |
| 6810 | return false; |
| 6811 | |
| 6812 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6813 | ctx->restrictions.sqe_flags_required)) |
| 6814 | return false; |
| 6815 | |
| 6816 | return true; |
| 6817 | } |
| 6818 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6819 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6820 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6821 | IOSQE_BUFFER_SELECT) |
| 6822 | |
| 6823 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6824 | const struct io_uring_sqe *sqe, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6825 | struct io_submit_state *state) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6826 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6827 | unsigned int sqe_flags; |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6828 | int id, ret; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6829 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6830 | req->opcode = READ_ONCE(sqe->opcode); |
| 6831 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6832 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6833 | req->file = NULL; |
| 6834 | req->ctx = ctx; |
| 6835 | req->flags = 0; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6836 | req->link = NULL; |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6837 | req->fixed_file_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6838 | /* one is dropped after submission, the other at completion */ |
| 6839 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6840 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6841 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6842 | |
| 6843 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6844 | return -EINVAL; |
| 6845 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6846 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6847 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6848 | |
| 6849 | sqe_flags = READ_ONCE(sqe->flags); |
| 6850 | /* enforce forwards compatibility on users */ |
| 6851 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6852 | return -EINVAL; |
| 6853 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6854 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6855 | return -EACCES; |
| 6856 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6857 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6858 | !io_op_defs[req->opcode].buffer_select) |
| 6859 | return -EOPNOTSUPP; |
| 6860 | |
| 6861 | id = READ_ONCE(sqe->personality); |
| 6862 | if (id) { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6863 | struct io_identity *iod; |
| 6864 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6865 | iod = idr_find(&ctx->personality_idr, id); |
| 6866 | if (unlikely(!iod)) |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6867 | return -EINVAL; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6868 | refcount_inc(&iod->count); |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 6869 | |
| 6870 | __io_req_init_async(req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6871 | get_cred(iod->creds); |
| 6872 | req->work.identity = iod; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6873 | req->work.flags |= IO_WQ_WORK_CREDS; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6874 | } |
| 6875 | |
| 6876 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 6877 | req->flags |= sqe_flags; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6878 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6879 | /* |
| 6880 | * Plug now if we have more than 1 IO left after this, and the target |
| 6881 | * is potentially a read/write to block based storage. |
| 6882 | */ |
| 6883 | if (!state->plug_started && state->ios_left > 1 && |
| 6884 | io_op_defs[req->opcode].plug) { |
| 6885 | blk_start_plug(&state->plug); |
| 6886 | state->plug_started = true; |
| 6887 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6888 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6889 | ret = 0; |
| 6890 | if (io_op_defs[req->opcode].needs_file) { |
| 6891 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6892 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6893 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
| 6894 | if (unlikely(!req->file && |
| 6895 | !io_op_defs[req->opcode].needs_file_no_error)) |
| 6896 | ret = -EBADF; |
| 6897 | } |
| 6898 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6899 | state->ios_left--; |
| 6900 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6901 | } |
| 6902 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6903 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6904 | { |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6905 | struct io_submit_state state; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6906 | struct io_submit_link link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6907 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6908 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6909 | /* 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] | 6910 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6911 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6912 | return -EBUSY; |
| 6913 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6914 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6915 | /* make sure SQ entry isn't read before tail */ |
| 6916 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6917 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6918 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6919 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6920 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6921 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6922 | refcount_add(nr, ¤t->usage); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6923 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6924 | io_submit_state_start(&state, ctx, nr); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6925 | link.head = NULL; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6926 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6927 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6928 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6929 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6930 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6931 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 6932 | sqe = io_get_sqe(ctx); |
| 6933 | if (unlikely(!sqe)) { |
| 6934 | io_consume_sqe(ctx); |
| 6935 | break; |
| 6936 | } |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6937 | req = io_alloc_req(ctx, &state); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6938 | if (unlikely(!req)) { |
| 6939 | if (!submitted) |
| 6940 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6941 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6942 | } |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6943 | io_consume_sqe(ctx); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6944 | /* will complete beyond this point, count as submitted */ |
| 6945 | submitted++; |
| 6946 | |
Pavel Begunkov | 692d836 | 2020-10-10 18:34:13 +0100 | [diff] [blame] | 6947 | err = io_init_req(ctx, req, sqe, &state); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6948 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6949 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6950 | io_put_req(req); |
| 6951 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6952 | break; |
| 6953 | } |
| 6954 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6955 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6956 | true, io_async_submit(ctx)); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6957 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6958 | if (err) |
| 6959 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6960 | } |
| 6961 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6962 | if (unlikely(submitted != nr)) { |
| 6963 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6964 | struct io_uring_task *tctx = current->io_uring; |
| 6965 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6966 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6967 | percpu_ref_put_many(&ctx->refs, unused); |
| 6968 | percpu_counter_sub(&tctx->inflight, unused); |
| 6969 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6970 | } |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6971 | if (link.head) |
| 6972 | io_queue_link_head(link.head, &state.comp); |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6973 | io_submit_state_end(&state); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6974 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6975 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6976 | io_commit_sqring(ctx); |
| 6977 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6978 | return submitted; |
| 6979 | } |
| 6980 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6981 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6982 | { |
| 6983 | /* Tell userspace we may need a wakeup call */ |
| 6984 | spin_lock_irq(&ctx->completion_lock); |
| 6985 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6986 | spin_unlock_irq(&ctx->completion_lock); |
| 6987 | } |
| 6988 | |
| 6989 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6990 | { |
| 6991 | spin_lock_irq(&ctx->completion_lock); |
| 6992 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6993 | spin_unlock_irq(&ctx->completion_lock); |
| 6994 | } |
| 6995 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6996 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6997 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6998 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6999 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7000 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7001 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7002 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 7003 | if (cap_entries && to_submit > 8) |
| 7004 | to_submit = 8; |
| 7005 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7006 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 7007 | unsigned nr_events = 0; |
| 7008 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7009 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7010 | if (!list_empty(&ctx->iopoll_list)) |
| 7011 | io_do_iopoll(ctx, &nr_events, 0); |
| 7012 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 7013 | if (to_submit && !ctx->sqo_dead && |
| 7014 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7015 | ret = io_submit_sqes(ctx, to_submit); |
| 7016 | mutex_unlock(&ctx->uring_lock); |
| 7017 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7018 | |
| 7019 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7020 | wake_up(&ctx->sqo_sq_wait); |
| 7021 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7022 | return ret; |
| 7023 | } |
| 7024 | |
| 7025 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7026 | { |
| 7027 | struct io_ring_ctx *ctx; |
| 7028 | unsigned sq_thread_idle = 0; |
| 7029 | |
| 7030 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7031 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 7032 | sq_thread_idle = ctx->sq_thread_idle; |
| 7033 | } |
| 7034 | |
| 7035 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7036 | } |
| 7037 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7038 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 7039 | { |
| 7040 | struct io_ring_ctx *ctx; |
| 7041 | |
| 7042 | while (!list_empty(&sqd->ctx_new_list)) { |
| 7043 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7044 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 7045 | complete(&ctx->sq_thread_comp); |
| 7046 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7047 | |
| 7048 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7049 | } |
| 7050 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7051 | static int io_sq_thread(void *data) |
| 7052 | { |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7053 | struct cgroup_subsys_state *cur_css = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7054 | struct files_struct *old_files = current->files; |
| 7055 | struct nsproxy *old_nsproxy = current->nsproxy; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7056 | const struct cred *old_cred = NULL; |
| 7057 | struct io_sq_data *sqd = data; |
| 7058 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7059 | unsigned long timeout = 0; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7060 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7061 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7062 | task_lock(current); |
| 7063 | current->files = NULL; |
| 7064 | current->nsproxy = NULL; |
| 7065 | task_unlock(current); |
| 7066 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7067 | while (!kthread_should_stop()) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7068 | int ret; |
| 7069 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7070 | |
| 7071 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7072 | * Any changes to the sqd lists are synchronized through the |
| 7073 | * kthread parking. This synchronizes the thread vs users, |
| 7074 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7075 | */ |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7076 | if (kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7077 | kthread_parkme(); |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7078 | /* |
| 7079 | * When sq thread is unparked, in case the previous park operation |
| 7080 | * comes from io_put_sq_data(), which means that sq thread is going |
| 7081 | * to be stopped, so here needs to have a check. |
| 7082 | */ |
| 7083 | if (kthread_should_stop()) |
| 7084 | break; |
| 7085 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7086 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7087 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7088 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7089 | timeout = jiffies + sqd->sq_thread_idle; |
| 7090 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7091 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7092 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7093 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7094 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7095 | if (current->cred != ctx->creds) { |
| 7096 | if (old_cred) |
| 7097 | revert_creds(old_cred); |
| 7098 | old_cred = override_creds(ctx->creds); |
| 7099 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7100 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 7101 | #ifdef CONFIG_AUDIT |
| 7102 | current->loginuid = ctx->loginuid; |
| 7103 | current->sessionid = ctx->sessionid; |
| 7104 | #endif |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7105 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7106 | ret = __io_sq_thread(ctx, cap_entries); |
| 7107 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7108 | sqt_spin = true; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7109 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7110 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7111 | } |
| 7112 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7113 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7114 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7115 | io_sq_thread_drop_mm_files(); |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7116 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7117 | if (sqt_spin) |
| 7118 | timeout = jiffies + sqd->sq_thread_idle; |
| 7119 | continue; |
| 7120 | } |
| 7121 | |
| 7122 | if (kthread_should_park()) |
| 7123 | continue; |
| 7124 | |
| 7125 | needs_sched = true; |
| 7126 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 7127 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7128 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7129 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7130 | needs_sched = false; |
| 7131 | break; |
| 7132 | } |
| 7133 | if (io_sqring_entries(ctx)) { |
| 7134 | needs_sched = false; |
| 7135 | break; |
| 7136 | } |
| 7137 | } |
| 7138 | |
| 7139 | if (needs_sched) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7140 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7141 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7142 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7143 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7144 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7145 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7146 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7147 | |
| 7148 | finish_wait(&sqd->wait, &wait); |
| 7149 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7150 | } |
| 7151 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7152 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7153 | io_sq_thread_drop_mm_files(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7154 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7155 | if (cur_css) |
| 7156 | io_sq_thread_unassociate_blkcg(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7157 | if (old_cred) |
| 7158 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7159 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7160 | task_lock(current); |
| 7161 | current->files = old_files; |
| 7162 | current->nsproxy = old_nsproxy; |
| 7163 | task_unlock(current); |
| 7164 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7165 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7166 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7167 | return 0; |
| 7168 | } |
| 7169 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7170 | struct io_wait_queue { |
| 7171 | struct wait_queue_entry wq; |
| 7172 | struct io_ring_ctx *ctx; |
| 7173 | unsigned to_wait; |
| 7174 | unsigned nr_timeouts; |
| 7175 | }; |
| 7176 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7177 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7178 | { |
| 7179 | struct io_ring_ctx *ctx = iowq->ctx; |
| 7180 | |
| 7181 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7182 | * 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] | 7183 | * started waiting. For timeouts, we always want to return to userspace, |
| 7184 | * regardless of event count. |
| 7185 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7186 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7187 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 7188 | } |
| 7189 | |
| 7190 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7191 | int wake_flags, void *key) |
| 7192 | { |
| 7193 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7194 | wq); |
| 7195 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7196 | /* |
| 7197 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7198 | * the task, and the next invocation will do it. |
| 7199 | */ |
| 7200 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 7201 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7202 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7203 | } |
| 7204 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7205 | static int io_run_task_work_sig(void) |
| 7206 | { |
| 7207 | if (io_run_task_work()) |
| 7208 | return 1; |
| 7209 | if (!signal_pending(current)) |
| 7210 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7211 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 7212 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7213 | return -EINTR; |
| 7214 | } |
| 7215 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7216 | /* |
| 7217 | * Wait until events become available, if we don't already have some. The |
| 7218 | * application must reap them itself, as they reside on the shared cq ring. |
| 7219 | */ |
| 7220 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7221 | const sigset_t __user *sig, size_t sigsz, |
| 7222 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7223 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7224 | struct io_wait_queue iowq = { |
| 7225 | .wq = { |
| 7226 | .private = current, |
| 7227 | .func = io_wake_function, |
| 7228 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 7229 | }, |
| 7230 | .ctx = ctx, |
| 7231 | .to_wait = min_events, |
| 7232 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7233 | struct io_rings *rings = ctx->rings; |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7234 | struct timespec64 ts; |
| 7235 | signed long timeout = 0; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 7236 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7237 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7238 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7239 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 7240 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7241 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7242 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7243 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7244 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7245 | |
| 7246 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7247 | #ifdef CONFIG_COMPAT |
| 7248 | if (in_compat_syscall()) |
| 7249 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7250 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7251 | else |
| 7252 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7253 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7254 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7255 | if (ret) |
| 7256 | return ret; |
| 7257 | } |
| 7258 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7259 | if (uts) { |
| 7260 | if (get_timespec64(&ts, uts)) |
| 7261 | return -EFAULT; |
| 7262 | timeout = timespec64_to_jiffies(&ts); |
| 7263 | } |
| 7264 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7265 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7266 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7267 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7268 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7269 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7270 | TASK_INTERRUPTIBLE); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 7271 | /* make sure we run task_work before checking for signals */ |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7272 | ret = io_run_task_work_sig(); |
| 7273 | if (ret > 0) |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7274 | continue; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7275 | else if (ret < 0) |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 7276 | break; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7277 | if (io_should_wake(&iowq)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7278 | break; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7279 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 7280 | continue; |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7281 | if (uts) { |
| 7282 | timeout = schedule_timeout(timeout); |
| 7283 | if (timeout == 0) { |
| 7284 | ret = -ETIME; |
| 7285 | break; |
| 7286 | } |
| 7287 | } else { |
| 7288 | schedule(); |
| 7289 | } |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7290 | } while (1); |
| 7291 | finish_wait(&ctx->wait, &iowq.wq); |
| 7292 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7293 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7294 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7295 | 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] | 7296 | } |
| 7297 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7298 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7299 | { |
| 7300 | #if defined(CONFIG_UNIX) |
| 7301 | if (ctx->ring_sock) { |
| 7302 | struct sock *sock = ctx->ring_sock->sk; |
| 7303 | struct sk_buff *skb; |
| 7304 | |
| 7305 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7306 | kfree_skb(skb); |
| 7307 | } |
| 7308 | #else |
| 7309 | int i; |
| 7310 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7311 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7312 | struct file *file; |
| 7313 | |
| 7314 | file = io_file_from_index(ctx, i); |
| 7315 | if (file) |
| 7316 | fput(file); |
| 7317 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7318 | #endif |
| 7319 | } |
| 7320 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7321 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 7322 | { |
| 7323 | struct fixed_file_data *data; |
| 7324 | |
| 7325 | data = container_of(ref, struct fixed_file_data, refs); |
| 7326 | complete(&data->done); |
| 7327 | } |
| 7328 | |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7329 | static void io_sqe_files_set_node(struct fixed_file_data *file_data, |
| 7330 | struct fixed_file_ref_node *ref_node) |
| 7331 | { |
| 7332 | spin_lock_bh(&file_data->lock); |
| 7333 | file_data->node = ref_node; |
| 7334 | list_add_tail(&ref_node->node, &file_data->ref_list); |
| 7335 | spin_unlock_bh(&file_data->lock); |
| 7336 | percpu_ref_get(&file_data->refs); |
| 7337 | } |
| 7338 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7339 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7340 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7341 | struct fixed_file_data *data = ctx->file_data; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7342 | struct fixed_file_ref_node *backup_node, *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7343 | unsigned nr_tables, i; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7344 | int ret; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7345 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7346 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7347 | return -ENXIO; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7348 | backup_node = alloc_fixed_file_ref_node(ctx); |
| 7349 | if (!backup_node) |
| 7350 | return -ENOMEM; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7351 | |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7352 | spin_lock_bh(&data->lock); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 7353 | ref_node = data->node; |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7354 | spin_unlock_bh(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7355 | if (ref_node) |
| 7356 | percpu_ref_kill(&ref_node->refs); |
| 7357 | |
| 7358 | percpu_ref_kill(&data->refs); |
| 7359 | |
| 7360 | /* wait for all refs nodes to complete */ |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7361 | flush_delayed_work(&ctx->file_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7362 | do { |
| 7363 | ret = wait_for_completion_interruptible(&data->done); |
| 7364 | if (!ret) |
| 7365 | break; |
| 7366 | ret = io_run_task_work_sig(); |
| 7367 | if (ret < 0) { |
| 7368 | percpu_ref_resurrect(&data->refs); |
| 7369 | reinit_completion(&data->done); |
| 7370 | io_sqe_files_set_node(data, backup_node); |
| 7371 | return ret; |
| 7372 | } |
| 7373 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7374 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7375 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7376 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7377 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7378 | kfree(data->table[i].files); |
| 7379 | kfree(data->table); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7380 | percpu_ref_exit(&data->refs); |
| 7381 | kfree(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7382 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7383 | ctx->nr_user_files = 0; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7384 | destroy_fixed_file_ref_node(backup_node); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7385 | return 0; |
| 7386 | } |
| 7387 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7388 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7389 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7390 | if (refcount_dec_and_test(&sqd->refs)) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7391 | /* |
| 7392 | * The park is a bit of a work-around, without it we get |
| 7393 | * warning spews on shutdown with SQPOLL set and affinity |
| 7394 | * set to a single CPU. |
| 7395 | */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7396 | if (sqd->thread) { |
| 7397 | kthread_park(sqd->thread); |
| 7398 | kthread_stop(sqd->thread); |
| 7399 | } |
| 7400 | |
| 7401 | kfree(sqd); |
| 7402 | } |
| 7403 | } |
| 7404 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7405 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7406 | { |
| 7407 | struct io_ring_ctx *ctx_attach; |
| 7408 | struct io_sq_data *sqd; |
| 7409 | struct fd f; |
| 7410 | |
| 7411 | f = fdget(p->wq_fd); |
| 7412 | if (!f.file) |
| 7413 | return ERR_PTR(-ENXIO); |
| 7414 | if (f.file->f_op != &io_uring_fops) { |
| 7415 | fdput(f); |
| 7416 | return ERR_PTR(-EINVAL); |
| 7417 | } |
| 7418 | |
| 7419 | ctx_attach = f.file->private_data; |
| 7420 | sqd = ctx_attach->sq_data; |
| 7421 | if (!sqd) { |
| 7422 | fdput(f); |
| 7423 | return ERR_PTR(-EINVAL); |
| 7424 | } |
| 7425 | |
| 7426 | refcount_inc(&sqd->refs); |
| 7427 | fdput(f); |
| 7428 | return sqd; |
| 7429 | } |
| 7430 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7431 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7432 | { |
| 7433 | struct io_sq_data *sqd; |
| 7434 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7435 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7436 | return io_attach_sq_data(p); |
| 7437 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7438 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7439 | if (!sqd) |
| 7440 | return ERR_PTR(-ENOMEM); |
| 7441 | |
| 7442 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7443 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7444 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7445 | mutex_init(&sqd->ctx_lock); |
| 7446 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7447 | init_waitqueue_head(&sqd->wait); |
| 7448 | return sqd; |
| 7449 | } |
| 7450 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7451 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7452 | __releases(&sqd->lock) |
| 7453 | { |
| 7454 | if (!sqd->thread) |
| 7455 | return; |
| 7456 | kthread_unpark(sqd->thread); |
| 7457 | mutex_unlock(&sqd->lock); |
| 7458 | } |
| 7459 | |
| 7460 | static void io_sq_thread_park(struct io_sq_data *sqd) |
| 7461 | __acquires(&sqd->lock) |
| 7462 | { |
| 7463 | if (!sqd->thread) |
| 7464 | return; |
| 7465 | mutex_lock(&sqd->lock); |
| 7466 | kthread_park(sqd->thread); |
| 7467 | } |
| 7468 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7469 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 7470 | { |
| 7471 | struct io_sq_data *sqd = ctx->sq_data; |
| 7472 | |
| 7473 | if (sqd) { |
| 7474 | if (sqd->thread) { |
| 7475 | /* |
| 7476 | * We may arrive here from the error branch in |
| 7477 | * io_sq_offload_create() where the kthread is created |
| 7478 | * without being waked up, thus wake it up now to make |
| 7479 | * sure the wait will complete. |
| 7480 | */ |
| 7481 | wake_up_process(sqd->thread); |
| 7482 | wait_for_completion(&ctx->sq_thread_comp); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7483 | |
| 7484 | io_sq_thread_park(sqd); |
| 7485 | } |
| 7486 | |
| 7487 | mutex_lock(&sqd->ctx_lock); |
| 7488 | list_del(&ctx->sqd_list); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7489 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7490 | mutex_unlock(&sqd->ctx_lock); |
| 7491 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7492 | if (sqd->thread) |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7493 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7494 | |
| 7495 | io_put_sq_data(sqd); |
| 7496 | ctx->sq_data = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7497 | } |
| 7498 | } |
| 7499 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7500 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 7501 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7502 | io_sq_thread_stop(ctx); |
| 7503 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7504 | if (ctx->io_wq) { |
| 7505 | io_wq_destroy(ctx->io_wq); |
| 7506 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7507 | } |
| 7508 | } |
| 7509 | |
| 7510 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7511 | /* |
| 7512 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7513 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7514 | * loops in the file referencing. |
| 7515 | */ |
| 7516 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7517 | { |
| 7518 | struct sock *sk = ctx->ring_sock->sk; |
| 7519 | struct scm_fp_list *fpl; |
| 7520 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7521 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7522 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7523 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7524 | if (!fpl) |
| 7525 | return -ENOMEM; |
| 7526 | |
| 7527 | skb = alloc_skb(0, GFP_KERNEL); |
| 7528 | if (!skb) { |
| 7529 | kfree(fpl); |
| 7530 | return -ENOMEM; |
| 7531 | } |
| 7532 | |
| 7533 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7534 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7535 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7536 | fpl->user = get_uid(ctx->user); |
| 7537 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7538 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7539 | |
| 7540 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7541 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7542 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7543 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7544 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7545 | } |
| 7546 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7547 | if (nr_files) { |
| 7548 | fpl->max = SCM_MAX_FD; |
| 7549 | fpl->count = nr_files; |
| 7550 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7551 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7552 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7553 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7554 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7555 | for (i = 0; i < nr_files; i++) |
| 7556 | fput(fpl->fp[i]); |
| 7557 | } else { |
| 7558 | kfree_skb(skb); |
| 7559 | kfree(fpl); |
| 7560 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7561 | |
| 7562 | return 0; |
| 7563 | } |
| 7564 | |
| 7565 | /* |
| 7566 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7567 | * causes regular reference counting to break down. We rely on the UNIX |
| 7568 | * garbage collection to take care of this problem for us. |
| 7569 | */ |
| 7570 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7571 | { |
| 7572 | unsigned left, total; |
| 7573 | int ret = 0; |
| 7574 | |
| 7575 | total = 0; |
| 7576 | left = ctx->nr_user_files; |
| 7577 | while (left) { |
| 7578 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7579 | |
| 7580 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7581 | if (ret) |
| 7582 | break; |
| 7583 | left -= this_files; |
| 7584 | total += this_files; |
| 7585 | } |
| 7586 | |
| 7587 | if (!ret) |
| 7588 | return 0; |
| 7589 | |
| 7590 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7591 | struct file *file = io_file_from_index(ctx, total); |
| 7592 | |
| 7593 | if (file) |
| 7594 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7595 | total++; |
| 7596 | } |
| 7597 | |
| 7598 | return ret; |
| 7599 | } |
| 7600 | #else |
| 7601 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7602 | { |
| 7603 | return 0; |
| 7604 | } |
| 7605 | #endif |
| 7606 | |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7607 | static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data, |
| 7608 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7609 | { |
| 7610 | int i; |
| 7611 | |
| 7612 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7613 | struct fixed_file_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7614 | unsigned this_files; |
| 7615 | |
| 7616 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7617 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7618 | GFP_KERNEL); |
| 7619 | if (!table->files) |
| 7620 | break; |
| 7621 | nr_files -= this_files; |
| 7622 | } |
| 7623 | |
| 7624 | if (i == nr_tables) |
| 7625 | return 0; |
| 7626 | |
| 7627 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7628 | struct fixed_file_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7629 | kfree(table->files); |
| 7630 | } |
| 7631 | return 1; |
| 7632 | } |
| 7633 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7634 | 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] | 7635 | { |
| 7636 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7637 | struct sock *sock = ctx->ring_sock->sk; |
| 7638 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7639 | struct sk_buff *skb; |
| 7640 | int i; |
| 7641 | |
| 7642 | __skb_queue_head_init(&list); |
| 7643 | |
| 7644 | /* |
| 7645 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7646 | * remove this entry and rearrange the file array. |
| 7647 | */ |
| 7648 | skb = skb_dequeue(head); |
| 7649 | while (skb) { |
| 7650 | struct scm_fp_list *fp; |
| 7651 | |
| 7652 | fp = UNIXCB(skb).fp; |
| 7653 | for (i = 0; i < fp->count; i++) { |
| 7654 | int left; |
| 7655 | |
| 7656 | if (fp->fp[i] != file) |
| 7657 | continue; |
| 7658 | |
| 7659 | unix_notinflight(fp->user, fp->fp[i]); |
| 7660 | left = fp->count - 1 - i; |
| 7661 | if (left) { |
| 7662 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7663 | left * sizeof(struct file *)); |
| 7664 | } |
| 7665 | fp->count--; |
| 7666 | if (!fp->count) { |
| 7667 | kfree_skb(skb); |
| 7668 | skb = NULL; |
| 7669 | } else { |
| 7670 | __skb_queue_tail(&list, skb); |
| 7671 | } |
| 7672 | fput(file); |
| 7673 | file = NULL; |
| 7674 | break; |
| 7675 | } |
| 7676 | |
| 7677 | if (!file) |
| 7678 | break; |
| 7679 | |
| 7680 | __skb_queue_tail(&list, skb); |
| 7681 | |
| 7682 | skb = skb_dequeue(head); |
| 7683 | } |
| 7684 | |
| 7685 | if (skb_peek(&list)) { |
| 7686 | spin_lock_irq(&head->lock); |
| 7687 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7688 | __skb_queue_tail(head, skb); |
| 7689 | spin_unlock_irq(&head->lock); |
| 7690 | } |
| 7691 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7692 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7693 | #endif |
| 7694 | } |
| 7695 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7696 | struct io_file_put { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7697 | struct list_head list; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7698 | struct file *file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7699 | }; |
| 7700 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7701 | static void __io_file_put_work(struct fixed_file_ref_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7702 | { |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7703 | struct fixed_file_data *file_data = ref_node->file_data; |
| 7704 | struct io_ring_ctx *ctx = file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7705 | struct io_file_put *pfile, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7706 | |
| 7707 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7708 | list_del(&pfile->list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7709 | io_ring_file_put(ctx, pfile->file); |
| 7710 | kfree(pfile); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7711 | } |
| 7712 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7713 | percpu_ref_exit(&ref_node->refs); |
| 7714 | kfree(ref_node); |
| 7715 | percpu_ref_put(&file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7716 | } |
| 7717 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7718 | static void io_file_put_work(struct work_struct *work) |
| 7719 | { |
| 7720 | struct io_ring_ctx *ctx; |
| 7721 | struct llist_node *node; |
| 7722 | |
| 7723 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); |
| 7724 | node = llist_del_all(&ctx->file_put_llist); |
| 7725 | |
| 7726 | while (node) { |
| 7727 | struct fixed_file_ref_node *ref_node; |
| 7728 | struct llist_node *next = node->next; |
| 7729 | |
| 7730 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); |
| 7731 | __io_file_put_work(ref_node); |
| 7732 | node = next; |
| 7733 | } |
| 7734 | } |
| 7735 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7736 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 7737 | { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7738 | struct fixed_file_ref_node *ref_node; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7739 | struct fixed_file_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7740 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7741 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7742 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7743 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7744 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7745 | data = ref_node->file_data; |
| 7746 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7747 | |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7748 | spin_lock_bh(&data->lock); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7749 | ref_node->done = true; |
| 7750 | |
| 7751 | while (!list_empty(&data->ref_list)) { |
| 7752 | ref_node = list_first_entry(&data->ref_list, |
| 7753 | struct fixed_file_ref_node, node); |
| 7754 | /* recycle ref nodes in order */ |
| 7755 | if (!ref_node->done) |
| 7756 | break; |
| 7757 | list_del(&ref_node->node); |
| 7758 | first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist); |
| 7759 | } |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7760 | spin_unlock_bh(&data->lock); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7761 | |
| 7762 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7763 | delay = 0; |
| 7764 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7765 | if (!delay) |
| 7766 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); |
| 7767 | else if (first_add) |
| 7768 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 7772 | struct io_ring_ctx *ctx) |
| 7773 | { |
| 7774 | struct fixed_file_ref_node *ref_node; |
| 7775 | |
| 7776 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7777 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7778 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7779 | |
| 7780 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
| 7781 | 0, GFP_KERNEL)) { |
| 7782 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7783 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7784 | } |
| 7785 | INIT_LIST_HEAD(&ref_node->node); |
| 7786 | INIT_LIST_HEAD(&ref_node->file_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7787 | ref_node->file_data = ctx->file_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7788 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7789 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) |
| 7793 | { |
| 7794 | percpu_ref_exit(&ref_node->refs); |
| 7795 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7796 | } |
| 7797 | |
| 7798 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7799 | unsigned nr_args) |
| 7800 | { |
| 7801 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7802 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7803 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7804 | int fd, ret = -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7805 | struct fixed_file_ref_node *ref_node; |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7806 | struct fixed_file_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7807 | |
| 7808 | if (ctx->file_data) |
| 7809 | return -EBUSY; |
| 7810 | if (!nr_args) |
| 7811 | return -EINVAL; |
| 7812 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7813 | return -EMFILE; |
| 7814 | |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7815 | file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 7816 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7817 | return -ENOMEM; |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7818 | file_data->ctx = ctx; |
| 7819 | init_completion(&file_data->done); |
| 7820 | INIT_LIST_HEAD(&file_data->ref_list); |
| 7821 | spin_lock_init(&file_data->lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7822 | |
| 7823 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7824 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7825 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7826 | if (!file_data->table) |
| 7827 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7828 | |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7829 | if (percpu_ref_init(&file_data->refs, io_file_ref_kill, |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7830 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 7831 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7832 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7833 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
| 7834 | goto out_ref; |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7835 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7836 | |
| 7837 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 7838 | struct fixed_file_table *table; |
| 7839 | unsigned index; |
| 7840 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7841 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7842 | ret = -EFAULT; |
| 7843 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7844 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7845 | /* allow sparse sets */ |
| 7846 | if (fd == -1) |
| 7847 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7848 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7849 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7850 | ret = -EBADF; |
| 7851 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7852 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7853 | |
| 7854 | /* |
| 7855 | * Don't allow io_uring instances to be registered. If UNIX |
| 7856 | * isn't enabled, then this causes a reference cycle and this |
| 7857 | * instance can never get freed. If UNIX is enabled we'll |
| 7858 | * handle it just fine, but there's still no point in allowing |
| 7859 | * a ring fd as it doesn't support regular read/write anyway. |
| 7860 | */ |
| 7861 | if (file->f_op == &io_uring_fops) { |
| 7862 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7863 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7864 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7865 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7866 | index = i & IORING_FILE_TABLE_MASK; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7867 | table->files[index] = file; |
| 7868 | } |
| 7869 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7870 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7871 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7872 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7873 | return ret; |
| 7874 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7875 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7876 | ref_node = alloc_fixed_file_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7877 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7878 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7879 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7880 | } |
| 7881 | |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7882 | io_sqe_files_set_node(file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7883 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7884 | out_fput: |
| 7885 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7886 | file = io_file_from_index(ctx, i); |
| 7887 | if (file) |
| 7888 | fput(file); |
| 7889 | } |
| 7890 | for (i = 0; i < nr_tables; i++) |
| 7891 | kfree(file_data->table[i].files); |
| 7892 | ctx->nr_user_files = 0; |
| 7893 | out_ref: |
| 7894 | percpu_ref_exit(&file_data->refs); |
| 7895 | out_free: |
| 7896 | kfree(file_data->table); |
| 7897 | kfree(file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7898 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7899 | return ret; |
| 7900 | } |
| 7901 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7902 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7903 | int index) |
| 7904 | { |
| 7905 | #if defined(CONFIG_UNIX) |
| 7906 | struct sock *sock = ctx->ring_sock->sk; |
| 7907 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7908 | struct sk_buff *skb; |
| 7909 | |
| 7910 | /* |
| 7911 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7912 | * file set. If there's no room, fall back to allocating a new skb |
| 7913 | * and filling it in. |
| 7914 | */ |
| 7915 | spin_lock_irq(&head->lock); |
| 7916 | skb = skb_peek(head); |
| 7917 | if (skb) { |
| 7918 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7919 | |
| 7920 | if (fpl->count < SCM_MAX_FD) { |
| 7921 | __skb_unlink(skb, head); |
| 7922 | spin_unlock_irq(&head->lock); |
| 7923 | fpl->fp[fpl->count] = get_file(file); |
| 7924 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7925 | fpl->count++; |
| 7926 | spin_lock_irq(&head->lock); |
| 7927 | __skb_queue_head(head, skb); |
| 7928 | } else { |
| 7929 | skb = NULL; |
| 7930 | } |
| 7931 | } |
| 7932 | spin_unlock_irq(&head->lock); |
| 7933 | |
| 7934 | if (skb) { |
| 7935 | fput(file); |
| 7936 | return 0; |
| 7937 | } |
| 7938 | |
| 7939 | return __io_sqe_files_scm(ctx, 1, index); |
| 7940 | #else |
| 7941 | return 0; |
| 7942 | #endif |
| 7943 | } |
| 7944 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7945 | static int io_queue_file_removal(struct fixed_file_data *data, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7946 | struct file *file) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7947 | { |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7948 | struct io_file_put *pfile; |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 7949 | struct fixed_file_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7950 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7951 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7952 | if (!pfile) |
| 7953 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7954 | |
| 7955 | pfile->file = file; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7956 | list_add(&pfile->list, &ref_node->file_list); |
| 7957 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7958 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7959 | } |
| 7960 | |
| 7961 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 7962 | struct io_uring_files_update *up, |
| 7963 | unsigned nr_args) |
| 7964 | { |
| 7965 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7966 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7967 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7968 | __s32 __user *fds; |
| 7969 | int fd, i, err; |
| 7970 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7971 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7972 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7973 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7974 | return -EOVERFLOW; |
| 7975 | if (done > ctx->nr_user_files) |
| 7976 | return -EINVAL; |
| 7977 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7978 | ref_node = alloc_fixed_file_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7979 | if (!ref_node) |
| 7980 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7981 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7982 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7983 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7984 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7985 | struct fixed_file_table *table; |
| 7986 | unsigned index; |
| 7987 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7988 | err = 0; |
| 7989 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7990 | err = -EFAULT; |
| 7991 | break; |
| 7992 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7993 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 7994 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7995 | index = i & IORING_FILE_TABLE_MASK; |
| 7996 | if (table->files[index]) { |
Jiufei Xue | 98dfd50 | 2020-09-01 13:35:02 +0800 | [diff] [blame] | 7997 | file = table->files[index]; |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7998 | err = io_queue_file_removal(data, file); |
| 7999 | if (err) |
| 8000 | break; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8001 | table->files[index] = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8002 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8003 | } |
| 8004 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8005 | file = fget(fd); |
| 8006 | if (!file) { |
| 8007 | err = -EBADF; |
| 8008 | break; |
| 8009 | } |
| 8010 | /* |
| 8011 | * Don't allow io_uring instances to be registered. If |
| 8012 | * UNIX isn't enabled, then this causes a reference |
| 8013 | * cycle and this instance can never get freed. If UNIX |
| 8014 | * is enabled we'll handle it just fine, but there's |
| 8015 | * still no point in allowing a ring fd as it doesn't |
| 8016 | * support regular read/write anyway. |
| 8017 | */ |
| 8018 | if (file->f_op == &io_uring_fops) { |
| 8019 | fput(file); |
| 8020 | err = -EBADF; |
| 8021 | break; |
| 8022 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8023 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8024 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8025 | if (err) { |
Jiufei Xue | 95d1c8e | 2020-09-02 17:59:39 +0800 | [diff] [blame] | 8026 | table->files[index] = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8027 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8028 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8029 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8030 | } |
| 8031 | nr_args--; |
| 8032 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8033 | up->offset++; |
| 8034 | } |
| 8035 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8036 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 8037 | percpu_ref_kill(&data->node->refs); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 8038 | io_sqe_files_set_node(data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8039 | } else |
| 8040 | destroy_fixed_file_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8041 | |
| 8042 | return done ? done : err; |
| 8043 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8044 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8045 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 8046 | unsigned nr_args) |
| 8047 | { |
| 8048 | struct io_uring_files_update up; |
| 8049 | |
| 8050 | if (!ctx->file_data) |
| 8051 | return -ENXIO; |
| 8052 | if (!nr_args) |
| 8053 | return -EINVAL; |
| 8054 | if (copy_from_user(&up, arg, sizeof(up))) |
| 8055 | return -EFAULT; |
| 8056 | if (up.resv) |
| 8057 | return -EINVAL; |
| 8058 | |
| 8059 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 8060 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8061 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8062 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8063 | { |
| 8064 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8065 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8066 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8067 | io_put_req(req); |
| 8068 | } |
| 8069 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8070 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8071 | struct io_uring_params *p) |
| 8072 | { |
| 8073 | struct io_wq_data data; |
| 8074 | struct fd f; |
| 8075 | struct io_ring_ctx *ctx_attach; |
| 8076 | unsigned int concurrency; |
| 8077 | int ret = 0; |
| 8078 | |
| 8079 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8080 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8081 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8082 | |
| 8083 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 8084 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8085 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 8086 | |
| 8087 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 8088 | if (IS_ERR(ctx->io_wq)) { |
| 8089 | ret = PTR_ERR(ctx->io_wq); |
| 8090 | ctx->io_wq = NULL; |
| 8091 | } |
| 8092 | return ret; |
| 8093 | } |
| 8094 | |
| 8095 | f = fdget(p->wq_fd); |
| 8096 | if (!f.file) |
| 8097 | return -EBADF; |
| 8098 | |
| 8099 | if (f.file->f_op != &io_uring_fops) { |
| 8100 | ret = -EINVAL; |
| 8101 | goto out_fput; |
| 8102 | } |
| 8103 | |
| 8104 | ctx_attach = f.file->private_data; |
| 8105 | /* @io_wq is protected by holding the fd */ |
| 8106 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 8107 | ret = -EINVAL; |
| 8108 | goto out_fput; |
| 8109 | } |
| 8110 | |
| 8111 | ctx->io_wq = ctx_attach->io_wq; |
| 8112 | out_fput: |
| 8113 | fdput(f); |
| 8114 | return ret; |
| 8115 | } |
| 8116 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8117 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 8118 | { |
| 8119 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8120 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8121 | |
| 8122 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 8123 | if (unlikely(!tctx)) |
| 8124 | return -ENOMEM; |
| 8125 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8126 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8127 | if (unlikely(ret)) { |
| 8128 | kfree(tctx); |
| 8129 | return ret; |
| 8130 | } |
| 8131 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8132 | xa_init(&tctx->xa); |
| 8133 | init_waitqueue_head(&tctx->wait); |
| 8134 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8135 | atomic_set(&tctx->in_idle, 0); |
| 8136 | tctx->sqpoll = false; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8137 | io_init_identity(&tctx->__identity); |
| 8138 | tctx->identity = &tctx->__identity; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8139 | task->io_uring = tctx; |
| 8140 | return 0; |
| 8141 | } |
| 8142 | |
| 8143 | void __io_uring_free(struct task_struct *tsk) |
| 8144 | { |
| 8145 | struct io_uring_task *tctx = tsk->io_uring; |
| 8146 | |
| 8147 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8148 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
| 8149 | if (tctx->identity != &tctx->__identity) |
| 8150 | kfree(tctx->identity); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8151 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8152 | kfree(tctx); |
| 8153 | tsk->io_uring = NULL; |
| 8154 | } |
| 8155 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8156 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8157 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8158 | { |
| 8159 | int ret; |
| 8160 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8161 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8162 | struct io_sq_data *sqd; |
| 8163 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8164 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 8165 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8166 | goto err; |
| 8167 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8168 | sqd = io_get_sq_data(p); |
| 8169 | if (IS_ERR(sqd)) { |
| 8170 | ret = PTR_ERR(sqd); |
| 8171 | goto err; |
| 8172 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8173 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8174 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8175 | io_sq_thread_park(sqd); |
| 8176 | mutex_lock(&sqd->ctx_lock); |
| 8177 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 8178 | mutex_unlock(&sqd->ctx_lock); |
| 8179 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8180 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8181 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8182 | if (!ctx->sq_thread_idle) |
| 8183 | ctx->sq_thread_idle = HZ; |
| 8184 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8185 | if (sqd->thread) |
| 8186 | goto done; |
| 8187 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8188 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8189 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8190 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8191 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8192 | if (cpu >= nr_cpu_ids) |
| 8193 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 8194 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8195 | goto err; |
| 8196 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8197 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8198 | cpu, "io_uring-sq"); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8199 | } else { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8200 | sqd->thread = kthread_create(io_sq_thread, sqd, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8201 | "io_uring-sq"); |
| 8202 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8203 | if (IS_ERR(sqd->thread)) { |
| 8204 | ret = PTR_ERR(sqd->thread); |
| 8205 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8206 | goto err; |
| 8207 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8208 | ret = io_uring_alloc_task_context(sqd->thread); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8209 | if (ret) |
| 8210 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8211 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8212 | /* Can't have SQ_AFF without SQPOLL */ |
| 8213 | ret = -EINVAL; |
| 8214 | goto err; |
| 8215 | } |
| 8216 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8217 | done: |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8218 | ret = io_init_wq_offload(ctx, p); |
| 8219 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8220 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8221 | |
| 8222 | return 0; |
| 8223 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 8224 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8225 | return ret; |
| 8226 | } |
| 8227 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8228 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 8229 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8230 | struct io_sq_data *sqd = ctx->sq_data; |
| 8231 | |
| 8232 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
| 8233 | wake_up_process(sqd->thread); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8234 | } |
| 8235 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8236 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8237 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8238 | { |
| 8239 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8240 | } |
| 8241 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8242 | static inline int __io_account_mem(struct user_struct *user, |
| 8243 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8244 | { |
| 8245 | unsigned long page_limit, cur_pages, new_pages; |
| 8246 | |
| 8247 | /* Don't allow more pages than we can safely lock */ |
| 8248 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8249 | |
| 8250 | do { |
| 8251 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8252 | new_pages = cur_pages + nr_pages; |
| 8253 | if (new_pages > page_limit) |
| 8254 | return -ENOMEM; |
| 8255 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8256 | new_pages) != cur_pages); |
| 8257 | |
| 8258 | return 0; |
| 8259 | } |
| 8260 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8261 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 8262 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8263 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8264 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8265 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8266 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8267 | if (ctx->mm_account) { |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8268 | if (acct == ACCT_LOCKED) { |
| 8269 | mmap_write_lock(ctx->mm_account); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8270 | ctx->mm_account->locked_vm -= nr_pages; |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8271 | mmap_write_unlock(ctx->mm_account); |
| 8272 | }else if (acct == ACCT_PINNED) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8273 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8274 | } |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8275 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8276 | } |
| 8277 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8278 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 8279 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8280 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8281 | int ret; |
| 8282 | |
| 8283 | if (ctx->limit_mem) { |
| 8284 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8285 | if (ret) |
| 8286 | return ret; |
| 8287 | } |
| 8288 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8289 | if (ctx->mm_account) { |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8290 | if (acct == ACCT_LOCKED) { |
| 8291 | mmap_write_lock(ctx->mm_account); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8292 | ctx->mm_account->locked_vm += nr_pages; |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8293 | mmap_write_unlock(ctx->mm_account); |
| 8294 | } else if (acct == ACCT_PINNED) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8295 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8296 | } |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8297 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8298 | |
| 8299 | return 0; |
| 8300 | } |
| 8301 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8302 | static void io_mem_free(void *ptr) |
| 8303 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8304 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8305 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8306 | if (!ptr) |
| 8307 | return; |
| 8308 | |
| 8309 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8310 | if (put_page_testzero(page)) |
| 8311 | free_compound_page(page); |
| 8312 | } |
| 8313 | |
| 8314 | static void *io_mem_alloc(size_t size) |
| 8315 | { |
| 8316 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 8317 | __GFP_NORETRY; |
| 8318 | |
| 8319 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8320 | } |
| 8321 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8322 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8323 | size_t *sq_offset) |
| 8324 | { |
| 8325 | struct io_rings *rings; |
| 8326 | size_t off, sq_array_size; |
| 8327 | |
| 8328 | off = struct_size(rings, cqes, cq_entries); |
| 8329 | if (off == SIZE_MAX) |
| 8330 | return SIZE_MAX; |
| 8331 | |
| 8332 | #ifdef CONFIG_SMP |
| 8333 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8334 | if (off == 0) |
| 8335 | return SIZE_MAX; |
| 8336 | #endif |
| 8337 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8338 | if (sq_offset) |
| 8339 | *sq_offset = off; |
| 8340 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8341 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8342 | if (sq_array_size == SIZE_MAX) |
| 8343 | return SIZE_MAX; |
| 8344 | |
| 8345 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8346 | return SIZE_MAX; |
| 8347 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8348 | return off; |
| 8349 | } |
| 8350 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8351 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 8352 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8353 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8354 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8355 | pages = (size_t)1 << get_order( |
| 8356 | rings_size(sq_entries, cq_entries, NULL)); |
| 8357 | pages += (size_t)1 << get_order( |
| 8358 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8359 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8360 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8361 | } |
| 8362 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8363 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 8364 | { |
| 8365 | int i, j; |
| 8366 | |
| 8367 | if (!ctx->user_bufs) |
| 8368 | return -ENXIO; |
| 8369 | |
| 8370 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8371 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8372 | |
| 8373 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8374 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8375 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8376 | if (imu->acct_pages) |
| 8377 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8378 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8379 | imu->nr_bvecs = 0; |
| 8380 | } |
| 8381 | |
| 8382 | kfree(ctx->user_bufs); |
| 8383 | ctx->user_bufs = NULL; |
| 8384 | ctx->nr_user_bufs = 0; |
| 8385 | return 0; |
| 8386 | } |
| 8387 | |
| 8388 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8389 | void __user *arg, unsigned index) |
| 8390 | { |
| 8391 | struct iovec __user *src; |
| 8392 | |
| 8393 | #ifdef CONFIG_COMPAT |
| 8394 | if (ctx->compat) { |
| 8395 | struct compat_iovec __user *ciovs; |
| 8396 | struct compat_iovec ciov; |
| 8397 | |
| 8398 | ciovs = (struct compat_iovec __user *) arg; |
| 8399 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8400 | return -EFAULT; |
| 8401 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8402 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8403 | dst->iov_len = ciov.iov_len; |
| 8404 | return 0; |
| 8405 | } |
| 8406 | #endif |
| 8407 | src = (struct iovec __user *) arg; |
| 8408 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8409 | return -EFAULT; |
| 8410 | return 0; |
| 8411 | } |
| 8412 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8413 | /* |
| 8414 | * Not super efficient, but this is just a registration time. And we do cache |
| 8415 | * the last compound head, so generally we'll only do a full search if we don't |
| 8416 | * match that one. |
| 8417 | * |
| 8418 | * We check if the given compound head page has already been accounted, to |
| 8419 | * avoid double accounting it. This allows us to account the full size of the |
| 8420 | * page, not just the constituent pages of a huge page. |
| 8421 | */ |
| 8422 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8423 | int nr_pages, struct page *hpage) |
| 8424 | { |
| 8425 | int i, j; |
| 8426 | |
| 8427 | /* check current page array */ |
| 8428 | for (i = 0; i < nr_pages; i++) { |
| 8429 | if (!PageCompound(pages[i])) |
| 8430 | continue; |
| 8431 | if (compound_head(pages[i]) == hpage) |
| 8432 | return true; |
| 8433 | } |
| 8434 | |
| 8435 | /* check previously registered pages */ |
| 8436 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8437 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8438 | |
| 8439 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8440 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8441 | continue; |
| 8442 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8443 | return true; |
| 8444 | } |
| 8445 | } |
| 8446 | |
| 8447 | return false; |
| 8448 | } |
| 8449 | |
| 8450 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8451 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8452 | struct page **last_hpage) |
| 8453 | { |
| 8454 | int i, ret; |
| 8455 | |
| 8456 | for (i = 0; i < nr_pages; i++) { |
| 8457 | if (!PageCompound(pages[i])) { |
| 8458 | imu->acct_pages++; |
| 8459 | } else { |
| 8460 | struct page *hpage; |
| 8461 | |
| 8462 | hpage = compound_head(pages[i]); |
| 8463 | if (hpage == *last_hpage) |
| 8464 | continue; |
| 8465 | *last_hpage = hpage; |
| 8466 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8467 | continue; |
| 8468 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8469 | } |
| 8470 | } |
| 8471 | |
| 8472 | if (!imu->acct_pages) |
| 8473 | return 0; |
| 8474 | |
| 8475 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); |
| 8476 | if (ret) |
| 8477 | imu->acct_pages = 0; |
| 8478 | return ret; |
| 8479 | } |
| 8480 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8481 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8482 | unsigned nr_args) |
| 8483 | { |
| 8484 | struct vm_area_struct **vmas = NULL; |
| 8485 | struct page **pages = NULL; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8486 | struct page *last_hpage = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8487 | int i, j, got_pages = 0; |
| 8488 | int ret = -EINVAL; |
| 8489 | |
| 8490 | if (ctx->user_bufs) |
| 8491 | return -EBUSY; |
| 8492 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8493 | return -EINVAL; |
| 8494 | |
| 8495 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8496 | GFP_KERNEL); |
| 8497 | if (!ctx->user_bufs) |
| 8498 | return -ENOMEM; |
| 8499 | |
| 8500 | for (i = 0; i < nr_args; i++) { |
| 8501 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8502 | unsigned long off, start, end, ubuf; |
| 8503 | int pret, nr_pages; |
| 8504 | struct iovec iov; |
| 8505 | size_t size; |
| 8506 | |
| 8507 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8508 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 8509 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8510 | |
| 8511 | /* |
| 8512 | * Don't impose further limits on the size and buffer |
| 8513 | * constraints here, we'll -EINVAL later when IO is |
| 8514 | * submitted if they are wrong. |
| 8515 | */ |
| 8516 | ret = -EFAULT; |
| 8517 | if (!iov.iov_base || !iov.iov_len) |
| 8518 | goto err; |
| 8519 | |
| 8520 | /* arbitrary limit, but we need something */ |
| 8521 | if (iov.iov_len > SZ_1G) |
| 8522 | goto err; |
| 8523 | |
| 8524 | ubuf = (unsigned long) iov.iov_base; |
| 8525 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8526 | start = ubuf >> PAGE_SHIFT; |
| 8527 | nr_pages = end - start; |
| 8528 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8529 | ret = 0; |
| 8530 | if (!pages || nr_pages > got_pages) { |
Denis Efremov | a8c73c1 | 2020-06-05 12:32:03 +0300 | [diff] [blame] | 8531 | kvfree(vmas); |
| 8532 | kvfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8533 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8534 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8535 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8536 | sizeof(struct vm_area_struct *), |
| 8537 | GFP_KERNEL); |
| 8538 | if (!pages || !vmas) { |
| 8539 | ret = -ENOMEM; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8540 | goto err; |
| 8541 | } |
| 8542 | got_pages = nr_pages; |
| 8543 | } |
| 8544 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8545 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8546 | GFP_KERNEL); |
| 8547 | ret = -ENOMEM; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8548 | if (!imu->bvec) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8549 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8550 | |
| 8551 | ret = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 8552 | mmap_read_lock(current->mm); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 8553 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 8554 | FOLL_WRITE | FOLL_LONGTERM, |
| 8555 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8556 | if (pret == nr_pages) { |
| 8557 | /* don't support file backed memory */ |
| 8558 | for (j = 0; j < nr_pages; j++) { |
| 8559 | struct vm_area_struct *vma = vmas[j]; |
| 8560 | |
| 8561 | if (vma->vm_file && |
| 8562 | !is_file_hugepages(vma->vm_file)) { |
| 8563 | ret = -EOPNOTSUPP; |
| 8564 | break; |
| 8565 | } |
| 8566 | } |
| 8567 | } else { |
| 8568 | ret = pret < 0 ? pret : -EFAULT; |
| 8569 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 8570 | mmap_read_unlock(current->mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8571 | if (ret) { |
| 8572 | /* |
| 8573 | * if we did partial map, or found file backed vmas, |
| 8574 | * release any pages we did get |
| 8575 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 8576 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8577 | unpin_user_pages(pages, pret); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8578 | kvfree(imu->bvec); |
| 8579 | goto err; |
| 8580 | } |
| 8581 | |
| 8582 | ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage); |
| 8583 | if (ret) { |
| 8584 | unpin_user_pages(pages, pret); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8585 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8586 | goto err; |
| 8587 | } |
| 8588 | |
| 8589 | off = ubuf & ~PAGE_MASK; |
| 8590 | size = iov.iov_len; |
| 8591 | for (j = 0; j < nr_pages; j++) { |
| 8592 | size_t vec_len; |
| 8593 | |
| 8594 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8595 | imu->bvec[j].bv_page = pages[j]; |
| 8596 | imu->bvec[j].bv_len = vec_len; |
| 8597 | imu->bvec[j].bv_offset = off; |
| 8598 | off = 0; |
| 8599 | size -= vec_len; |
| 8600 | } |
| 8601 | /* store original address for later verification */ |
| 8602 | imu->ubuf = ubuf; |
| 8603 | imu->len = iov.iov_len; |
| 8604 | imu->nr_bvecs = nr_pages; |
| 8605 | |
| 8606 | ctx->nr_user_bufs++; |
| 8607 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8608 | kvfree(pages); |
| 8609 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8610 | return 0; |
| 8611 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8612 | kvfree(pages); |
| 8613 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8614 | io_sqe_buffer_unregister(ctx); |
| 8615 | return ret; |
| 8616 | } |
| 8617 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8618 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8619 | { |
| 8620 | __s32 __user *fds = arg; |
| 8621 | int fd; |
| 8622 | |
| 8623 | if (ctx->cq_ev_fd) |
| 8624 | return -EBUSY; |
| 8625 | |
| 8626 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8627 | return -EFAULT; |
| 8628 | |
| 8629 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8630 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8631 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8632 | ctx->cq_ev_fd = NULL; |
| 8633 | return ret; |
| 8634 | } |
| 8635 | |
| 8636 | return 0; |
| 8637 | } |
| 8638 | |
| 8639 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8640 | { |
| 8641 | if (ctx->cq_ev_fd) { |
| 8642 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8643 | ctx->cq_ev_fd = NULL; |
| 8644 | return 0; |
| 8645 | } |
| 8646 | |
| 8647 | return -ENXIO; |
| 8648 | } |
| 8649 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8650 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8651 | { |
| 8652 | struct io_ring_ctx *ctx = data; |
| 8653 | struct io_buffer *buf = p; |
| 8654 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8655 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8656 | return 0; |
| 8657 | } |
| 8658 | |
| 8659 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8660 | { |
| 8661 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8662 | idr_destroy(&ctx->io_buffer_idr); |
| 8663 | } |
| 8664 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8665 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8666 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8667 | io_finish_async(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8668 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8669 | |
| 8670 | if (ctx->sqo_task) { |
| 8671 | put_task_struct(ctx->sqo_task); |
| 8672 | ctx->sqo_task = NULL; |
| 8673 | mmdrop(ctx->mm_account); |
| 8674 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8675 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8676 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 8677 | #ifdef CONFIG_BLK_CGROUP |
| 8678 | if (ctx->sqo_blkcg_css) |
| 8679 | css_put(ctx->sqo_blkcg_css); |
| 8680 | #endif |
| 8681 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8682 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8683 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8684 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8685 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8686 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8687 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8688 | if (ctx->ring_sock) { |
| 8689 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8690 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8691 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8692 | #endif |
| 8693 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8694 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8695 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8696 | |
| 8697 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8698 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8699 | put_cred(ctx->creds); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8700 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 8701 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8702 | kfree(ctx); |
| 8703 | } |
| 8704 | |
| 8705 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8706 | { |
| 8707 | struct io_ring_ctx *ctx = file->private_data; |
| 8708 | __poll_t mask = 0; |
| 8709 | |
| 8710 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8711 | /* |
| 8712 | * synchronizes with barrier from wq_has_sleeper call in |
| 8713 | * io_commit_cqring |
| 8714 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8715 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8716 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8717 | mask |= EPOLLOUT | EPOLLWRNORM; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8718 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 8719 | if (io_cqring_events(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8720 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8721 | |
| 8722 | return mask; |
| 8723 | } |
| 8724 | |
| 8725 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8726 | { |
| 8727 | struct io_ring_ctx *ctx = file->private_data; |
| 8728 | |
| 8729 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8730 | } |
| 8731 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8732 | static int io_remove_personalities(int id, void *p, void *data) |
| 8733 | { |
| 8734 | struct io_ring_ctx *ctx = data; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8735 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8736 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8737 | iod = idr_remove(&ctx->personality_idr, id); |
| 8738 | if (iod) { |
| 8739 | put_cred(iod->creds); |
| 8740 | if (refcount_dec_and_test(&iod->count)) |
| 8741 | kfree(iod); |
| 8742 | } |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8743 | return 0; |
| 8744 | } |
| 8745 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8746 | static void io_ring_exit_work(struct work_struct *work) |
| 8747 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8748 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8749 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8750 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8751 | /* |
| 8752 | * If we're doing polled IO and end up having requests being |
| 8753 | * submitted async (out-of-line), then completions can come in while |
| 8754 | * we're waiting for refs to drop. We need to reap these manually, |
| 8755 | * as nobody else will be looking for them. |
| 8756 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8757 | do { |
Pavel Begunkov | 90df085 | 2021-01-04 20:43:30 +0000 | [diff] [blame] | 8758 | __io_uring_cancel_task_requests(ctx, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8759 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8760 | io_ring_ctx_free(ctx); |
| 8761 | } |
| 8762 | |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8763 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8764 | { |
| 8765 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8766 | |
| 8767 | return req->ctx == data; |
| 8768 | } |
| 8769 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8770 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8771 | { |
| 8772 | mutex_lock(&ctx->uring_lock); |
| 8773 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8774 | |
| 8775 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8776 | ctx->sqo_dead = 1; |
| 8777 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8778 | /* if force is set, the ring is going away. always drop after that */ |
| 8779 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8780 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8781 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8782 | mutex_unlock(&ctx->uring_lock); |
| 8783 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8784 | io_kill_timeouts(ctx, NULL, NULL); |
| 8785 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8786 | |
| 8787 | if (ctx->io_wq) |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8788 | io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8789 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8790 | /* if we failed setting up the ctx, we might not have any rings */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8791 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8792 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8793 | |
| 8794 | /* |
| 8795 | * Do this upfront, so we won't have a grace period where the ring |
| 8796 | * is closed but resources aren't reaped yet. This can cause |
| 8797 | * spurious failure in setting up a new ring. |
| 8798 | */ |
Jens Axboe | 760618f | 2020-07-24 12:53:31 -0600 | [diff] [blame] | 8799 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
| 8800 | ACCT_LOCKED); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8801 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8802 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8803 | /* |
| 8804 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8805 | * if we're exiting a ton of rings at the same time. It just adds |
| 8806 | * noise and overhead, there's no discernable change in runtime |
| 8807 | * over using system_wq. |
| 8808 | */ |
| 8809 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8813 | { |
| 8814 | struct io_ring_ctx *ctx = file->private_data; |
| 8815 | |
| 8816 | file->private_data = NULL; |
| 8817 | io_ring_ctx_wait_and_kill(ctx); |
| 8818 | return 0; |
| 8819 | } |
| 8820 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8821 | struct io_task_cancel { |
| 8822 | struct task_struct *task; |
| 8823 | struct files_struct *files; |
| 8824 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8825 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8826 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8827 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8828 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8829 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8830 | bool ret; |
| 8831 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8832 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8833 | unsigned long flags; |
| 8834 | struct io_ring_ctx *ctx = req->ctx; |
| 8835 | |
| 8836 | /* protect against races with linked timeouts */ |
| 8837 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8838 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8839 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8840 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8841 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8842 | } |
| 8843 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8844 | } |
| 8845 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8846 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8847 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8848 | struct files_struct *files) |
| 8849 | { |
| 8850 | struct io_defer_entry *de = NULL; |
| 8851 | LIST_HEAD(list); |
| 8852 | |
| 8853 | spin_lock_irq(&ctx->completion_lock); |
| 8854 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8855 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8856 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8857 | break; |
| 8858 | } |
| 8859 | } |
| 8860 | spin_unlock_irq(&ctx->completion_lock); |
| 8861 | |
| 8862 | while (!list_empty(&list)) { |
| 8863 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8864 | list_del_init(&de->list); |
| 8865 | req_set_fail_links(de->req); |
| 8866 | io_put_req(de->req); |
| 8867 | io_req_complete(de->req, -ECANCELED); |
| 8868 | kfree(de); |
| 8869 | } |
| 8870 | } |
| 8871 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8872 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8873 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8874 | struct files_struct *files) |
| 8875 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8876 | while (!list_empty_careful(&ctx->inflight_list)) { |
Pavel Begunkov | bee749b | 2020-11-25 02:19:23 +0000 | [diff] [blame] | 8877 | struct io_task_cancel cancel = { .task = task, .files = files }; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8878 | struct io_kiocb *req; |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8879 | DEFINE_WAIT(wait); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8880 | bool found = false; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8881 | |
| 8882 | spin_lock_irq(&ctx->inflight_lock); |
| 8883 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 8884 | if (!io_match_task(req, task, files)) |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8885 | continue; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8886 | found = true; |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8887 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8888 | } |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8889 | if (found) |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8890 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 8891 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8892 | spin_unlock_irq(&ctx->inflight_lock); |
| 8893 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8894 | /* We need to keep going until we don't find a matching req */ |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8895 | if (!found) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8896 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8897 | |
| 8898 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); |
| 8899 | io_poll_remove_all(ctx, task, files); |
| 8900 | io_kill_timeouts(ctx, task, files); |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 8901 | io_cqring_overflow_flush(ctx, true, task, files); |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 8902 | /* cancellations _may_ trigger task work */ |
| 8903 | io_run_task_work(); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8904 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8905 | finish_wait(&task->io_uring->wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8906 | } |
| 8907 | } |
| 8908 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8909 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8910 | struct task_struct *task) |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8911 | { |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8912 | while (1) { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8913 | struct io_task_cancel cancel = { .task = task, .files = NULL, }; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8914 | enum io_wq_cancel cret; |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8915 | bool ret = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8916 | |
Pavel Begunkov | 90df085 | 2021-01-04 20:43:30 +0000 | [diff] [blame] | 8917 | if (ctx->io_wq) { |
| 8918 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, |
| 8919 | &cancel, true); |
| 8920 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8921 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8922 | |
| 8923 | /* SQPOLL thread does its own polling */ |
| 8924 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { |
| 8925 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8926 | io_iopoll_try_reap_events(ctx); |
| 8927 | ret = true; |
| 8928 | } |
| 8929 | } |
| 8930 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8931 | ret |= io_poll_remove_all(ctx, task, NULL); |
| 8932 | ret |= io_kill_timeouts(ctx, task, NULL); |
Pavel Begunkov | 55583d7 | 2020-12-20 13:21:43 +0000 | [diff] [blame] | 8933 | ret |= io_run_task_work(); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8934 | if (!ret) |
| 8935 | break; |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8936 | cond_resched(); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8937 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8938 | } |
| 8939 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8940 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 8941 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8942 | mutex_lock(&ctx->uring_lock); |
| 8943 | ctx->sqo_dead = 1; |
| 8944 | mutex_unlock(&ctx->uring_lock); |
| 8945 | |
| 8946 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 8947 | if (ctx->rings) |
| 8948 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8951 | /* |
| 8952 | * We need to iteratively cancel requests, in case a request has dependent |
| 8953 | * hard links. These persist even for failure of cancelations, hence keep |
| 8954 | * looping until none are found. |
| 8955 | */ |
| 8956 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8957 | struct files_struct *files) |
| 8958 | { |
| 8959 | struct task_struct *task = current; |
| 8960 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8961 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8962 | /* for SQPOLL only sqo_task has task notes */ |
Pavel Begunkov | 6b393a1 | 2021-01-16 05:32:29 +0000 | [diff] [blame] | 8963 | WARN_ON_ONCE(ctx->sqo_task != current); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8964 | io_disable_sqo_submit(ctx); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8965 | task = ctx->sq_data->thread; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8966 | atomic_inc(&task->io_uring->in_idle); |
| 8967 | io_sq_thread_park(ctx->sq_data); |
| 8968 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8969 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8970 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8971 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8972 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8973 | if (!files) |
| 8974 | __io_uring_cancel_task_requests(ctx, task); |
Pavel Begunkov | bee749b | 2020-11-25 02:19:23 +0000 | [diff] [blame] | 8975 | else |
| 8976 | io_uring_cancel_files(ctx, task, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8977 | |
| 8978 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
| 8979 | atomic_dec(&task->io_uring->in_idle); |
| 8980 | /* |
| 8981 | * If the files that are going away are the ones in the thread |
| 8982 | * identity, clear them out. |
| 8983 | */ |
| 8984 | if (task->io_uring->identity->files == files) |
| 8985 | task->io_uring->identity->files = NULL; |
| 8986 | io_sq_thread_unpark(ctx->sq_data); |
| 8987 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8988 | } |
| 8989 | |
| 8990 | /* |
| 8991 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8992 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8993 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8994 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8995 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8996 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8997 | |
| 8998 | if (unlikely(!tctx)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8999 | ret = io_uring_alloc_task_context(current); |
| 9000 | if (unlikely(ret)) |
| 9001 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9002 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9003 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9004 | if (tctx->last != file) { |
| 9005 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9006 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9007 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9008 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9009 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 9010 | file, GFP_KERNEL)); |
| 9011 | if (ret) { |
| 9012 | fput(file); |
| 9013 | return ret; |
| 9014 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9015 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9016 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9017 | } |
| 9018 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9019 | /* |
| 9020 | * This is race safe in that the task itself is doing this, hence it |
| 9021 | * cannot be going through the exit/cancel paths at the same time. |
| 9022 | * This cannot be modified while exit/cancel is running. |
| 9023 | */ |
| 9024 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9025 | tctx->sqpoll = true; |
| 9026 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9027 | return 0; |
| 9028 | } |
| 9029 | |
| 9030 | /* |
| 9031 | * Remove this io_uring_file -> task mapping. |
| 9032 | */ |
| 9033 | static void io_uring_del_task_file(struct file *file) |
| 9034 | { |
| 9035 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9036 | |
| 9037 | if (tctx->last == file) |
| 9038 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 9039 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9040 | if (file) |
| 9041 | fput(file); |
| 9042 | } |
| 9043 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9044 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 9045 | { |
| 9046 | struct file *file; |
| 9047 | unsigned long index; |
| 9048 | |
| 9049 | xa_for_each(&tctx->xa, index, file) |
| 9050 | io_uring_del_task_file(file); |
| 9051 | } |
| 9052 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9053 | void __io_uring_files_cancel(struct files_struct *files) |
| 9054 | { |
| 9055 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 9056 | struct file *file; |
| 9057 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9058 | |
| 9059 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9060 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9061 | xa_for_each(&tctx->xa, index, file) |
| 9062 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9063 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9064 | |
| 9065 | if (files) |
| 9066 | io_uring_remove_task_files(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9067 | } |
| 9068 | |
| 9069 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 9070 | { |
| 9071 | unsigned long index; |
| 9072 | struct file *file; |
| 9073 | s64 inflight; |
| 9074 | |
| 9075 | inflight = percpu_counter_sum(&tctx->inflight); |
| 9076 | if (!tctx->sqpoll) |
| 9077 | return inflight; |
| 9078 | |
| 9079 | /* |
| 9080 | * If we have SQPOLL rings, then we need to iterate and find them, and |
| 9081 | * add the pending count for those. |
| 9082 | */ |
| 9083 | xa_for_each(&tctx->xa, index, file) { |
| 9084 | struct io_ring_ctx *ctx = file->private_data; |
| 9085 | |
| 9086 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9087 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; |
| 9088 | |
| 9089 | inflight += percpu_counter_sum(&__tctx->inflight); |
| 9090 | } |
| 9091 | } |
| 9092 | |
| 9093 | return inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9094 | } |
| 9095 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9096 | /* |
| 9097 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 9098 | * requests. |
| 9099 | */ |
| 9100 | void __io_uring_task_cancel(void) |
| 9101 | { |
| 9102 | struct io_uring_task *tctx = current->io_uring; |
| 9103 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9104 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9105 | |
| 9106 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9107 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9108 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 9109 | /* trigger io_disable_sqo_submit() */ |
| 9110 | if (tctx->sqpoll) |
| 9111 | __io_uring_files_cancel(NULL); |
| 9112 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9113 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9114 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9115 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9116 | if (!inflight) |
| 9117 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9118 | __io_uring_files_cancel(NULL); |
| 9119 | |
| 9120 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9121 | |
| 9122 | /* |
| 9123 | * If we've seen completions, retry. This avoids a race where |
| 9124 | * a completion comes in before we did prepare_to_wait(). |
| 9125 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9126 | if (inflight != tctx_inflight(tctx)) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9127 | continue; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9128 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9129 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9130 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9131 | |
Jens Axboe | a8d13db | 2021-01-15 16:04:23 -0700 | [diff] [blame] | 9132 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9133 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9134 | |
| 9135 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9136 | } |
| 9137 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9138 | static int io_uring_flush(struct file *file, void *data) |
| 9139 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9140 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9141 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9142 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame^] | 9143 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) |
| 9144 | io_uring_cancel_task_requests(ctx, NULL); |
| 9145 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9146 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9147 | return 0; |
| 9148 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9149 | /* we should have cancelled and erased it before PF_EXITING */ |
| 9150 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 9151 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9152 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9153 | /* |
| 9154 | * fput() is pending, will be 2 if the only other ref is our potential |
| 9155 | * task file note. If the task is exiting, drop regardless of count. |
| 9156 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9157 | if (atomic_long_read(&file->f_count) != 2) |
| 9158 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9159 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9160 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9161 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 9162 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 9163 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9164 | /* sqo_dead check is for when this happens after cancellation */ |
| 9165 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9166 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 9167 | |
| 9168 | io_disable_sqo_submit(ctx); |
| 9169 | } |
| 9170 | |
| 9171 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 9172 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9173 | return 0; |
| 9174 | } |
| 9175 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9176 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9177 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9178 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9179 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9180 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9181 | struct page *page; |
| 9182 | void *ptr; |
| 9183 | |
| 9184 | switch (offset) { |
| 9185 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9186 | case IORING_OFF_CQ_RING: |
| 9187 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9188 | break; |
| 9189 | case IORING_OFF_SQES: |
| 9190 | ptr = ctx->sq_sqes; |
| 9191 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9192 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9193 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9194 | } |
| 9195 | |
| 9196 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9197 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9198 | return ERR_PTR(-EINVAL); |
| 9199 | |
| 9200 | return ptr; |
| 9201 | } |
| 9202 | |
| 9203 | #ifdef CONFIG_MMU |
| 9204 | |
| 9205 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9206 | { |
| 9207 | size_t sz = vma->vm_end - vma->vm_start; |
| 9208 | unsigned long pfn; |
| 9209 | void *ptr; |
| 9210 | |
| 9211 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9212 | if (IS_ERR(ptr)) |
| 9213 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9214 | |
| 9215 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9216 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9217 | } |
| 9218 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9219 | #else /* !CONFIG_MMU */ |
| 9220 | |
| 9221 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9222 | { |
| 9223 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9224 | } |
| 9225 | |
| 9226 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9227 | { |
| 9228 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9229 | } |
| 9230 | |
| 9231 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9232 | unsigned long addr, unsigned long len, |
| 9233 | unsigned long pgoff, unsigned long flags) |
| 9234 | { |
| 9235 | void *ptr; |
| 9236 | |
| 9237 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9238 | if (IS_ERR(ptr)) |
| 9239 | return PTR_ERR(ptr); |
| 9240 | |
| 9241 | return (unsigned long) ptr; |
| 9242 | } |
| 9243 | |
| 9244 | #endif /* !CONFIG_MMU */ |
| 9245 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9246 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9247 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9248 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9249 | DEFINE_WAIT(wait); |
| 9250 | |
| 9251 | do { |
| 9252 | if (!io_sqring_full(ctx)) |
| 9253 | break; |
| 9254 | |
| 9255 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9256 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9257 | if (unlikely(ctx->sqo_dead)) { |
| 9258 | ret = -EOWNERDEAD; |
| 9259 | goto out; |
| 9260 | } |
| 9261 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9262 | if (!io_sqring_full(ctx)) |
| 9263 | break; |
| 9264 | |
| 9265 | schedule(); |
| 9266 | } while (!signal_pending(current)); |
| 9267 | |
| 9268 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9269 | out: |
| 9270 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9271 | } |
| 9272 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9273 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9274 | struct __kernel_timespec __user **ts, |
| 9275 | const sigset_t __user **sig) |
| 9276 | { |
| 9277 | struct io_uring_getevents_arg arg; |
| 9278 | |
| 9279 | /* |
| 9280 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9281 | * is just a pointer to the sigset_t. |
| 9282 | */ |
| 9283 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9284 | *sig = (const sigset_t __user *) argp; |
| 9285 | *ts = NULL; |
| 9286 | return 0; |
| 9287 | } |
| 9288 | |
| 9289 | /* |
| 9290 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9291 | * timespec and sigset_t pointers if good. |
| 9292 | */ |
| 9293 | if (*argsz != sizeof(arg)) |
| 9294 | return -EINVAL; |
| 9295 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9296 | return -EFAULT; |
| 9297 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9298 | *argsz = arg.sigmask_sz; |
| 9299 | *ts = u64_to_user_ptr(arg.ts); |
| 9300 | return 0; |
| 9301 | } |
| 9302 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9303 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9304 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9305 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9306 | { |
| 9307 | struct io_ring_ctx *ctx; |
| 9308 | long ret = -EBADF; |
| 9309 | int submitted = 0; |
| 9310 | struct fd f; |
| 9311 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9312 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9313 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9314 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9315 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9316 | return -EINVAL; |
| 9317 | |
| 9318 | f = fdget(fd); |
| 9319 | if (!f.file) |
| 9320 | return -EBADF; |
| 9321 | |
| 9322 | ret = -EOPNOTSUPP; |
| 9323 | if (f.file->f_op != &io_uring_fops) |
| 9324 | goto out_fput; |
| 9325 | |
| 9326 | ret = -ENXIO; |
| 9327 | ctx = f.file->private_data; |
| 9328 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9329 | goto out_fput; |
| 9330 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9331 | ret = -EBADFD; |
| 9332 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9333 | goto out; |
| 9334 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9335 | /* |
| 9336 | * For SQ polling, the thread will do all submissions and completions. |
| 9337 | * Just return the requested submit count, and wake the thread if |
| 9338 | * we were asked to. |
| 9339 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9340 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9341 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9342 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9343 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9344 | ret = -EOWNERDEAD; |
| 9345 | if (unlikely(ctx->sqo_dead)) |
| 9346 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9347 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9348 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9349 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9350 | ret = io_sqpoll_wait_sq(ctx); |
| 9351 | if (ret) |
| 9352 | goto out; |
| 9353 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9354 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9355 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9356 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9357 | if (unlikely(ret)) |
| 9358 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9359 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9360 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9361 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9362 | |
| 9363 | if (submitted != to_submit) |
| 9364 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9365 | } |
| 9366 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9367 | const sigset_t __user *sig; |
| 9368 | struct __kernel_timespec __user *ts; |
| 9369 | |
| 9370 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9371 | if (unlikely(ret)) |
| 9372 | goto out; |
| 9373 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9374 | min_complete = min(min_complete, ctx->cq_entries); |
| 9375 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9376 | /* |
| 9377 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9378 | * space applications don't need to do io completion events |
| 9379 | * polling again, they can rely on io_sq_thread to do polling |
| 9380 | * work, which can reduce cpu usage and uring_lock contention. |
| 9381 | */ |
| 9382 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9383 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9384 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9385 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9386 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9387 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9388 | } |
| 9389 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9390 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9391 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9392 | out_fput: |
| 9393 | fdput(f); |
| 9394 | return submitted ? submitted : ret; |
| 9395 | } |
| 9396 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9397 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9398 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9399 | { |
Jens Axboe | 6b47ab8 | 2020-11-05 09:50:16 -0700 | [diff] [blame] | 9400 | struct io_identity *iod = p; |
| 9401 | const struct cred *cred = iod->creds; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9402 | struct seq_file *m = data; |
| 9403 | struct user_namespace *uns = seq_user_ns(m); |
| 9404 | struct group_info *gi; |
| 9405 | kernel_cap_t cap; |
| 9406 | unsigned __capi; |
| 9407 | int g; |
| 9408 | |
| 9409 | seq_printf(m, "%5d\n", id); |
| 9410 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9411 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9412 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9413 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9414 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9415 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9416 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9417 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9418 | seq_puts(m, "\n\tGroups:\t"); |
| 9419 | gi = cred->group_info; |
| 9420 | for (g = 0; g < gi->ngroups; g++) { |
| 9421 | seq_put_decimal_ull(m, g ? " " : "", |
| 9422 | from_kgid_munged(uns, gi->gid[g])); |
| 9423 | } |
| 9424 | seq_puts(m, "\n\tCapEff:\t"); |
| 9425 | cap = cred->cap_effective; |
| 9426 | CAP_FOR_EACH_U32(__capi) |
| 9427 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9428 | seq_putc(m, '\n'); |
| 9429 | return 0; |
| 9430 | } |
| 9431 | |
| 9432 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9433 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9434 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9435 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9436 | int i; |
| 9437 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9438 | /* |
| 9439 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9440 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9441 | * cases. If we fail to get the lock, we just don't iterate any |
| 9442 | * structures that could be going away outside the io_uring mutex. |
| 9443 | */ |
| 9444 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9445 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9446 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9447 | sq = ctx->sq_data; |
| 9448 | |
| 9449 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9450 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9451 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9452 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9453 | struct fixed_file_table *table; |
| 9454 | struct file *f; |
| 9455 | |
| 9456 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 9457 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 9458 | if (f) |
| 9459 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9460 | else |
| 9461 | seq_printf(m, "%5u: <none>\n", i); |
| 9462 | } |
| 9463 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9464 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9465 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9466 | |
| 9467 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9468 | (unsigned int) buf->len); |
| 9469 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9470 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9471 | seq_printf(m, "Personalities:\n"); |
| 9472 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9473 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9474 | seq_printf(m, "PollList:\n"); |
| 9475 | spin_lock_irq(&ctx->completion_lock); |
| 9476 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9477 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9478 | struct io_kiocb *req; |
| 9479 | |
| 9480 | hlist_for_each_entry(req, list, hash_node) |
| 9481 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9482 | req->task->task_works != NULL); |
| 9483 | } |
| 9484 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9485 | if (has_lock) |
| 9486 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9487 | } |
| 9488 | |
| 9489 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9490 | { |
| 9491 | struct io_ring_ctx *ctx = f->private_data; |
| 9492 | |
| 9493 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9494 | __io_uring_show_fdinfo(ctx, m); |
| 9495 | percpu_ref_put(&ctx->refs); |
| 9496 | } |
| 9497 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9498 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9499 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9500 | static const struct file_operations io_uring_fops = { |
| 9501 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9502 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9503 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9504 | #ifndef CONFIG_MMU |
| 9505 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9506 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9507 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9508 | .poll = io_uring_poll, |
| 9509 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9510 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9511 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9512 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9513 | }; |
| 9514 | |
| 9515 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9516 | struct io_uring_params *p) |
| 9517 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9518 | struct io_rings *rings; |
| 9519 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9520 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9521 | /* make sure these are sane, as we already accounted them */ |
| 9522 | ctx->sq_entries = p->sq_entries; |
| 9523 | ctx->cq_entries = p->cq_entries; |
| 9524 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9525 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9526 | if (size == SIZE_MAX) |
| 9527 | return -EOVERFLOW; |
| 9528 | |
| 9529 | rings = io_mem_alloc(size); |
| 9530 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9531 | return -ENOMEM; |
| 9532 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9533 | ctx->rings = rings; |
| 9534 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9535 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9536 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9537 | rings->sq_ring_entries = p->sq_entries; |
| 9538 | rings->cq_ring_entries = p->cq_entries; |
| 9539 | ctx->sq_mask = rings->sq_ring_mask; |
| 9540 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9541 | |
| 9542 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9543 | if (size == SIZE_MAX) { |
| 9544 | io_mem_free(ctx->rings); |
| 9545 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9546 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9547 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9548 | |
| 9549 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9550 | if (!ctx->sq_sqes) { |
| 9551 | io_mem_free(ctx->rings); |
| 9552 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9553 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9554 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9555 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9556 | return 0; |
| 9557 | } |
| 9558 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9559 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9560 | { |
| 9561 | int ret, fd; |
| 9562 | |
| 9563 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9564 | if (fd < 0) |
| 9565 | return fd; |
| 9566 | |
| 9567 | ret = io_uring_add_task_file(ctx, file); |
| 9568 | if (ret) { |
| 9569 | put_unused_fd(fd); |
| 9570 | return ret; |
| 9571 | } |
| 9572 | fd_install(fd, file); |
| 9573 | return fd; |
| 9574 | } |
| 9575 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9576 | /* |
| 9577 | * Allocate an anonymous fd, this is what constitutes the application |
| 9578 | * visible backing of an io_uring instance. The application mmaps this |
| 9579 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9580 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9581 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9582 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9583 | { |
| 9584 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9585 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9586 | int ret; |
| 9587 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9588 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9589 | &ctx->ring_sock); |
| 9590 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9591 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9592 | #endif |
| 9593 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9594 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9595 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9596 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9597 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9598 | sock_release(ctx->ring_sock); |
| 9599 | ctx->ring_sock = NULL; |
| 9600 | } else { |
| 9601 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9602 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9603 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9604 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9605 | } |
| 9606 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9607 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9608 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9609 | { |
| 9610 | struct user_struct *user = NULL; |
| 9611 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9612 | struct file *file; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9613 | bool limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9614 | int ret; |
| 9615 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9616 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9617 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9618 | if (entries > IORING_MAX_ENTRIES) { |
| 9619 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9620 | return -EINVAL; |
| 9621 | entries = IORING_MAX_ENTRIES; |
| 9622 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9623 | |
| 9624 | /* |
| 9625 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9626 | * application to drive a higher depth than the size of the SQ ring, |
| 9627 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9628 | * some flexibility in overcommitting a bit. If the application has |
| 9629 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9630 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9631 | */ |
| 9632 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9633 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9634 | /* |
| 9635 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9636 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9637 | * any cq vs sq ring sizing. |
| 9638 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9639 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9640 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9641 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9642 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9643 | return -EINVAL; |
| 9644 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9645 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9646 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9647 | if (p->cq_entries < p->sq_entries) |
| 9648 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9649 | } else { |
| 9650 | p->cq_entries = 2 * p->sq_entries; |
| 9651 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9652 | |
| 9653 | user = get_uid(current_user()); |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9654 | limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9655 | |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9656 | if (limit_mem) { |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9657 | ret = __io_account_mem(user, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9658 | ring_pages(p->sq_entries, p->cq_entries)); |
| 9659 | if (ret) { |
| 9660 | free_uid(user); |
| 9661 | return ret; |
| 9662 | } |
| 9663 | } |
| 9664 | |
| 9665 | ctx = io_ring_ctx_alloc(p); |
| 9666 | if (!ctx) { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9667 | if (limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9668 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9669 | p->cq_entries)); |
| 9670 | free_uid(user); |
| 9671 | return -ENOMEM; |
| 9672 | } |
| 9673 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9674 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 9675 | ctx->creds = get_current_cred(); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 9676 | #ifdef CONFIG_AUDIT |
| 9677 | ctx->loginuid = current->loginuid; |
| 9678 | ctx->sessionid = current->sessionid; |
| 9679 | #endif |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9680 | ctx->sqo_task = get_task_struct(current); |
| 9681 | |
| 9682 | /* |
| 9683 | * This is just grabbed for accounting purposes. When a process exits, |
| 9684 | * the mm is exited and dropped before the files, hence we need to hang |
| 9685 | * on to this mm purely for the purposes of being able to unaccount |
| 9686 | * memory (locked/pinned vm). It's not used for anything else. |
| 9687 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9688 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9689 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9690 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 9691 | #ifdef CONFIG_BLK_CGROUP |
| 9692 | /* |
| 9693 | * The sq thread will belong to the original cgroup it was inited in. |
| 9694 | * If the cgroup goes offline (e.g. disabling the io controller), then |
| 9695 | * issued bios will be associated with the closest cgroup later in the |
| 9696 | * block layer. |
| 9697 | */ |
| 9698 | rcu_read_lock(); |
| 9699 | ctx->sqo_blkcg_css = blkcg_css(); |
| 9700 | ret = css_tryget_online(ctx->sqo_blkcg_css); |
| 9701 | rcu_read_unlock(); |
| 9702 | if (!ret) { |
| 9703 | /* don't init against a dying cgroup, have the user try again */ |
| 9704 | ctx->sqo_blkcg_css = NULL; |
| 9705 | ret = -ENODEV; |
| 9706 | goto err; |
| 9707 | } |
| 9708 | #endif |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9709 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9710 | /* |
| 9711 | * Account memory _before_ installing the file descriptor. Once |
| 9712 | * the descriptor is installed, it can get closed at any time. Also |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9713 | * do this before hitting the general error path, as ring freeing |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9714 | * will un-account as well. |
| 9715 | */ |
| 9716 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), |
| 9717 | ACCT_LOCKED); |
| 9718 | ctx->limit_mem = limit_mem; |
| 9719 | |
| 9720 | ret = io_allocate_scq_urings(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9721 | if (ret) |
| 9722 | goto err; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9723 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9724 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9725 | if (ret) |
| 9726 | goto err; |
| 9727 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9728 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9729 | io_sq_offload_start(ctx); |
| 9730 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9731 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
| 9732 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9733 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9734 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9735 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9736 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9737 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9738 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
| 9739 | |
| 9740 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9741 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9742 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9743 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9744 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9745 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9746 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9747 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9748 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9749 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9750 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9751 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9752 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
| 9753 | IORING_FEAT_EXT_ARG; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9754 | |
| 9755 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9756 | ret = -EFAULT; |
| 9757 | goto err; |
| 9758 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9759 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9760 | file = io_uring_get_file(ctx); |
| 9761 | if (IS_ERR(file)) { |
| 9762 | ret = PTR_ERR(file); |
| 9763 | goto err; |
| 9764 | } |
| 9765 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9766 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9767 | * Install ring fd as the very last thing, so we don't risk someone |
| 9768 | * having closed it before we finish setup |
| 9769 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9770 | ret = io_uring_install_fd(ctx, file); |
| 9771 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9772 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9773 | /* fput will clean it up */ |
| 9774 | fput(file); |
| 9775 | return ret; |
| 9776 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9777 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9778 | 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] | 9779 | return ret; |
| 9780 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9781 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9782 | io_ring_ctx_wait_and_kill(ctx); |
| 9783 | return ret; |
| 9784 | } |
| 9785 | |
| 9786 | /* |
| 9787 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9788 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9789 | * params structure passed in. |
| 9790 | */ |
| 9791 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9792 | { |
| 9793 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9794 | int i; |
| 9795 | |
| 9796 | if (copy_from_user(&p, params, sizeof(p))) |
| 9797 | return -EFAULT; |
| 9798 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9799 | if (p.resv[i]) |
| 9800 | return -EINVAL; |
| 9801 | } |
| 9802 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9803 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9804 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9805 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9806 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9807 | return -EINVAL; |
| 9808 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9809 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9810 | } |
| 9811 | |
| 9812 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9813 | struct io_uring_params __user *, params) |
| 9814 | { |
| 9815 | return io_uring_setup(entries, params); |
| 9816 | } |
| 9817 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9818 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9819 | { |
| 9820 | struct io_uring_probe *p; |
| 9821 | size_t size; |
| 9822 | int i, ret; |
| 9823 | |
| 9824 | size = struct_size(p, ops, nr_args); |
| 9825 | if (size == SIZE_MAX) |
| 9826 | return -EOVERFLOW; |
| 9827 | p = kzalloc(size, GFP_KERNEL); |
| 9828 | if (!p) |
| 9829 | return -ENOMEM; |
| 9830 | |
| 9831 | ret = -EFAULT; |
| 9832 | if (copy_from_user(p, arg, size)) |
| 9833 | goto out; |
| 9834 | ret = -EINVAL; |
| 9835 | if (memchr_inv(p, 0, size)) |
| 9836 | goto out; |
| 9837 | |
| 9838 | p->last_op = IORING_OP_LAST - 1; |
| 9839 | if (nr_args > IORING_OP_LAST) |
| 9840 | nr_args = IORING_OP_LAST; |
| 9841 | |
| 9842 | for (i = 0; i < nr_args; i++) { |
| 9843 | p->ops[i].op = i; |
| 9844 | if (!io_op_defs[i].not_supported) |
| 9845 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9846 | } |
| 9847 | p->ops_len = i; |
| 9848 | |
| 9849 | ret = 0; |
| 9850 | if (copy_to_user(arg, p, size)) |
| 9851 | ret = -EFAULT; |
| 9852 | out: |
| 9853 | kfree(p); |
| 9854 | return ret; |
| 9855 | } |
| 9856 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9857 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9858 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9859 | struct io_identity *id; |
| 9860 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9861 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9862 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 9863 | if (unlikely(!id)) |
| 9864 | return -ENOMEM; |
| 9865 | |
| 9866 | io_init_identity(id); |
| 9867 | id->creds = get_current_cred(); |
| 9868 | |
| 9869 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); |
| 9870 | if (ret < 0) { |
| 9871 | put_cred(id->creds); |
| 9872 | kfree(id); |
| 9873 | } |
| 9874 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9875 | } |
| 9876 | |
| 9877 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 9878 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9879 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9880 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9881 | iod = idr_remove(&ctx->personality_idr, id); |
| 9882 | if (iod) { |
| 9883 | put_cred(iod->creds); |
| 9884 | if (refcount_dec_and_test(&iod->count)) |
| 9885 | kfree(iod); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9886 | return 0; |
| 9887 | } |
| 9888 | |
| 9889 | return -EINVAL; |
| 9890 | } |
| 9891 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9892 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9893 | unsigned int nr_args) |
| 9894 | { |
| 9895 | struct io_uring_restriction *res; |
| 9896 | size_t size; |
| 9897 | int i, ret; |
| 9898 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9899 | /* Restrictions allowed only if rings started disabled */ |
| 9900 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9901 | return -EBADFD; |
| 9902 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9903 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9904 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9905 | return -EBUSY; |
| 9906 | |
| 9907 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9908 | return -EINVAL; |
| 9909 | |
| 9910 | size = array_size(nr_args, sizeof(*res)); |
| 9911 | if (size == SIZE_MAX) |
| 9912 | return -EOVERFLOW; |
| 9913 | |
| 9914 | res = memdup_user(arg, size); |
| 9915 | if (IS_ERR(res)) |
| 9916 | return PTR_ERR(res); |
| 9917 | |
| 9918 | ret = 0; |
| 9919 | |
| 9920 | for (i = 0; i < nr_args; i++) { |
| 9921 | switch (res[i].opcode) { |
| 9922 | case IORING_RESTRICTION_REGISTER_OP: |
| 9923 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9924 | ret = -EINVAL; |
| 9925 | goto out; |
| 9926 | } |
| 9927 | |
| 9928 | __set_bit(res[i].register_op, |
| 9929 | ctx->restrictions.register_op); |
| 9930 | break; |
| 9931 | case IORING_RESTRICTION_SQE_OP: |
| 9932 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9933 | ret = -EINVAL; |
| 9934 | goto out; |
| 9935 | } |
| 9936 | |
| 9937 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9938 | break; |
| 9939 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9940 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9941 | break; |
| 9942 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9943 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9944 | break; |
| 9945 | default: |
| 9946 | ret = -EINVAL; |
| 9947 | goto out; |
| 9948 | } |
| 9949 | } |
| 9950 | |
| 9951 | out: |
| 9952 | /* Reset all restrictions if an error happened */ |
| 9953 | if (ret != 0) |
| 9954 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9955 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9956 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9957 | |
| 9958 | kfree(res); |
| 9959 | return ret; |
| 9960 | } |
| 9961 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9962 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9963 | { |
| 9964 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9965 | return -EBADFD; |
| 9966 | |
| 9967 | if (ctx->restrictions.registered) |
| 9968 | ctx->restricted = 1; |
| 9969 | |
| 9970 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9971 | |
| 9972 | io_sq_offload_start(ctx); |
| 9973 | |
| 9974 | return 0; |
| 9975 | } |
| 9976 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9977 | static bool io_register_op_must_quiesce(int op) |
| 9978 | { |
| 9979 | switch (op) { |
| 9980 | case IORING_UNREGISTER_FILES: |
| 9981 | case IORING_REGISTER_FILES_UPDATE: |
| 9982 | case IORING_REGISTER_PROBE: |
| 9983 | case IORING_REGISTER_PERSONALITY: |
| 9984 | case IORING_UNREGISTER_PERSONALITY: |
| 9985 | return false; |
| 9986 | default: |
| 9987 | return true; |
| 9988 | } |
| 9989 | } |
| 9990 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9991 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9992 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9993 | __releases(ctx->uring_lock) |
| 9994 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9995 | { |
| 9996 | int ret; |
| 9997 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9998 | /* |
| 9999 | * We're inside the ring mutex, if the ref is already dying, then |
| 10000 | * someone else killed the ctx or is already going through |
| 10001 | * io_uring_register(). |
| 10002 | */ |
| 10003 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10004 | return -ENXIO; |
| 10005 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10006 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10007 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10008 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10009 | /* |
| 10010 | * Drop uring mutex before waiting for references to exit. If |
| 10011 | * another thread is currently inside io_uring_enter() it might |
| 10012 | * need to grab the uring_lock to make progress. If we hold it |
| 10013 | * here across the drain wait, then we can deadlock. It's safe |
| 10014 | * to drop the mutex here, since no new references will come in |
| 10015 | * after we've killed the percpu ref. |
| 10016 | */ |
| 10017 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10018 | do { |
| 10019 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 10020 | if (!ret) |
| 10021 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 10022 | ret = io_run_task_work_sig(); |
| 10023 | if (ret < 0) |
| 10024 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10025 | } while (1); |
| 10026 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10027 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10028 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10029 | if (ret) { |
| 10030 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10031 | goto out_quiesce; |
| 10032 | } |
| 10033 | } |
| 10034 | |
| 10035 | if (ctx->restricted) { |
| 10036 | if (opcode >= IORING_REGISTER_LAST) { |
| 10037 | ret = -EINVAL; |
| 10038 | goto out; |
| 10039 | } |
| 10040 | |
| 10041 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 10042 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10043 | goto out; |
| 10044 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10045 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10046 | |
| 10047 | switch (opcode) { |
| 10048 | case IORING_REGISTER_BUFFERS: |
| 10049 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 10050 | break; |
| 10051 | case IORING_UNREGISTER_BUFFERS: |
| 10052 | ret = -EINVAL; |
| 10053 | if (arg || nr_args) |
| 10054 | break; |
| 10055 | ret = io_sqe_buffer_unregister(ctx); |
| 10056 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10057 | case IORING_REGISTER_FILES: |
| 10058 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 10059 | break; |
| 10060 | case IORING_UNREGISTER_FILES: |
| 10061 | ret = -EINVAL; |
| 10062 | if (arg || nr_args) |
| 10063 | break; |
| 10064 | ret = io_sqe_files_unregister(ctx); |
| 10065 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10066 | case IORING_REGISTER_FILES_UPDATE: |
| 10067 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 10068 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10069 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10070 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10071 | ret = -EINVAL; |
| 10072 | if (nr_args != 1) |
| 10073 | break; |
| 10074 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10075 | if (ret) |
| 10076 | break; |
| 10077 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10078 | ctx->eventfd_async = 1; |
| 10079 | else |
| 10080 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10081 | break; |
| 10082 | case IORING_UNREGISTER_EVENTFD: |
| 10083 | ret = -EINVAL; |
| 10084 | if (arg || nr_args) |
| 10085 | break; |
| 10086 | ret = io_eventfd_unregister(ctx); |
| 10087 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10088 | case IORING_REGISTER_PROBE: |
| 10089 | ret = -EINVAL; |
| 10090 | if (!arg || nr_args > 256) |
| 10091 | break; |
| 10092 | ret = io_probe(ctx, arg, nr_args); |
| 10093 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10094 | case IORING_REGISTER_PERSONALITY: |
| 10095 | ret = -EINVAL; |
| 10096 | if (arg || nr_args) |
| 10097 | break; |
| 10098 | ret = io_register_personality(ctx); |
| 10099 | break; |
| 10100 | case IORING_UNREGISTER_PERSONALITY: |
| 10101 | ret = -EINVAL; |
| 10102 | if (arg) |
| 10103 | break; |
| 10104 | ret = io_unregister_personality(ctx, nr_args); |
| 10105 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10106 | case IORING_REGISTER_ENABLE_RINGS: |
| 10107 | ret = -EINVAL; |
| 10108 | if (arg || nr_args) |
| 10109 | break; |
| 10110 | ret = io_register_enable_rings(ctx); |
| 10111 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10112 | case IORING_REGISTER_RESTRICTIONS: |
| 10113 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10114 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10115 | default: |
| 10116 | ret = -EINVAL; |
| 10117 | break; |
| 10118 | } |
| 10119 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10120 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10121 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10122 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10123 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10124 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10125 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10126 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10127 | return ret; |
| 10128 | } |
| 10129 | |
| 10130 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10131 | void __user *, arg, unsigned int, nr_args) |
| 10132 | { |
| 10133 | struct io_ring_ctx *ctx; |
| 10134 | long ret = -EBADF; |
| 10135 | struct fd f; |
| 10136 | |
| 10137 | f = fdget(fd); |
| 10138 | if (!f.file) |
| 10139 | return -EBADF; |
| 10140 | |
| 10141 | ret = -EOPNOTSUPP; |
| 10142 | if (f.file->f_op != &io_uring_fops) |
| 10143 | goto out_fput; |
| 10144 | |
| 10145 | ctx = f.file->private_data; |
| 10146 | |
| 10147 | mutex_lock(&ctx->uring_lock); |
| 10148 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10149 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10150 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10151 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10152 | out_fput: |
| 10153 | fdput(f); |
| 10154 | return ret; |
| 10155 | } |
| 10156 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10157 | static int __init io_uring_init(void) |
| 10158 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10159 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10160 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10161 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10162 | } while (0) |
| 10163 | |
| 10164 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10165 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10166 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10167 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10168 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10169 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10170 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10171 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10172 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10173 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10174 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10175 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10176 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10177 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10178 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10179 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10180 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10181 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10182 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10183 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10184 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10185 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10186 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10187 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10188 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10189 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10190 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10191 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10192 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 10193 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10194 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10195 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10196 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 10197 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10198 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 10199 | return 0; |
| 10200 | }; |
| 10201 | __initcall(io_uring_init); |