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); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1028 | |
| 1029 | static struct kmem_cache *req_cachep; |
| 1030 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1031 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1032 | |
| 1033 | struct sock *io_uring_get_socket(struct file *file) |
| 1034 | { |
| 1035 | #if defined(CONFIG_UNIX) |
| 1036 | if (file->f_op == &io_uring_fops) { |
| 1037 | struct io_ring_ctx *ctx = file->private_data; |
| 1038 | |
| 1039 | return ctx->ring_sock->sk; |
| 1040 | } |
| 1041 | #endif |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1045 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1046 | #define io_for_each_link(pos, head) \ |
| 1047 | for (pos = (head); pos; pos = pos->link) |
| 1048 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1049 | static inline void io_clean_op(struct io_kiocb *req) |
| 1050 | { |
Pavel Begunkov | bb17534 | 2020-08-20 11:33:35 +0300 | [diff] [blame] | 1051 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
| 1052 | REQ_F_INFLIGHT)) |
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 | |
| 1072 | if (task && head->task != task) |
| 1073 | return false; |
| 1074 | if (!files) |
| 1075 | return true; |
| 1076 | |
| 1077 | io_for_each_link(req, head) { |
| 1078 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 1079 | (req->work.flags & IO_WQ_WORK_FILES) && |
| 1080 | req->work.identity->files == files) |
| 1081 | return true; |
| 1082 | } |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1086 | static void io_sq_thread_drop_mm_files(void) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1087 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1088 | struct files_struct *files = current->files; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1089 | struct mm_struct *mm = current->mm; |
| 1090 | |
| 1091 | if (mm) { |
| 1092 | kthread_unuse_mm(mm); |
| 1093 | mmput(mm); |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1094 | current->mm = NULL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1095 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1096 | if (files) { |
| 1097 | struct nsproxy *nsproxy = current->nsproxy; |
| 1098 | |
| 1099 | task_lock(current); |
| 1100 | current->files = NULL; |
| 1101 | current->nsproxy = NULL; |
| 1102 | task_unlock(current); |
| 1103 | put_files_struct(files); |
| 1104 | put_nsproxy(nsproxy); |
| 1105 | } |
| 1106 | } |
| 1107 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1108 | static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx) |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1109 | { |
Pavel Begunkov | 621fadc | 2021-01-11 04:00:31 +0000 | [diff] [blame] | 1110 | if (current->flags & PF_EXITING) |
| 1111 | return -EFAULT; |
| 1112 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1113 | if (!current->files) { |
| 1114 | struct files_struct *files; |
| 1115 | struct nsproxy *nsproxy; |
| 1116 | |
| 1117 | task_lock(ctx->sqo_task); |
| 1118 | files = ctx->sqo_task->files; |
| 1119 | if (!files) { |
| 1120 | task_unlock(ctx->sqo_task); |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1121 | return -EOWNERDEAD; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1122 | } |
| 1123 | atomic_inc(&files->count); |
| 1124 | get_nsproxy(ctx->sqo_task->nsproxy); |
| 1125 | nsproxy = ctx->sqo_task->nsproxy; |
| 1126 | task_unlock(ctx->sqo_task); |
| 1127 | |
| 1128 | task_lock(current); |
| 1129 | current->files = files; |
| 1130 | current->nsproxy = nsproxy; |
| 1131 | task_unlock(current); |
| 1132 | } |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1133 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| 1137 | { |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1138 | struct mm_struct *mm; |
| 1139 | |
Pavel Begunkov | 621fadc | 2021-01-11 04:00:31 +0000 | [diff] [blame] | 1140 | if (current->flags & PF_EXITING) |
| 1141 | return -EFAULT; |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1142 | if (current->mm) |
| 1143 | return 0; |
| 1144 | |
| 1145 | /* Should never happen */ |
| 1146 | if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) |
| 1147 | return -EFAULT; |
| 1148 | |
| 1149 | task_lock(ctx->sqo_task); |
| 1150 | mm = ctx->sqo_task->mm; |
| 1151 | if (unlikely(!mm || !mmget_not_zero(mm))) |
| 1152 | mm = NULL; |
| 1153 | task_unlock(ctx->sqo_task); |
| 1154 | |
| 1155 | if (mm) { |
| 1156 | kthread_use_mm(mm); |
| 1157 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1158 | } |
| 1159 | |
Jens Axboe | 4b70cf9 | 2020-11-02 10:39:05 -0700 | [diff] [blame] | 1160 | return -EFAULT; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1161 | } |
| 1162 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1163 | static int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx, |
| 1164 | struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1165 | { |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1166 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1167 | int ret; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1168 | |
| 1169 | if (def->work_flags & IO_WQ_WORK_MM) { |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1170 | ret = __io_sq_thread_acquire_mm(ctx); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1171 | if (unlikely(ret)) |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
Pavel Begunkov | 1a38ffc | 2020-11-08 12:55:55 +0000 | [diff] [blame] | 1175 | if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) { |
| 1176 | ret = __io_sq_thread_acquire_files(ctx); |
| 1177 | if (unlikely(ret)) |
| 1178 | return ret; |
| 1179 | } |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 1180 | |
| 1181 | return 0; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1182 | } |
| 1183 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1184 | static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
| 1185 | struct cgroup_subsys_state **cur_css) |
| 1186 | |
| 1187 | { |
| 1188 | #ifdef CONFIG_BLK_CGROUP |
| 1189 | /* puts the old one when swapping */ |
| 1190 | if (*cur_css != ctx->sqo_blkcg_css) { |
| 1191 | kthread_associate_blkcg(ctx->sqo_blkcg_css); |
| 1192 | *cur_css = ctx->sqo_blkcg_css; |
| 1193 | } |
| 1194 | #endif |
| 1195 | } |
| 1196 | |
| 1197 | static void io_sq_thread_unassociate_blkcg(void) |
| 1198 | { |
| 1199 | #ifdef CONFIG_BLK_CGROUP |
| 1200 | kthread_associate_blkcg(NULL); |
| 1201 | #endif |
| 1202 | } |
| 1203 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1204 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1205 | { |
| 1206 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1207 | req->flags |= REQ_F_FAIL_LINK; |
| 1208 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1209 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1210 | /* |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1211 | * None of these are dereferenced, they are simply used to check if any of |
| 1212 | * them have changed. If we're under current and check they are still the |
| 1213 | * same, we're fine to grab references to them for actual out-of-line use. |
| 1214 | */ |
| 1215 | static void io_init_identity(struct io_identity *id) |
| 1216 | { |
| 1217 | id->files = current->files; |
| 1218 | id->mm = current->mm; |
| 1219 | #ifdef CONFIG_BLK_CGROUP |
| 1220 | rcu_read_lock(); |
| 1221 | id->blkcg_css = blkcg_css(); |
| 1222 | rcu_read_unlock(); |
| 1223 | #endif |
| 1224 | id->creds = current_cred(); |
| 1225 | id->nsproxy = current->nsproxy; |
| 1226 | id->fs = current->fs; |
| 1227 | id->fsize = rlimit(RLIMIT_FSIZE); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1228 | #ifdef CONFIG_AUDIT |
| 1229 | id->loginuid = current->loginuid; |
| 1230 | id->sessionid = current->sessionid; |
| 1231 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1232 | refcount_set(&id->count, 1); |
| 1233 | } |
| 1234 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1235 | static inline void __io_req_init_async(struct io_kiocb *req) |
| 1236 | { |
| 1237 | memset(&req->work, 0, sizeof(req->work)); |
| 1238 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1239 | } |
| 1240 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1241 | /* |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1242 | * Note: must call io_req_init_async() for the first time you |
| 1243 | * touch any members of io_wq_work. |
| 1244 | */ |
| 1245 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1246 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1247 | struct io_uring_task *tctx = current->io_uring; |
| 1248 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1249 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1250 | return; |
| 1251 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1252 | __io_req_init_async(req); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1253 | |
| 1254 | /* Grab a ref if this isn't our static identity */ |
| 1255 | req->work.identity = tctx->identity; |
| 1256 | if (tctx->identity != &tctx->__identity) |
| 1257 | refcount_inc(&req->work.identity->count); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1258 | } |
| 1259 | |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 1260 | static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| 1261 | { |
| 1262 | return ctx->flags & IORING_SETUP_SQPOLL; |
| 1263 | } |
| 1264 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1265 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1266 | { |
| 1267 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1268 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1269 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1272 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1273 | { |
| 1274 | return !req->timeout.off; |
| 1275 | } |
| 1276 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1277 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1278 | { |
| 1279 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1280 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1281 | |
| 1282 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1283 | if (!ctx) |
| 1284 | return NULL; |
| 1285 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1286 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 1287 | if (!ctx->fallback_req) |
| 1288 | goto err; |
| 1289 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1290 | /* |
| 1291 | * Use 5 bits less than the max cq entries, that should give us around |
| 1292 | * 32 entries per hash list if totally full and uniformly spread. |
| 1293 | */ |
| 1294 | hash_bits = ilog2(p->cq_entries); |
| 1295 | hash_bits -= 5; |
| 1296 | if (hash_bits <= 0) |
| 1297 | hash_bits = 1; |
| 1298 | ctx->cancel_hash_bits = hash_bits; |
| 1299 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1300 | GFP_KERNEL); |
| 1301 | if (!ctx->cancel_hash) |
| 1302 | goto err; |
| 1303 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1304 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1305 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1306 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1307 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1308 | |
| 1309 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1310 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1311 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1312 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1313 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1314 | init_completion(&ctx->ref_comp); |
| 1315 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1316 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1317 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1318 | mutex_init(&ctx->uring_lock); |
| 1319 | init_waitqueue_head(&ctx->wait); |
| 1320 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1321 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1322 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1323 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1324 | spin_lock_init(&ctx->inflight_lock); |
| 1325 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1326 | INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| 1327 | init_llist_head(&ctx->file_put_llist); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1328 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1329 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1330 | if (ctx->fallback_req) |
| 1331 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1332 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1333 | kfree(ctx); |
| 1334 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1337 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1338 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1339 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1340 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1341 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1342 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1343 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1344 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1345 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1346 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1347 | } |
| 1348 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1349 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1350 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1351 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1352 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 1353 | /* order cqe stores with ring update */ |
| 1354 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1357 | 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] | 1358 | { |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1359 | if (req->work.identity == &tctx->__identity) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1360 | return; |
| 1361 | if (refcount_dec_and_test(&req->work.identity->count)) |
| 1362 | kfree(req->work.identity); |
| 1363 | } |
| 1364 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1365 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1366 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1367 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1368 | return; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1369 | |
| 1370 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1371 | |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1372 | if (req->work.flags & IO_WQ_WORK_MM) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1373 | mmdrop(req->work.identity->mm); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1374 | req->work.flags &= ~IO_WQ_WORK_MM; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1375 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 1376 | #ifdef CONFIG_BLK_CGROUP |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1377 | if (req->work.flags & IO_WQ_WORK_BLKCG) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1378 | css_put(req->work.identity->blkcg_css); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1379 | req->work.flags &= ~IO_WQ_WORK_BLKCG; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1380 | } |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1381 | #endif |
| 1382 | if (req->work.flags & IO_WQ_WORK_CREDS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1383 | put_cred(req->work.identity->creds); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1384 | req->work.flags &= ~IO_WQ_WORK_CREDS; |
| 1385 | } |
| 1386 | if (req->work.flags & IO_WQ_WORK_FS) { |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1387 | struct fs_struct *fs = req->work.identity->fs; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1388 | |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1389 | spin_lock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1390 | if (--fs->users) |
| 1391 | fs = NULL; |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 1392 | spin_unlock(&req->work.identity->fs->lock); |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1393 | if (fs) |
| 1394 | free_fs_struct(fs); |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 1395 | req->work.flags &= ~IO_WQ_WORK_FS; |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 1396 | } |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1397 | |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1398 | io_put_identity(req->task->io_uring, req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Create a private copy of io_identity, since some fields don't match |
| 1403 | * the current context. |
| 1404 | */ |
| 1405 | static bool io_identity_cow(struct io_kiocb *req) |
| 1406 | { |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1407 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1408 | const struct cred *creds = NULL; |
| 1409 | struct io_identity *id; |
| 1410 | |
| 1411 | if (req->work.flags & IO_WQ_WORK_CREDS) |
| 1412 | creds = req->work.identity->creds; |
| 1413 | |
| 1414 | id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); |
| 1415 | if (unlikely(!id)) { |
| 1416 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * We can safely just re-init the creds we copied Either the field |
| 1422 | * matches the current one, or we haven't grabbed it yet. The only |
| 1423 | * exception is ->creds, through registered personalities, so handle |
| 1424 | * that one separately. |
| 1425 | */ |
| 1426 | io_init_identity(id); |
| 1427 | if (creds) |
Pavel Begunkov | e8c954d | 2020-12-06 22:22:46 +0000 | [diff] [blame] | 1428 | id->creds = creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1429 | |
| 1430 | /* add one for this request */ |
| 1431 | refcount_inc(&id->count); |
| 1432 | |
Jens Axboe | cb8a8ae | 2020-11-03 12:19:07 -0700 | [diff] [blame] | 1433 | /* drop tctx and req identity references, if needed */ |
| 1434 | if (tctx->identity != &tctx->__identity && |
| 1435 | refcount_dec_and_test(&tctx->identity->count)) |
| 1436 | kfree(tctx->identity); |
| 1437 | if (req->work.identity != &tctx->__identity && |
| 1438 | refcount_dec_and_test(&req->work.identity->count)) |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1439 | kfree(req->work.identity); |
| 1440 | |
| 1441 | req->work.identity = id; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 1442 | tctx->identity = id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1443 | return true; |
| 1444 | } |
| 1445 | |
| 1446 | static bool io_grab_identity(struct io_kiocb *req) |
| 1447 | { |
| 1448 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 5c3462c | 2020-10-15 09:02:33 -0600 | [diff] [blame] | 1449 | struct io_identity *id = req->work.identity; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1450 | struct io_ring_ctx *ctx = req->ctx; |
| 1451 | |
Jens Axboe | 6922833 | 2020-10-20 14:28:41 -0600 | [diff] [blame] | 1452 | if (def->work_flags & IO_WQ_WORK_FSIZE) { |
| 1453 | if (id->fsize != rlimit(RLIMIT_FSIZE)) |
| 1454 | return false; |
| 1455 | req->work.flags |= IO_WQ_WORK_FSIZE; |
| 1456 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1457 | #ifdef CONFIG_BLK_CGROUP |
| 1458 | if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
| 1459 | (def->work_flags & IO_WQ_WORK_BLKCG)) { |
| 1460 | rcu_read_lock(); |
| 1461 | if (id->blkcg_css != blkcg_css()) { |
| 1462 | rcu_read_unlock(); |
| 1463 | return false; |
| 1464 | } |
| 1465 | /* |
| 1466 | * This should be rare, either the cgroup is dying or the task |
| 1467 | * is moving cgroups. Just punt to root for the handful of ios. |
| 1468 | */ |
| 1469 | if (css_tryget_online(id->blkcg_css)) |
| 1470 | req->work.flags |= IO_WQ_WORK_BLKCG; |
| 1471 | rcu_read_unlock(); |
| 1472 | } |
| 1473 | #endif |
| 1474 | if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
| 1475 | if (id->creds != current_cred()) |
| 1476 | return false; |
| 1477 | get_cred(id->creds); |
| 1478 | req->work.flags |= IO_WQ_WORK_CREDS; |
| 1479 | } |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 1480 | #ifdef CONFIG_AUDIT |
| 1481 | if (!uid_eq(current->loginuid, id->loginuid) || |
| 1482 | current->sessionid != id->sessionid) |
| 1483 | return false; |
| 1484 | #endif |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1485 | if (!(req->work.flags & IO_WQ_WORK_FS) && |
| 1486 | (def->work_flags & IO_WQ_WORK_FS)) { |
| 1487 | if (current->fs != id->fs) |
| 1488 | return false; |
| 1489 | spin_lock(&id->fs->lock); |
| 1490 | if (!id->fs->in_exec) { |
| 1491 | id->fs->users++; |
| 1492 | req->work.flags |= IO_WQ_WORK_FS; |
| 1493 | } else { |
| 1494 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1495 | } |
| 1496 | spin_unlock(¤t->fs->lock); |
| 1497 | } |
Pavel Begunkov | af60470 | 2020-11-25 18:41:28 +0000 | [diff] [blame] | 1498 | if (!(req->work.flags & IO_WQ_WORK_FILES) && |
| 1499 | (def->work_flags & IO_WQ_WORK_FILES) && |
| 1500 | !(req->flags & REQ_F_NO_FILE_TABLE)) { |
| 1501 | if (id->files != current->files || |
| 1502 | id->nsproxy != current->nsproxy) |
| 1503 | return false; |
| 1504 | atomic_inc(&id->files->count); |
| 1505 | get_nsproxy(id->nsproxy); |
| 1506 | req->flags |= REQ_F_INFLIGHT; |
| 1507 | |
| 1508 | spin_lock_irq(&ctx->inflight_lock); |
| 1509 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1510 | spin_unlock_irq(&ctx->inflight_lock); |
| 1511 | req->work.flags |= IO_WQ_WORK_FILES; |
| 1512 | } |
Jens Axboe | 7778877 | 2020-12-29 10:50:46 -0700 | [diff] [blame] | 1513 | if (!(req->work.flags & IO_WQ_WORK_MM) && |
| 1514 | (def->work_flags & IO_WQ_WORK_MM)) { |
| 1515 | if (id->mm != current->mm) |
| 1516 | return false; |
| 1517 | mmgrab(id->mm); |
| 1518 | req->work.flags |= IO_WQ_WORK_MM; |
| 1519 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1520 | |
| 1521 | return true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1522 | } |
| 1523 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1524 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1525 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1526 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1527 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1528 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1529 | io_req_init_async(req); |
| 1530 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1531 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1532 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1533 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1534 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1535 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1536 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1537 | } else { |
| 1538 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1539 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1540 | } |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1541 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1542 | /* if we fail grabbing identity, we must COW, regrab, and retry */ |
| 1543 | if (io_grab_identity(req)) |
| 1544 | return; |
| 1545 | |
| 1546 | if (!io_identity_cow(req)) |
| 1547 | return; |
| 1548 | |
| 1549 | /* can't fail at this point */ |
| 1550 | if (!io_grab_identity(req)) |
| 1551 | WARN_ON(1); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1552 | } |
| 1553 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1554 | static void io_prep_async_link(struct io_kiocb *req) |
| 1555 | { |
| 1556 | struct io_kiocb *cur; |
| 1557 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1558 | io_for_each_link(cur, req) |
| 1559 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1560 | } |
| 1561 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1562 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1563 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1564 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1565 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1566 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1567 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1568 | &req->work, req->flags); |
| 1569 | io_wq_enqueue(ctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1570 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1571 | } |
| 1572 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1573 | static void io_queue_async_work(struct io_kiocb *req) |
| 1574 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1575 | struct io_kiocb *link; |
| 1576 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1577 | /* init ->work of the whole link before punting */ |
| 1578 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1579 | link = __io_queue_async_work(req); |
| 1580 | |
| 1581 | if (link) |
| 1582 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1583 | } |
| 1584 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1585 | static void io_kill_timeout(struct io_kiocb *req) |
| 1586 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1587 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1588 | int ret; |
| 1589 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1590 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1591 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1592 | atomic_set(&req->ctx->cq_timeouts, |
| 1593 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1594 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1595 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1596 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1600 | /* |
| 1601 | * Returns true if we found and killed one or more timeouts |
| 1602 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1603 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1604 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1605 | { |
| 1606 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1607 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1608 | |
| 1609 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1610 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1611 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1612 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1613 | canceled++; |
| 1614 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1615 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1616 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1617 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1618 | } |
| 1619 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1620 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1621 | { |
| 1622 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1623 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1624 | struct io_defer_entry, list); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1625 | struct io_kiocb *link; |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1626 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1627 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1628 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1629 | list_del_init(&de->list); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1630 | /* punt-init is done before queueing for defer */ |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1631 | link = __io_queue_async_work(de->req); |
| 1632 | if (link) { |
| 1633 | __io_queue_linked_timeout(link); |
| 1634 | /* drop submission reference */ |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1635 | io_put_req_deferred(link, 1); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1636 | } |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1637 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1638 | } while (!list_empty(&ctx->defer_list)); |
| 1639 | } |
| 1640 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1641 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1642 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1643 | u32 seq; |
| 1644 | |
| 1645 | if (list_empty(&ctx->timeout_list)) |
| 1646 | return; |
| 1647 | |
| 1648 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1649 | |
| 1650 | do { |
| 1651 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1652 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1653 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1654 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1655 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1656 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1657 | |
| 1658 | /* |
| 1659 | * Since seq can easily wrap around over time, subtract |
| 1660 | * the last seq at which timeouts were flushed before comparing. |
| 1661 | * Assuming not more than 2^31-1 events have happened since, |
| 1662 | * these subtractions won't have wrapped, so we can check if |
| 1663 | * target is in [last_seq, current_seq] by comparing the two. |
| 1664 | */ |
| 1665 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1666 | events_got = seq - ctx->cq_last_tm_flush; |
| 1667 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1668 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1669 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1670 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1671 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1672 | } while (!list_empty(&ctx->timeout_list)); |
| 1673 | |
| 1674 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1675 | } |
| 1676 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1677 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1678 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1679 | io_flush_timeouts(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1680 | __io_commit_cqring(ctx); |
| 1681 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1682 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1683 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1684 | } |
| 1685 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1686 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1687 | { |
| 1688 | struct io_rings *r = ctx->rings; |
| 1689 | |
| 1690 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1691 | } |
| 1692 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1693 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1694 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1695 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1696 | unsigned tail; |
| 1697 | |
| 1698 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1699 | /* |
| 1700 | * writes to the cq entry need to come after reading head; the |
| 1701 | * control dependency is enough as we're using WRITE_ONCE to |
| 1702 | * fill the cq entry |
| 1703 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1704 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1705 | return NULL; |
| 1706 | |
| 1707 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1708 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1711 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1712 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1713 | if (!ctx->cq_ev_fd) |
| 1714 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1715 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1716 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1717 | if (!ctx->eventfd_async) |
| 1718 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1719 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1722 | static inline unsigned __io_cqring_events(struct io_ring_ctx *ctx) |
| 1723 | { |
| 1724 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1725 | } |
| 1726 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1727 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1728 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1729 | /* see waitqueue_active() comment */ |
| 1730 | smp_mb(); |
| 1731 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1732 | if (waitqueue_active(&ctx->wait)) |
| 1733 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1734 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1735 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1736 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1737 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1738 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1739 | wake_up_interruptible(&ctx->cq_wait); |
| 1740 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1741 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1742 | } |
| 1743 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1744 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1745 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1746 | /* see waitqueue_active() comment */ |
| 1747 | smp_mb(); |
| 1748 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1749 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1750 | if (waitqueue_active(&ctx->wait)) |
| 1751 | wake_up(&ctx->wait); |
| 1752 | } |
| 1753 | if (io_should_trigger_evfd(ctx)) |
| 1754 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1755 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1756 | wake_up_interruptible(&ctx->cq_wait); |
| 1757 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1758 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1761 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1762 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1763 | struct task_struct *tsk, |
| 1764 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1765 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1766 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1767 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1768 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1769 | unsigned long flags; |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1770 | bool all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1771 | LIST_HEAD(list); |
| 1772 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1773 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1774 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1775 | |
| 1776 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1777 | 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] | 1778 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1779 | continue; |
| 1780 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1781 | cqe = io_get_cqring(ctx); |
| 1782 | if (!cqe && !force) |
| 1783 | break; |
| 1784 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1785 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1786 | if (cqe) { |
| 1787 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1788 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1789 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1790 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1791 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1792 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1793 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1797 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1798 | if (all_flushed) { |
| 1799 | clear_bit(0, &ctx->sq_check_overflow); |
| 1800 | clear_bit(0, &ctx->cq_check_overflow); |
| 1801 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1802 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1803 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1804 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1805 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1806 | io_cqring_ev_posted(ctx); |
| 1807 | |
| 1808 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1809 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1810 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1811 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1812 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1813 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1814 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1817 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1818 | struct task_struct *tsk, |
| 1819 | struct files_struct *files) |
| 1820 | { |
| 1821 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1822 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1823 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1824 | mutex_lock(&ctx->uring_lock); |
| 1825 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1826 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1827 | mutex_unlock(&ctx->uring_lock); |
| 1828 | } |
| 1829 | } |
| 1830 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1831 | 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] | 1832 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1833 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1834 | struct io_uring_cqe *cqe; |
| 1835 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1836 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1837 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1838 | /* |
| 1839 | * If we can't get a cq entry, userspace overflowed the |
| 1840 | * submission (by quite a lot). Increment the overflow count in |
| 1841 | * the ring. |
| 1842 | */ |
| 1843 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1844 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1845 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1846 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1847 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1848 | } else if (ctx->cq_overflow_flushed || |
| 1849 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1850 | /* |
| 1851 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1852 | * then we cannot store the request for later flushing, we need |
| 1853 | * to drop it on the floor. |
| 1854 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1855 | ctx->cached_cq_overflow++; |
| 1856 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1857 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1858 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1859 | set_bit(0, &ctx->sq_check_overflow); |
| 1860 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1861 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1862 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1863 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1864 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1865 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1866 | refcount_inc(&req->refs); |
| 1867 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1871 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1872 | { |
| 1873 | __io_cqring_fill_event(req, res, 0); |
| 1874 | } |
| 1875 | |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1876 | 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] | 1877 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1878 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1879 | unsigned long flags; |
| 1880 | |
| 1881 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1882 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1883 | io_commit_cqring(ctx); |
| 1884 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1885 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1886 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1889 | static void io_submit_flush_completions(struct io_comp_state *cs) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1890 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1891 | struct io_ring_ctx *ctx = cs->ctx; |
| 1892 | |
| 1893 | spin_lock_irq(&ctx->completion_lock); |
| 1894 | while (!list_empty(&cs->list)) { |
| 1895 | struct io_kiocb *req; |
| 1896 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1897 | req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
| 1898 | list_del(&req->compl.list); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1899 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1900 | |
| 1901 | /* |
| 1902 | * io_free_req() doesn't care about completion_lock unless one |
| 1903 | * of these flags is set. REQ_F_WORK_INITIALIZED is in the list |
| 1904 | * because of a potential deadlock with req->work.fs->lock |
| 1905 | */ |
| 1906 | if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT |
| 1907 | |REQ_F_WORK_INITIALIZED)) { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1908 | spin_unlock_irq(&ctx->completion_lock); |
| 1909 | io_put_req(req); |
| 1910 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1911 | } else { |
| 1912 | io_put_req(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | io_commit_cqring(ctx); |
| 1916 | spin_unlock_irq(&ctx->completion_lock); |
| 1917 | |
| 1918 | io_cqring_ev_posted(ctx); |
| 1919 | cs->nr = 0; |
| 1920 | } |
| 1921 | |
| 1922 | static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, |
| 1923 | struct io_comp_state *cs) |
| 1924 | { |
| 1925 | if (!cs) { |
| 1926 | io_cqring_add_event(req, res, cflags); |
| 1927 | io_put_req(req); |
| 1928 | } else { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1929 | io_clean_op(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1930 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1931 | req->compl.cflags = cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1932 | list_add_tail(&req->compl.list, &cs->list); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1933 | if (++cs->nr >= 32) |
| 1934 | io_submit_flush_completions(cs); |
| 1935 | } |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | static void io_req_complete(struct io_kiocb *req, long res) |
| 1939 | { |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 1940 | __io_req_complete(req, res, 0, NULL); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1943 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1944 | { |
| 1945 | return req == (struct io_kiocb *) |
| 1946 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1947 | } |
| 1948 | |
| 1949 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1950 | { |
| 1951 | struct io_kiocb *req; |
| 1952 | |
| 1953 | req = ctx->fallback_req; |
Bijan Mottahedeh | dd461af | 2020-04-29 17:47:50 -0700 | [diff] [blame] | 1954 | if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1955 | return req; |
| 1956 | |
| 1957 | return NULL; |
| 1958 | } |
| 1959 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1960 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| 1961 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1962 | { |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1963 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1964 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1965 | size_t sz; |
| 1966 | int ret; |
| 1967 | |
| 1968 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1969 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1970 | |
| 1971 | /* |
| 1972 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1973 | * retry single alloc to be on the safe side. |
| 1974 | */ |
| 1975 | if (unlikely(ret <= 0)) { |
| 1976 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1977 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1978 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1979 | ret = 1; |
| 1980 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1981 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1984 | state->free_reqs--; |
| 1985 | return state->reqs[state->free_reqs]; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1986 | fallback: |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 1987 | return io_get_fallback_req(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1990 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1991 | bool fixed) |
| 1992 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1993 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1994 | fput(file); |
| 1995 | } |
| 1996 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1997 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1998 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1999 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2000 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2001 | if (req->async_data) |
| 2002 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2003 | if (req->file) |
| 2004 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 2005 | if (req->fixed_file_refs) |
| 2006 | percpu_ref_put(req->fixed_file_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2007 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2008 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2009 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2010 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2011 | { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 2012 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2013 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2014 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2015 | io_dismantle_req(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2016 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 2017 | percpu_counter_dec(&tctx->inflight); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 2018 | if (atomic_read(&tctx->in_idle)) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 2019 | wake_up(&tctx->wait); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2020 | put_task_struct(req->task); |
| 2021 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 2022 | if (likely(!io_is_fallback_req(req))) |
| 2023 | kmem_cache_free(req_cachep, req); |
| 2024 | else |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2025 | clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
| 2026 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2027 | } |
| 2028 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2029 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2030 | { |
| 2031 | struct io_kiocb *nxt = req->link; |
| 2032 | |
| 2033 | req->link = nxt->link; |
| 2034 | nxt->link = NULL; |
| 2035 | } |
| 2036 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2037 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2038 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2039 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2040 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2041 | bool cancelled = false; |
| 2042 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2043 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2044 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2045 | link = req->link; |
| 2046 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 2047 | /* |
| 2048 | * Can happen if a linked timeout fired and link had been like |
| 2049 | * req -> link t-out -> link t-out [-> ...] |
| 2050 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2051 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 2052 | struct io_timeout_data *io = link->async_data; |
| 2053 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2054 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2055 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2056 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2057 | ret = hrtimer_try_to_cancel(&io->timer); |
| 2058 | if (ret != -1) { |
| 2059 | io_cqring_fill_event(link, -ECANCELED); |
| 2060 | io_commit_cqring(ctx); |
| 2061 | cancelled = true; |
| 2062 | } |
| 2063 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2064 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2065 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 2066 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2067 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2068 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2069 | io_put_req(link); |
| 2070 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2071 | } |
| 2072 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2073 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2074 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2075 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2076 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2077 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2078 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2079 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2080 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2081 | link = req->link; |
| 2082 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2083 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2084 | while (link) { |
| 2085 | nxt = link->link; |
| 2086 | link->link = NULL; |
| 2087 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2088 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2089 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* |
| 2092 | * It's ok to free under spinlock as they're not linked anymore, |
| 2093 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| 2094 | * work.fs->lock. |
| 2095 | */ |
| 2096 | if (link->flags & REQ_F_WORK_INITIALIZED) |
| 2097 | io_put_req_deferred(link, 2); |
| 2098 | else |
| 2099 | io_double_put_req(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2100 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2101 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2102 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2103 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2104 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2105 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2106 | } |
| 2107 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2108 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2109 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2110 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 2111 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2112 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2113 | /* |
| 2114 | * If LINK is set, we have dependent requests in this chain. If we |
| 2115 | * didn't fail this request, queue the first one up, moving any other |
| 2116 | * dependencies to the next request. In case of failure, fail the rest |
| 2117 | * of the chain. |
| 2118 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2119 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 2120 | struct io_kiocb *nxt = req->link; |
| 2121 | |
| 2122 | req->link = NULL; |
| 2123 | return nxt; |
| 2124 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2125 | io_fail_links(req); |
| 2126 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2127 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2128 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2129 | 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] | 2130 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2131 | if (likely(!(req->link) && !(req->flags & REQ_F_LINK_TIMEOUT))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 2132 | return NULL; |
| 2133 | return __io_req_find_next(req); |
| 2134 | } |
| 2135 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2136 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2137 | { |
| 2138 | struct task_struct *tsk = req->task; |
| 2139 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2140 | enum task_work_notify_mode notify; |
| 2141 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2142 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2143 | if (tsk->flags & PF_EXITING) |
| 2144 | return -ESRCH; |
| 2145 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2146 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2147 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2148 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2149 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2150 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2151 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2152 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2153 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2154 | notify = TWA_SIGNAL; |
| 2155 | |
Jens Axboe | 87c4311 | 2020-09-30 21:00:14 -0600 | [diff] [blame] | 2156 | ret = task_work_add(tsk, &req->task_work, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2157 | if (!ret) |
| 2158 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 2159 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2160 | return ret; |
| 2161 | } |
| 2162 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2163 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 2164 | { |
| 2165 | struct io_ring_ctx *ctx = req->ctx; |
| 2166 | |
| 2167 | spin_lock_irq(&ctx->completion_lock); |
| 2168 | io_cqring_fill_event(req, error); |
| 2169 | io_commit_cqring(ctx); |
| 2170 | spin_unlock_irq(&ctx->completion_lock); |
| 2171 | |
| 2172 | io_cqring_ev_posted(ctx); |
| 2173 | req_set_fail_links(req); |
| 2174 | io_double_put_req(req); |
| 2175 | } |
| 2176 | |
| 2177 | static void io_req_task_cancel(struct callback_head *cb) |
| 2178 | { |
| 2179 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2180 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2181 | |
| 2182 | __io_req_task_cancel(req, -ECANCELED); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2183 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2187 | { |
| 2188 | struct io_ring_ctx *ctx = req->ctx; |
| 2189 | |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2190 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 2191 | if (!ctx->sqo_dead && |
| 2192 | !__io_sq_thread_acquire_mm(ctx) && |
| 2193 | !__io_sq_thread_acquire_files(ctx)) |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 2194 | __io_queue_sqe(req, NULL); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2195 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2196 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2197 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2198 | } |
| 2199 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2200 | static void io_req_task_submit(struct callback_head *cb) |
| 2201 | { |
| 2202 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2203 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2204 | |
| 2205 | __io_req_task_submit(req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2206 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | static void io_req_task_queue(struct io_kiocb *req) |
| 2210 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2211 | int ret; |
| 2212 | |
| 2213 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2214 | percpu_ref_get(&req->ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2215 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2216 | ret = io_req_task_work_add(req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2217 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2218 | struct task_struct *tsk; |
| 2219 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2220 | init_task_work(&req->task_work, io_req_task_cancel); |
| 2221 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2222 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 2223 | wake_up_process(tsk); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2224 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2225 | } |
| 2226 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2227 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2228 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2229 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2230 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2231 | if (nxt) |
| 2232 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2233 | } |
| 2234 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2235 | static void io_free_req(struct io_kiocb *req) |
| 2236 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2237 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2238 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2239 | } |
| 2240 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2241 | struct req_batch { |
| 2242 | void *reqs[IO_IOPOLL_BATCH]; |
| 2243 | int to_free; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2244 | |
| 2245 | struct task_struct *task; |
| 2246 | int task_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2247 | }; |
| 2248 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2249 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2250 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2251 | rb->to_free = 0; |
| 2252 | rb->task_refs = 0; |
| 2253 | rb->task = NULL; |
| 2254 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2255 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2256 | static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
| 2257 | struct req_batch *rb) |
| 2258 | { |
| 2259 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 2260 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
| 2261 | rb->to_free = 0; |
| 2262 | } |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2263 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2264 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2265 | struct req_batch *rb) |
| 2266 | { |
| 2267 | if (rb->to_free) |
| 2268 | __io_req_free_batch_flush(ctx, rb); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2269 | if (rb->task) { |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 2270 | struct io_uring_task *tctx = rb->task->io_uring; |
| 2271 | |
| 2272 | percpu_counter_sub(&tctx->inflight, rb->task_refs); |
Jens Axboe | c93cc9e | 2021-01-16 11:52:11 -0700 | [diff] [blame] | 2273 | if (atomic_read(&tctx->in_idle)) |
| 2274 | wake_up(&tctx->wait); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2275 | put_task_struct_many(rb->task, rb->task_refs); |
| 2276 | rb->task = NULL; |
| 2277 | } |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) |
| 2281 | { |
| 2282 | if (unlikely(io_is_fallback_req(req))) { |
| 2283 | io_free_req(req); |
| 2284 | return; |
| 2285 | } |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2286 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2287 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2288 | if (req->task != rb->task) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 2289 | if (rb->task) { |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 2290 | struct io_uring_task *tctx = rb->task->io_uring; |
| 2291 | |
| 2292 | percpu_counter_sub(&tctx->inflight, rb->task_refs); |
Jens Axboe | c93cc9e | 2021-01-16 11:52:11 -0700 | [diff] [blame] | 2293 | if (atomic_read(&tctx->in_idle)) |
| 2294 | wake_up(&tctx->wait); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2295 | put_task_struct_many(rb->task, rb->task_refs); |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2296 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2297 | rb->task = req->task; |
| 2298 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2299 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2300 | rb->task_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2301 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2302 | io_dismantle_req(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2303 | rb->reqs[rb->to_free++] = req; |
| 2304 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 2305 | __io_req_free_batch_flush(req->ctx, rb); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2306 | } |
| 2307 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2308 | /* |
| 2309 | * Drop reference to request, return next in chain (if there is one) if this |
| 2310 | * was the last reference to this request. |
| 2311 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2312 | 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] | 2313 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2314 | struct io_kiocb *nxt = NULL; |
| 2315 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2316 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2317 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2318 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2319 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2320 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2323 | static void io_put_req(struct io_kiocb *req) |
| 2324 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2325 | if (refcount_dec_and_test(&req->refs)) |
| 2326 | io_free_req(req); |
| 2327 | } |
| 2328 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2329 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2330 | { |
| 2331 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2332 | |
| 2333 | io_free_req(req); |
| 2334 | } |
| 2335 | |
| 2336 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2337 | { |
| 2338 | int ret; |
| 2339 | |
| 2340 | init_task_work(&req->task_work, io_put_req_deferred_cb); |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2341 | ret = io_req_task_work_add(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2342 | if (unlikely(ret)) { |
| 2343 | struct task_struct *tsk; |
| 2344 | |
| 2345 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 2346 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2347 | wake_up_process(tsk); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2352 | { |
| 2353 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2354 | io_free_req_deferred(req); |
| 2355 | } |
| 2356 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 2357 | static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2358 | { |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 2359 | struct io_kiocb *nxt; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2360 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 2361 | /* |
| 2362 | * A ref is owned by io-wq in which context we're. So, if that's the |
| 2363 | * last one, it's safe to steal next work. False negatives are Ok, |
| 2364 | * it just will be re-punted async in io_put_work() |
| 2365 | */ |
| 2366 | if (refcount_read(&req->refs) != 1) |
| 2367 | return NULL; |
| 2368 | |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2369 | nxt = io_req_find_next(req); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 2370 | return nxt ? &nxt->work : NULL; |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2371 | } |
| 2372 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2373 | static void io_double_put_req(struct io_kiocb *req) |
| 2374 | { |
| 2375 | /* drop both submit and complete references */ |
| 2376 | if (refcount_sub_and_test(2, &req->refs)) |
| 2377 | io_free_req(req); |
| 2378 | } |
| 2379 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2380 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2381 | { |
| 2382 | /* See comment at the top of this file */ |
| 2383 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2384 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2385 | } |
| 2386 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2387 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2388 | { |
| 2389 | struct io_rings *rings = ctx->rings; |
| 2390 | |
| 2391 | /* make sure SQ entry isn't read before tail */ |
| 2392 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2393 | } |
| 2394 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2395 | 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] | 2396 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2397 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2398 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2399 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2400 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2401 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2402 | kfree(kbuf); |
| 2403 | return cflags; |
| 2404 | } |
| 2405 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2406 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2407 | { |
| 2408 | struct io_buffer *kbuf; |
| 2409 | |
| 2410 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2411 | return io_put_kbuf(req, kbuf); |
| 2412 | } |
| 2413 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2414 | static inline bool io_run_task_work(void) |
| 2415 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2416 | /* |
| 2417 | * Not safe to run on exiting task, and the task_work handling will |
| 2418 | * not add work to such a task. |
| 2419 | */ |
| 2420 | if (unlikely(current->flags & PF_EXITING)) |
| 2421 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2422 | if (current->task_works) { |
| 2423 | __set_current_state(TASK_RUNNING); |
| 2424 | task_work_run(); |
| 2425 | return true; |
| 2426 | } |
| 2427 | |
| 2428 | return false; |
| 2429 | } |
| 2430 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2431 | static void io_iopoll_queue(struct list_head *again) |
| 2432 | { |
| 2433 | struct io_kiocb *req; |
| 2434 | |
| 2435 | do { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2436 | req = list_first_entry(again, struct io_kiocb, inflight_entry); |
| 2437 | list_del(&req->inflight_entry); |
Pavel Begunkov | 81b68a5 | 2020-07-30 18:43:46 +0300 | [diff] [blame] | 2438 | __io_complete_rw(req, -EAGAIN, 0, NULL); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2439 | } while (!list_empty(again)); |
| 2440 | } |
| 2441 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2442 | /* |
| 2443 | * Find and free completed poll iocbs |
| 2444 | */ |
| 2445 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2446 | struct list_head *done) |
| 2447 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2448 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2449 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2450 | LIST_HEAD(again); |
| 2451 | |
| 2452 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2453 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2454 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2455 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2456 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2457 | int cflags = 0; |
| 2458 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2459 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2460 | if (READ_ONCE(req->result) == -EAGAIN) { |
Jens Axboe | 56450c2 | 2020-08-26 18:58:26 -0600 | [diff] [blame] | 2461 | req->result = 0; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2462 | req->iopoll_completed = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2463 | list_move_tail(&req->inflight_entry, &again); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2464 | continue; |
| 2465 | } |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2466 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2467 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2468 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2469 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2470 | |
| 2471 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2472 | (*nr_events)++; |
| 2473 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2474 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2475 | io_req_free_batch(&rb, req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2476 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2477 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2478 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2479 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2480 | io_req_free_batch_finish(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2481 | |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2482 | if (!list_empty(&again)) |
| 2483 | io_iopoll_queue(&again); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2486 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2487 | long min) |
| 2488 | { |
| 2489 | struct io_kiocb *req, *tmp; |
| 2490 | LIST_HEAD(done); |
| 2491 | bool spin; |
| 2492 | int ret; |
| 2493 | |
| 2494 | /* |
| 2495 | * Only spin for completions if we don't have multiple devices hanging |
| 2496 | * off our complete list, and we're under the requested amount. |
| 2497 | */ |
| 2498 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2499 | |
| 2500 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2501 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2502 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2503 | |
| 2504 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2505 | * Move completed and retryable entries to our local lists. |
| 2506 | * If we find a request that requires polling, break out |
| 2507 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2508 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2509 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2510 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2511 | continue; |
| 2512 | } |
| 2513 | if (!list_empty(&done)) |
| 2514 | break; |
| 2515 | |
| 2516 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2517 | if (ret < 0) |
| 2518 | break; |
| 2519 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2520 | /* iopoll may have completed current req */ |
| 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); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2523 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2524 | if (ret && spin) |
| 2525 | spin = false; |
| 2526 | ret = 0; |
| 2527 | } |
| 2528 | |
| 2529 | if (!list_empty(&done)) |
| 2530 | io_iopoll_complete(ctx, nr_events, &done); |
| 2531 | |
| 2532 | return ret; |
| 2533 | } |
| 2534 | |
| 2535 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2536 | * 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] | 2537 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2538 | * as a non-spinning completion check. |
| 2539 | */ |
| 2540 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2541 | long min) |
| 2542 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2543 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2544 | int ret; |
| 2545 | |
| 2546 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2547 | if (ret < 0) |
| 2548 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2549 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2550 | return 0; |
| 2551 | } |
| 2552 | |
| 2553 | return 1; |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * We can't just wait for polled events to come to us, we have to actively |
| 2558 | * find and complete them. |
| 2559 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2560 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2561 | { |
| 2562 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2563 | return; |
| 2564 | |
| 2565 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2566 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2567 | unsigned int nr_events = 0; |
| 2568 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2569 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2570 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2571 | /* let it sleep and repeat later if can't complete a request */ |
| 2572 | if (nr_events == 0) |
| 2573 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2574 | /* |
| 2575 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2576 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2577 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2578 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2579 | if (need_resched()) { |
| 2580 | mutex_unlock(&ctx->uring_lock); |
| 2581 | cond_resched(); |
| 2582 | mutex_lock(&ctx->uring_lock); |
| 2583 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2584 | } |
| 2585 | mutex_unlock(&ctx->uring_lock); |
| 2586 | } |
| 2587 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2588 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2589 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2590 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2591 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2592 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2593 | /* |
| 2594 | * We disallow the app entering submit/complete with polling, but we |
| 2595 | * still need to lock the ring to prevent racing with polled issue |
| 2596 | * that got punted to a workqueue. |
| 2597 | */ |
| 2598 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2599 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2600 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2601 | * Don't enter poll loop if we already have events pending. |
| 2602 | * If we do, we can potentially be spinning for commands that |
| 2603 | * already triggered a CQE (eg in error). |
| 2604 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2605 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2606 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2607 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2608 | break; |
| 2609 | |
| 2610 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2611 | * If a submit got punted to a workqueue, we can have the |
| 2612 | * application entering polling for a command before it gets |
| 2613 | * issued. That app will hold the uring_lock for the duration |
| 2614 | * of the poll right here, so we need to take a breather every |
| 2615 | * now and then to ensure that the issue has a chance to add |
| 2616 | * the poll to the issued list. Otherwise we can spin here |
| 2617 | * forever, while the workqueue is stuck trying to acquire the |
| 2618 | * very same mutex. |
| 2619 | */ |
| 2620 | if (!(++iters & 7)) { |
| 2621 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2622 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2623 | mutex_lock(&ctx->uring_lock); |
| 2624 | } |
| 2625 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2626 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2627 | if (ret <= 0) |
| 2628 | break; |
| 2629 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2630 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2631 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2632 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2633 | return ret; |
| 2634 | } |
| 2635 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2636 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2637 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2638 | /* |
| 2639 | * Tell lockdep we inherited freeze protection from submission |
| 2640 | * thread. |
| 2641 | */ |
| 2642 | if (req->flags & REQ_F_ISREG) { |
| 2643 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2644 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2645 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2646 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2647 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2650 | static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| 2651 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2652 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2653 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2654 | int cflags = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2655 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2656 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2657 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2658 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2659 | if (res != req->result) |
| 2660 | req_set_fail_links(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2661 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2662 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2663 | __io_req_complete(req, res, cflags, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2664 | } |
| 2665 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2666 | #ifdef CONFIG_BLOCK |
| 2667 | static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| 2668 | { |
| 2669 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 2670 | ssize_t ret = -ECANCELED; |
| 2671 | struct iov_iter iter; |
| 2672 | int rw; |
| 2673 | |
| 2674 | if (error) { |
| 2675 | ret = error; |
| 2676 | goto end_req; |
| 2677 | } |
| 2678 | |
| 2679 | switch (req->opcode) { |
| 2680 | case IORING_OP_READV: |
| 2681 | case IORING_OP_READ_FIXED: |
| 2682 | case IORING_OP_READ: |
| 2683 | rw = READ; |
| 2684 | break; |
| 2685 | case IORING_OP_WRITEV: |
| 2686 | case IORING_OP_WRITE_FIXED: |
| 2687 | case IORING_OP_WRITE: |
| 2688 | rw = WRITE; |
| 2689 | break; |
| 2690 | default: |
| 2691 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2692 | req->opcode); |
| 2693 | goto end_req; |
| 2694 | } |
| 2695 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2696 | if (!req->async_data) { |
Jens Axboe | 8f3d749 | 2020-09-14 09:28:14 -0600 | [diff] [blame] | 2697 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2698 | if (ret < 0) |
| 2699 | goto end_req; |
| 2700 | ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
| 2701 | if (!ret) |
| 2702 | return true; |
| 2703 | kfree(iovec); |
| 2704 | } else { |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2705 | return true; |
Jens Axboe | 8f3d749 | 2020-09-14 09:28:14 -0600 | [diff] [blame] | 2706 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2707 | end_req: |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2708 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2709 | return false; |
| 2710 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2711 | #endif |
| 2712 | |
| 2713 | static bool io_rw_reissue(struct io_kiocb *req, long res) |
| 2714 | { |
| 2715 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2716 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2717 | int ret; |
| 2718 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2719 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2720 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2721 | if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| 2722 | return false; |
| 2723 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2724 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2725 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 2726 | ret = io_sq_thread_acquire_mm_files(req->ctx, req); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 2727 | |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2728 | if (io_resubmit_prep(req, ret)) { |
| 2729 | refcount_inc(&req->refs); |
| 2730 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2731 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2732 | } |
| 2733 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2734 | #endif |
| 2735 | return false; |
| 2736 | } |
| 2737 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2738 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2739 | struct io_comp_state *cs) |
| 2740 | { |
| 2741 | if (!io_rw_reissue(req, res)) |
| 2742 | io_complete_rw_common(&req->rw.kiocb, res, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2746 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2747 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2748 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2749 | __io_complete_rw(req, res, res2, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2752 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2753 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2754 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2755 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2756 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2757 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2758 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2759 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2760 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2761 | |
| 2762 | WRITE_ONCE(req->result, res); |
| 2763 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2764 | smp_wmb(); |
| 2765 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /* |
| 2769 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2770 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2771 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2772 | * accessing the kiocb cookie. |
| 2773 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2774 | 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] | 2775 | { |
| 2776 | struct io_ring_ctx *ctx = req->ctx; |
| 2777 | |
| 2778 | /* |
| 2779 | * Track whether we have multiple files in our lists. This will impact |
| 2780 | * how we do polling eventually, not spinning if we're on potentially |
| 2781 | * different devices. |
| 2782 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2783 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2784 | ctx->poll_multi_file = false; |
| 2785 | } else if (!ctx->poll_multi_file) { |
| 2786 | struct io_kiocb *list_req; |
| 2787 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2788 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2789 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2790 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2791 | ctx->poll_multi_file = true; |
| 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | * For fast devices, IO may have already completed. If it has, add |
| 2796 | * it to the front so we find it first. |
| 2797 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2798 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2799 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2800 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2801 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2802 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2803 | /* |
| 2804 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2805 | * task context or in io worker task context. If current task context is |
| 2806 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2807 | */ |
| 2808 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2809 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2810 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2813 | static inline void __io_state_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2814 | { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2815 | fput_many(state->file, state->file_refs); |
| 2816 | state->file_refs = 0; |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2820 | { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2821 | if (state->file_refs) |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2822 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | /* |
| 2826 | * Get as many references to a file as we have IOs left in this submission, |
| 2827 | * assuming most submissions are for one file, or at least that each file |
| 2828 | * has more than one submission. |
| 2829 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2830 | 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] | 2831 | { |
| 2832 | if (!state) |
| 2833 | return fget(fd); |
| 2834 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2835 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2836 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2837 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2838 | return state->file; |
| 2839 | } |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2840 | __io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2841 | } |
| 2842 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2843 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2844 | return NULL; |
| 2845 | |
| 2846 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2847 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2848 | return state->file; |
| 2849 | } |
| 2850 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2851 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2852 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2853 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2854 | } |
| 2855 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2856 | /* |
| 2857 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2858 | * any file. For now, just ensure that anything potentially problematic is done |
| 2859 | * inline. |
| 2860 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2861 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2862 | { |
| 2863 | umode_t mode = file_inode(file)->i_mode; |
| 2864 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2865 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2866 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2867 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2868 | return true; |
| 2869 | return false; |
| 2870 | } |
| 2871 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2872 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2873 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2874 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2875 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2876 | file->f_op != &io_uring_fops) |
| 2877 | return true; |
| 2878 | return false; |
| 2879 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2880 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2881 | /* any ->read/write should understand O_NONBLOCK */ |
| 2882 | if (file->f_flags & O_NONBLOCK) |
| 2883 | return true; |
| 2884 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2885 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2886 | return false; |
| 2887 | |
| 2888 | if (rw == READ) |
| 2889 | return file->f_op->read_iter != NULL; |
| 2890 | |
| 2891 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2894 | 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] | 2895 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2896 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2897 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2898 | unsigned ioprio; |
| 2899 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2900 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2901 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 2902 | req->flags |= REQ_F_ISREG; |
| 2903 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2904 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2905 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 2906 | req->flags |= REQ_F_CUR_POS; |
| 2907 | kiocb->ki_pos = req->file->f_pos; |
| 2908 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2909 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2910 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2911 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2912 | if (unlikely(ret)) |
| 2913 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2914 | |
| 2915 | ioprio = READ_ONCE(sqe->ioprio); |
| 2916 | if (ioprio) { |
| 2917 | ret = ioprio_check_cap(ioprio); |
| 2918 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2919 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2920 | |
| 2921 | kiocb->ki_ioprio = ioprio; |
| 2922 | } else |
| 2923 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2924 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2925 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2926 | if (kiocb->ki_flags & IOCB_NOWAIT) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 2927 | req->flags |= REQ_F_NOWAIT; |
| 2928 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2929 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2930 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2931 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2932 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2933 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2934 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2935 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2936 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2937 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2938 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2939 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2940 | kiocb->ki_complete = io_complete_rw; |
| 2941 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2942 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2943 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2944 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2945 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2946 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2950 | { |
| 2951 | switch (ret) { |
| 2952 | case -EIOCBQUEUED: |
| 2953 | break; |
| 2954 | case -ERESTARTSYS: |
| 2955 | case -ERESTARTNOINTR: |
| 2956 | case -ERESTARTNOHAND: |
| 2957 | case -ERESTART_RESTARTBLOCK: |
| 2958 | /* |
| 2959 | * We can't just restart the syscall, since previously |
| 2960 | * submitted sqes may already be in progress. Just fail this |
| 2961 | * IO with EINTR. |
| 2962 | */ |
| 2963 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2964 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2965 | default: |
| 2966 | kiocb->ki_complete(kiocb, ret, 0); |
| 2967 | } |
| 2968 | } |
| 2969 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2970 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
| 2971 | struct io_comp_state *cs) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2972 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2973 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2974 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2975 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2976 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2977 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2978 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2979 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2980 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2981 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2982 | } |
| 2983 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2984 | if (req->flags & REQ_F_CUR_POS) |
| 2985 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2986 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2987 | __io_complete_rw(req, ret, 0, cs); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2988 | else |
| 2989 | io_rw_done(kiocb, ret); |
| 2990 | } |
| 2991 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2992 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2993 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2994 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2995 | struct io_ring_ctx *ctx = req->ctx; |
| 2996 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2997 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 2998 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2999 | size_t offset; |
| 3000 | u64 buf_addr; |
| 3001 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3002 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3003 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3004 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3005 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3006 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3007 | |
| 3008 | /* overflow */ |
| 3009 | if (buf_addr + len < buf_addr) |
| 3010 | return -EFAULT; |
| 3011 | /* not inside the mapped region */ |
| 3012 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 3013 | return -EFAULT; |
| 3014 | |
| 3015 | /* |
| 3016 | * May not be a start of buffer, set size appropriately |
| 3017 | * and advance us to the beginning. |
| 3018 | */ |
| 3019 | offset = buf_addr - imu->ubuf; |
| 3020 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3021 | |
| 3022 | if (offset) { |
| 3023 | /* |
| 3024 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3025 | * using the latter parts of a big fixed buffer - it iterates |
| 3026 | * over each segment manually. We can cheat a bit here, because |
| 3027 | * we know that: |
| 3028 | * |
| 3029 | * 1) it's a BVEC iter, we set it up |
| 3030 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3031 | * first and last bvec |
| 3032 | * |
| 3033 | * So just find our index, and adjust the iterator afterwards. |
| 3034 | * If the offset is within the first bvec (or the whole first |
| 3035 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3036 | * since we can just skip the first segment, which may not |
| 3037 | * be PAGE_SIZE aligned. |
| 3038 | */ |
| 3039 | const struct bio_vec *bvec = imu->bvec; |
| 3040 | |
| 3041 | if (offset <= bvec->bv_len) { |
| 3042 | iov_iter_advance(iter, offset); |
| 3043 | } else { |
| 3044 | unsigned long seg_skip; |
| 3045 | |
| 3046 | /* skip first vec */ |
| 3047 | offset -= bvec->bv_len; |
| 3048 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3049 | |
| 3050 | iter->bvec = bvec + seg_skip; |
| 3051 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3052 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3053 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 3057 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3060 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3061 | { |
| 3062 | if (needs_lock) |
| 3063 | mutex_unlock(&ctx->uring_lock); |
| 3064 | } |
| 3065 | |
| 3066 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3067 | { |
| 3068 | /* |
| 3069 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3070 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3071 | * The only exception is when we've detached the request and issue it |
| 3072 | * from an async worker thread, grab the lock for that case. |
| 3073 | */ |
| 3074 | if (needs_lock) |
| 3075 | mutex_lock(&ctx->uring_lock); |
| 3076 | } |
| 3077 | |
| 3078 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 3079 | int bgid, struct io_buffer *kbuf, |
| 3080 | bool needs_lock) |
| 3081 | { |
| 3082 | struct io_buffer *head; |
| 3083 | |
| 3084 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3085 | return kbuf; |
| 3086 | |
| 3087 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3088 | |
| 3089 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3090 | |
| 3091 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 3092 | if (head) { |
| 3093 | if (!list_empty(&head->list)) { |
| 3094 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3095 | list); |
| 3096 | list_del(&kbuf->list); |
| 3097 | } else { |
| 3098 | kbuf = head; |
| 3099 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 3100 | } |
| 3101 | if (*len > kbuf->len) |
| 3102 | *len = kbuf->len; |
| 3103 | } else { |
| 3104 | kbuf = ERR_PTR(-ENOBUFS); |
| 3105 | } |
| 3106 | |
| 3107 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 3108 | |
| 3109 | return kbuf; |
| 3110 | } |
| 3111 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3112 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 3113 | bool needs_lock) |
| 3114 | { |
| 3115 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3116 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3117 | |
| 3118 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3119 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3120 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 3121 | if (IS_ERR(kbuf)) |
| 3122 | return kbuf; |
| 3123 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 3124 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3125 | return u64_to_user_ptr(kbuf->addr); |
| 3126 | } |
| 3127 | |
| 3128 | #ifdef CONFIG_COMPAT |
| 3129 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 3130 | bool needs_lock) |
| 3131 | { |
| 3132 | struct compat_iovec __user *uiov; |
| 3133 | compat_ssize_t clen; |
| 3134 | void __user *buf; |
| 3135 | ssize_t len; |
| 3136 | |
| 3137 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3138 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3139 | return -EFAULT; |
| 3140 | if (__get_user(clen, &uiov->iov_len)) |
| 3141 | return -EFAULT; |
| 3142 | if (clen < 0) |
| 3143 | return -EINVAL; |
| 3144 | |
| 3145 | len = clen; |
| 3146 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3147 | if (IS_ERR(buf)) |
| 3148 | return PTR_ERR(buf); |
| 3149 | iov[0].iov_base = buf; |
| 3150 | iov[0].iov_len = (compat_size_t) len; |
| 3151 | return 0; |
| 3152 | } |
| 3153 | #endif |
| 3154 | |
| 3155 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3156 | bool needs_lock) |
| 3157 | { |
| 3158 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3159 | void __user *buf; |
| 3160 | ssize_t len; |
| 3161 | |
| 3162 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3163 | return -EFAULT; |
| 3164 | |
| 3165 | len = iov[0].iov_len; |
| 3166 | if (len < 0) |
| 3167 | return -EINVAL; |
| 3168 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 3169 | if (IS_ERR(buf)) |
| 3170 | return PTR_ERR(buf); |
| 3171 | iov[0].iov_base = buf; |
| 3172 | iov[0].iov_len = len; |
| 3173 | return 0; |
| 3174 | } |
| 3175 | |
| 3176 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 3177 | bool needs_lock) |
| 3178 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3179 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 3180 | struct io_buffer *kbuf; |
| 3181 | |
| 3182 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 3183 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3184 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3185 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3186 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3187 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3188 | return -EINVAL; |
| 3189 | |
| 3190 | #ifdef CONFIG_COMPAT |
| 3191 | if (req->ctx->compat) |
| 3192 | return io_compat_import(req, iov, needs_lock); |
| 3193 | #endif |
| 3194 | |
| 3195 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 3196 | } |
| 3197 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3198 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
Jens Axboe | 8452fd0 | 2020-08-18 13:58:33 -0700 | [diff] [blame] | 3199 | struct iovec **iovec, struct iov_iter *iter, |
| 3200 | bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3201 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3202 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 3203 | size_t sqe_len = req->rw.len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3204 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3205 | u8 opcode; |
| 3206 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3207 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 3208 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3209 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3210 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3211 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3212 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3213 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3214 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3215 | return -EINVAL; |
| 3216 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3217 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3218 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3219 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3220 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3221 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3222 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3225 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3226 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3227 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3230 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3231 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3232 | if (!ret) { |
| 3233 | ret = (*iovec)->iov_len; |
| 3234 | iov_iter_init(iter, rw, *iovec, 1, ret); |
| 3235 | } |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3236 | *iovec = NULL; |
| 3237 | return ret; |
| 3238 | } |
| 3239 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3240 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3241 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3244 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3245 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3246 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3247 | } |
| 3248 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3249 | /* |
| 3250 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3251 | * by looping over ->read() or ->write() manually. |
| 3252 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3253 | 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] | 3254 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3255 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3256 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3257 | ssize_t ret = 0; |
| 3258 | |
| 3259 | /* |
| 3260 | * Don't support polled IO through this interface, and we can't |
| 3261 | * support non-blocking either. For the latter, this just causes |
| 3262 | * the kiocb to be handled from an async context. |
| 3263 | */ |
| 3264 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3265 | return -EOPNOTSUPP; |
| 3266 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3267 | return -EAGAIN; |
| 3268 | |
| 3269 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3270 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3271 | ssize_t nr; |
| 3272 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3273 | if (!iov_iter_is_bvec(iter)) { |
| 3274 | iovec = iov_iter_iovec(iter); |
| 3275 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3276 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3277 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3278 | } |
| 3279 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3280 | if (rw == READ) { |
| 3281 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3282 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3283 | } else { |
| 3284 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3285 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | if (nr < 0) { |
| 3289 | if (!ret) |
| 3290 | ret = nr; |
| 3291 | break; |
| 3292 | } |
| 3293 | ret += nr; |
| 3294 | if (nr != iovec.iov_len) |
| 3295 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3296 | req->rw.len -= nr; |
| 3297 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3298 | iov_iter_advance(iter, nr); |
| 3299 | } |
| 3300 | |
| 3301 | return ret; |
| 3302 | } |
| 3303 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3304 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3305 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3306 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3307 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3308 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3309 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3310 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3311 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3312 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3313 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3314 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3315 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3316 | unsigned iov_off = 0; |
| 3317 | |
| 3318 | rw->iter.iov = rw->fast_iov; |
| 3319 | if (iter->iov != fast_iov) { |
| 3320 | iov_off = iter->iov - fast_iov; |
| 3321 | rw->iter.iov += iov_off; |
| 3322 | } |
| 3323 | if (rw->fast_iov != fast_iov) |
| 3324 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3325 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3326 | } else { |
| 3327 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3331 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3332 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3333 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3334 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3335 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3336 | } |
| 3337 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3338 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3339 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3340 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3341 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3342 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3343 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3346 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3347 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3348 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3349 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3350 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3351 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3352 | if (!req->async_data) { |
| 3353 | if (__io_alloc_async_data(req)) |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3354 | return -ENOMEM; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3355 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3356 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3357 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3358 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3359 | } |
| 3360 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3361 | 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] | 3362 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3363 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3364 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3365 | ssize_t ret; |
| 3366 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3367 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3368 | if (unlikely(ret < 0)) |
| 3369 | return ret; |
| 3370 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3371 | iorw->bytes_done = 0; |
| 3372 | iorw->free_iovec = iov; |
| 3373 | if (iov) |
| 3374 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3375 | return 0; |
| 3376 | } |
| 3377 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3378 | 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] | 3379 | { |
| 3380 | ssize_t ret; |
| 3381 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3382 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3383 | if (ret) |
| 3384 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3385 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3386 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3387 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3388 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3389 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3390 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3391 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3392 | return io_rw_prep_async(req, READ); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3395 | /* |
| 3396 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3397 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3398 | * This gets called when the page is unlocked, and we generally expect that to |
| 3399 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3400 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3401 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3402 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3403 | * slow path. |
| 3404 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3405 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3406 | int sync, void *arg) |
| 3407 | { |
| 3408 | struct wait_page_queue *wpq; |
| 3409 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3410 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3411 | int ret; |
| 3412 | |
| 3413 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3414 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3415 | if (!wake_page_match(wpq, key)) |
| 3416 | return 0; |
| 3417 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3418 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3419 | list_del_init(&wait->entry); |
| 3420 | |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3421 | init_task_work(&req->task_work, io_req_task_submit); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 3422 | percpu_ref_get(&req->ctx->refs); |
| 3423 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3424 | /* submit ref gets dropped, acquire a new one */ |
| 3425 | refcount_inc(&req->refs); |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 3426 | ret = io_req_task_work_add(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3427 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3428 | struct task_struct *tsk; |
| 3429 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3430 | /* queue just for cancelation */ |
Pavel Begunkov | e737512 | 2020-07-12 20:42:04 +0300 | [diff] [blame] | 3431 | init_task_work(&req->task_work, io_req_task_cancel); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3432 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 3433 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 3434 | wake_up_process(tsk); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3435 | } |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3436 | return 1; |
| 3437 | } |
| 3438 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3439 | /* |
| 3440 | * This controls whether a given IO request should be armed for async page |
| 3441 | * based retry. If we return false here, the request is handed to the async |
| 3442 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3443 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3444 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3445 | * will register a callback when the page is unlocked at IO completion. Through |
| 3446 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3447 | * That retry will attempt the buffered read again. The retry will generally |
| 3448 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3449 | * async worker threads for a blocking retry. |
| 3450 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3451 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3452 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3453 | struct io_async_rw *rw = req->async_data; |
| 3454 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3455 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3456 | |
| 3457 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3458 | if (req->flags & REQ_F_NOWAIT) |
| 3459 | return false; |
| 3460 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3461 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3462 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3463 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3464 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3465 | /* |
| 3466 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3467 | * support callback based unlocks |
| 3468 | */ |
| 3469 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3470 | return false; |
| 3471 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3472 | wait->wait.func = io_async_buf_func; |
| 3473 | wait->wait.private = req; |
| 3474 | wait->wait.flags = 0; |
| 3475 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3476 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3477 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3478 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3479 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3483 | { |
| 3484 | if (req->file->f_op->read_iter) |
| 3485 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3486 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3487 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3488 | else |
| 3489 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3490 | } |
| 3491 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3492 | static int io_read(struct io_kiocb *req, bool force_nonblock, |
| 3493 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3494 | { |
| 3495 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3496 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3497 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3498 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3499 | ssize_t io_size, ret, ret2; |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3500 | bool no_async; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3501 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3502 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3503 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3504 | iovec = NULL; |
| 3505 | } else { |
| 3506 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3507 | if (ret < 0) |
| 3508 | return ret; |
| 3509 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3510 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3511 | req->result = io_size; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3512 | ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3513 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3514 | /* Ensure we clear previously set non-block flag */ |
| 3515 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3516 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3517 | else |
| 3518 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3519 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3520 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3521 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3522 | no_async = force_nonblock && !io_file_supports_async(req->file, READ); |
| 3523 | if (no_async) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3524 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3525 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3526 | 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] | 3527 | if (unlikely(ret)) |
| 3528 | goto out_free; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3529 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3530 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3531 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3532 | if (!ret) { |
| 3533 | goto done; |
| 3534 | } else if (ret == -EIOCBQUEUED) { |
| 3535 | ret = 0; |
| 3536 | goto out_free; |
| 3537 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3538 | /* IOPOLL retry should happen for io-wq threads */ |
| 3539 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3540 | goto done; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3541 | /* no retry on NONBLOCK marked file */ |
| 3542 | if (req->file->f_flags & O_NONBLOCK) |
| 3543 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3544 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3545 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3546 | ret = 0; |
| 3547 | goto copy_iov; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3548 | } else if (ret < 0) { |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3549 | /* make sure -ERESTARTSYS -> -EINTR is done */ |
| 3550 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | /* read it all, or we did blocking attempt. no retry. */ |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3554 | if (!iov_iter_count(iter) || !force_nonblock || |
| 3555 | (req->file->f_flags & O_NONBLOCK)) |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3556 | goto done; |
| 3557 | |
| 3558 | io_size -= ret; |
| 3559 | copy_iov: |
| 3560 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
| 3561 | if (ret2) { |
| 3562 | ret = ret2; |
| 3563 | goto out_free; |
| 3564 | } |
Jens Axboe | f5cac8b | 2020-09-14 09:30:38 -0600 | [diff] [blame] | 3565 | if (no_async) |
| 3566 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3567 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3568 | /* it's copied and will be cleaned with ->io */ |
| 3569 | iovec = NULL; |
| 3570 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3571 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3572 | retry: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3573 | rw->bytes_done += ret; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3574 | /* if we can retry, do so with the callbacks armed */ |
| 3575 | if (!io_rw_should_retry(req)) { |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3576 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3577 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3578 | } |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3579 | |
| 3580 | /* |
| 3581 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If we |
| 3582 | * get -EIOCBQUEUED, then we'll get a notification when the desired |
| 3583 | * page gets unlocked. We can also get a partial read here, and if we |
| 3584 | * do, then just retry at the new offset. |
| 3585 | */ |
| 3586 | ret = io_iter_do_read(req, iter); |
| 3587 | if (ret == -EIOCBQUEUED) { |
| 3588 | ret = 0; |
| 3589 | goto out_free; |
| 3590 | } else if (ret > 0 && ret < io_size) { |
| 3591 | /* we got some bytes, but not all. retry. */ |
| 3592 | goto retry; |
| 3593 | } |
| 3594 | done: |
| 3595 | kiocb_done(kiocb, ret, cs); |
| 3596 | ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3597 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3598 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3599 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3600 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3601 | return ret; |
| 3602 | } |
| 3603 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3604 | 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] | 3605 | { |
| 3606 | ssize_t ret; |
| 3607 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3608 | ret = io_prep_rw(req, sqe); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3609 | if (ret) |
| 3610 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3611 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3612 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3613 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3614 | |
Pavel Begunkov | 5f798be | 2020-02-08 13:28:02 +0300 | [diff] [blame] | 3615 | /* either don't need iovec imported or already have it */ |
Pavel Begunkov | 2d19989 | 2020-09-30 22:57:35 +0300 | [diff] [blame] | 3616 | if (!req->async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3617 | return 0; |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3618 | return io_rw_prep_async(req, WRITE); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 3621 | static int io_write(struct io_kiocb *req, bool force_nonblock, |
| 3622 | struct io_comp_state *cs) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3623 | { |
| 3624 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3625 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3626 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3627 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3628 | ssize_t ret, ret2, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3629 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3630 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3631 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3632 | iovec = NULL; |
| 3633 | } else { |
| 3634 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3635 | if (ret < 0) |
| 3636 | return ret; |
| 3637 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3638 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3639 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3640 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3641 | /* Ensure we clear previously set non-block flag */ |
| 3642 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3643 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3644 | else |
| 3645 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3646 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3647 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3648 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3649 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3650 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3651 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3652 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3653 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3654 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3655 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3656 | 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] | 3657 | if (unlikely(ret)) |
| 3658 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3659 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3660 | /* |
| 3661 | * Open-code file_start_write here to grab freeze protection, |
| 3662 | * which will be released by another thread in |
| 3663 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3664 | * released so that it doesn't complain about the held lock when |
| 3665 | * we return to userspace. |
| 3666 | */ |
| 3667 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3668 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3669 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3670 | SB_FREEZE_WRITE); |
| 3671 | } |
| 3672 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3673 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3674 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3675 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3676 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3677 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3678 | else |
| 3679 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3680 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3681 | /* |
| 3682 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3683 | * retry them without IOCB_NOWAIT. |
| 3684 | */ |
| 3685 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3686 | ret2 = -EAGAIN; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3687 | /* no retry on NONBLOCK marked file */ |
| 3688 | if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK)) |
| 3689 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3690 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3691 | /* IOPOLL retry should happen for io-wq threads */ |
| 3692 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3693 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3694 | done: |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3695 | kiocb_done(kiocb, ret2, cs); |
| 3696 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3697 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3698 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3699 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3700 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3701 | if (!ret) |
| 3702 | return -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3703 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3704 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3705 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3706 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3707 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3708 | return ret; |
| 3709 | } |
| 3710 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3711 | static int io_renameat_prep(struct io_kiocb *req, |
| 3712 | const struct io_uring_sqe *sqe) |
| 3713 | { |
| 3714 | struct io_rename *ren = &req->rename; |
| 3715 | const char __user *oldf, *newf; |
| 3716 | |
| 3717 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3718 | return -EBADF; |
| 3719 | |
| 3720 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3721 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3722 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3723 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3724 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3725 | |
| 3726 | ren->oldpath = getname(oldf); |
| 3727 | if (IS_ERR(ren->oldpath)) |
| 3728 | return PTR_ERR(ren->oldpath); |
| 3729 | |
| 3730 | ren->newpath = getname(newf); |
| 3731 | if (IS_ERR(ren->newpath)) { |
| 3732 | putname(ren->oldpath); |
| 3733 | return PTR_ERR(ren->newpath); |
| 3734 | } |
| 3735 | |
| 3736 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | static int io_renameat(struct io_kiocb *req, bool force_nonblock) |
| 3741 | { |
| 3742 | struct io_rename *ren = &req->rename; |
| 3743 | int ret; |
| 3744 | |
| 3745 | if (force_nonblock) |
| 3746 | return -EAGAIN; |
| 3747 | |
| 3748 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3749 | ren->newpath, ren->flags); |
| 3750 | |
| 3751 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3752 | if (ret < 0) |
| 3753 | req_set_fail_links(req); |
| 3754 | io_req_complete(req, ret); |
| 3755 | return 0; |
| 3756 | } |
| 3757 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3758 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3759 | const struct io_uring_sqe *sqe) |
| 3760 | { |
| 3761 | struct io_unlink *un = &req->unlink; |
| 3762 | const char __user *fname; |
| 3763 | |
| 3764 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3765 | return -EBADF; |
| 3766 | |
| 3767 | un->dfd = READ_ONCE(sqe->fd); |
| 3768 | |
| 3769 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3770 | if (un->flags & ~AT_REMOVEDIR) |
| 3771 | return -EINVAL; |
| 3772 | |
| 3773 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3774 | un->filename = getname(fname); |
| 3775 | if (IS_ERR(un->filename)) |
| 3776 | return PTR_ERR(un->filename); |
| 3777 | |
| 3778 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
| 3782 | static int io_unlinkat(struct io_kiocb *req, bool force_nonblock) |
| 3783 | { |
| 3784 | struct io_unlink *un = &req->unlink; |
| 3785 | int ret; |
| 3786 | |
| 3787 | if (force_nonblock) |
| 3788 | return -EAGAIN; |
| 3789 | |
| 3790 | if (un->flags & AT_REMOVEDIR) |
| 3791 | ret = do_rmdir(un->dfd, un->filename); |
| 3792 | else |
| 3793 | ret = do_unlinkat(un->dfd, un->filename); |
| 3794 | |
| 3795 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3796 | if (ret < 0) |
| 3797 | req_set_fail_links(req); |
| 3798 | io_req_complete(req, ret); |
| 3799 | return 0; |
| 3800 | } |
| 3801 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3802 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3803 | const struct io_uring_sqe *sqe) |
| 3804 | { |
| 3805 | #if defined(CONFIG_NET) |
| 3806 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3807 | return -EINVAL; |
| 3808 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3809 | sqe->buf_index) |
| 3810 | return -EINVAL; |
| 3811 | |
| 3812 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3813 | return 0; |
| 3814 | #else |
| 3815 | return -EOPNOTSUPP; |
| 3816 | #endif |
| 3817 | } |
| 3818 | |
| 3819 | static int io_shutdown(struct io_kiocb *req, bool force_nonblock) |
| 3820 | { |
| 3821 | #if defined(CONFIG_NET) |
| 3822 | struct socket *sock; |
| 3823 | int ret; |
| 3824 | |
| 3825 | if (force_nonblock) |
| 3826 | return -EAGAIN; |
| 3827 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3828 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3829 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3830 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3831 | |
| 3832 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3833 | if (ret < 0) |
| 3834 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3835 | io_req_complete(req, ret); |
| 3836 | return 0; |
| 3837 | #else |
| 3838 | return -EOPNOTSUPP; |
| 3839 | #endif |
| 3840 | } |
| 3841 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3842 | static int __io_splice_prep(struct io_kiocb *req, |
| 3843 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3844 | { |
| 3845 | struct io_splice* sp = &req->splice; |
| 3846 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3847 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3848 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3849 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3850 | |
| 3851 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3852 | sp->len = READ_ONCE(sqe->len); |
| 3853 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3854 | |
| 3855 | if (unlikely(sp->flags & ~valid_flags)) |
| 3856 | return -EINVAL; |
| 3857 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3858 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3859 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3860 | if (!sp->file_in) |
| 3861 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3862 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3863 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3864 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3865 | /* |
| 3866 | * Splice operation will be punted aync, and here need to |
| 3867 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3868 | */ |
| 3869 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3870 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3871 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3872 | |
| 3873 | return 0; |
| 3874 | } |
| 3875 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3876 | static int io_tee_prep(struct io_kiocb *req, |
| 3877 | const struct io_uring_sqe *sqe) |
| 3878 | { |
| 3879 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3880 | return -EINVAL; |
| 3881 | return __io_splice_prep(req, sqe); |
| 3882 | } |
| 3883 | |
| 3884 | static int io_tee(struct io_kiocb *req, bool force_nonblock) |
| 3885 | { |
| 3886 | struct io_splice *sp = &req->splice; |
| 3887 | struct file *in = sp->file_in; |
| 3888 | struct file *out = sp->file_out; |
| 3889 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3890 | long ret = 0; |
| 3891 | |
| 3892 | if (force_nonblock) |
| 3893 | return -EAGAIN; |
| 3894 | if (sp->len) |
| 3895 | ret = do_tee(in, out, sp->len, flags); |
| 3896 | |
| 3897 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3898 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3899 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3900 | if (ret != sp->len) |
| 3901 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3902 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3903 | return 0; |
| 3904 | } |
| 3905 | |
| 3906 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3907 | { |
| 3908 | struct io_splice* sp = &req->splice; |
| 3909 | |
| 3910 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3911 | sp->off_out = READ_ONCE(sqe->off); |
| 3912 | return __io_splice_prep(req, sqe); |
| 3913 | } |
| 3914 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 3915 | static int io_splice(struct io_kiocb *req, bool force_nonblock) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3916 | { |
| 3917 | struct io_splice *sp = &req->splice; |
| 3918 | struct file *in = sp->file_in; |
| 3919 | struct file *out = sp->file_out; |
| 3920 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3921 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3922 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3923 | |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3924 | if (force_nonblock) |
| 3925 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3926 | |
| 3927 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3928 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3929 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3930 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3931 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3932 | |
| 3933 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3934 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3935 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3936 | if (ret != sp->len) |
| 3937 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3938 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3939 | return 0; |
| 3940 | } |
| 3941 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3942 | /* |
| 3943 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3944 | */ |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3945 | 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] | 3946 | { |
| 3947 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3948 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3949 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3950 | return -EINVAL; |
| 3951 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 3952 | __io_req_complete(req, 0, 0, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3953 | return 0; |
| 3954 | } |
| 3955 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3956 | 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] | 3957 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3958 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3959 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3960 | if (!req->file) |
| 3961 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3962 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3963 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3964 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3965 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3966 | return -EINVAL; |
| 3967 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3968 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3969 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3970 | return -EINVAL; |
| 3971 | |
| 3972 | req->sync.off = READ_ONCE(sqe->off); |
| 3973 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3974 | return 0; |
| 3975 | } |
| 3976 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3977 | static int io_fsync(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3978 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3979 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3980 | int ret; |
| 3981 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3982 | /* fsync always requires a blocking context */ |
| 3983 | if (force_nonblock) |
| 3984 | return -EAGAIN; |
| 3985 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3986 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3987 | end > 0 ? end : LLONG_MAX, |
| 3988 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3989 | if (ret < 0) |
| 3990 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3991 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3992 | return 0; |
| 3993 | } |
| 3994 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3995 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3996 | const struct io_uring_sqe *sqe) |
| 3997 | { |
| 3998 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3999 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4000 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4001 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4002 | |
| 4003 | req->sync.off = READ_ONCE(sqe->off); |
| 4004 | req->sync.len = READ_ONCE(sqe->addr); |
| 4005 | req->sync.mode = READ_ONCE(sqe->len); |
| 4006 | return 0; |
| 4007 | } |
| 4008 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4009 | static int io_fallocate(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4010 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4011 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4012 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4013 | /* fallocate always requiring blocking context */ |
| 4014 | if (force_nonblock) |
| 4015 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4016 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4017 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4018 | if (ret < 0) |
| 4019 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4020 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4021 | return 0; |
| 4022 | } |
| 4023 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4024 | 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] | 4025 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4026 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4027 | int ret; |
| 4028 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4029 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4030 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4031 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4032 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4033 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4034 | /* open.how should be already initialised */ |
| 4035 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4036 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4037 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4038 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4039 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4040 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4041 | if (IS_ERR(req->open.filename)) { |
| 4042 | ret = PTR_ERR(req->open.filename); |
| 4043 | req->open.filename = NULL; |
| 4044 | return ret; |
| 4045 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4046 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 4047 | req->open.ignore_nonblock = false; |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4048 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4049 | return 0; |
| 4050 | } |
| 4051 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4052 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4053 | { |
| 4054 | u64 flags, mode; |
| 4055 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4056 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4057 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4058 | mode = READ_ONCE(sqe->len); |
| 4059 | flags = READ_ONCE(sqe->open_flags); |
| 4060 | req->open.how = build_open_how(flags, mode); |
| 4061 | return __io_openat_prep(req, sqe); |
| 4062 | } |
| 4063 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4064 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4065 | { |
| 4066 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4067 | size_t len; |
| 4068 | int ret; |
| 4069 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4070 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 4071 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4072 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4073 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4074 | if (len < OPEN_HOW_SIZE_VER0) |
| 4075 | return -EINVAL; |
| 4076 | |
| 4077 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4078 | len); |
| 4079 | if (ret) |
| 4080 | return ret; |
| 4081 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4082 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4085 | static int io_openat2(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4086 | { |
| 4087 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4088 | struct file *file; |
| 4089 | int ret; |
| 4090 | |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 4091 | if (force_nonblock && !req->open.ignore_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4092 | return -EAGAIN; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4093 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4094 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4095 | if (ret) |
| 4096 | goto err; |
| 4097 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4098 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4099 | if (ret < 0) |
| 4100 | goto err; |
| 4101 | |
| 4102 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 4103 | if (IS_ERR(file)) { |
| 4104 | put_unused_fd(ret); |
| 4105 | ret = PTR_ERR(file); |
Jens Axboe | 944d144 | 2020-11-13 16:48:44 -0700 | [diff] [blame] | 4106 | /* |
| 4107 | * A work-around to ensure that /proc/self works that way |
| 4108 | * that it should - if we get -EOPNOTSUPP back, then assume |
| 4109 | * that proc_self_get_link() failed us because we're in async |
| 4110 | * context. We should be safe to retry this from the task |
| 4111 | * itself with force_nonblock == false set, as it should not |
| 4112 | * block on lookup. Would be nice to know this upfront and |
| 4113 | * avoid the async dance, but doesn't seem feasible. |
| 4114 | */ |
| 4115 | if (ret == -EOPNOTSUPP && io_wq_current_is_worker()) { |
| 4116 | req->open.ignore_nonblock = true; |
| 4117 | refcount_inc(&req->refs); |
| 4118 | io_req_task_queue(req); |
| 4119 | return 0; |
| 4120 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4121 | } else { |
| 4122 | fsnotify_open(file); |
| 4123 | fd_install(ret, file); |
| 4124 | } |
| 4125 | err: |
| 4126 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4127 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4128 | if (ret < 0) |
| 4129 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4130 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4131 | return 0; |
| 4132 | } |
| 4133 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4134 | static int io_openat(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4135 | { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4136 | return io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4137 | } |
| 4138 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4139 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4140 | const struct io_uring_sqe *sqe) |
| 4141 | { |
| 4142 | struct io_provide_buf *p = &req->pbuf; |
| 4143 | u64 tmp; |
| 4144 | |
| 4145 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 4146 | return -EINVAL; |
| 4147 | |
| 4148 | tmp = READ_ONCE(sqe->fd); |
| 4149 | if (!tmp || tmp > USHRT_MAX) |
| 4150 | return -EINVAL; |
| 4151 | |
| 4152 | memset(p, 0, sizeof(*p)); |
| 4153 | p->nbufs = tmp; |
| 4154 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4155 | return 0; |
| 4156 | } |
| 4157 | |
| 4158 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4159 | int bgid, unsigned nbufs) |
| 4160 | { |
| 4161 | unsigned i = 0; |
| 4162 | |
| 4163 | /* shouldn't happen */ |
| 4164 | if (!nbufs) |
| 4165 | return 0; |
| 4166 | |
| 4167 | /* the head kbuf is the list itself */ |
| 4168 | while (!list_empty(&buf->list)) { |
| 4169 | struct io_buffer *nxt; |
| 4170 | |
| 4171 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4172 | list_del(&nxt->list); |
| 4173 | kfree(nxt); |
| 4174 | if (++i == nbufs) |
| 4175 | return i; |
| 4176 | } |
| 4177 | i++; |
| 4178 | kfree(buf); |
| 4179 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 4180 | |
| 4181 | return i; |
| 4182 | } |
| 4183 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4184 | static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, |
| 4185 | struct io_comp_state *cs) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4186 | { |
| 4187 | struct io_provide_buf *p = &req->pbuf; |
| 4188 | struct io_ring_ctx *ctx = req->ctx; |
| 4189 | struct io_buffer *head; |
| 4190 | int ret = 0; |
| 4191 | |
| 4192 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4193 | |
| 4194 | lockdep_assert_held(&ctx->uring_lock); |
| 4195 | |
| 4196 | ret = -ENOENT; |
| 4197 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4198 | if (head) |
| 4199 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4200 | if (ret < 0) |
| 4201 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4202 | |
| 4203 | /* need to hold the lock to complete IOPOLL requests */ |
| 4204 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 4205 | __io_req_complete(req, ret, 0, cs); |
| 4206 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4207 | } else { |
| 4208 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4209 | __io_req_complete(req, ret, 0, cs); |
| 4210 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4211 | return 0; |
| 4212 | } |
| 4213 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4214 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4215 | const struct io_uring_sqe *sqe) |
| 4216 | { |
| 4217 | struct io_provide_buf *p = &req->pbuf; |
| 4218 | u64 tmp; |
| 4219 | |
| 4220 | if (sqe->ioprio || sqe->rw_flags) |
| 4221 | return -EINVAL; |
| 4222 | |
| 4223 | tmp = READ_ONCE(sqe->fd); |
| 4224 | if (!tmp || tmp > USHRT_MAX) |
| 4225 | return -E2BIG; |
| 4226 | p->nbufs = tmp; |
| 4227 | p->addr = READ_ONCE(sqe->addr); |
| 4228 | p->len = READ_ONCE(sqe->len); |
| 4229 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 4230 | 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] | 4231 | return -EFAULT; |
| 4232 | |
| 4233 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4234 | tmp = READ_ONCE(sqe->off); |
| 4235 | if (tmp > USHRT_MAX) |
| 4236 | return -E2BIG; |
| 4237 | p->bid = tmp; |
| 4238 | return 0; |
| 4239 | } |
| 4240 | |
| 4241 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4242 | { |
| 4243 | struct io_buffer *buf; |
| 4244 | u64 addr = pbuf->addr; |
| 4245 | int i, bid = pbuf->bid; |
| 4246 | |
| 4247 | for (i = 0; i < pbuf->nbufs; i++) { |
| 4248 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 4249 | if (!buf) |
| 4250 | break; |
| 4251 | |
| 4252 | buf->addr = addr; |
| 4253 | buf->len = pbuf->len; |
| 4254 | buf->bid = bid; |
| 4255 | addr += pbuf->len; |
| 4256 | bid++; |
| 4257 | if (!*head) { |
| 4258 | INIT_LIST_HEAD(&buf->list); |
| 4259 | *head = buf; |
| 4260 | } else { |
| 4261 | list_add_tail(&buf->list, &(*head)->list); |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | return i ? i : -ENOMEM; |
| 4266 | } |
| 4267 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4268 | static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, |
| 4269 | struct io_comp_state *cs) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4270 | { |
| 4271 | struct io_provide_buf *p = &req->pbuf; |
| 4272 | struct io_ring_ctx *ctx = req->ctx; |
| 4273 | struct io_buffer *head, *list; |
| 4274 | int ret = 0; |
| 4275 | |
| 4276 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4277 | |
| 4278 | lockdep_assert_held(&ctx->uring_lock); |
| 4279 | |
| 4280 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4281 | |
| 4282 | ret = io_add_buffers(p, &head); |
| 4283 | if (ret < 0) |
| 4284 | goto out; |
| 4285 | |
| 4286 | if (!list) { |
| 4287 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4288 | GFP_KERNEL); |
| 4289 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4290 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4291 | goto out; |
| 4292 | } |
| 4293 | } |
| 4294 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4295 | if (ret < 0) |
| 4296 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4297 | |
| 4298 | /* need to hold the lock to complete IOPOLL requests */ |
| 4299 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 4300 | __io_req_complete(req, ret, 0, cs); |
| 4301 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4302 | } else { |
| 4303 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4304 | __io_req_complete(req, ret, 0, cs); |
| 4305 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4306 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4309 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4310 | const struct io_uring_sqe *sqe) |
| 4311 | { |
| 4312 | #if defined(CONFIG_EPOLL) |
| 4313 | if (sqe->ioprio || sqe->buf_index) |
| 4314 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4315 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4316 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4317 | |
| 4318 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4319 | req->epoll.op = READ_ONCE(sqe->len); |
| 4320 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4321 | |
| 4322 | if (ep_op_has_event(req->epoll.op)) { |
| 4323 | struct epoll_event __user *ev; |
| 4324 | |
| 4325 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4326 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4327 | return -EFAULT; |
| 4328 | } |
| 4329 | |
| 4330 | return 0; |
| 4331 | #else |
| 4332 | return -EOPNOTSUPP; |
| 4333 | #endif |
| 4334 | } |
| 4335 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4336 | static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock, |
| 4337 | struct io_comp_state *cs) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4338 | { |
| 4339 | #if defined(CONFIG_EPOLL) |
| 4340 | struct io_epoll *ie = &req->epoll; |
| 4341 | int ret; |
| 4342 | |
| 4343 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4344 | if (force_nonblock && ret == -EAGAIN) |
| 4345 | return -EAGAIN; |
| 4346 | |
| 4347 | if (ret < 0) |
| 4348 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4349 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4350 | return 0; |
| 4351 | #else |
| 4352 | return -EOPNOTSUPP; |
| 4353 | #endif |
| 4354 | } |
| 4355 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4356 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4357 | { |
| 4358 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4359 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4360 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4361 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4362 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4363 | |
| 4364 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4365 | req->madvise.len = READ_ONCE(sqe->len); |
| 4366 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4367 | return 0; |
| 4368 | #else |
| 4369 | return -EOPNOTSUPP; |
| 4370 | #endif |
| 4371 | } |
| 4372 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4373 | static int io_madvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4374 | { |
| 4375 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4376 | struct io_madvise *ma = &req->madvise; |
| 4377 | int ret; |
| 4378 | |
| 4379 | if (force_nonblock) |
| 4380 | return -EAGAIN; |
| 4381 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4382 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4383 | if (ret < 0) |
| 4384 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4385 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4386 | return 0; |
| 4387 | #else |
| 4388 | return -EOPNOTSUPP; |
| 4389 | #endif |
| 4390 | } |
| 4391 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4392 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4393 | { |
| 4394 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4395 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4396 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4397 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4398 | |
| 4399 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4400 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4401 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4402 | return 0; |
| 4403 | } |
| 4404 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4405 | static int io_fadvise(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4406 | { |
| 4407 | struct io_fadvise *fa = &req->fadvise; |
| 4408 | int ret; |
| 4409 | |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4410 | if (force_nonblock) { |
| 4411 | switch (fa->advice) { |
| 4412 | case POSIX_FADV_NORMAL: |
| 4413 | case POSIX_FADV_RANDOM: |
| 4414 | case POSIX_FADV_SEQUENTIAL: |
| 4415 | break; |
| 4416 | default: |
| 4417 | return -EAGAIN; |
| 4418 | } |
| 4419 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4420 | |
| 4421 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4422 | if (ret < 0) |
| 4423 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4424 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4425 | return 0; |
| 4426 | } |
| 4427 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4428 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4429 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4430 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4431 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4432 | if (sqe->ioprio || sqe->buf_index) |
| 4433 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4434 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4435 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4436 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4437 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4438 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4439 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4440 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4441 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4442 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4443 | return 0; |
| 4444 | } |
| 4445 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 4446 | static int io_statx(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4447 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4448 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4449 | int ret; |
| 4450 | |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4451 | if (force_nonblock) { |
| 4452 | /* only need file table for an actual valid fd */ |
| 4453 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4454 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4455 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4456 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4457 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4458 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4459 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4460 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4461 | if (ret < 0) |
| 4462 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4463 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4464 | return 0; |
| 4465 | } |
| 4466 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4467 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4468 | { |
| 4469 | /* |
| 4470 | * 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] | 4471 | * leave the 'file' in an undeterminate state, and here need to modify |
| 4472 | * io_wq_work.flags, so initialize io_wq_work firstly. |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4473 | */ |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 4474 | io_req_init_async(req); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4475 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 4476 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4477 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4478 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4479 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4480 | sqe->rw_flags || sqe->buf_index) |
| 4481 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4482 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4483 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4484 | |
| 4485 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 4486 | if ((req->file && req->file->f_op == &io_uring_fops)) |
Jens Axboe | fd2206e | 2020-06-02 16:40:47 -0600 | [diff] [blame] | 4487 | return -EBADF; |
| 4488 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4489 | req->close.put_file = NULL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4490 | return 0; |
| 4491 | } |
| 4492 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4493 | static int io_close(struct io_kiocb *req, bool force_nonblock, |
| 4494 | struct io_comp_state *cs) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4495 | { |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4496 | struct io_close *close = &req->close; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4497 | int ret; |
| 4498 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4499 | /* might be already done during nonblock submission */ |
| 4500 | if (!close->put_file) { |
Eric W. Biederman | 9fe83c4 | 2020-11-20 17:14:40 -0600 | [diff] [blame] | 4501 | ret = close_fd_get_file(close->fd, &close->put_file); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4502 | if (ret < 0) |
| 4503 | return (ret == -ENOENT) ? -EBADF : ret; |
| 4504 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4505 | |
| 4506 | /* 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] | 4507 | if (close->put_file->f_op->flush && force_nonblock) { |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4508 | /* was never set, but play safe */ |
| 4509 | req->flags &= ~REQ_F_NOWAIT; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4510 | /* avoid grabbing files - we don't need the files */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 4511 | req->flags |= REQ_F_NO_FILE_TABLE; |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4512 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4513 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4514 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4515 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 4516 | ret = filp_close(close->put_file, req->work.identity->files); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4517 | if (ret < 0) |
| 4518 | req_set_fail_links(req); |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4519 | fput(close->put_file); |
| 4520 | close->put_file = NULL; |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4521 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4522 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4523 | } |
| 4524 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4525 | 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] | 4526 | { |
| 4527 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4528 | |
| 4529 | if (!req->file) |
| 4530 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4531 | |
| 4532 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4533 | return -EINVAL; |
| 4534 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4535 | return -EINVAL; |
| 4536 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4537 | req->sync.off = READ_ONCE(sqe->off); |
| 4538 | req->sync.len = READ_ONCE(sqe->len); |
| 4539 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4540 | return 0; |
| 4541 | } |
| 4542 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4543 | 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] | 4544 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4545 | int ret; |
| 4546 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4547 | /* sync_file_range always requires a blocking context */ |
| 4548 | if (force_nonblock) |
| 4549 | return -EAGAIN; |
| 4550 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4551 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4552 | req->sync.flags); |
| 4553 | if (ret < 0) |
| 4554 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4555 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4556 | return 0; |
| 4557 | } |
| 4558 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4559 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4560 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4561 | struct io_async_msghdr *kmsg) |
| 4562 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4563 | struct io_async_msghdr *async_msg = req->async_data; |
| 4564 | |
| 4565 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4566 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4567 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4568 | if (kmsg->iov != kmsg->fast_iov) |
| 4569 | kfree(kmsg->iov); |
| 4570 | return -ENOMEM; |
| 4571 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4572 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4573 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4574 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4575 | return -EAGAIN; |
| 4576 | } |
| 4577 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4578 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4579 | struct io_async_msghdr *iomsg) |
| 4580 | { |
| 4581 | iomsg->iov = iomsg->fast_iov; |
| 4582 | iomsg->msg.msg_name = &iomsg->addr; |
| 4583 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
| 4584 | req->sr_msg.msg_flags, &iomsg->iov); |
| 4585 | } |
| 4586 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4587 | 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] | 4588 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4589 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4590 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4591 | int ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4592 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4593 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4594 | return -EINVAL; |
| 4595 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4596 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4597 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4598 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4599 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4600 | #ifdef CONFIG_COMPAT |
| 4601 | if (req->ctx->compat) |
| 4602 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4603 | #endif |
| 4604 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4605 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4606 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4607 | ret = io_sendmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4608 | if (!ret) |
| 4609 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4610 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4611 | } |
| 4612 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4613 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 4614 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4615 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4616 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4617 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4618 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4619 | int ret; |
| 4620 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4621 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4622 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4623 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4624 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4625 | if (req->async_data) { |
| 4626 | kmsg = req->async_data; |
| 4627 | kmsg->msg.msg_name = &kmsg->addr; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4628 | /* if iov is set, it's allocated already */ |
| 4629 | if (!kmsg->iov) |
| 4630 | kmsg->iov = kmsg->fast_iov; |
| 4631 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4632 | } else { |
| 4633 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4634 | if (ret) |
| 4635 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4636 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4637 | } |
| 4638 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4639 | flags = req->sr_msg.msg_flags; |
| 4640 | if (flags & MSG_DONTWAIT) |
| 4641 | req->flags |= REQ_F_NOWAIT; |
| 4642 | else if (force_nonblock) |
| 4643 | flags |= MSG_DONTWAIT; |
| 4644 | |
| 4645 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
| 4646 | if (force_nonblock && ret == -EAGAIN) |
| 4647 | return io_setup_async_msg(req, kmsg); |
| 4648 | if (ret == -ERESTARTSYS) |
| 4649 | ret = -EINTR; |
| 4650 | |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4651 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4652 | kfree(kmsg->iov); |
| 4653 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4654 | if (ret < 0) |
| 4655 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4656 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4657 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4660 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 4661 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4662 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4663 | struct io_sr_msg *sr = &req->sr_msg; |
| 4664 | struct msghdr msg; |
| 4665 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4666 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4667 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4668 | int ret; |
| 4669 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4670 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4671 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4672 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4673 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4674 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4675 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4676 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4677 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4678 | msg.msg_name = NULL; |
| 4679 | msg.msg_control = NULL; |
| 4680 | msg.msg_controllen = 0; |
| 4681 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4682 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4683 | flags = req->sr_msg.msg_flags; |
| 4684 | if (flags & MSG_DONTWAIT) |
| 4685 | req->flags |= REQ_F_NOWAIT; |
| 4686 | else if (force_nonblock) |
| 4687 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4688 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4689 | msg.msg_flags = flags; |
| 4690 | ret = sock_sendmsg(sock, &msg); |
| 4691 | if (force_nonblock && ret == -EAGAIN) |
| 4692 | return -EAGAIN; |
| 4693 | if (ret == -ERESTARTSYS) |
| 4694 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4695 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4696 | if (ret < 0) |
| 4697 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4698 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4699 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4702 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4703 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4704 | { |
| 4705 | struct io_sr_msg *sr = &req->sr_msg; |
| 4706 | struct iovec __user *uiov; |
| 4707 | size_t iov_len; |
| 4708 | int ret; |
| 4709 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4710 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4711 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4712 | if (ret) |
| 4713 | return ret; |
| 4714 | |
| 4715 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4716 | if (iov_len > 1) |
| 4717 | return -EINVAL; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4718 | if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4719 | return -EFAULT; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4720 | sr->len = iomsg->iov[0].iov_len; |
| 4721 | iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4722 | sr->len); |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4723 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4724 | } else { |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4725 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
| 4726 | &iomsg->iov, &iomsg->msg.msg_iter, |
| 4727 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4728 | if (ret > 0) |
| 4729 | ret = 0; |
| 4730 | } |
| 4731 | |
| 4732 | return ret; |
| 4733 | } |
| 4734 | |
| 4735 | #ifdef CONFIG_COMPAT |
| 4736 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4737 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4738 | { |
| 4739 | struct compat_msghdr __user *msg_compat; |
| 4740 | struct io_sr_msg *sr = &req->sr_msg; |
| 4741 | struct compat_iovec __user *uiov; |
| 4742 | compat_uptr_t ptr; |
| 4743 | compat_size_t len; |
| 4744 | int ret; |
| 4745 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4746 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4747 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4748 | &ptr, &len); |
| 4749 | if (ret) |
| 4750 | return ret; |
| 4751 | |
| 4752 | uiov = compat_ptr(ptr); |
| 4753 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4754 | compat_ssize_t clen; |
| 4755 | |
| 4756 | if (len > 1) |
| 4757 | return -EINVAL; |
| 4758 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4759 | return -EFAULT; |
| 4760 | if (__get_user(clen, &uiov->iov_len)) |
| 4761 | return -EFAULT; |
| 4762 | if (clen < 0) |
| 4763 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4764 | sr->len = clen; |
| 4765 | iomsg->iov[0].iov_len = clen; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4766 | iomsg->iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4767 | } else { |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4768 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
| 4769 | UIO_FASTIOV, &iomsg->iov, |
| 4770 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4771 | if (ret < 0) |
| 4772 | return ret; |
| 4773 | } |
| 4774 | |
| 4775 | return 0; |
| 4776 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4777 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4778 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4779 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4780 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4781 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4782 | iomsg->msg.msg_name = &iomsg->addr; |
| 4783 | iomsg->iov = iomsg->fast_iov; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4784 | |
| 4785 | #ifdef CONFIG_COMPAT |
| 4786 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4787 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4788 | #endif |
| 4789 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4790 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4791 | } |
| 4792 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4793 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4794 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4795 | { |
| 4796 | struct io_sr_msg *sr = &req->sr_msg; |
| 4797 | struct io_buffer *kbuf; |
| 4798 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4799 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4800 | if (IS_ERR(kbuf)) |
| 4801 | return kbuf; |
| 4802 | |
| 4803 | sr->kbuf = kbuf; |
| 4804 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4805 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4806 | } |
| 4807 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4808 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4809 | { |
| 4810 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4811 | } |
| 4812 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4813 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 4814 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4815 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4816 | struct io_async_msghdr *async_msg = req->async_data; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4817 | struct io_sr_msg *sr = &req->sr_msg; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4818 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4819 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4820 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4821 | return -EINVAL; |
| 4822 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4823 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4824 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4825 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4826 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4827 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4828 | #ifdef CONFIG_COMPAT |
| 4829 | if (req->ctx->compat) |
| 4830 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4831 | #endif |
| 4832 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4833 | if (!async_msg || !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4834 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4835 | ret = io_recvmsg_copy_hdr(req, async_msg); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4836 | if (!ret) |
| 4837 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4838 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4839 | } |
| 4840 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4841 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 4842 | struct io_comp_state *cs) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4843 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4844 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4845 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4846 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4847 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4848 | int ret, cflags = 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4849 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4850 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4851 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4852 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4853 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4854 | if (req->async_data) { |
| 4855 | kmsg = req->async_data; |
| 4856 | kmsg->msg.msg_name = &kmsg->addr; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4857 | /* if iov is set, it's allocated already */ |
| 4858 | if (!kmsg->iov) |
| 4859 | kmsg->iov = kmsg->fast_iov; |
| 4860 | kmsg->msg.msg_iter.iov = kmsg->iov; |
| 4861 | } else { |
| 4862 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4863 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4864 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4865 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4866 | } |
| 4867 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4868 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4869 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4870 | if (IS_ERR(kbuf)) |
| 4871 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4872 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 4873 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov, |
| 4874 | 1, req->sr_msg.len); |
| 4875 | } |
| 4876 | |
| 4877 | flags = req->sr_msg.msg_flags; |
| 4878 | if (flags & MSG_DONTWAIT) |
| 4879 | req->flags |= REQ_F_NOWAIT; |
| 4880 | else if (force_nonblock) |
| 4881 | flags |= MSG_DONTWAIT; |
| 4882 | |
| 4883 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4884 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4885 | if (force_nonblock && ret == -EAGAIN) |
| 4886 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4887 | if (ret == -ERESTARTSYS) |
| 4888 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4889 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4890 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4891 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4892 | if (kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 4893 | kfree(kmsg->iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4894 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4895 | if (ret < 0) |
| 4896 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4897 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4898 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4899 | } |
| 4900 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4901 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 4902 | struct io_comp_state *cs) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4903 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4904 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4905 | struct io_sr_msg *sr = &req->sr_msg; |
| 4906 | struct msghdr msg; |
| 4907 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4908 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4909 | struct iovec iov; |
| 4910 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4911 | int ret, cflags = 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4912 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4913 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4914 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4915 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4916 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4917 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4918 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4919 | if (IS_ERR(kbuf)) |
| 4920 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4921 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4922 | } |
| 4923 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4924 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4925 | if (unlikely(ret)) |
| 4926 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4927 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4928 | msg.msg_name = NULL; |
| 4929 | msg.msg_control = NULL; |
| 4930 | msg.msg_controllen = 0; |
| 4931 | msg.msg_namelen = 0; |
| 4932 | msg.msg_iocb = NULL; |
| 4933 | msg.msg_flags = 0; |
| 4934 | |
| 4935 | flags = req->sr_msg.msg_flags; |
| 4936 | if (flags & MSG_DONTWAIT) |
| 4937 | req->flags |= REQ_F_NOWAIT; |
| 4938 | else if (force_nonblock) |
| 4939 | flags |= MSG_DONTWAIT; |
| 4940 | |
| 4941 | ret = sock_recvmsg(sock, &msg, flags); |
| 4942 | if (force_nonblock && ret == -EAGAIN) |
| 4943 | return -EAGAIN; |
| 4944 | if (ret == -ERESTARTSYS) |
| 4945 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4946 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4947 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4948 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4949 | if (ret < 0) |
| 4950 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4951 | __io_req_complete(req, ret, cflags, cs); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4952 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4953 | } |
| 4954 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4955 | 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] | 4956 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4957 | struct io_accept *accept = &req->accept; |
| 4958 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4959 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4960 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4961 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4962 | return -EINVAL; |
| 4963 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4964 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4965 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4966 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4967 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4968 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4969 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4970 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4971 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 4972 | struct io_comp_state *cs) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4973 | { |
| 4974 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4975 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4976 | int ret; |
| 4977 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4978 | if (req->file->f_flags & O_NONBLOCK) |
| 4979 | req->flags |= REQ_F_NOWAIT; |
| 4980 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4981 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4982 | accept->addr_len, accept->flags, |
| 4983 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4984 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4985 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4986 | if (ret < 0) { |
| 4987 | if (ret == -ERESTARTSYS) |
| 4988 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4989 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4990 | } |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 4991 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4992 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4993 | } |
| 4994 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4995 | 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] | 4996 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4997 | struct io_connect *conn = &req->connect; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4998 | struct io_async_connect *io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4999 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5000 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5001 | return -EINVAL; |
| 5002 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 5003 | return -EINVAL; |
| 5004 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5005 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5006 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 5007 | |
| 5008 | if (!io) |
| 5009 | return 0; |
| 5010 | |
| 5011 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5012 | &io->address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5015 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 5016 | struct io_comp_state *cs) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5017 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5018 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5019 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5020 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5021 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5022 | if (req->async_data) { |
| 5023 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5024 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5025 | ret = move_addr_to_kernel(req->connect.addr, |
| 5026 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5027 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5028 | if (ret) |
| 5029 | goto out; |
| 5030 | io = &__io; |
| 5031 | } |
| 5032 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5033 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5034 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5035 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5036 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5037 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5038 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5039 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5040 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5041 | ret = -ENOMEM; |
| 5042 | goto out; |
| 5043 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5044 | io = req->async_data; |
| 5045 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5046 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5047 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5048 | if (ret == -ERESTARTSYS) |
| 5049 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5050 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5051 | if (ret < 0) |
| 5052 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5053 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5054 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5055 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5056 | #else /* !CONFIG_NET */ |
| 5057 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5058 | { |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5059 | return -EOPNOTSUPP; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5062 | static int io_sendmsg(struct io_kiocb *req, bool force_nonblock, |
| 5063 | struct io_comp_state *cs) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5064 | { |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5065 | return -EOPNOTSUPP; |
| 5066 | } |
| 5067 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5068 | static int io_send(struct io_kiocb *req, bool force_nonblock, |
| 5069 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5070 | { |
| 5071 | return -EOPNOTSUPP; |
| 5072 | } |
| 5073 | |
| 5074 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 5075 | const struct io_uring_sqe *sqe) |
| 5076 | { |
| 5077 | return -EOPNOTSUPP; |
| 5078 | } |
| 5079 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5080 | static int io_recvmsg(struct io_kiocb *req, bool force_nonblock, |
| 5081 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5082 | { |
| 5083 | return -EOPNOTSUPP; |
| 5084 | } |
| 5085 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5086 | static int io_recv(struct io_kiocb *req, bool force_nonblock, |
| 5087 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5088 | { |
| 5089 | return -EOPNOTSUPP; |
| 5090 | } |
| 5091 | |
| 5092 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5093 | { |
| 5094 | return -EOPNOTSUPP; |
| 5095 | } |
| 5096 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5097 | static int io_accept(struct io_kiocb *req, bool force_nonblock, |
| 5098 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5099 | { |
| 5100 | return -EOPNOTSUPP; |
| 5101 | } |
| 5102 | |
| 5103 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5104 | { |
| 5105 | return -EOPNOTSUPP; |
| 5106 | } |
| 5107 | |
Randy Dunlap | 1e16c2f | 2020-06-26 16:32:50 -0700 | [diff] [blame] | 5108 | static int io_connect(struct io_kiocb *req, bool force_nonblock, |
| 5109 | struct io_comp_state *cs) |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5110 | { |
| 5111 | return -EOPNOTSUPP; |
| 5112 | } |
| 5113 | #endif /* CONFIG_NET */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5114 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5115 | struct io_poll_table { |
| 5116 | struct poll_table_struct pt; |
| 5117 | struct io_kiocb *req; |
| 5118 | int error; |
| 5119 | }; |
| 5120 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5121 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 5122 | __poll_t mask, task_work_func_t func) |
| 5123 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5124 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5125 | |
| 5126 | /* for instances that support it check for an event match first: */ |
| 5127 | if (mask && !(mask & poll->events)) |
| 5128 | return 0; |
| 5129 | |
| 5130 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5131 | |
| 5132 | list_del_init(&poll->wait.entry); |
| 5133 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5134 | req->result = mask; |
| 5135 | init_task_work(&req->task_work, func); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5136 | percpu_ref_get(&req->ctx->refs); |
| 5137 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5138 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5139 | * If this fails, then the task is exiting. When a task exits, the |
| 5140 | * work gets canceled, so just cancel this request as well instead |
| 5141 | * of executing it. We can't safely execute it anyway, as we may not |
| 5142 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5143 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 5144 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5145 | if (unlikely(ret)) { |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 5146 | struct task_struct *tsk; |
| 5147 | |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5148 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5149 | tsk = io_wq_get_task(req->ctx->io_wq); |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 5150 | task_work_add(tsk, &req->task_work, TWA_NONE); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 5151 | wake_up_process(tsk); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 5152 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5153 | return 1; |
| 5154 | } |
| 5155 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5156 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5157 | __acquires(&req->ctx->completion_lock) |
| 5158 | { |
| 5159 | struct io_ring_ctx *ctx = req->ctx; |
| 5160 | |
| 5161 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5162 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5163 | |
| 5164 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5165 | } |
| 5166 | |
| 5167 | spin_lock_irq(&ctx->completion_lock); |
| 5168 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5169 | add_wait_queue(poll->head, &poll->wait); |
| 5170 | return true; |
| 5171 | } |
| 5172 | |
| 5173 | return false; |
| 5174 | } |
| 5175 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5176 | 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] | 5177 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5178 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5179 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5180 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5181 | return req->apoll->double_poll; |
| 5182 | } |
| 5183 | |
| 5184 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5185 | { |
| 5186 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5187 | return &req->poll; |
| 5188 | return &req->apoll->poll; |
| 5189 | } |
| 5190 | |
| 5191 | static void io_poll_remove_double(struct io_kiocb *req) |
| 5192 | { |
| 5193 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5194 | |
| 5195 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5196 | |
| 5197 | if (poll && poll->head) { |
| 5198 | struct wait_queue_head *head = poll->head; |
| 5199 | |
| 5200 | spin_lock(&head->lock); |
| 5201 | list_del_init(&poll->wait.entry); |
| 5202 | if (poll->wait.private) |
| 5203 | refcount_dec(&req->refs); |
| 5204 | poll->head = NULL; |
| 5205 | spin_unlock(&head->lock); |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 5210 | { |
| 5211 | struct io_ring_ctx *ctx = req->ctx; |
| 5212 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5213 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5214 | req->poll.done = true; |
| 5215 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 5216 | io_commit_cqring(ctx); |
| 5217 | } |
| 5218 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5219 | static void io_poll_task_func(struct callback_head *cb) |
| 5220 | { |
| 5221 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5222 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5223 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5224 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5225 | if (io_poll_rewait(req, &req->poll)) { |
| 5226 | spin_unlock_irq(&ctx->completion_lock); |
| 5227 | } else { |
| 5228 | hash_del(&req->hash_node); |
| 5229 | io_poll_complete(req, req->result, 0); |
| 5230 | spin_unlock_irq(&ctx->completion_lock); |
| 5231 | |
| 5232 | nxt = io_put_req_find_next(req); |
| 5233 | io_cqring_ev_posted(ctx); |
| 5234 | if (nxt) |
| 5235 | __io_req_task_submit(nxt); |
| 5236 | } |
| 5237 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5238 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5242 | int sync, void *key) |
| 5243 | { |
| 5244 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5245 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5246 | __poll_t mask = key_to_poll(key); |
| 5247 | |
| 5248 | /* for instances that support it check for an event match first: */ |
| 5249 | if (mask && !(mask & poll->events)) |
| 5250 | return 0; |
| 5251 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5252 | list_del_init(&wait->entry); |
| 5253 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5254 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5255 | bool done; |
| 5256 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5257 | spin_lock(&poll->head->lock); |
| 5258 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5259 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5260 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5261 | /* make sure double remove sees this as being gone */ |
| 5262 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5263 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5264 | if (!done) { |
| 5265 | /* use wait func handler, so it matches the rq type */ |
| 5266 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5267 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5268 | } |
| 5269 | refcount_dec(&req->refs); |
| 5270 | return 1; |
| 5271 | } |
| 5272 | |
| 5273 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5274 | wait_queue_func_t wake_func) |
| 5275 | { |
| 5276 | poll->head = NULL; |
| 5277 | poll->done = false; |
| 5278 | poll->canceled = false; |
| 5279 | poll->events = events; |
| 5280 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5281 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5282 | } |
| 5283 | |
| 5284 | 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] | 5285 | struct wait_queue_head *head, |
| 5286 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5287 | { |
| 5288 | struct io_kiocb *req = pt->req; |
| 5289 | |
| 5290 | /* |
| 5291 | * If poll->head is already set, it's because the file being polled |
| 5292 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5293 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5294 | */ |
| 5295 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5296 | struct io_poll_iocb *poll_one = poll; |
| 5297 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5298 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5299 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5300 | pt->error = -EINVAL; |
| 5301 | return; |
| 5302 | } |
| 5303 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5304 | if (!poll) { |
| 5305 | pt->error = -ENOMEM; |
| 5306 | return; |
| 5307 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5308 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5309 | refcount_inc(&req->refs); |
| 5310 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5311 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | pt->error = 0; |
| 5315 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5316 | |
| 5317 | if (poll->events & EPOLLEXCLUSIVE) |
| 5318 | add_wait_queue_exclusive(head, &poll->wait); |
| 5319 | else |
| 5320 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5324 | struct poll_table_struct *p) |
| 5325 | { |
| 5326 | 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] | 5327 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5328 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5329 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5330 | } |
| 5331 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5332 | static void io_async_task_func(struct callback_head *cb) |
| 5333 | { |
| 5334 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5335 | struct async_poll *apoll = req->apoll; |
| 5336 | struct io_ring_ctx *ctx = req->ctx; |
| 5337 | |
| 5338 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5339 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5340 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5341 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5342 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5343 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5344 | } |
| 5345 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5346 | /* 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] | 5347 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5348 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5349 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5350 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5351 | spin_unlock_irq(&ctx->completion_lock); |
| 5352 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5353 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5354 | __io_req_task_submit(req); |
| 5355 | else |
| 5356 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5357 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5358 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5359 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5360 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5364 | void *key) |
| 5365 | { |
| 5366 | struct io_kiocb *req = wait->private; |
| 5367 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5368 | |
| 5369 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5370 | key_to_poll(key)); |
| 5371 | |
| 5372 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5373 | } |
| 5374 | |
| 5375 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5376 | { |
| 5377 | struct io_ring_ctx *ctx = req->ctx; |
| 5378 | struct hlist_head *list; |
| 5379 | |
| 5380 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5381 | hlist_add_head(&req->hash_node, list); |
| 5382 | } |
| 5383 | |
| 5384 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5385 | struct io_poll_iocb *poll, |
| 5386 | struct io_poll_table *ipt, __poll_t mask, |
| 5387 | wait_queue_func_t wake_func) |
| 5388 | __acquires(&ctx->completion_lock) |
| 5389 | { |
| 5390 | struct io_ring_ctx *ctx = req->ctx; |
| 5391 | bool cancel = false; |
| 5392 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5393 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5394 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5395 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5396 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5397 | |
| 5398 | ipt->pt._key = mask; |
| 5399 | ipt->req = req; |
| 5400 | ipt->error = -EINVAL; |
| 5401 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5402 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5403 | |
| 5404 | spin_lock_irq(&ctx->completion_lock); |
| 5405 | if (likely(poll->head)) { |
| 5406 | spin_lock(&poll->head->lock); |
| 5407 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5408 | if (ipt->error) |
| 5409 | cancel = true; |
| 5410 | ipt->error = 0; |
| 5411 | mask = 0; |
| 5412 | } |
| 5413 | if (mask || ipt->error) |
| 5414 | list_del_init(&poll->wait.entry); |
| 5415 | else if (cancel) |
| 5416 | WRITE_ONCE(poll->canceled, true); |
| 5417 | else if (!poll->done) /* actually waiting for an event */ |
| 5418 | io_poll_req_insert(req); |
| 5419 | spin_unlock(&poll->head->lock); |
| 5420 | } |
| 5421 | |
| 5422 | return mask; |
| 5423 | } |
| 5424 | |
| 5425 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5426 | { |
| 5427 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5428 | struct io_ring_ctx *ctx = req->ctx; |
| 5429 | struct async_poll *apoll; |
| 5430 | struct io_poll_table ipt; |
| 5431 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5432 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5433 | |
| 5434 | if (!req->file || !file_can_poll(req->file)) |
| 5435 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5436 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5437 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5438 | if (def->pollin) |
| 5439 | rw = READ; |
| 5440 | else if (def->pollout) |
| 5441 | rw = WRITE; |
| 5442 | else |
| 5443 | return false; |
| 5444 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5445 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5446 | return false; |
| 5447 | |
| 5448 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5449 | if (unlikely(!apoll)) |
| 5450 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5451 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5452 | |
| 5453 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5454 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5455 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5456 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5457 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5458 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5459 | if (def->pollout) |
| 5460 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5461 | |
| 5462 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5463 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5464 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5465 | mask &= ~POLLIN; |
| 5466 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5467 | mask |= POLLERR | POLLPRI; |
| 5468 | |
| 5469 | ipt.pt._qproc = io_async_queue_proc; |
| 5470 | |
| 5471 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5472 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5473 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5474 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5475 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5476 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5477 | kfree(apoll); |
| 5478 | return false; |
| 5479 | } |
| 5480 | spin_unlock_irq(&ctx->completion_lock); |
| 5481 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5482 | apoll->poll.events); |
| 5483 | return true; |
| 5484 | } |
| 5485 | |
| 5486 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5487 | struct io_poll_iocb *poll) |
| 5488 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5489 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5490 | |
| 5491 | spin_lock(&poll->head->lock); |
| 5492 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5493 | if (!list_empty(&poll->wait.entry)) { |
| 5494 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5495 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5496 | } |
| 5497 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5498 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5499 | return do_complete; |
| 5500 | } |
| 5501 | |
| 5502 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5503 | { |
| 5504 | bool do_complete; |
| 5505 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5506 | io_poll_remove_double(req); |
| 5507 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5508 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5509 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5510 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5511 | struct async_poll *apoll = req->apoll; |
| 5512 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5513 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5514 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5515 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5516 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5517 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5518 | kfree(apoll); |
| 5519 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5520 | } |
| 5521 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5522 | if (do_complete) { |
| 5523 | io_cqring_fill_event(req, -ECANCELED); |
| 5524 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5525 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5526 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5530 | } |
| 5531 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5532 | /* |
| 5533 | * Returns true if we found and killed one or more poll requests |
| 5534 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5535 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5536 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5537 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5538 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5539 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5540 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5541 | |
| 5542 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5543 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5544 | struct hlist_head *list; |
| 5545 | |
| 5546 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5547 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5548 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5549 | posted += io_poll_remove_one(req); |
| 5550 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5551 | } |
| 5552 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5553 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5554 | if (posted) |
| 5555 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5556 | |
| 5557 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5558 | } |
| 5559 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5560 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5561 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5562 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5563 | struct io_kiocb *req; |
| 5564 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5565 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5566 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5567 | if (sqe_addr != req->user_data) |
| 5568 | continue; |
| 5569 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5570 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5571 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | return -ENOENT; |
| 5575 | } |
| 5576 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5577 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5578 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5579 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5580 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5581 | return -EINVAL; |
| 5582 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5583 | sqe->poll_events) |
| 5584 | return -EINVAL; |
| 5585 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5586 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5587 | return 0; |
| 5588 | } |
| 5589 | |
| 5590 | /* |
| 5591 | * Find a running poll command that matches one specified in sqe->addr, |
| 5592 | * and remove it if found. |
| 5593 | */ |
| 5594 | static int io_poll_remove(struct io_kiocb *req) |
| 5595 | { |
| 5596 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5597 | int ret; |
| 5598 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5599 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5600 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5601 | spin_unlock_irq(&ctx->completion_lock); |
| 5602 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5603 | if (ret < 0) |
| 5604 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5605 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5606 | return 0; |
| 5607 | } |
| 5608 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5609 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5610 | void *key) |
| 5611 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5612 | struct io_kiocb *req = wait->private; |
| 5613 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5614 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5615 | 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] | 5616 | } |
| 5617 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5618 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5619 | struct poll_table_struct *p) |
| 5620 | { |
| 5621 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5622 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5623 | __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] | 5624 | } |
| 5625 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5626 | 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] | 5627 | { |
| 5628 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5629 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5630 | |
| 5631 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5632 | return -EINVAL; |
| 5633 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5634 | return -EINVAL; |
| 5635 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5636 | events = READ_ONCE(sqe->poll32_events); |
| 5637 | #ifdef __BIG_ENDIAN |
| 5638 | events = swahw32(events); |
| 5639 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5640 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5641 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5642 | return 0; |
| 5643 | } |
| 5644 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5645 | static int io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5646 | { |
| 5647 | struct io_poll_iocb *poll = &req->poll; |
| 5648 | struct io_ring_ctx *ctx = req->ctx; |
| 5649 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5650 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5651 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5652 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5653 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5654 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5655 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5656 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5657 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5658 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5659 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5660 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5661 | spin_unlock_irq(&ctx->completion_lock); |
| 5662 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5663 | if (mask) { |
| 5664 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5665 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5666 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5667 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5668 | } |
| 5669 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5670 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5671 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5672 | struct io_timeout_data *data = container_of(timer, |
| 5673 | struct io_timeout_data, timer); |
| 5674 | struct io_kiocb *req = data->req; |
| 5675 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5676 | unsigned long flags; |
| 5677 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5678 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5679 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5680 | atomic_set(&req->ctx->cq_timeouts, |
| 5681 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5682 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5683 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5684 | io_commit_cqring(ctx); |
| 5685 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5686 | |
| 5687 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5688 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5689 | io_put_req(req); |
| 5690 | return HRTIMER_NORESTART; |
| 5691 | } |
| 5692 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5693 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5694 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5695 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5696 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5697 | struct io_kiocb *req; |
| 5698 | int ret = -ENOENT; |
| 5699 | |
| 5700 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5701 | if (user_data == req->user_data) { |
| 5702 | ret = 0; |
| 5703 | break; |
| 5704 | } |
| 5705 | } |
| 5706 | |
| 5707 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5708 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5709 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5710 | io = req->async_data; |
| 5711 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5712 | if (ret == -1) |
| 5713 | return ERR_PTR(-EALREADY); |
| 5714 | list_del_init(&req->timeout.list); |
| 5715 | return req; |
| 5716 | } |
| 5717 | |
| 5718 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5719 | { |
| 5720 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5721 | |
| 5722 | if (IS_ERR(req)) |
| 5723 | return PTR_ERR(req); |
| 5724 | |
| 5725 | req_set_fail_links(req); |
| 5726 | io_cqring_fill_event(req, -ECANCELED); |
| 5727 | io_put_req_deferred(req, 1); |
| 5728 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5729 | } |
| 5730 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5731 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5732 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5733 | { |
| 5734 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5735 | struct io_timeout_data *data; |
| 5736 | |
| 5737 | if (IS_ERR(req)) |
| 5738 | return PTR_ERR(req); |
| 5739 | |
| 5740 | req->timeout.off = 0; /* noseq */ |
| 5741 | data = req->async_data; |
| 5742 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5743 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5744 | data->timer.function = io_timeout_fn; |
| 5745 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5746 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5747 | } |
| 5748 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5749 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5750 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5751 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5752 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5753 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5754 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5755 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5756 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5757 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5758 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5759 | return -EINVAL; |
| 5760 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5761 | tr->addr = READ_ONCE(sqe->addr); |
| 5762 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5763 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5764 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5765 | return -EINVAL; |
| 5766 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5767 | return -EFAULT; |
| 5768 | } else if (tr->flags) { |
| 5769 | /* timeout removal doesn't support flags */ |
| 5770 | return -EINVAL; |
| 5771 | } |
| 5772 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5773 | return 0; |
| 5774 | } |
| 5775 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5776 | /* |
| 5777 | * Remove or update an existing timeout command |
| 5778 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5779 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5780 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5781 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5782 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5783 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5784 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5785 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5786 | if (req->timeout_rem.flags & IORING_TIMEOUT_UPDATE) { |
| 5787 | enum hrtimer_mode mode = (tr->flags & IORING_TIMEOUT_ABS) |
| 5788 | ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; |
| 5789 | |
| 5790 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 5791 | } else { |
| 5792 | ret = io_timeout_cancel(ctx, tr->addr); |
| 5793 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5794 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5795 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5796 | io_commit_cqring(ctx); |
| 5797 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5798 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5799 | if (ret < 0) |
| 5800 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5801 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5802 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5803 | } |
| 5804 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5805 | 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] | 5806 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5807 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5808 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5809 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5810 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5811 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5812 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5813 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5814 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5815 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5816 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5817 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5818 | flags = READ_ONCE(sqe->timeout_flags); |
| 5819 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5820 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5821 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5822 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5823 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5824 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5825 | return -ENOMEM; |
| 5826 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5827 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5828 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5829 | |
| 5830 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5831 | return -EFAULT; |
| 5832 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5833 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5834 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5835 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5836 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5837 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5838 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5839 | return 0; |
| 5840 | } |
| 5841 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 5842 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5843 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5844 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5845 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5846 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5847 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5848 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5849 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5850 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5851 | /* |
| 5852 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5853 | * timeout event to be satisfied. If it isn't set, then this is |
| 5854 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5855 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5856 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5857 | entry = ctx->timeout_list.prev; |
| 5858 | goto add; |
| 5859 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5860 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5861 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5862 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5863 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5864 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5865 | * This is safe because ->completion_lock is held, and submissions |
| 5866 | * and completions are never mixed in the same ->completion_lock section. |
| 5867 | */ |
| 5868 | ctx->cq_last_tm_flush = tail; |
| 5869 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5870 | /* |
| 5871 | * Insertion sort, ensuring the first entry in the list is always |
| 5872 | * the one we need first. |
| 5873 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5874 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5875 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5876 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5877 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5878 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5879 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5880 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5881 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5882 | break; |
| 5883 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5884 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5885 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5886 | data->timer.function = io_timeout_fn; |
| 5887 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5888 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5889 | return 0; |
| 5890 | } |
| 5891 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5892 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5893 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5894 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5895 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5896 | return req->user_data == (unsigned long) data; |
| 5897 | } |
| 5898 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5899 | 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] | 5900 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5901 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5902 | int ret = 0; |
| 5903 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 5904 | 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] | 5905 | switch (cancel_ret) { |
| 5906 | case IO_WQ_CANCEL_OK: |
| 5907 | ret = 0; |
| 5908 | break; |
| 5909 | case IO_WQ_CANCEL_RUNNING: |
| 5910 | ret = -EALREADY; |
| 5911 | break; |
| 5912 | case IO_WQ_CANCEL_NOTFOUND: |
| 5913 | ret = -ENOENT; |
| 5914 | break; |
| 5915 | } |
| 5916 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5917 | return ret; |
| 5918 | } |
| 5919 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5920 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5921 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5922 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5923 | { |
| 5924 | unsigned long flags; |
| 5925 | int ret; |
| 5926 | |
| 5927 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 5928 | if (ret != -ENOENT) { |
| 5929 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5930 | goto done; |
| 5931 | } |
| 5932 | |
| 5933 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5934 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5935 | if (ret != -ENOENT) |
| 5936 | goto done; |
| 5937 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5938 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5939 | if (!ret) |
| 5940 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5941 | io_cqring_fill_event(req, ret); |
| 5942 | io_commit_cqring(ctx); |
| 5943 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5944 | io_cqring_ev_posted(ctx); |
| 5945 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5946 | if (ret < 0) |
| 5947 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5948 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5949 | } |
| 5950 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5951 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5952 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5953 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5954 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5955 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5956 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5957 | return -EINVAL; |
| 5958 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5959 | return -EINVAL; |
| 5960 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5961 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5962 | return 0; |
| 5963 | } |
| 5964 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5965 | static int io_async_cancel(struct io_kiocb *req) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5966 | { |
| 5967 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5968 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5969 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5970 | return 0; |
| 5971 | } |
| 5972 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5973 | static int io_files_update_prep(struct io_kiocb *req, |
| 5974 | const struct io_uring_sqe *sqe) |
| 5975 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5976 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5977 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5978 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5979 | return -EINVAL; |
| 5980 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5981 | return -EINVAL; |
| 5982 | |
| 5983 | req->files_update.offset = READ_ONCE(sqe->off); |
| 5984 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 5985 | if (!req->files_update.nr_args) |
| 5986 | return -EINVAL; |
| 5987 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 5988 | return 0; |
| 5989 | } |
| 5990 | |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 5991 | static int io_files_update(struct io_kiocb *req, bool force_nonblock, |
| 5992 | struct io_comp_state *cs) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5993 | { |
| 5994 | struct io_ring_ctx *ctx = req->ctx; |
| 5995 | struct io_uring_files_update up; |
| 5996 | int ret; |
| 5997 | |
Jens Axboe | f86cd20 | 2020-01-29 13:46:44 -0700 | [diff] [blame] | 5998 | if (force_nonblock) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5999 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6000 | |
| 6001 | up.offset = req->files_update.offset; |
| 6002 | up.fds = req->files_update.arg; |
| 6003 | |
| 6004 | mutex_lock(&ctx->uring_lock); |
| 6005 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 6006 | mutex_unlock(&ctx->uring_lock); |
| 6007 | |
| 6008 | if (ret < 0) |
| 6009 | req_set_fail_links(req); |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6010 | __io_req_complete(req, ret, 0, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6011 | return 0; |
| 6012 | } |
| 6013 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6014 | 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] | 6015 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6016 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6017 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6018 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6019 | case IORING_OP_READV: |
| 6020 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6021 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6022 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6023 | case IORING_OP_WRITEV: |
| 6024 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6025 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6026 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6027 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6028 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6029 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6030 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6031 | case IORING_OP_FSYNC: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6032 | return io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6033 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6034 | return io_prep_sfr(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6035 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6036 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6037 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6038 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6039 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6040 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6041 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6042 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6043 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6044 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6045 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6046 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6047 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6048 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6049 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6050 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6051 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6052 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6053 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6054 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6055 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6056 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6057 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6058 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6059 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6060 | return io_files_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6061 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6062 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6063 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6064 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6065 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6066 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6067 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6068 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6069 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6070 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6071 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6072 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6073 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6074 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6075 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6076 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6077 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6078 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6079 | case IORING_OP_SHUTDOWN: |
| 6080 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6081 | case IORING_OP_RENAMEAT: |
| 6082 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6083 | case IORING_OP_UNLINKAT: |
| 6084 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6085 | } |
| 6086 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6087 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6088 | req->opcode); |
| 6089 | return-EINVAL; |
| 6090 | } |
| 6091 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6092 | static int io_req_defer_prep(struct io_kiocb *req, |
| 6093 | const struct io_uring_sqe *sqe) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6094 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6095 | if (!sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6096 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6097 | if (io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6098 | return -EAGAIN; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6099 | return io_req_prep(req, sqe); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6100 | } |
| 6101 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6102 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6103 | { |
| 6104 | struct io_kiocb *pos; |
| 6105 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6106 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6107 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6108 | io_for_each_link(pos, req) |
| 6109 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6110 | |
| 6111 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 6112 | return total_submitted - nr_reqs; |
| 6113 | } |
| 6114 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6115 | 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] | 6116 | { |
| 6117 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6118 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6119 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6120 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6121 | |
| 6122 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6123 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6124 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6125 | return 0; |
| 6126 | |
| 6127 | seq = io_get_sequence(req); |
| 6128 | /* Still a chance to pass the sequence check */ |
| 6129 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6130 | return 0; |
| 6131 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6132 | if (!req->async_data) { |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6133 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6134 | if (ret) |
Pavel Begunkov | 650b548 | 2020-05-17 14:02:11 +0300 | [diff] [blame] | 6135 | return ret; |
| 6136 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6137 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6138 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6139 | if (!de) |
| 6140 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6141 | |
| 6142 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6143 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6144 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6145 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6146 | io_queue_async_work(req); |
| 6147 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6148 | } |
| 6149 | |
| 6150 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6151 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6152 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6153 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6154 | spin_unlock_irq(&ctx->completion_lock); |
| 6155 | return -EIOCBQUEUED; |
| 6156 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6157 | |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6158 | static void io_req_drop_files(struct io_kiocb *req) |
| 6159 | { |
| 6160 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 6161 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6162 | unsigned long flags; |
| 6163 | |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6164 | put_files_struct(req->work.identity->files); |
| 6165 | put_nsproxy(req->work.identity->nsproxy); |
Pavel Begunkov | dfea9fc | 2020-12-18 13:12:21 +0000 | [diff] [blame] | 6166 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 6167 | list_del(&req->inflight_entry); |
| 6168 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 6169 | req->flags &= ~REQ_F_INFLIGHT; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6170 | req->work.flags &= ~IO_WQ_WORK_FILES; |
Pavel Begunkov | dfea9fc | 2020-12-18 13:12:21 +0000 | [diff] [blame] | 6171 | if (atomic_read(&tctx->in_idle)) |
| 6172 | wake_up(&tctx->wait); |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6173 | } |
| 6174 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 6175 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6176 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6177 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6178 | switch (req->opcode) { |
| 6179 | case IORING_OP_READV: |
| 6180 | case IORING_OP_READ_FIXED: |
| 6181 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6182 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6183 | break; |
| 6184 | case IORING_OP_RECVMSG: |
| 6185 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6186 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6187 | break; |
| 6188 | } |
| 6189 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6190 | } |
| 6191 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6192 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6193 | switch (req->opcode) { |
| 6194 | case IORING_OP_READV: |
| 6195 | case IORING_OP_READ_FIXED: |
| 6196 | case IORING_OP_READ: |
| 6197 | case IORING_OP_WRITEV: |
| 6198 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6199 | case IORING_OP_WRITE: { |
| 6200 | struct io_async_rw *io = req->async_data; |
| 6201 | if (io->free_iovec) |
| 6202 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6203 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6204 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6205 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6206 | case IORING_OP_SENDMSG: { |
| 6207 | struct io_async_msghdr *io = req->async_data; |
| 6208 | if (io->iov != io->fast_iov) |
| 6209 | kfree(io->iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6210 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6211 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6212 | case IORING_OP_SPLICE: |
| 6213 | case IORING_OP_TEE: |
| 6214 | io_put_file(req, req->splice.file_in, |
| 6215 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 6216 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6217 | case IORING_OP_OPENAT: |
| 6218 | case IORING_OP_OPENAT2: |
| 6219 | if (req->open.filename) |
| 6220 | putname(req->open.filename); |
| 6221 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6222 | case IORING_OP_RENAMEAT: |
| 6223 | putname(req->rename.oldpath); |
| 6224 | putname(req->rename.newpath); |
| 6225 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6226 | case IORING_OP_UNLINKAT: |
| 6227 | putname(req->unlink.filename); |
| 6228 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6229 | } |
| 6230 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6231 | } |
Pavel Begunkov | bb17534 | 2020-08-20 11:33:35 +0300 | [diff] [blame] | 6232 | |
Jens Axboe | f573d38 | 2020-09-22 10:19:24 -0600 | [diff] [blame] | 6233 | if (req->flags & REQ_F_INFLIGHT) |
| 6234 | io_req_drop_files(req); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6235 | } |
| 6236 | |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6237 | static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock, |
| 6238 | struct io_comp_state *cs) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6239 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6240 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6241 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6242 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6243 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6244 | case IORING_OP_NOP: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6245 | ret = io_nop(req, cs); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6246 | break; |
| 6247 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6248 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6249 | case IORING_OP_READ: |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 6250 | ret = io_read(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6251 | break; |
| 6252 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6253 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6254 | case IORING_OP_WRITE: |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 6255 | ret = io_write(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6256 | break; |
| 6257 | case IORING_OP_FSYNC: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6258 | ret = io_fsync(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6259 | break; |
| 6260 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6261 | ret = io_poll_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6262 | break; |
| 6263 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6264 | ret = io_poll_remove(req); |
| 6265 | break; |
| 6266 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6267 | ret = io_sync_file_range(req, force_nonblock); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6268 | break; |
| 6269 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6270 | ret = io_sendmsg(req, force_nonblock, cs); |
| 6271 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6272 | case IORING_OP_SEND: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6273 | ret = io_send(req, force_nonblock, cs); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6274 | break; |
| 6275 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6276 | ret = io_recvmsg(req, force_nonblock, cs); |
| 6277 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6278 | case IORING_OP_RECV: |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6279 | ret = io_recv(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6280 | break; |
| 6281 | case IORING_OP_TIMEOUT: |
| 6282 | ret = io_timeout(req); |
| 6283 | break; |
| 6284 | case IORING_OP_TIMEOUT_REMOVE: |
| 6285 | ret = io_timeout_remove(req); |
| 6286 | break; |
| 6287 | case IORING_OP_ACCEPT: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6288 | ret = io_accept(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6289 | break; |
| 6290 | case IORING_OP_CONNECT: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6291 | ret = io_connect(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6292 | break; |
| 6293 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6294 | ret = io_async_cancel(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6295 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6296 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6297 | ret = io_fallocate(req, force_nonblock); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6298 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6299 | case IORING_OP_OPENAT: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6300 | ret = io_openat(req, force_nonblock); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6301 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6302 | case IORING_OP_CLOSE: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6303 | ret = io_close(req, force_nonblock, cs); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6304 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6305 | case IORING_OP_FILES_UPDATE: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6306 | ret = io_files_update(req, force_nonblock, cs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6307 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6308 | case IORING_OP_STATX: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6309 | ret = io_statx(req, force_nonblock); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6310 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6311 | case IORING_OP_FADVISE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6312 | ret = io_fadvise(req, force_nonblock); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6313 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6314 | case IORING_OP_MADVISE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6315 | ret = io_madvise(req, force_nonblock); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6316 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6317 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6318 | ret = io_openat2(req, force_nonblock); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6319 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6320 | case IORING_OP_EPOLL_CTL: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6321 | ret = io_epoll_ctl(req, force_nonblock, cs); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6322 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6323 | case IORING_OP_SPLICE: |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6324 | ret = io_splice(req, force_nonblock); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6325 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6326 | case IORING_OP_PROVIDE_BUFFERS: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6327 | ret = io_provide_buffers(req, force_nonblock, cs); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6328 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6329 | case IORING_OP_REMOVE_BUFFERS: |
Jens Axboe | 229a7b6 | 2020-06-22 10:13:11 -0600 | [diff] [blame] | 6330 | ret = io_remove_buffers(req, force_nonblock, cs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6331 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6332 | case IORING_OP_TEE: |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6333 | ret = io_tee(req, force_nonblock); |
| 6334 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6335 | case IORING_OP_SHUTDOWN: |
| 6336 | ret = io_shutdown(req, force_nonblock); |
| 6337 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6338 | case IORING_OP_RENAMEAT: |
| 6339 | ret = io_renameat(req, force_nonblock); |
| 6340 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6341 | case IORING_OP_UNLINKAT: |
| 6342 | ret = io_unlinkat(req, force_nonblock); |
| 6343 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6344 | default: |
| 6345 | ret = -EINVAL; |
| 6346 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6350 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6351 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6352 | /* If the op doesn't have a file, we're not polling for it */ |
| 6353 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6354 | const bool in_async = io_wq_current_is_worker(); |
| 6355 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6356 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6357 | if (in_async) |
| 6358 | mutex_lock(&ctx->uring_lock); |
| 6359 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6360 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6361 | |
| 6362 | if (in_async) |
| 6363 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6364 | } |
| 6365 | |
| 6366 | return 0; |
| 6367 | } |
| 6368 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 6369 | 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] | 6370 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6371 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6372 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6373 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6374 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6375 | timeout = io_prep_linked_timeout(req); |
| 6376 | if (timeout) |
| 6377 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6378 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 6379 | /* if NO_CANCEL is set, we must still run the work */ |
| 6380 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 6381 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6382 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 6383 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6384 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6385 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6386 | do { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6387 | ret = io_issue_sqe(req, false, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6388 | /* |
| 6389 | * We can get EAGAIN for polled IO even though we're |
| 6390 | * forcing a sync submission from here, since we can't |
| 6391 | * wait for request slots on the block side. |
| 6392 | */ |
| 6393 | if (ret != -EAGAIN) |
| 6394 | break; |
| 6395 | cond_resched(); |
| 6396 | } while (1); |
| 6397 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6398 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6399 | if (ret) { |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6400 | struct io_ring_ctx *lock_ctx = NULL; |
Xiaoguang Wang | dad1b12 | 2020-12-06 22:22:42 +0000 | [diff] [blame] | 6401 | |
Xiaoguang Wang | c07e671 | 2020-12-14 23:49:41 +0800 | [diff] [blame] | 6402 | if (req->ctx->flags & IORING_SETUP_IOPOLL) |
| 6403 | lock_ctx = req->ctx; |
| 6404 | |
| 6405 | /* |
| 6406 | * io_iopoll_complete() does not hold completion_lock to |
| 6407 | * complete polled io, so here for polled io, we can not call |
| 6408 | * io_req_complete() directly, otherwise there maybe concurrent |
| 6409 | * access to cqring, defer_list, etc, which is not safe. Given |
| 6410 | * that io_iopoll_complete() is always called under uring_lock, |
| 6411 | * so here for polled io, we also get uring_lock to complete |
| 6412 | * it. |
| 6413 | */ |
| 6414 | if (lock_ctx) |
| 6415 | mutex_lock(&lock_ctx->uring_lock); |
| 6416 | |
| 6417 | req_set_fail_links(req); |
| 6418 | io_req_complete(req, ret); |
| 6419 | |
| 6420 | if (lock_ctx) |
| 6421 | mutex_unlock(&lock_ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6422 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6423 | |
Pavel Begunkov | f4db718 | 2020-06-25 18:20:54 +0300 | [diff] [blame] | 6424 | return io_steal_work(req); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6425 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6426 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6427 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6428 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6429 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6430 | struct fixed_file_table *table; |
| 6431 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6432 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6433 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6434 | } |
| 6435 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6436 | static struct file *io_file_get(struct io_submit_state *state, |
| 6437 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6438 | { |
| 6439 | struct io_ring_ctx *ctx = req->ctx; |
| 6440 | struct file *file; |
| 6441 | |
| 6442 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6443 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6444 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6445 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6446 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6447 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6448 | } else { |
| 6449 | trace_io_uring_file_get(ctx, fd); |
| 6450 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6451 | } |
| 6452 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6453 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6454 | } |
| 6455 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6456 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6457 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6458 | struct io_timeout_data *data = container_of(timer, |
| 6459 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6460 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6461 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6462 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6463 | |
| 6464 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6465 | prev = req->timeout.head; |
| 6466 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6467 | |
| 6468 | /* |
| 6469 | * We don't expect the list to be empty, that will only happen if we |
| 6470 | * race with the completion of the linked work. |
| 6471 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6472 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6473 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6474 | else |
| 6475 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6476 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6477 | |
| 6478 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6479 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6480 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6481 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6482 | } else { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6483 | io_req_complete(req, -ETIME); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6484 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6485 | return HRTIMER_NORESTART; |
| 6486 | } |
| 6487 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6488 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6489 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6490 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6491 | * If the back reference is NULL, then our linked request finished |
| 6492 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6493 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6494 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6495 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6496 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6497 | data->timer.function = io_link_timeout_fn; |
| 6498 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6499 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6500 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6501 | } |
| 6502 | |
| 6503 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6504 | { |
| 6505 | struct io_ring_ctx *ctx = req->ctx; |
| 6506 | |
| 6507 | spin_lock_irq(&ctx->completion_lock); |
| 6508 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6509 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6510 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6511 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6512 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6513 | } |
| 6514 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6515 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6516 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6517 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6518 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6519 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6520 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6521 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6522 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6523 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6524 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6525 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6526 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6527 | } |
| 6528 | |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6529 | 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] | 6530 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6531 | struct io_kiocb *linked_timeout; |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6532 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6533 | int ret; |
| 6534 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 6535 | again: |
| 6536 | linked_timeout = io_prep_linked_timeout(req); |
| 6537 | |
Pavel Begunkov | 2e5aa6c | 2020-10-18 10:17:37 +0100 | [diff] [blame] | 6538 | if ((req->flags & REQ_F_WORK_INITIALIZED) && |
| 6539 | (req->work.flags & IO_WQ_WORK_CREDS) && |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6540 | req->work.identity->creds != current_cred()) { |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6541 | if (old_creds) |
| 6542 | revert_creds(old_creds); |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6543 | if (old_creds == req->work.identity->creds) |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6544 | old_creds = NULL; /* restored original creds */ |
| 6545 | else |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 6546 | old_creds = override_creds(req->work.identity->creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6547 | } |
| 6548 | |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6549 | ret = io_issue_sqe(req, true, cs); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6550 | |
| 6551 | /* |
| 6552 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6553 | * doesn't support non-blocking read/write attempts |
| 6554 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6555 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6556 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6557 | /* |
| 6558 | * Queued up for async execution, worker will release |
| 6559 | * submit reference when the iocb is actually submitted. |
| 6560 | */ |
| 6561 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6562 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 6563 | |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6564 | if (linked_timeout) |
| 6565 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6566 | } else if (likely(!ret)) { |
| 6567 | /* drop submission reference */ |
| 6568 | req = io_put_req_find_next(req); |
| 6569 | if (linked_timeout) |
| 6570 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6571 | |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6572 | if (req) { |
| 6573 | if (!(req->flags & REQ_F_FORCE_ASYNC)) |
| 6574 | goto again; |
| 6575 | io_queue_async_work(req); |
| 6576 | } |
| 6577 | } else { |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6578 | /* un-prep timeout, so it'll be killed as any other linked */ |
| 6579 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6580 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6581 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6582 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6583 | } |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6584 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6585 | if (old_creds) |
| 6586 | revert_creds(old_creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6587 | } |
| 6588 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6589 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 6590 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6591 | { |
| 6592 | int ret; |
| 6593 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6594 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6595 | if (ret) { |
| 6596 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6597 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6598 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6599 | io_put_req(req); |
| 6600 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6601 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6602 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6603 | if (!req->async_data) { |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6604 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6605 | if (unlikely(ret)) |
Pavel Begunkov | bd2ab18 | 2020-05-17 14:02:12 +0300 | [diff] [blame] | 6606 | goto fail_req; |
| 6607 | } |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6608 | io_queue_async_work(req); |
| 6609 | } else { |
Pavel Begunkov | c1379e2 | 2020-09-30 22:57:56 +0300 | [diff] [blame] | 6610 | if (sqe) { |
| 6611 | ret = io_req_prep(req, sqe); |
| 6612 | if (unlikely(ret)) |
| 6613 | goto fail_req; |
| 6614 | } |
| 6615 | __io_queue_sqe(req, cs); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6616 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6617 | } |
| 6618 | |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6619 | static inline void io_queue_link_head(struct io_kiocb *req, |
| 6620 | struct io_comp_state *cs) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6621 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6622 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6623 | io_put_req(req); |
| 6624 | io_req_complete(req, -ECANCELED); |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6625 | } else |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6626 | io_queue_sqe(req, NULL, cs); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6627 | } |
| 6628 | |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6629 | struct io_submit_link { |
| 6630 | struct io_kiocb *head; |
| 6631 | struct io_kiocb *last; |
| 6632 | }; |
| 6633 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6634 | 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] | 6635 | struct io_submit_link *link, struct io_comp_state *cs) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6636 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6637 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6638 | int ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6639 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6640 | /* |
| 6641 | * If we already have a head request, queue this one for async |
| 6642 | * submittal once the head completes. If we don't have a head but |
| 6643 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6644 | * submitted sync once the chain is complete. If none of those |
| 6645 | * conditions are true (normal request), then just queue it. |
| 6646 | */ |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6647 | if (link->head) { |
| 6648 | struct io_kiocb *head = link->head; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6649 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6650 | /* |
| 6651 | * Taking sequential execution of a link, draining both sides |
| 6652 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6653 | * requests in the link. So, it drains the head and the |
| 6654 | * next after the link request. The last one is done via |
| 6655 | * drain_next flag to persist the effect across calls. |
| 6656 | */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6657 | if (req->flags & REQ_F_IO_DRAIN) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6658 | head->flags |= REQ_F_IO_DRAIN; |
| 6659 | ctx->drain_next = 1; |
| 6660 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6661 | ret = io_req_defer_prep(req, sqe); |
Pavel Begunkov | 327d6d9 | 2020-07-15 12:46:51 +0300 | [diff] [blame] | 6662 | if (unlikely(ret)) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6663 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6664 | head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6665 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6666 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6667 | trace_io_uring_link(ctx, req, head); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6668 | link->last->link = req; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6669 | link->last = req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6670 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6671 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6672 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6673 | io_queue_link_head(head, cs); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6674 | link->head = NULL; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6675 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6676 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6677 | if (unlikely(ctx->drain_next)) { |
| 6678 | req->flags |= REQ_F_IO_DRAIN; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6679 | ctx->drain_next = 0; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6680 | } |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6681 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [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)) |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6684 | req->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6685 | link->head = req; |
| 6686 | link->last = req; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6687 | } else { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6688 | io_queue_sqe(req, sqe, cs); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6689 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6690 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6691 | |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6692 | return 0; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6693 | } |
| 6694 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6695 | /* |
| 6696 | * Batched submission is done, ensure local IO is flushed out. |
| 6697 | */ |
| 6698 | static void io_submit_state_end(struct io_submit_state *state) |
| 6699 | { |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6700 | if (!list_empty(&state->comp.list)) |
| 6701 | io_submit_flush_completions(&state->comp); |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6702 | if (state->plug_started) |
| 6703 | blk_finish_plug(&state->plug); |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 6704 | io_state_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6705 | if (state->free_reqs) |
Pavel Begunkov | 6c8a313 | 2020-02-01 03:58:00 +0300 | [diff] [blame] | 6706 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6707 | } |
| 6708 | |
| 6709 | /* |
| 6710 | * Start submission side cache. |
| 6711 | */ |
| 6712 | static void io_submit_state_start(struct io_submit_state *state, |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6713 | struct io_ring_ctx *ctx, unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6714 | { |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6715 | state->plug_started = false; |
Jens Axboe | 013538b | 2020-06-22 09:29:15 -0600 | [diff] [blame] | 6716 | state->comp.nr = 0; |
| 6717 | INIT_LIST_HEAD(&state->comp.list); |
| 6718 | state->comp.ctx = ctx; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 6719 | state->free_reqs = 0; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 6720 | state->file_refs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 6721 | state->ios_left = max_ios; |
| 6722 | } |
| 6723 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6724 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6725 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6726 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6727 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 6728 | /* |
| 6729 | * Ensure any loads from the SQEs are done at this point, |
| 6730 | * since once we write the new head, the application could |
| 6731 | * write new data to them. |
| 6732 | */ |
| 6733 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6734 | } |
| 6735 | |
| 6736 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6737 | * 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] | 6738 | * that is mapped by userspace. This means that care needs to be taken to |
| 6739 | * ensure that reads are stable, as we cannot rely on userspace always |
| 6740 | * being a good citizen. If members of the sqe are validated and then later |
| 6741 | * used, it's important that those reads are done through READ_ONCE() to |
| 6742 | * prevent a re-load down the line. |
| 6743 | */ |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6744 | 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] | 6745 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6746 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6747 | unsigned head; |
| 6748 | |
| 6749 | /* |
| 6750 | * The cached sq head (or cq tail) serves two purposes: |
| 6751 | * |
| 6752 | * 1) allows us to batch the cost of updating the user visible |
| 6753 | * head updates. |
| 6754 | * 2) allows the kernel side to track the head on its own, even |
| 6755 | * though the application is the one updating it. |
| 6756 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6757 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6758 | if (likely(head < ctx->sq_entries)) |
| 6759 | return &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6760 | |
| 6761 | /* drop invalid entries */ |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 6762 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6763 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6764 | return NULL; |
| 6765 | } |
| 6766 | |
| 6767 | static inline void io_consume_sqe(struct io_ring_ctx *ctx) |
| 6768 | { |
| 6769 | ctx->cached_sq_head++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6770 | } |
| 6771 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6772 | /* |
| 6773 | * Check SQE restrictions (opcode and flags). |
| 6774 | * |
| 6775 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6776 | */ |
| 6777 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6778 | struct io_kiocb *req, |
| 6779 | unsigned int sqe_flags) |
| 6780 | { |
| 6781 | if (!ctx->restricted) |
| 6782 | return true; |
| 6783 | |
| 6784 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6785 | return false; |
| 6786 | |
| 6787 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6788 | ctx->restrictions.sqe_flags_required) |
| 6789 | return false; |
| 6790 | |
| 6791 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6792 | ctx->restrictions.sqe_flags_required)) |
| 6793 | return false; |
| 6794 | |
| 6795 | return true; |
| 6796 | } |
| 6797 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6798 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 6799 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 6800 | IOSQE_BUFFER_SELECT) |
| 6801 | |
| 6802 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 6803 | const struct io_uring_sqe *sqe, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6804 | struct io_submit_state *state) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6805 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6806 | unsigned int sqe_flags; |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6807 | int id, ret; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6808 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6809 | req->opcode = READ_ONCE(sqe->opcode); |
| 6810 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6811 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6812 | req->file = NULL; |
| 6813 | req->ctx = ctx; |
| 6814 | req->flags = 0; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6815 | req->link = NULL; |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6816 | req->fixed_file_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6817 | /* one is dropped after submission, the other at completion */ |
| 6818 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6819 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6820 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6821 | |
| 6822 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6823 | return -EINVAL; |
| 6824 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6825 | if (unlikely(io_sq_thread_acquire_mm_files(ctx, req))) |
Jens Axboe | 9d8426a | 2020-06-16 18:42:49 -0600 | [diff] [blame] | 6826 | return -EFAULT; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6827 | |
| 6828 | sqe_flags = READ_ONCE(sqe->flags); |
| 6829 | /* enforce forwards compatibility on users */ |
| 6830 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) |
| 6831 | return -EINVAL; |
| 6832 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6833 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6834 | return -EACCES; |
| 6835 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6836 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6837 | !io_op_defs[req->opcode].buffer_select) |
| 6838 | return -EOPNOTSUPP; |
| 6839 | |
| 6840 | id = READ_ONCE(sqe->personality); |
| 6841 | if (id) { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6842 | struct io_identity *iod; |
| 6843 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6844 | iod = idr_find(&ctx->personality_idr, id); |
| 6845 | if (unlikely(!iod)) |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6846 | return -EINVAL; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6847 | refcount_inc(&iod->count); |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 6848 | |
| 6849 | __io_req_init_async(req); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 6850 | get_cred(iod->creds); |
| 6851 | req->work.identity = iod; |
Jens Axboe | dfead8a | 2020-10-14 10:12:37 -0600 | [diff] [blame] | 6852 | req->work.flags |= IO_WQ_WORK_CREDS; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6853 | } |
| 6854 | |
| 6855 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 6856 | req->flags |= sqe_flags; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6857 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6858 | /* |
| 6859 | * Plug now if we have more than 1 IO left after this, and the target |
| 6860 | * is potentially a read/write to block based storage. |
| 6861 | */ |
| 6862 | if (!state->plug_started && state->ios_left > 1 && |
| 6863 | io_op_defs[req->opcode].plug) { |
| 6864 | blk_start_plug(&state->plug); |
| 6865 | state->plug_started = true; |
| 6866 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6867 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6868 | ret = 0; |
| 6869 | if (io_op_defs[req->opcode].needs_file) { |
| 6870 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6871 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6872 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
| 6873 | if (unlikely(!req->file && |
| 6874 | !io_op_defs[req->opcode].needs_file_no_error)) |
| 6875 | ret = -EBADF; |
| 6876 | } |
| 6877 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6878 | state->ios_left--; |
| 6879 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6880 | } |
| 6881 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6882 | 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] | 6883 | { |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6884 | struct io_submit_state state; |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6885 | struct io_submit_link link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6886 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6887 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6888 | /* 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] | 6889 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6890 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6891 | return -EBUSY; |
| 6892 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6893 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6894 | /* make sure SQ entry isn't read before tail */ |
| 6895 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6896 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6897 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6898 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6899 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6900 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6901 | refcount_add(nr, ¤t->usage); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6902 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6903 | io_submit_state_start(&state, ctx, nr); |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6904 | link.head = NULL; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6905 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6906 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6907 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6908 | struct io_kiocb *req; |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6909 | int err; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6910 | |
Pavel Begunkov | b1e50e5 | 2020-04-08 08:58:44 +0300 | [diff] [blame] | 6911 | sqe = io_get_sqe(ctx); |
| 6912 | if (unlikely(!sqe)) { |
| 6913 | io_consume_sqe(ctx); |
| 6914 | break; |
| 6915 | } |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6916 | req = io_alloc_req(ctx, &state); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6917 | if (unlikely(!req)) { |
| 6918 | if (!submitted) |
| 6919 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6920 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6921 | } |
Pavel Begunkov | 709b302 | 2020-04-08 08:58:43 +0300 | [diff] [blame] | 6922 | io_consume_sqe(ctx); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6923 | /* will complete beyond this point, count as submitted */ |
| 6924 | submitted++; |
| 6925 | |
Pavel Begunkov | 692d836 | 2020-10-10 18:34:13 +0100 | [diff] [blame] | 6926 | err = io_init_req(ctx, req, sqe, &state); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6927 | if (unlikely(err)) { |
Pavel Begunkov | 1cb1edb | 2020-02-06 21:16:09 +0300 | [diff] [blame] | 6928 | fail_req: |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6929 | io_put_req(req); |
| 6930 | io_req_complete(req, err); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6931 | break; |
| 6932 | } |
| 6933 | |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 6934 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
Pavel Begunkov | 0cdaf76 | 2020-05-17 14:13:40 +0300 | [diff] [blame] | 6935 | true, io_async_submit(ctx)); |
Jens Axboe | f13fad7 | 2020-06-22 09:34:30 -0600 | [diff] [blame] | 6936 | err = io_submit_sqe(req, sqe, &link, &state.comp); |
Pavel Begunkov | 1d4240c | 2020-04-12 02:05:03 +0300 | [diff] [blame] | 6937 | if (err) |
| 6938 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6939 | } |
| 6940 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6941 | if (unlikely(submitted != nr)) { |
| 6942 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6943 | struct io_uring_task *tctx = current->io_uring; |
| 6944 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6945 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6946 | percpu_ref_put_many(&ctx->refs, unused); |
| 6947 | percpu_counter_sub(&tctx->inflight, unused); |
| 6948 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6949 | } |
Pavel Begunkov | 863e056 | 2020-10-27 23:25:35 +0000 | [diff] [blame] | 6950 | if (link.head) |
| 6951 | io_queue_link_head(link.head, &state.comp); |
Jens Axboe | ac8691c | 2020-06-01 08:30:41 -0600 | [diff] [blame] | 6952 | io_submit_state_end(&state); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6953 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6954 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6955 | io_commit_sqring(ctx); |
| 6956 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6957 | return submitted; |
| 6958 | } |
| 6959 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6960 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6961 | { |
| 6962 | /* Tell userspace we may need a wakeup call */ |
| 6963 | spin_lock_irq(&ctx->completion_lock); |
| 6964 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6965 | spin_unlock_irq(&ctx->completion_lock); |
| 6966 | } |
| 6967 | |
| 6968 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6969 | { |
| 6970 | spin_lock_irq(&ctx->completion_lock); |
| 6971 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6972 | spin_unlock_irq(&ctx->completion_lock); |
| 6973 | } |
| 6974 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6975 | 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] | 6976 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6977 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6978 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6979 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6980 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6981 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6982 | if (cap_entries && to_submit > 8) |
| 6983 | to_submit = 8; |
| 6984 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6985 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6986 | unsigned nr_events = 0; |
| 6987 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6988 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6989 | if (!list_empty(&ctx->iopoll_list)) |
| 6990 | io_do_iopoll(ctx, &nr_events, 0); |
| 6991 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6992 | if (to_submit && !ctx->sqo_dead && |
| 6993 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6994 | ret = io_submit_sqes(ctx, to_submit); |
| 6995 | mutex_unlock(&ctx->uring_lock); |
| 6996 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6997 | |
| 6998 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6999 | wake_up(&ctx->sqo_sq_wait); |
| 7000 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7001 | return ret; |
| 7002 | } |
| 7003 | |
| 7004 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 7005 | { |
| 7006 | struct io_ring_ctx *ctx; |
| 7007 | unsigned sq_thread_idle = 0; |
| 7008 | |
| 7009 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7010 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 7011 | sq_thread_idle = ctx->sq_thread_idle; |
| 7012 | } |
| 7013 | |
| 7014 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7015 | } |
| 7016 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7017 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 7018 | { |
| 7019 | struct io_ring_ctx *ctx; |
| 7020 | |
| 7021 | while (!list_empty(&sqd->ctx_new_list)) { |
| 7022 | 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] | 7023 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 7024 | complete(&ctx->sq_thread_comp); |
| 7025 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7026 | |
| 7027 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7028 | } |
| 7029 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7030 | static int io_sq_thread(void *data) |
| 7031 | { |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7032 | struct cgroup_subsys_state *cur_css = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7033 | struct files_struct *old_files = current->files; |
| 7034 | struct nsproxy *old_nsproxy = current->nsproxy; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7035 | const struct cred *old_cred = NULL; |
| 7036 | struct io_sq_data *sqd = data; |
| 7037 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7038 | unsigned long timeout = 0; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7039 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7040 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7041 | task_lock(current); |
| 7042 | current->files = NULL; |
| 7043 | current->nsproxy = NULL; |
| 7044 | task_unlock(current); |
| 7045 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7046 | while (!kthread_should_stop()) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7047 | int ret; |
| 7048 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7049 | |
| 7050 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7051 | * Any changes to the sqd lists are synchronized through the |
| 7052 | * kthread parking. This synchronizes the thread vs users, |
| 7053 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7054 | */ |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7055 | if (kthread_should_park()) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7056 | kthread_parkme(); |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 7057 | /* |
| 7058 | * When sq thread is unparked, in case the previous park operation |
| 7059 | * comes from io_put_sq_data(), which means that sq thread is going |
| 7060 | * to be stopped, so here needs to have a check. |
| 7061 | */ |
| 7062 | if (kthread_should_stop()) |
| 7063 | break; |
| 7064 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7065 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7066 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7067 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7068 | timeout = jiffies + sqd->sq_thread_idle; |
| 7069 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7070 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7071 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7072 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7073 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7074 | if (current->cred != ctx->creds) { |
| 7075 | if (old_cred) |
| 7076 | revert_creds(old_cred); |
| 7077 | old_cred = override_creds(ctx->creds); |
| 7078 | } |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7079 | io_sq_thread_associate_blkcg(ctx, &cur_css); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 7080 | #ifdef CONFIG_AUDIT |
| 7081 | current->loginuid = ctx->loginuid; |
| 7082 | current->sessionid = ctx->sessionid; |
| 7083 | #endif |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7084 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7085 | ret = __io_sq_thread(ctx, cap_entries); |
| 7086 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 7087 | sqt_spin = true; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7088 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7089 | io_sq_thread_drop_mm_files(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7090 | } |
| 7091 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7092 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7093 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7094 | io_sq_thread_drop_mm_files(); |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7095 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7096 | if (sqt_spin) |
| 7097 | timeout = jiffies + sqd->sq_thread_idle; |
| 7098 | continue; |
| 7099 | } |
| 7100 | |
| 7101 | if (kthread_should_park()) |
| 7102 | continue; |
| 7103 | |
| 7104 | needs_sched = true; |
| 7105 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 7106 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 7107 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 7108 | !list_empty_careful(&ctx->iopoll_list)) { |
| 7109 | needs_sched = false; |
| 7110 | break; |
| 7111 | } |
| 7112 | if (io_sqring_entries(ctx)) { |
| 7113 | needs_sched = false; |
| 7114 | break; |
| 7115 | } |
| 7116 | } |
| 7117 | |
| 7118 | if (needs_sched) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7119 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7120 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7121 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7122 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7123 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7124 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7125 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7126 | |
| 7127 | finish_wait(&sqd->wait, &wait); |
| 7128 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7129 | } |
| 7130 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7131 | io_run_task_work(); |
Pavel Begunkov | d434ab6 | 2021-01-11 04:00:30 +0000 | [diff] [blame] | 7132 | io_sq_thread_drop_mm_files(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7133 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 7134 | if (cur_css) |
| 7135 | io_sq_thread_unassociate_blkcg(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7136 | if (old_cred) |
| 7137 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7138 | |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7139 | task_lock(current); |
| 7140 | current->files = old_files; |
| 7141 | current->nsproxy = old_nsproxy; |
| 7142 | task_unlock(current); |
| 7143 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7144 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 7145 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7146 | return 0; |
| 7147 | } |
| 7148 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7149 | struct io_wait_queue { |
| 7150 | struct wait_queue_entry wq; |
| 7151 | struct io_ring_ctx *ctx; |
| 7152 | unsigned to_wait; |
| 7153 | unsigned nr_timeouts; |
| 7154 | }; |
| 7155 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7156 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7157 | { |
| 7158 | struct io_ring_ctx *ctx = iowq->ctx; |
| 7159 | |
| 7160 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7161 | * 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] | 7162 | * started waiting. For timeouts, we always want to return to userspace, |
| 7163 | * regardless of event count. |
| 7164 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7165 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7166 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 7167 | } |
| 7168 | |
| 7169 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7170 | int wake_flags, void *key) |
| 7171 | { |
| 7172 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7173 | wq); |
| 7174 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7175 | /* |
| 7176 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7177 | * the task, and the next invocation will do it. |
| 7178 | */ |
| 7179 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 7180 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7181 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7182 | } |
| 7183 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7184 | static int io_run_task_work_sig(void) |
| 7185 | { |
| 7186 | if (io_run_task_work()) |
| 7187 | return 1; |
| 7188 | if (!signal_pending(current)) |
| 7189 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7190 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 7191 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7192 | return -EINTR; |
| 7193 | } |
| 7194 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7195 | /* |
| 7196 | * Wait until events become available, if we don't already have some. The |
| 7197 | * application must reap them itself, as they reside on the shared cq ring. |
| 7198 | */ |
| 7199 | 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] | 7200 | const sigset_t __user *sig, size_t sigsz, |
| 7201 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7202 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7203 | struct io_wait_queue iowq = { |
| 7204 | .wq = { |
| 7205 | .private = current, |
| 7206 | .func = io_wake_function, |
| 7207 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 7208 | }, |
| 7209 | .ctx = ctx, |
| 7210 | .to_wait = min_events, |
| 7211 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7212 | struct io_rings *rings = ctx->rings; |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7213 | struct timespec64 ts; |
| 7214 | signed long timeout = 0; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 7215 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7216 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7217 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7218 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 7219 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7220 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7221 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7222 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7223 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7224 | |
| 7225 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7226 | #ifdef CONFIG_COMPAT |
| 7227 | if (in_compat_syscall()) |
| 7228 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7229 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7230 | else |
| 7231 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7232 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7233 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7234 | if (ret) |
| 7235 | return ret; |
| 7236 | } |
| 7237 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7238 | if (uts) { |
| 7239 | if (get_timespec64(&ts, uts)) |
| 7240 | return -EFAULT; |
| 7241 | timeout = timespec64_to_jiffies(&ts); |
| 7242 | } |
| 7243 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7244 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7245 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7246 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7247 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7248 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7249 | TASK_INTERRUPTIBLE); |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 7250 | /* make sure we run task_work before checking for signals */ |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7251 | ret = io_run_task_work_sig(); |
| 7252 | if (ret > 0) |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7253 | continue; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7254 | else if (ret < 0) |
Jens Axboe | ce593a6 | 2020-06-30 12:39:05 -0600 | [diff] [blame] | 7255 | break; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7256 | if (io_should_wake(&iowq)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7257 | break; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7258 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 7259 | continue; |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7260 | if (uts) { |
| 7261 | timeout = schedule_timeout(timeout); |
| 7262 | if (timeout == 0) { |
| 7263 | ret = -ETIME; |
| 7264 | break; |
| 7265 | } |
| 7266 | } else { |
| 7267 | schedule(); |
| 7268 | } |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7269 | } while (1); |
| 7270 | finish_wait(&ctx->wait, &iowq.wq); |
| 7271 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7272 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7273 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7274 | 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] | 7275 | } |
| 7276 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7277 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7278 | { |
| 7279 | #if defined(CONFIG_UNIX) |
| 7280 | if (ctx->ring_sock) { |
| 7281 | struct sock *sock = ctx->ring_sock->sk; |
| 7282 | struct sk_buff *skb; |
| 7283 | |
| 7284 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7285 | kfree_skb(skb); |
| 7286 | } |
| 7287 | #else |
| 7288 | int i; |
| 7289 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7290 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7291 | struct file *file; |
| 7292 | |
| 7293 | file = io_file_from_index(ctx, i); |
| 7294 | if (file) |
| 7295 | fput(file); |
| 7296 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7297 | #endif |
| 7298 | } |
| 7299 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7300 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 7301 | { |
| 7302 | struct fixed_file_data *data; |
| 7303 | |
| 7304 | data = container_of(ref, struct fixed_file_data, refs); |
| 7305 | complete(&data->done); |
| 7306 | } |
| 7307 | |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7308 | static void io_sqe_files_set_node(struct fixed_file_data *file_data, |
| 7309 | struct fixed_file_ref_node *ref_node) |
| 7310 | { |
| 7311 | spin_lock_bh(&file_data->lock); |
| 7312 | file_data->node = ref_node; |
| 7313 | list_add_tail(&ref_node->node, &file_data->ref_list); |
| 7314 | spin_unlock_bh(&file_data->lock); |
| 7315 | percpu_ref_get(&file_data->refs); |
| 7316 | } |
| 7317 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7318 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7319 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7320 | struct fixed_file_data *data = ctx->file_data; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7321 | struct fixed_file_ref_node *backup_node, *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7322 | unsigned nr_tables, i; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7323 | int ret; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7324 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7325 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7326 | return -ENXIO; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7327 | backup_node = alloc_fixed_file_ref_node(ctx); |
| 7328 | if (!backup_node) |
| 7329 | return -ENOMEM; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7330 | |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7331 | spin_lock_bh(&data->lock); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 7332 | ref_node = data->node; |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7333 | spin_unlock_bh(&data->lock); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7334 | if (ref_node) |
| 7335 | percpu_ref_kill(&ref_node->refs); |
| 7336 | |
| 7337 | percpu_ref_kill(&data->refs); |
| 7338 | |
| 7339 | /* wait for all refs nodes to complete */ |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7340 | flush_delayed_work(&ctx->file_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7341 | do { |
| 7342 | ret = wait_for_completion_interruptible(&data->done); |
| 7343 | if (!ret) |
| 7344 | break; |
| 7345 | ret = io_run_task_work_sig(); |
| 7346 | if (ret < 0) { |
| 7347 | percpu_ref_resurrect(&data->refs); |
| 7348 | reinit_completion(&data->done); |
| 7349 | io_sqe_files_set_node(data, backup_node); |
| 7350 | return ret; |
| 7351 | } |
| 7352 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7353 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7354 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7355 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7356 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7357 | kfree(data->table[i].files); |
| 7358 | kfree(data->table); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7359 | percpu_ref_exit(&data->refs); |
| 7360 | kfree(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7361 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7362 | ctx->nr_user_files = 0; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7363 | destroy_fixed_file_ref_node(backup_node); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7364 | return 0; |
| 7365 | } |
| 7366 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7367 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7368 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7369 | if (refcount_dec_and_test(&sqd->refs)) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 7370 | /* |
| 7371 | * The park is a bit of a work-around, without it we get |
| 7372 | * warning spews on shutdown with SQPOLL set and affinity |
| 7373 | * set to a single CPU. |
| 7374 | */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7375 | if (sqd->thread) { |
| 7376 | kthread_park(sqd->thread); |
| 7377 | kthread_stop(sqd->thread); |
| 7378 | } |
| 7379 | |
| 7380 | kfree(sqd); |
| 7381 | } |
| 7382 | } |
| 7383 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7384 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7385 | { |
| 7386 | struct io_ring_ctx *ctx_attach; |
| 7387 | struct io_sq_data *sqd; |
| 7388 | struct fd f; |
| 7389 | |
| 7390 | f = fdget(p->wq_fd); |
| 7391 | if (!f.file) |
| 7392 | return ERR_PTR(-ENXIO); |
| 7393 | if (f.file->f_op != &io_uring_fops) { |
| 7394 | fdput(f); |
| 7395 | return ERR_PTR(-EINVAL); |
| 7396 | } |
| 7397 | |
| 7398 | ctx_attach = f.file->private_data; |
| 7399 | sqd = ctx_attach->sq_data; |
| 7400 | if (!sqd) { |
| 7401 | fdput(f); |
| 7402 | return ERR_PTR(-EINVAL); |
| 7403 | } |
| 7404 | |
| 7405 | refcount_inc(&sqd->refs); |
| 7406 | fdput(f); |
| 7407 | return sqd; |
| 7408 | } |
| 7409 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7410 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7411 | { |
| 7412 | struct io_sq_data *sqd; |
| 7413 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7414 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7415 | return io_attach_sq_data(p); |
| 7416 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7417 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7418 | if (!sqd) |
| 7419 | return ERR_PTR(-ENOMEM); |
| 7420 | |
| 7421 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7422 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7423 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7424 | mutex_init(&sqd->ctx_lock); |
| 7425 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7426 | init_waitqueue_head(&sqd->wait); |
| 7427 | return sqd; |
| 7428 | } |
| 7429 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7430 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7431 | __releases(&sqd->lock) |
| 7432 | { |
| 7433 | if (!sqd->thread) |
| 7434 | return; |
| 7435 | kthread_unpark(sqd->thread); |
| 7436 | mutex_unlock(&sqd->lock); |
| 7437 | } |
| 7438 | |
| 7439 | static void io_sq_thread_park(struct io_sq_data *sqd) |
| 7440 | __acquires(&sqd->lock) |
| 7441 | { |
| 7442 | if (!sqd->thread) |
| 7443 | return; |
| 7444 | mutex_lock(&sqd->lock); |
| 7445 | kthread_park(sqd->thread); |
| 7446 | } |
| 7447 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7448 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 7449 | { |
| 7450 | struct io_sq_data *sqd = ctx->sq_data; |
| 7451 | |
| 7452 | if (sqd) { |
| 7453 | if (sqd->thread) { |
| 7454 | /* |
| 7455 | * We may arrive here from the error branch in |
| 7456 | * io_sq_offload_create() where the kthread is created |
| 7457 | * without being waked up, thus wake it up now to make |
| 7458 | * sure the wait will complete. |
| 7459 | */ |
| 7460 | wake_up_process(sqd->thread); |
| 7461 | wait_for_completion(&ctx->sq_thread_comp); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7462 | |
| 7463 | io_sq_thread_park(sqd); |
| 7464 | } |
| 7465 | |
| 7466 | mutex_lock(&sqd->ctx_lock); |
| 7467 | list_del(&ctx->sqd_list); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7468 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7469 | mutex_unlock(&sqd->ctx_lock); |
| 7470 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7471 | if (sqd->thread) |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7472 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7473 | |
| 7474 | io_put_sq_data(sqd); |
| 7475 | ctx->sq_data = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7476 | } |
| 7477 | } |
| 7478 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7479 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 7480 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7481 | io_sq_thread_stop(ctx); |
| 7482 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 7483 | if (ctx->io_wq) { |
| 7484 | io_wq_destroy(ctx->io_wq); |
| 7485 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7486 | } |
| 7487 | } |
| 7488 | |
| 7489 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7490 | /* |
| 7491 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7492 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7493 | * loops in the file referencing. |
| 7494 | */ |
| 7495 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7496 | { |
| 7497 | struct sock *sk = ctx->ring_sock->sk; |
| 7498 | struct scm_fp_list *fpl; |
| 7499 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7500 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7501 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7502 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7503 | if (!fpl) |
| 7504 | return -ENOMEM; |
| 7505 | |
| 7506 | skb = alloc_skb(0, GFP_KERNEL); |
| 7507 | if (!skb) { |
| 7508 | kfree(fpl); |
| 7509 | return -ENOMEM; |
| 7510 | } |
| 7511 | |
| 7512 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7513 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7514 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7515 | fpl->user = get_uid(ctx->user); |
| 7516 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7517 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7518 | |
| 7519 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7520 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7521 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7522 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7523 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7524 | } |
| 7525 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7526 | if (nr_files) { |
| 7527 | fpl->max = SCM_MAX_FD; |
| 7528 | fpl->count = nr_files; |
| 7529 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7530 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7531 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7532 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7533 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7534 | for (i = 0; i < nr_files; i++) |
| 7535 | fput(fpl->fp[i]); |
| 7536 | } else { |
| 7537 | kfree_skb(skb); |
| 7538 | kfree(fpl); |
| 7539 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7540 | |
| 7541 | return 0; |
| 7542 | } |
| 7543 | |
| 7544 | /* |
| 7545 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7546 | * causes regular reference counting to break down. We rely on the UNIX |
| 7547 | * garbage collection to take care of this problem for us. |
| 7548 | */ |
| 7549 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7550 | { |
| 7551 | unsigned left, total; |
| 7552 | int ret = 0; |
| 7553 | |
| 7554 | total = 0; |
| 7555 | left = ctx->nr_user_files; |
| 7556 | while (left) { |
| 7557 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7558 | |
| 7559 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7560 | if (ret) |
| 7561 | break; |
| 7562 | left -= this_files; |
| 7563 | total += this_files; |
| 7564 | } |
| 7565 | |
| 7566 | if (!ret) |
| 7567 | return 0; |
| 7568 | |
| 7569 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7570 | struct file *file = io_file_from_index(ctx, total); |
| 7571 | |
| 7572 | if (file) |
| 7573 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7574 | total++; |
| 7575 | } |
| 7576 | |
| 7577 | return ret; |
| 7578 | } |
| 7579 | #else |
| 7580 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7581 | { |
| 7582 | return 0; |
| 7583 | } |
| 7584 | #endif |
| 7585 | |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7586 | static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data, |
| 7587 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7588 | { |
| 7589 | int i; |
| 7590 | |
| 7591 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7592 | struct fixed_file_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7593 | unsigned this_files; |
| 7594 | |
| 7595 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7596 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7597 | GFP_KERNEL); |
| 7598 | if (!table->files) |
| 7599 | break; |
| 7600 | nr_files -= this_files; |
| 7601 | } |
| 7602 | |
| 7603 | if (i == nr_tables) |
| 7604 | return 0; |
| 7605 | |
| 7606 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7607 | struct fixed_file_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7608 | kfree(table->files); |
| 7609 | } |
| 7610 | return 1; |
| 7611 | } |
| 7612 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7613 | 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] | 7614 | { |
| 7615 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7616 | struct sock *sock = ctx->ring_sock->sk; |
| 7617 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7618 | struct sk_buff *skb; |
| 7619 | int i; |
| 7620 | |
| 7621 | __skb_queue_head_init(&list); |
| 7622 | |
| 7623 | /* |
| 7624 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7625 | * remove this entry and rearrange the file array. |
| 7626 | */ |
| 7627 | skb = skb_dequeue(head); |
| 7628 | while (skb) { |
| 7629 | struct scm_fp_list *fp; |
| 7630 | |
| 7631 | fp = UNIXCB(skb).fp; |
| 7632 | for (i = 0; i < fp->count; i++) { |
| 7633 | int left; |
| 7634 | |
| 7635 | if (fp->fp[i] != file) |
| 7636 | continue; |
| 7637 | |
| 7638 | unix_notinflight(fp->user, fp->fp[i]); |
| 7639 | left = fp->count - 1 - i; |
| 7640 | if (left) { |
| 7641 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7642 | left * sizeof(struct file *)); |
| 7643 | } |
| 7644 | fp->count--; |
| 7645 | if (!fp->count) { |
| 7646 | kfree_skb(skb); |
| 7647 | skb = NULL; |
| 7648 | } else { |
| 7649 | __skb_queue_tail(&list, skb); |
| 7650 | } |
| 7651 | fput(file); |
| 7652 | file = NULL; |
| 7653 | break; |
| 7654 | } |
| 7655 | |
| 7656 | if (!file) |
| 7657 | break; |
| 7658 | |
| 7659 | __skb_queue_tail(&list, skb); |
| 7660 | |
| 7661 | skb = skb_dequeue(head); |
| 7662 | } |
| 7663 | |
| 7664 | if (skb_peek(&list)) { |
| 7665 | spin_lock_irq(&head->lock); |
| 7666 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7667 | __skb_queue_tail(head, skb); |
| 7668 | spin_unlock_irq(&head->lock); |
| 7669 | } |
| 7670 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7671 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7672 | #endif |
| 7673 | } |
| 7674 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7675 | struct io_file_put { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7676 | struct list_head list; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7677 | struct file *file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7678 | }; |
| 7679 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7680 | 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] | 7681 | { |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7682 | struct fixed_file_data *file_data = ref_node->file_data; |
| 7683 | struct io_ring_ctx *ctx = file_data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7684 | struct io_file_put *pfile, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7685 | |
| 7686 | list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) { |
Jens Axboe | 6a4d07c | 2020-05-15 14:30:38 -0600 | [diff] [blame] | 7687 | list_del(&pfile->list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7688 | io_ring_file_put(ctx, pfile->file); |
| 7689 | kfree(pfile); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7690 | } |
| 7691 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7692 | percpu_ref_exit(&ref_node->refs); |
| 7693 | kfree(ref_node); |
| 7694 | percpu_ref_put(&file_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7695 | } |
| 7696 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7697 | static void io_file_put_work(struct work_struct *work) |
| 7698 | { |
| 7699 | struct io_ring_ctx *ctx; |
| 7700 | struct llist_node *node; |
| 7701 | |
| 7702 | ctx = container_of(work, struct io_ring_ctx, file_put_work.work); |
| 7703 | node = llist_del_all(&ctx->file_put_llist); |
| 7704 | |
| 7705 | while (node) { |
| 7706 | struct fixed_file_ref_node *ref_node; |
| 7707 | struct llist_node *next = node->next; |
| 7708 | |
| 7709 | ref_node = llist_entry(node, struct fixed_file_ref_node, llist); |
| 7710 | __io_file_put_work(ref_node); |
| 7711 | node = next; |
| 7712 | } |
| 7713 | } |
| 7714 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7715 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 7716 | { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7717 | struct fixed_file_ref_node *ref_node; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7718 | struct fixed_file_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7719 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7720 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7721 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7722 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7723 | ref_node = container_of(ref, struct fixed_file_ref_node, refs); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7724 | data = ref_node->file_data; |
| 7725 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7726 | |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7727 | spin_lock_bh(&data->lock); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7728 | ref_node->done = true; |
| 7729 | |
| 7730 | while (!list_empty(&data->ref_list)) { |
| 7731 | ref_node = list_first_entry(&data->ref_list, |
| 7732 | struct fixed_file_ref_node, node); |
| 7733 | /* recycle ref nodes in order */ |
| 7734 | if (!ref_node->done) |
| 7735 | break; |
| 7736 | list_del(&ref_node->node); |
| 7737 | first_add |= llist_add(&ref_node->llist, &ctx->file_put_llist); |
| 7738 | } |
Jens Axboe | ac0648a | 2020-11-23 09:37:51 -0700 | [diff] [blame] | 7739 | spin_unlock_bh(&data->lock); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7740 | |
| 7741 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7742 | delay = 0; |
| 7743 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7744 | if (!delay) |
| 7745 | mod_delayed_work(system_wq, &ctx->file_put_work, 0); |
| 7746 | else if (first_add) |
| 7747 | queue_delayed_work(system_wq, &ctx->file_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7748 | } |
| 7749 | |
| 7750 | static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| 7751 | struct io_ring_ctx *ctx) |
| 7752 | { |
| 7753 | struct fixed_file_ref_node *ref_node; |
| 7754 | |
| 7755 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7756 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7757 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7758 | |
| 7759 | if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero, |
| 7760 | 0, GFP_KERNEL)) { |
| 7761 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7762 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7763 | } |
| 7764 | INIT_LIST_HEAD(&ref_node->node); |
| 7765 | INIT_LIST_HEAD(&ref_node->file_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7766 | ref_node->file_data = ctx->file_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7767 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7768 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node) |
| 7772 | { |
| 7773 | percpu_ref_exit(&ref_node->refs); |
| 7774 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7778 | unsigned nr_args) |
| 7779 | { |
| 7780 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7781 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7782 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7783 | int fd, ret = -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7784 | struct fixed_file_ref_node *ref_node; |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7785 | struct fixed_file_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7786 | |
| 7787 | if (ctx->file_data) |
| 7788 | return -EBUSY; |
| 7789 | if (!nr_args) |
| 7790 | return -EINVAL; |
| 7791 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7792 | return -EMFILE; |
| 7793 | |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7794 | file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 7795 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7796 | return -ENOMEM; |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7797 | file_data->ctx = ctx; |
| 7798 | init_completion(&file_data->done); |
| 7799 | INIT_LIST_HEAD(&file_data->ref_list); |
| 7800 | spin_lock_init(&file_data->lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7801 | |
| 7802 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7803 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7804 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7805 | if (!file_data->table) |
| 7806 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7807 | |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7808 | if (percpu_ref_init(&file_data->refs, io_file_ref_kill, |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7809 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 7810 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7811 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7812 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
| 7813 | goto out_ref; |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7814 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7815 | |
| 7816 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 7817 | struct fixed_file_table *table; |
| 7818 | unsigned index; |
| 7819 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7820 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7821 | ret = -EFAULT; |
| 7822 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7823 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7824 | /* allow sparse sets */ |
| 7825 | if (fd == -1) |
| 7826 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7827 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7828 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7829 | ret = -EBADF; |
| 7830 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7831 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7832 | |
| 7833 | /* |
| 7834 | * Don't allow io_uring instances to be registered. If UNIX |
| 7835 | * isn't enabled, then this causes a reference cycle and this |
| 7836 | * instance can never get freed. If UNIX is enabled we'll |
| 7837 | * handle it just fine, but there's still no point in allowing |
| 7838 | * a ring fd as it doesn't support regular read/write anyway. |
| 7839 | */ |
| 7840 | if (file->f_op == &io_uring_fops) { |
| 7841 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7842 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7843 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7844 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7845 | index = i & IORING_FILE_TABLE_MASK; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7846 | table->files[index] = file; |
| 7847 | } |
| 7848 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7849 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7850 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7851 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7852 | return ret; |
| 7853 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7854 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7855 | ref_node = alloc_fixed_file_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7856 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7857 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7858 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7859 | } |
| 7860 | |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7861 | io_sqe_files_set_node(file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7862 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7863 | out_fput: |
| 7864 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7865 | file = io_file_from_index(ctx, i); |
| 7866 | if (file) |
| 7867 | fput(file); |
| 7868 | } |
| 7869 | for (i = 0; i < nr_tables; i++) |
| 7870 | kfree(file_data->table[i].files); |
| 7871 | ctx->nr_user_files = 0; |
| 7872 | out_ref: |
| 7873 | percpu_ref_exit(&file_data->refs); |
| 7874 | out_free: |
| 7875 | kfree(file_data->table); |
| 7876 | kfree(file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7877 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7878 | return ret; |
| 7879 | } |
| 7880 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7881 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7882 | int index) |
| 7883 | { |
| 7884 | #if defined(CONFIG_UNIX) |
| 7885 | struct sock *sock = ctx->ring_sock->sk; |
| 7886 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7887 | struct sk_buff *skb; |
| 7888 | |
| 7889 | /* |
| 7890 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7891 | * file set. If there's no room, fall back to allocating a new skb |
| 7892 | * and filling it in. |
| 7893 | */ |
| 7894 | spin_lock_irq(&head->lock); |
| 7895 | skb = skb_peek(head); |
| 7896 | if (skb) { |
| 7897 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7898 | |
| 7899 | if (fpl->count < SCM_MAX_FD) { |
| 7900 | __skb_unlink(skb, head); |
| 7901 | spin_unlock_irq(&head->lock); |
| 7902 | fpl->fp[fpl->count] = get_file(file); |
| 7903 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7904 | fpl->count++; |
| 7905 | spin_lock_irq(&head->lock); |
| 7906 | __skb_queue_head(head, skb); |
| 7907 | } else { |
| 7908 | skb = NULL; |
| 7909 | } |
| 7910 | } |
| 7911 | spin_unlock_irq(&head->lock); |
| 7912 | |
| 7913 | if (skb) { |
| 7914 | fput(file); |
| 7915 | return 0; |
| 7916 | } |
| 7917 | |
| 7918 | return __io_sqe_files_scm(ctx, 1, index); |
| 7919 | #else |
| 7920 | return 0; |
| 7921 | #endif |
| 7922 | } |
| 7923 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7924 | static int io_queue_file_removal(struct fixed_file_data *data, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7925 | struct file *file) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7926 | { |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7927 | struct io_file_put *pfile; |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 7928 | struct fixed_file_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7929 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7930 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7931 | if (!pfile) |
| 7932 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7933 | |
| 7934 | pfile->file = file; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7935 | list_add(&pfile->list, &ref_node->file_list); |
| 7936 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7937 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 7941 | struct io_uring_files_update *up, |
| 7942 | unsigned nr_args) |
| 7943 | { |
| 7944 | struct fixed_file_data *data = ctx->file_data; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7945 | struct fixed_file_ref_node *ref_node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7946 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7947 | __s32 __user *fds; |
| 7948 | int fd, i, err; |
| 7949 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7950 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7951 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7952 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7953 | return -EOVERFLOW; |
| 7954 | if (done > ctx->nr_user_files) |
| 7955 | return -EINVAL; |
| 7956 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7957 | ref_node = alloc_fixed_file_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7958 | if (!ref_node) |
| 7959 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7960 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7961 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7962 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7963 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7964 | struct fixed_file_table *table; |
| 7965 | unsigned index; |
| 7966 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7967 | err = 0; |
| 7968 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7969 | err = -EFAULT; |
| 7970 | break; |
| 7971 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7972 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 7973 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7974 | index = i & IORING_FILE_TABLE_MASK; |
| 7975 | if (table->files[index]) { |
Jiufei Xue | 98dfd50 | 2020-09-01 13:35:02 +0800 | [diff] [blame] | 7976 | file = table->files[index]; |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7977 | err = io_queue_file_removal(data, file); |
| 7978 | if (err) |
| 7979 | break; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7980 | table->files[index] = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7981 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7982 | } |
| 7983 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7984 | file = fget(fd); |
| 7985 | if (!file) { |
| 7986 | err = -EBADF; |
| 7987 | break; |
| 7988 | } |
| 7989 | /* |
| 7990 | * Don't allow io_uring instances to be registered. If |
| 7991 | * UNIX isn't enabled, then this causes a reference |
| 7992 | * cycle and this instance can never get freed. If UNIX |
| 7993 | * is enabled we'll handle it just fine, but there's |
| 7994 | * still no point in allowing a ring fd as it doesn't |
| 7995 | * support regular read/write anyway. |
| 7996 | */ |
| 7997 | if (file->f_op == &io_uring_fops) { |
| 7998 | fput(file); |
| 7999 | err = -EBADF; |
| 8000 | break; |
| 8001 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8002 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8003 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8004 | if (err) { |
Jiufei Xue | 95d1c8e | 2020-09-02 17:59:39 +0800 | [diff] [blame] | 8005 | table->files[index] = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8006 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8007 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8008 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8009 | } |
| 8010 | nr_args--; |
| 8011 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8012 | up->offset++; |
| 8013 | } |
| 8014 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8015 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 8016 | percpu_ref_kill(&data->node->refs); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 8017 | io_sqe_files_set_node(data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8018 | } else |
| 8019 | destroy_fixed_file_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8020 | |
| 8021 | return done ? done : err; |
| 8022 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8023 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8024 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 8025 | unsigned nr_args) |
| 8026 | { |
| 8027 | struct io_uring_files_update up; |
| 8028 | |
| 8029 | if (!ctx->file_data) |
| 8030 | return -ENXIO; |
| 8031 | if (!nr_args) |
| 8032 | return -EINVAL; |
| 8033 | if (copy_from_user(&up, arg, sizeof(up))) |
| 8034 | return -EFAULT; |
| 8035 | if (up.resv) |
| 8036 | return -EINVAL; |
| 8037 | |
| 8038 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 8039 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8040 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8041 | static void io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8042 | { |
| 8043 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8044 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8045 | /* Consider that io_steal_work() relies on this ref */ |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 8046 | io_put_req(req); |
| 8047 | } |
| 8048 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8049 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8050 | struct io_uring_params *p) |
| 8051 | { |
| 8052 | struct io_wq_data data; |
| 8053 | struct fd f; |
| 8054 | struct io_ring_ctx *ctx_attach; |
| 8055 | unsigned int concurrency; |
| 8056 | int ret = 0; |
| 8057 | |
| 8058 | data.user = ctx->user; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 8059 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8060 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8061 | |
| 8062 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 8063 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8064 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 8065 | |
| 8066 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 8067 | if (IS_ERR(ctx->io_wq)) { |
| 8068 | ret = PTR_ERR(ctx->io_wq); |
| 8069 | ctx->io_wq = NULL; |
| 8070 | } |
| 8071 | return ret; |
| 8072 | } |
| 8073 | |
| 8074 | f = fdget(p->wq_fd); |
| 8075 | if (!f.file) |
| 8076 | return -EBADF; |
| 8077 | |
| 8078 | if (f.file->f_op != &io_uring_fops) { |
| 8079 | ret = -EINVAL; |
| 8080 | goto out_fput; |
| 8081 | } |
| 8082 | |
| 8083 | ctx_attach = f.file->private_data; |
| 8084 | /* @io_wq is protected by holding the fd */ |
| 8085 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 8086 | ret = -EINVAL; |
| 8087 | goto out_fput; |
| 8088 | } |
| 8089 | |
| 8090 | ctx->io_wq = ctx_attach->io_wq; |
| 8091 | out_fput: |
| 8092 | fdput(f); |
| 8093 | return ret; |
| 8094 | } |
| 8095 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8096 | static int io_uring_alloc_task_context(struct task_struct *task) |
| 8097 | { |
| 8098 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8099 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8100 | |
| 8101 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 8102 | if (unlikely(!tctx)) |
| 8103 | return -ENOMEM; |
| 8104 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8105 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8106 | if (unlikely(ret)) { |
| 8107 | kfree(tctx); |
| 8108 | return ret; |
| 8109 | } |
| 8110 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8111 | xa_init(&tctx->xa); |
| 8112 | init_waitqueue_head(&tctx->wait); |
| 8113 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8114 | atomic_set(&tctx->in_idle, 0); |
| 8115 | tctx->sqpoll = false; |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8116 | io_init_identity(&tctx->__identity); |
| 8117 | tctx->identity = &tctx->__identity; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8118 | task->io_uring = tctx; |
| 8119 | return 0; |
| 8120 | } |
| 8121 | |
| 8122 | void __io_uring_free(struct task_struct *tsk) |
| 8123 | { |
| 8124 | struct io_uring_task *tctx = tsk->io_uring; |
| 8125 | |
| 8126 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | 500a373 | 2020-10-15 17:38:03 -0600 | [diff] [blame] | 8127 | WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1); |
| 8128 | if (tctx->identity != &tctx->__identity) |
| 8129 | kfree(tctx->identity); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8130 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8131 | kfree(tctx); |
| 8132 | tsk->io_uring = NULL; |
| 8133 | } |
| 8134 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8135 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8136 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8137 | { |
| 8138 | int ret; |
| 8139 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8140 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8141 | struct io_sq_data *sqd; |
| 8142 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8143 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 8144 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 8145 | goto err; |
| 8146 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8147 | sqd = io_get_sq_data(p); |
| 8148 | if (IS_ERR(sqd)) { |
| 8149 | ret = PTR_ERR(sqd); |
| 8150 | goto err; |
| 8151 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8152 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8153 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8154 | io_sq_thread_park(sqd); |
| 8155 | mutex_lock(&sqd->ctx_lock); |
| 8156 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 8157 | mutex_unlock(&sqd->ctx_lock); |
| 8158 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8159 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8160 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8161 | if (!ctx->sq_thread_idle) |
| 8162 | ctx->sq_thread_idle = HZ; |
| 8163 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8164 | if (sqd->thread) |
| 8165 | goto done; |
| 8166 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8167 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8168 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8169 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8170 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8171 | if (cpu >= nr_cpu_ids) |
| 8172 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 8173 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8174 | goto err; |
| 8175 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8176 | sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd, |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8177 | cpu, "io_uring-sq"); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8178 | } else { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8179 | sqd->thread = kthread_create(io_sq_thread, sqd, |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8180 | "io_uring-sq"); |
| 8181 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8182 | if (IS_ERR(sqd->thread)) { |
| 8183 | ret = PTR_ERR(sqd->thread); |
| 8184 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8185 | goto err; |
| 8186 | } |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8187 | ret = io_uring_alloc_task_context(sqd->thread); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8188 | if (ret) |
| 8189 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8190 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8191 | /* Can't have SQ_AFF without SQPOLL */ |
| 8192 | ret = -EINVAL; |
| 8193 | goto err; |
| 8194 | } |
| 8195 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8196 | done: |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8197 | ret = io_init_wq_offload(ctx, p); |
| 8198 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8199 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8200 | |
| 8201 | return 0; |
| 8202 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 8203 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8204 | return ret; |
| 8205 | } |
| 8206 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8207 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 8208 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8209 | struct io_sq_data *sqd = ctx->sq_data; |
| 8210 | |
| 8211 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
| 8212 | wake_up_process(sqd->thread); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 8213 | } |
| 8214 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8215 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8216 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8217 | { |
| 8218 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8219 | } |
| 8220 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8221 | static inline int __io_account_mem(struct user_struct *user, |
| 8222 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8223 | { |
| 8224 | unsigned long page_limit, cur_pages, new_pages; |
| 8225 | |
| 8226 | /* Don't allow more pages than we can safely lock */ |
| 8227 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8228 | |
| 8229 | do { |
| 8230 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8231 | new_pages = cur_pages + nr_pages; |
| 8232 | if (new_pages > page_limit) |
| 8233 | return -ENOMEM; |
| 8234 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8235 | new_pages) != cur_pages); |
| 8236 | |
| 8237 | return 0; |
| 8238 | } |
| 8239 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8240 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 8241 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8242 | { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 8243 | if (ctx->limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8244 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8245 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8246 | if (ctx->mm_account) { |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8247 | if (acct == ACCT_LOCKED) { |
| 8248 | mmap_write_lock(ctx->mm_account); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8249 | ctx->mm_account->locked_vm -= nr_pages; |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8250 | mmap_write_unlock(ctx->mm_account); |
| 8251 | }else if (acct == ACCT_PINNED) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8252 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8253 | } |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8254 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8255 | } |
| 8256 | |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8257 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages, |
| 8258 | enum io_mem_account acct) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8259 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8260 | int ret; |
| 8261 | |
| 8262 | if (ctx->limit_mem) { |
| 8263 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8264 | if (ret) |
| 8265 | return ret; |
| 8266 | } |
| 8267 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8268 | if (ctx->mm_account) { |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8269 | if (acct == ACCT_LOCKED) { |
| 8270 | mmap_write_lock(ctx->mm_account); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8271 | ctx->mm_account->locked_vm += nr_pages; |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8272 | mmap_write_unlock(ctx->mm_account); |
| 8273 | } else if (acct == ACCT_PINNED) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8274 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Jens Axboe | 4bc4a91 | 2020-12-17 07:53:33 -0700 | [diff] [blame] | 8275 | } |
Bijan Mottahedeh | 2e0464d | 2020-06-16 16:36:10 -0700 | [diff] [blame] | 8276 | } |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8277 | |
| 8278 | return 0; |
| 8279 | } |
| 8280 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8281 | static void io_mem_free(void *ptr) |
| 8282 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8283 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8284 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8285 | if (!ptr) |
| 8286 | return; |
| 8287 | |
| 8288 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8289 | if (put_page_testzero(page)) |
| 8290 | free_compound_page(page); |
| 8291 | } |
| 8292 | |
| 8293 | static void *io_mem_alloc(size_t size) |
| 8294 | { |
| 8295 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 8296 | __GFP_NORETRY; |
| 8297 | |
| 8298 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8299 | } |
| 8300 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8301 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8302 | size_t *sq_offset) |
| 8303 | { |
| 8304 | struct io_rings *rings; |
| 8305 | size_t off, sq_array_size; |
| 8306 | |
| 8307 | off = struct_size(rings, cqes, cq_entries); |
| 8308 | if (off == SIZE_MAX) |
| 8309 | return SIZE_MAX; |
| 8310 | |
| 8311 | #ifdef CONFIG_SMP |
| 8312 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8313 | if (off == 0) |
| 8314 | return SIZE_MAX; |
| 8315 | #endif |
| 8316 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8317 | if (sq_offset) |
| 8318 | *sq_offset = off; |
| 8319 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8320 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8321 | if (sq_array_size == SIZE_MAX) |
| 8322 | return SIZE_MAX; |
| 8323 | |
| 8324 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8325 | return SIZE_MAX; |
| 8326 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8327 | return off; |
| 8328 | } |
| 8329 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8330 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 8331 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8332 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8333 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8334 | pages = (size_t)1 << get_order( |
| 8335 | rings_size(sq_entries, cq_entries, NULL)); |
| 8336 | pages += (size_t)1 << get_order( |
| 8337 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8338 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8339 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8340 | } |
| 8341 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8342 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 8343 | { |
| 8344 | int i, j; |
| 8345 | |
| 8346 | if (!ctx->user_bufs) |
| 8347 | return -ENXIO; |
| 8348 | |
| 8349 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8350 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8351 | |
| 8352 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8353 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8354 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8355 | if (imu->acct_pages) |
| 8356 | io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8357 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8358 | imu->nr_bvecs = 0; |
| 8359 | } |
| 8360 | |
| 8361 | kfree(ctx->user_bufs); |
| 8362 | ctx->user_bufs = NULL; |
| 8363 | ctx->nr_user_bufs = 0; |
| 8364 | return 0; |
| 8365 | } |
| 8366 | |
| 8367 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8368 | void __user *arg, unsigned index) |
| 8369 | { |
| 8370 | struct iovec __user *src; |
| 8371 | |
| 8372 | #ifdef CONFIG_COMPAT |
| 8373 | if (ctx->compat) { |
| 8374 | struct compat_iovec __user *ciovs; |
| 8375 | struct compat_iovec ciov; |
| 8376 | |
| 8377 | ciovs = (struct compat_iovec __user *) arg; |
| 8378 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8379 | return -EFAULT; |
| 8380 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8381 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8382 | dst->iov_len = ciov.iov_len; |
| 8383 | return 0; |
| 8384 | } |
| 8385 | #endif |
| 8386 | src = (struct iovec __user *) arg; |
| 8387 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8388 | return -EFAULT; |
| 8389 | return 0; |
| 8390 | } |
| 8391 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8392 | /* |
| 8393 | * Not super efficient, but this is just a registration time. And we do cache |
| 8394 | * the last compound head, so generally we'll only do a full search if we don't |
| 8395 | * match that one. |
| 8396 | * |
| 8397 | * We check if the given compound head page has already been accounted, to |
| 8398 | * avoid double accounting it. This allows us to account the full size of the |
| 8399 | * page, not just the constituent pages of a huge page. |
| 8400 | */ |
| 8401 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8402 | int nr_pages, struct page *hpage) |
| 8403 | { |
| 8404 | int i, j; |
| 8405 | |
| 8406 | /* check current page array */ |
| 8407 | for (i = 0; i < nr_pages; i++) { |
| 8408 | if (!PageCompound(pages[i])) |
| 8409 | continue; |
| 8410 | if (compound_head(pages[i]) == hpage) |
| 8411 | return true; |
| 8412 | } |
| 8413 | |
| 8414 | /* check previously registered pages */ |
| 8415 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8416 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8417 | |
| 8418 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8419 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8420 | continue; |
| 8421 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8422 | return true; |
| 8423 | } |
| 8424 | } |
| 8425 | |
| 8426 | return false; |
| 8427 | } |
| 8428 | |
| 8429 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8430 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8431 | struct page **last_hpage) |
| 8432 | { |
| 8433 | int i, ret; |
| 8434 | |
| 8435 | for (i = 0; i < nr_pages; i++) { |
| 8436 | if (!PageCompound(pages[i])) { |
| 8437 | imu->acct_pages++; |
| 8438 | } else { |
| 8439 | struct page *hpage; |
| 8440 | |
| 8441 | hpage = compound_head(pages[i]); |
| 8442 | if (hpage == *last_hpage) |
| 8443 | continue; |
| 8444 | *last_hpage = hpage; |
| 8445 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8446 | continue; |
| 8447 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8448 | } |
| 8449 | } |
| 8450 | |
| 8451 | if (!imu->acct_pages) |
| 8452 | return 0; |
| 8453 | |
| 8454 | ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED); |
| 8455 | if (ret) |
| 8456 | imu->acct_pages = 0; |
| 8457 | return ret; |
| 8458 | } |
| 8459 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8460 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8461 | unsigned nr_args) |
| 8462 | { |
| 8463 | struct vm_area_struct **vmas = NULL; |
| 8464 | struct page **pages = NULL; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8465 | struct page *last_hpage = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8466 | int i, j, got_pages = 0; |
| 8467 | int ret = -EINVAL; |
| 8468 | |
| 8469 | if (ctx->user_bufs) |
| 8470 | return -EBUSY; |
| 8471 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8472 | return -EINVAL; |
| 8473 | |
| 8474 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8475 | GFP_KERNEL); |
| 8476 | if (!ctx->user_bufs) |
| 8477 | return -ENOMEM; |
| 8478 | |
| 8479 | for (i = 0; i < nr_args; i++) { |
| 8480 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8481 | unsigned long off, start, end, ubuf; |
| 8482 | int pret, nr_pages; |
| 8483 | struct iovec iov; |
| 8484 | size_t size; |
| 8485 | |
| 8486 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8487 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 8488 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8489 | |
| 8490 | /* |
| 8491 | * Don't impose further limits on the size and buffer |
| 8492 | * constraints here, we'll -EINVAL later when IO is |
| 8493 | * submitted if they are wrong. |
| 8494 | */ |
| 8495 | ret = -EFAULT; |
| 8496 | if (!iov.iov_base || !iov.iov_len) |
| 8497 | goto err; |
| 8498 | |
| 8499 | /* arbitrary limit, but we need something */ |
| 8500 | if (iov.iov_len > SZ_1G) |
| 8501 | goto err; |
| 8502 | |
| 8503 | ubuf = (unsigned long) iov.iov_base; |
| 8504 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8505 | start = ubuf >> PAGE_SHIFT; |
| 8506 | nr_pages = end - start; |
| 8507 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8508 | ret = 0; |
| 8509 | if (!pages || nr_pages > got_pages) { |
Denis Efremov | a8c73c1 | 2020-06-05 12:32:03 +0300 | [diff] [blame] | 8510 | kvfree(vmas); |
| 8511 | kvfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8512 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8513 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8514 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8515 | sizeof(struct vm_area_struct *), |
| 8516 | GFP_KERNEL); |
| 8517 | if (!pages || !vmas) { |
| 8518 | ret = -ENOMEM; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8519 | goto err; |
| 8520 | } |
| 8521 | got_pages = nr_pages; |
| 8522 | } |
| 8523 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8524 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8525 | GFP_KERNEL); |
| 8526 | ret = -ENOMEM; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8527 | if (!imu->bvec) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8528 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8529 | |
| 8530 | ret = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 8531 | mmap_read_lock(current->mm); |
John Hubbard | 2113b05 | 2020-01-30 22:13:13 -0800 | [diff] [blame] | 8532 | pret = pin_user_pages(ubuf, nr_pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 8533 | FOLL_WRITE | FOLL_LONGTERM, |
| 8534 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8535 | if (pret == nr_pages) { |
| 8536 | /* don't support file backed memory */ |
| 8537 | for (j = 0; j < nr_pages; j++) { |
| 8538 | struct vm_area_struct *vma = vmas[j]; |
| 8539 | |
| 8540 | if (vma->vm_file && |
| 8541 | !is_file_hugepages(vma->vm_file)) { |
| 8542 | ret = -EOPNOTSUPP; |
| 8543 | break; |
| 8544 | } |
| 8545 | } |
| 8546 | } else { |
| 8547 | ret = pret < 0 ? pret : -EFAULT; |
| 8548 | } |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 8549 | mmap_read_unlock(current->mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8550 | if (ret) { |
| 8551 | /* |
| 8552 | * if we did partial map, or found file backed vmas, |
| 8553 | * release any pages we did get |
| 8554 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 8555 | if (pret > 0) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8556 | unpin_user_pages(pages, pret); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8557 | kvfree(imu->bvec); |
| 8558 | goto err; |
| 8559 | } |
| 8560 | |
| 8561 | ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage); |
| 8562 | if (ret) { |
| 8563 | unpin_user_pages(pages, pret); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8564 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8565 | goto err; |
| 8566 | } |
| 8567 | |
| 8568 | off = ubuf & ~PAGE_MASK; |
| 8569 | size = iov.iov_len; |
| 8570 | for (j = 0; j < nr_pages; j++) { |
| 8571 | size_t vec_len; |
| 8572 | |
| 8573 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8574 | imu->bvec[j].bv_page = pages[j]; |
| 8575 | imu->bvec[j].bv_len = vec_len; |
| 8576 | imu->bvec[j].bv_offset = off; |
| 8577 | off = 0; |
| 8578 | size -= vec_len; |
| 8579 | } |
| 8580 | /* store original address for later verification */ |
| 8581 | imu->ubuf = ubuf; |
| 8582 | imu->len = iov.iov_len; |
| 8583 | imu->nr_bvecs = nr_pages; |
| 8584 | |
| 8585 | ctx->nr_user_bufs++; |
| 8586 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8587 | kvfree(pages); |
| 8588 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8589 | return 0; |
| 8590 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8591 | kvfree(pages); |
| 8592 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8593 | io_sqe_buffer_unregister(ctx); |
| 8594 | return ret; |
| 8595 | } |
| 8596 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8597 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8598 | { |
| 8599 | __s32 __user *fds = arg; |
| 8600 | int fd; |
| 8601 | |
| 8602 | if (ctx->cq_ev_fd) |
| 8603 | return -EBUSY; |
| 8604 | |
| 8605 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8606 | return -EFAULT; |
| 8607 | |
| 8608 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8609 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8610 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8611 | ctx->cq_ev_fd = NULL; |
| 8612 | return ret; |
| 8613 | } |
| 8614 | |
| 8615 | return 0; |
| 8616 | } |
| 8617 | |
| 8618 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8619 | { |
| 8620 | if (ctx->cq_ev_fd) { |
| 8621 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8622 | ctx->cq_ev_fd = NULL; |
| 8623 | return 0; |
| 8624 | } |
| 8625 | |
| 8626 | return -ENXIO; |
| 8627 | } |
| 8628 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8629 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8630 | { |
| 8631 | struct io_ring_ctx *ctx = data; |
| 8632 | struct io_buffer *buf = p; |
| 8633 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8634 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8635 | return 0; |
| 8636 | } |
| 8637 | |
| 8638 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8639 | { |
| 8640 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8641 | idr_destroy(&ctx->io_buffer_idr); |
| 8642 | } |
| 8643 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8644 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8645 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8646 | io_finish_async(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8647 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8648 | |
| 8649 | if (ctx->sqo_task) { |
| 8650 | put_task_struct(ctx->sqo_task); |
| 8651 | ctx->sqo_task = NULL; |
| 8652 | mmdrop(ctx->mm_account); |
| 8653 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8654 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8655 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 8656 | #ifdef CONFIG_BLK_CGROUP |
| 8657 | if (ctx->sqo_blkcg_css) |
| 8658 | css_put(ctx->sqo_blkcg_css); |
| 8659 | #endif |
| 8660 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8661 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8662 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8663 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8664 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8665 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8666 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8667 | if (ctx->ring_sock) { |
| 8668 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8669 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8670 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8671 | #endif |
| 8672 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8673 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8674 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8675 | |
| 8676 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8677 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 8678 | put_cred(ctx->creds); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8679 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 8680 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8681 | kfree(ctx); |
| 8682 | } |
| 8683 | |
| 8684 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8685 | { |
| 8686 | struct io_ring_ctx *ctx = file->private_data; |
| 8687 | __poll_t mask = 0; |
| 8688 | |
| 8689 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8690 | /* |
| 8691 | * synchronizes with barrier from wq_has_sleeper call in |
| 8692 | * io_commit_cqring |
| 8693 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8694 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8695 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8696 | mask |= EPOLLOUT | EPOLLWRNORM; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8697 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 8698 | if (io_cqring_events(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8699 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8700 | |
| 8701 | return mask; |
| 8702 | } |
| 8703 | |
| 8704 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8705 | { |
| 8706 | struct io_ring_ctx *ctx = file->private_data; |
| 8707 | |
| 8708 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8709 | } |
| 8710 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8711 | static int io_remove_personalities(int id, void *p, void *data) |
| 8712 | { |
| 8713 | struct io_ring_ctx *ctx = data; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8714 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8715 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8716 | iod = idr_remove(&ctx->personality_idr, id); |
| 8717 | if (iod) { |
| 8718 | put_cred(iod->creds); |
| 8719 | if (refcount_dec_and_test(&iod->count)) |
| 8720 | kfree(iod); |
| 8721 | } |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8722 | return 0; |
| 8723 | } |
| 8724 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8725 | static void io_ring_exit_work(struct work_struct *work) |
| 8726 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8727 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8728 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8729 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8730 | /* |
| 8731 | * If we're doing polled IO and end up having requests being |
| 8732 | * submitted async (out-of-line), then completions can come in while |
| 8733 | * we're waiting for refs to drop. We need to reap these manually, |
| 8734 | * as nobody else will be looking for them. |
| 8735 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8736 | do { |
Pavel Begunkov | 90df085 | 2021-01-04 20:43:30 +0000 | [diff] [blame] | 8737 | __io_uring_cancel_task_requests(ctx, NULL); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8738 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8739 | io_ring_ctx_free(ctx); |
| 8740 | } |
| 8741 | |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8742 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8743 | { |
| 8744 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8745 | |
| 8746 | return req->ctx == data; |
| 8747 | } |
| 8748 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8749 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8750 | { |
| 8751 | mutex_lock(&ctx->uring_lock); |
| 8752 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8753 | |
| 8754 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8755 | ctx->sqo_dead = 1; |
| 8756 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8757 | /* if force is set, the ring is going away. always drop after that */ |
| 8758 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8759 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8760 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8761 | mutex_unlock(&ctx->uring_lock); |
| 8762 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8763 | io_kill_timeouts(ctx, NULL, NULL); |
| 8764 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8765 | |
| 8766 | if (ctx->io_wq) |
Jens Axboe | 00c1864 | 2020-12-20 10:45:02 -0700 | [diff] [blame] | 8767 | 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] | 8768 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8769 | /* 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] | 8770 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8771 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8772 | |
| 8773 | /* |
| 8774 | * Do this upfront, so we won't have a grace period where the ring |
| 8775 | * is closed but resources aren't reaped yet. This can cause |
| 8776 | * spurious failure in setting up a new ring. |
| 8777 | */ |
Jens Axboe | 760618f | 2020-07-24 12:53:31 -0600 | [diff] [blame] | 8778 | io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries), |
| 8779 | ACCT_LOCKED); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8780 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8781 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8782 | /* |
| 8783 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8784 | * if we're exiting a ton of rings at the same time. It just adds |
| 8785 | * noise and overhead, there's no discernable change in runtime |
| 8786 | * over using system_wq. |
| 8787 | */ |
| 8788 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8789 | } |
| 8790 | |
| 8791 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8792 | { |
| 8793 | struct io_ring_ctx *ctx = file->private_data; |
| 8794 | |
| 8795 | file->private_data = NULL; |
| 8796 | io_ring_ctx_wait_and_kill(ctx); |
| 8797 | return 0; |
| 8798 | } |
| 8799 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8800 | struct io_task_cancel { |
| 8801 | struct task_struct *task; |
| 8802 | struct files_struct *files; |
| 8803 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8804 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8805 | 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] | 8806 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8807 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8808 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8809 | bool ret; |
| 8810 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8811 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8812 | unsigned long flags; |
| 8813 | struct io_ring_ctx *ctx = req->ctx; |
| 8814 | |
| 8815 | /* protect against races with linked timeouts */ |
| 8816 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8817 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8818 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8819 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8820 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8821 | } |
| 8822 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8823 | } |
| 8824 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8825 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8826 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8827 | struct files_struct *files) |
| 8828 | { |
| 8829 | struct io_defer_entry *de = NULL; |
| 8830 | LIST_HEAD(list); |
| 8831 | |
| 8832 | spin_lock_irq(&ctx->completion_lock); |
| 8833 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8834 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8835 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8836 | break; |
| 8837 | } |
| 8838 | } |
| 8839 | spin_unlock_irq(&ctx->completion_lock); |
| 8840 | |
| 8841 | while (!list_empty(&list)) { |
| 8842 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8843 | list_del_init(&de->list); |
| 8844 | req_set_fail_links(de->req); |
| 8845 | io_put_req(de->req); |
| 8846 | io_req_complete(de->req, -ECANCELED); |
| 8847 | kfree(de); |
| 8848 | } |
| 8849 | } |
| 8850 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8851 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8852 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8853 | struct files_struct *files) |
| 8854 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8855 | while (!list_empty_careful(&ctx->inflight_list)) { |
Pavel Begunkov | bee749b | 2020-11-25 02:19:23 +0000 | [diff] [blame] | 8856 | struct io_task_cancel cancel = { .task = task, .files = files }; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8857 | struct io_kiocb *req; |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8858 | DEFINE_WAIT(wait); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8859 | bool found = false; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8860 | |
| 8861 | spin_lock_irq(&ctx->inflight_lock); |
| 8862 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Pavel Begunkov | bee749b | 2020-11-25 02:19:23 +0000 | [diff] [blame] | 8863 | if (req->task != task || |
Jens Axboe | 98447d6 | 2020-10-14 10:48:51 -0600 | [diff] [blame] | 8864 | req->work.identity->files != files) |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8865 | continue; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8866 | found = true; |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8867 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8868 | } |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8869 | if (found) |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8870 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 8871 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8872 | spin_unlock_irq(&ctx->inflight_lock); |
| 8873 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 8874 | /* 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] | 8875 | if (!found) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8876 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8877 | |
| 8878 | io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); |
| 8879 | io_poll_remove_all(ctx, task, files); |
| 8880 | io_kill_timeouts(ctx, task, files); |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 8881 | /* cancellations _may_ trigger task work */ |
| 8882 | io_run_task_work(); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8883 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8884 | finish_wait(&task->io_uring->wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8885 | } |
| 8886 | } |
| 8887 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8888 | static void __io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8889 | struct task_struct *task) |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8890 | { |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8891 | while (1) { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8892 | struct io_task_cancel cancel = { .task = task, .files = NULL, }; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8893 | enum io_wq_cancel cret; |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8894 | bool ret = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8895 | |
Pavel Begunkov | 90df085 | 2021-01-04 20:43:30 +0000 | [diff] [blame] | 8896 | if (ctx->io_wq) { |
| 8897 | cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, |
| 8898 | &cancel, true); |
| 8899 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8900 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8901 | |
| 8902 | /* SQPOLL thread does its own polling */ |
| 8903 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { |
| 8904 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8905 | io_iopoll_try_reap_events(ctx); |
| 8906 | ret = true; |
| 8907 | } |
| 8908 | } |
| 8909 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8910 | ret |= io_poll_remove_all(ctx, task, NULL); |
| 8911 | ret |= io_kill_timeouts(ctx, task, NULL); |
Pavel Begunkov | 55583d7 | 2020-12-20 13:21:43 +0000 | [diff] [blame] | 8912 | ret |= io_run_task_work(); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8913 | if (!ret) |
| 8914 | break; |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8915 | cond_resched(); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8916 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8917 | } |
| 8918 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8919 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 8920 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8921 | mutex_lock(&ctx->uring_lock); |
| 8922 | ctx->sqo_dead = 1; |
| 8923 | mutex_unlock(&ctx->uring_lock); |
| 8924 | |
| 8925 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 8926 | if (ctx->rings) |
| 8927 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8928 | } |
| 8929 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8930 | /* |
| 8931 | * We need to iteratively cancel requests, in case a request has dependent |
| 8932 | * hard links. These persist even for failure of cancelations, hence keep |
| 8933 | * looping until none are found. |
| 8934 | */ |
| 8935 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8936 | struct files_struct *files) |
| 8937 | { |
| 8938 | struct task_struct *task = current; |
| 8939 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8940 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8941 | /* for SQPOLL only sqo_task has task notes */ |
Pavel Begunkov | 6b393a1 | 2021-01-16 05:32:29 +0000 | [diff] [blame] | 8942 | WARN_ON_ONCE(ctx->sqo_task != current); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8943 | io_disable_sqo_submit(ctx); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8944 | task = ctx->sq_data->thread; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8945 | atomic_inc(&task->io_uring->in_idle); |
| 8946 | io_sq_thread_park(ctx->sq_data); |
| 8947 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8948 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8949 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8950 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8951 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8952 | if (!files) |
| 8953 | __io_uring_cancel_task_requests(ctx, task); |
Pavel Begunkov | bee749b | 2020-11-25 02:19:23 +0000 | [diff] [blame] | 8954 | else |
| 8955 | io_uring_cancel_files(ctx, task, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8956 | |
| 8957 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
| 8958 | atomic_dec(&task->io_uring->in_idle); |
| 8959 | /* |
| 8960 | * If the files that are going away are the ones in the thread |
| 8961 | * identity, clear them out. |
| 8962 | */ |
| 8963 | if (task->io_uring->identity->files == files) |
| 8964 | task->io_uring->identity->files = NULL; |
| 8965 | io_sq_thread_unpark(ctx->sq_data); |
| 8966 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8967 | } |
| 8968 | |
| 8969 | /* |
| 8970 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8971 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8972 | 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] | 8973 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8974 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8975 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8976 | |
| 8977 | if (unlikely(!tctx)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8978 | ret = io_uring_alloc_task_context(current); |
| 8979 | if (unlikely(ret)) |
| 8980 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8981 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8982 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8983 | if (tctx->last != file) { |
| 8984 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8985 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8986 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8987 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8988 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 8989 | file, GFP_KERNEL)); |
| 8990 | if (ret) { |
| 8991 | fput(file); |
| 8992 | return ret; |
| 8993 | } |
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 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8996 | } |
| 8997 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8998 | /* |
| 8999 | * This is race safe in that the task itself is doing this, hence it |
| 9000 | * cannot be going through the exit/cancel paths at the same time. |
| 9001 | * This cannot be modified while exit/cancel is running. |
| 9002 | */ |
| 9003 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9004 | tctx->sqpoll = true; |
| 9005 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9006 | return 0; |
| 9007 | } |
| 9008 | |
| 9009 | /* |
| 9010 | * Remove this io_uring_file -> task mapping. |
| 9011 | */ |
| 9012 | static void io_uring_del_task_file(struct file *file) |
| 9013 | { |
| 9014 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9015 | |
| 9016 | if (tctx->last == file) |
| 9017 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 9018 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9019 | if (file) |
| 9020 | fput(file); |
| 9021 | } |
| 9022 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9023 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 9024 | { |
| 9025 | struct file *file; |
| 9026 | unsigned long index; |
| 9027 | |
| 9028 | xa_for_each(&tctx->xa, index, file) |
| 9029 | io_uring_del_task_file(file); |
| 9030 | } |
| 9031 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9032 | void __io_uring_files_cancel(struct files_struct *files) |
| 9033 | { |
| 9034 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 9035 | struct file *file; |
| 9036 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9037 | |
| 9038 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9039 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9040 | xa_for_each(&tctx->xa, index, file) |
| 9041 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9042 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9043 | |
| 9044 | if (files) |
| 9045 | io_uring_remove_task_files(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9046 | } |
| 9047 | |
| 9048 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 9049 | { |
| 9050 | unsigned long index; |
| 9051 | struct file *file; |
| 9052 | s64 inflight; |
| 9053 | |
| 9054 | inflight = percpu_counter_sum(&tctx->inflight); |
| 9055 | if (!tctx->sqpoll) |
| 9056 | return inflight; |
| 9057 | |
| 9058 | /* |
| 9059 | * If we have SQPOLL rings, then we need to iterate and find them, and |
| 9060 | * add the pending count for those. |
| 9061 | */ |
| 9062 | xa_for_each(&tctx->xa, index, file) { |
| 9063 | struct io_ring_ctx *ctx = file->private_data; |
| 9064 | |
| 9065 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9066 | struct io_uring_task *__tctx = ctx->sqo_task->io_uring; |
| 9067 | |
| 9068 | inflight += percpu_counter_sum(&__tctx->inflight); |
| 9069 | } |
| 9070 | } |
| 9071 | |
| 9072 | return inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9073 | } |
| 9074 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9075 | /* |
| 9076 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 9077 | * requests. |
| 9078 | */ |
| 9079 | void __io_uring_task_cancel(void) |
| 9080 | { |
| 9081 | struct io_uring_task *tctx = current->io_uring; |
| 9082 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9083 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9084 | |
| 9085 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9086 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9087 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9088 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9089 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9090 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9091 | if (!inflight) |
| 9092 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9093 | __io_uring_files_cancel(NULL); |
| 9094 | |
| 9095 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9096 | |
| 9097 | /* |
| 9098 | * If we've seen completions, retry. This avoids a race where |
| 9099 | * a completion comes in before we did prepare_to_wait(). |
| 9100 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9101 | if (inflight != tctx_inflight(tctx)) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9102 | continue; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9103 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9104 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9105 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9106 | |
Jens Axboe | a8d13db | 2021-01-15 16:04:23 -0700 | [diff] [blame] | 9107 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9108 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9109 | |
| 9110 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9111 | } |
| 9112 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9113 | static int io_uring_flush(struct file *file, void *data) |
| 9114 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9115 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9116 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9117 | |
| 9118 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9119 | return 0; |
| 9120 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9121 | /* we should have cancelled and erased it before PF_EXITING */ |
| 9122 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 9123 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9124 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9125 | /* |
| 9126 | * fput() is pending, will be 2 if the only other ref is our potential |
| 9127 | * task file note. If the task is exiting, drop regardless of count. |
| 9128 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 9129 | if (atomic_long_read(&file->f_count) != 2) |
| 9130 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 9131 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9132 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 9133 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame^] | 9134 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 9135 | xa_load(&tctx->xa, (unsigned long)file)); |
| 9136 | /* sqo_dead check is for when this happens after cancellation */ |
| 9137 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9138 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 9139 | |
| 9140 | io_disable_sqo_submit(ctx); |
| 9141 | } |
| 9142 | |
| 9143 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 9144 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9145 | return 0; |
| 9146 | } |
| 9147 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9148 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9149 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9150 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9151 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9152 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9153 | struct page *page; |
| 9154 | void *ptr; |
| 9155 | |
| 9156 | switch (offset) { |
| 9157 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9158 | case IORING_OFF_CQ_RING: |
| 9159 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9160 | break; |
| 9161 | case IORING_OFF_SQES: |
| 9162 | ptr = ctx->sq_sqes; |
| 9163 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9164 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9165 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9166 | } |
| 9167 | |
| 9168 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9169 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9170 | return ERR_PTR(-EINVAL); |
| 9171 | |
| 9172 | return ptr; |
| 9173 | } |
| 9174 | |
| 9175 | #ifdef CONFIG_MMU |
| 9176 | |
| 9177 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9178 | { |
| 9179 | size_t sz = vma->vm_end - vma->vm_start; |
| 9180 | unsigned long pfn; |
| 9181 | void *ptr; |
| 9182 | |
| 9183 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9184 | if (IS_ERR(ptr)) |
| 9185 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9186 | |
| 9187 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9188 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9189 | } |
| 9190 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9191 | #else /* !CONFIG_MMU */ |
| 9192 | |
| 9193 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9194 | { |
| 9195 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9196 | } |
| 9197 | |
| 9198 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9199 | { |
| 9200 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9201 | } |
| 9202 | |
| 9203 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9204 | unsigned long addr, unsigned long len, |
| 9205 | unsigned long pgoff, unsigned long flags) |
| 9206 | { |
| 9207 | void *ptr; |
| 9208 | |
| 9209 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9210 | if (IS_ERR(ptr)) |
| 9211 | return PTR_ERR(ptr); |
| 9212 | |
| 9213 | return (unsigned long) ptr; |
| 9214 | } |
| 9215 | |
| 9216 | #endif /* !CONFIG_MMU */ |
| 9217 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9218 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9219 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9220 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9221 | DEFINE_WAIT(wait); |
| 9222 | |
| 9223 | do { |
| 9224 | if (!io_sqring_full(ctx)) |
| 9225 | break; |
| 9226 | |
| 9227 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9228 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9229 | if (unlikely(ctx->sqo_dead)) { |
| 9230 | ret = -EOWNERDEAD; |
| 9231 | goto out; |
| 9232 | } |
| 9233 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9234 | if (!io_sqring_full(ctx)) |
| 9235 | break; |
| 9236 | |
| 9237 | schedule(); |
| 9238 | } while (!signal_pending(current)); |
| 9239 | |
| 9240 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9241 | out: |
| 9242 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9243 | } |
| 9244 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9245 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9246 | struct __kernel_timespec __user **ts, |
| 9247 | const sigset_t __user **sig) |
| 9248 | { |
| 9249 | struct io_uring_getevents_arg arg; |
| 9250 | |
| 9251 | /* |
| 9252 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9253 | * is just a pointer to the sigset_t. |
| 9254 | */ |
| 9255 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9256 | *sig = (const sigset_t __user *) argp; |
| 9257 | *ts = NULL; |
| 9258 | return 0; |
| 9259 | } |
| 9260 | |
| 9261 | /* |
| 9262 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9263 | * timespec and sigset_t pointers if good. |
| 9264 | */ |
| 9265 | if (*argsz != sizeof(arg)) |
| 9266 | return -EINVAL; |
| 9267 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9268 | return -EFAULT; |
| 9269 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9270 | *argsz = arg.sigmask_sz; |
| 9271 | *ts = u64_to_user_ptr(arg.ts); |
| 9272 | return 0; |
| 9273 | } |
| 9274 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9275 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9276 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9277 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9278 | { |
| 9279 | struct io_ring_ctx *ctx; |
| 9280 | long ret = -EBADF; |
| 9281 | int submitted = 0; |
| 9282 | struct fd f; |
| 9283 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9284 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9285 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9286 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9287 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9288 | return -EINVAL; |
| 9289 | |
| 9290 | f = fdget(fd); |
| 9291 | if (!f.file) |
| 9292 | return -EBADF; |
| 9293 | |
| 9294 | ret = -EOPNOTSUPP; |
| 9295 | if (f.file->f_op != &io_uring_fops) |
| 9296 | goto out_fput; |
| 9297 | |
| 9298 | ret = -ENXIO; |
| 9299 | ctx = f.file->private_data; |
| 9300 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9301 | goto out_fput; |
| 9302 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9303 | ret = -EBADFD; |
| 9304 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9305 | goto out; |
| 9306 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9307 | /* |
| 9308 | * For SQ polling, the thread will do all submissions and completions. |
| 9309 | * Just return the requested submit count, and wake the thread if |
| 9310 | * we were asked to. |
| 9311 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9312 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9313 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9314 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9315 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9316 | ret = -EOWNERDEAD; |
| 9317 | if (unlikely(ctx->sqo_dead)) |
| 9318 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9319 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9320 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9321 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9322 | ret = io_sqpoll_wait_sq(ctx); |
| 9323 | if (ret) |
| 9324 | goto out; |
| 9325 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9326 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9327 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9328 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9329 | if (unlikely(ret)) |
| 9330 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9331 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9332 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9333 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9334 | |
| 9335 | if (submitted != to_submit) |
| 9336 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9337 | } |
| 9338 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9339 | const sigset_t __user *sig; |
| 9340 | struct __kernel_timespec __user *ts; |
| 9341 | |
| 9342 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9343 | if (unlikely(ret)) |
| 9344 | goto out; |
| 9345 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9346 | min_complete = min(min_complete, ctx->cq_entries); |
| 9347 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9348 | /* |
| 9349 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9350 | * space applications don't need to do io completion events |
| 9351 | * polling again, they can rely on io_sq_thread to do polling |
| 9352 | * work, which can reduce cpu usage and uring_lock contention. |
| 9353 | */ |
| 9354 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9355 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9356 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9357 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9358 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9359 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9360 | } |
| 9361 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9362 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9363 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9364 | out_fput: |
| 9365 | fdput(f); |
| 9366 | return submitted ? submitted : ret; |
| 9367 | } |
| 9368 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9369 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9370 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9371 | { |
Jens Axboe | 6b47ab8 | 2020-11-05 09:50:16 -0700 | [diff] [blame] | 9372 | struct io_identity *iod = p; |
| 9373 | const struct cred *cred = iod->creds; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9374 | struct seq_file *m = data; |
| 9375 | struct user_namespace *uns = seq_user_ns(m); |
| 9376 | struct group_info *gi; |
| 9377 | kernel_cap_t cap; |
| 9378 | unsigned __capi; |
| 9379 | int g; |
| 9380 | |
| 9381 | seq_printf(m, "%5d\n", id); |
| 9382 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9383 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9384 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9385 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9386 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9387 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9388 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9389 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9390 | seq_puts(m, "\n\tGroups:\t"); |
| 9391 | gi = cred->group_info; |
| 9392 | for (g = 0; g < gi->ngroups; g++) { |
| 9393 | seq_put_decimal_ull(m, g ? " " : "", |
| 9394 | from_kgid_munged(uns, gi->gid[g])); |
| 9395 | } |
| 9396 | seq_puts(m, "\n\tCapEff:\t"); |
| 9397 | cap = cred->cap_effective; |
| 9398 | CAP_FOR_EACH_U32(__capi) |
| 9399 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9400 | seq_putc(m, '\n'); |
| 9401 | return 0; |
| 9402 | } |
| 9403 | |
| 9404 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9405 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9406 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9407 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9408 | int i; |
| 9409 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9410 | /* |
| 9411 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9412 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9413 | * cases. If we fail to get the lock, we just don't iterate any |
| 9414 | * structures that could be going away outside the io_uring mutex. |
| 9415 | */ |
| 9416 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9417 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9418 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9419 | sq = ctx->sq_data; |
| 9420 | |
| 9421 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9422 | 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] | 9423 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9424 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9425 | struct fixed_file_table *table; |
| 9426 | struct file *f; |
| 9427 | |
| 9428 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 9429 | f = table->files[i & IORING_FILE_TABLE_MASK]; |
| 9430 | if (f) |
| 9431 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9432 | else |
| 9433 | seq_printf(m, "%5u: <none>\n", i); |
| 9434 | } |
| 9435 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9436 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9437 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9438 | |
| 9439 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9440 | (unsigned int) buf->len); |
| 9441 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9442 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9443 | seq_printf(m, "Personalities:\n"); |
| 9444 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9445 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9446 | seq_printf(m, "PollList:\n"); |
| 9447 | spin_lock_irq(&ctx->completion_lock); |
| 9448 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9449 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9450 | struct io_kiocb *req; |
| 9451 | |
| 9452 | hlist_for_each_entry(req, list, hash_node) |
| 9453 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9454 | req->task->task_works != NULL); |
| 9455 | } |
| 9456 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9457 | if (has_lock) |
| 9458 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9459 | } |
| 9460 | |
| 9461 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9462 | { |
| 9463 | struct io_ring_ctx *ctx = f->private_data; |
| 9464 | |
| 9465 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9466 | __io_uring_show_fdinfo(ctx, m); |
| 9467 | percpu_ref_put(&ctx->refs); |
| 9468 | } |
| 9469 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9470 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9471 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9472 | static const struct file_operations io_uring_fops = { |
| 9473 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9474 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9475 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9476 | #ifndef CONFIG_MMU |
| 9477 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9478 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9479 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9480 | .poll = io_uring_poll, |
| 9481 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9482 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9483 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9484 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9485 | }; |
| 9486 | |
| 9487 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9488 | struct io_uring_params *p) |
| 9489 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9490 | struct io_rings *rings; |
| 9491 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9492 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9493 | /* make sure these are sane, as we already accounted them */ |
| 9494 | ctx->sq_entries = p->sq_entries; |
| 9495 | ctx->cq_entries = p->cq_entries; |
| 9496 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9497 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9498 | if (size == SIZE_MAX) |
| 9499 | return -EOVERFLOW; |
| 9500 | |
| 9501 | rings = io_mem_alloc(size); |
| 9502 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9503 | return -ENOMEM; |
| 9504 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9505 | ctx->rings = rings; |
| 9506 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9507 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9508 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9509 | rings->sq_ring_entries = p->sq_entries; |
| 9510 | rings->cq_ring_entries = p->cq_entries; |
| 9511 | ctx->sq_mask = rings->sq_ring_mask; |
| 9512 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9513 | |
| 9514 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9515 | if (size == SIZE_MAX) { |
| 9516 | io_mem_free(ctx->rings); |
| 9517 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9518 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9519 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9520 | |
| 9521 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9522 | if (!ctx->sq_sqes) { |
| 9523 | io_mem_free(ctx->rings); |
| 9524 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9525 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9526 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9527 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9528 | return 0; |
| 9529 | } |
| 9530 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9531 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9532 | { |
| 9533 | int ret, fd; |
| 9534 | |
| 9535 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9536 | if (fd < 0) |
| 9537 | return fd; |
| 9538 | |
| 9539 | ret = io_uring_add_task_file(ctx, file); |
| 9540 | if (ret) { |
| 9541 | put_unused_fd(fd); |
| 9542 | return ret; |
| 9543 | } |
| 9544 | fd_install(fd, file); |
| 9545 | return fd; |
| 9546 | } |
| 9547 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9548 | /* |
| 9549 | * Allocate an anonymous fd, this is what constitutes the application |
| 9550 | * visible backing of an io_uring instance. The application mmaps this |
| 9551 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9552 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9553 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9554 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9555 | { |
| 9556 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9557 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9558 | int ret; |
| 9559 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9560 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9561 | &ctx->ring_sock); |
| 9562 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9563 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9564 | #endif |
| 9565 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9566 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9567 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9568 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9569 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9570 | sock_release(ctx->ring_sock); |
| 9571 | ctx->ring_sock = NULL; |
| 9572 | } else { |
| 9573 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9574 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9575 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9576 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9577 | } |
| 9578 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9579 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9580 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9581 | { |
| 9582 | struct user_struct *user = NULL; |
| 9583 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9584 | struct file *file; |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9585 | bool limit_mem; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9586 | int ret; |
| 9587 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9588 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9589 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9590 | if (entries > IORING_MAX_ENTRIES) { |
| 9591 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9592 | return -EINVAL; |
| 9593 | entries = IORING_MAX_ENTRIES; |
| 9594 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9595 | |
| 9596 | /* |
| 9597 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9598 | * application to drive a higher depth than the size of the SQ ring, |
| 9599 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9600 | * some flexibility in overcommitting a bit. If the application has |
| 9601 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9602 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9603 | */ |
| 9604 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9605 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9606 | /* |
| 9607 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9608 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9609 | * any cq vs sq ring sizing. |
| 9610 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9611 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9612 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9613 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9614 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9615 | return -EINVAL; |
| 9616 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9617 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9618 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9619 | if (p->cq_entries < p->sq_entries) |
| 9620 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9621 | } else { |
| 9622 | p->cq_entries = 2 * p->sq_entries; |
| 9623 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9624 | |
| 9625 | user = get_uid(current_user()); |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9626 | limit_mem = !capable(CAP_IPC_LOCK); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9627 | |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9628 | if (limit_mem) { |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9629 | ret = __io_account_mem(user, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9630 | ring_pages(p->sq_entries, p->cq_entries)); |
| 9631 | if (ret) { |
| 9632 | free_uid(user); |
| 9633 | return ret; |
| 9634 | } |
| 9635 | } |
| 9636 | |
| 9637 | ctx = io_ring_ctx_alloc(p); |
| 9638 | if (!ctx) { |
Bijan Mottahedeh | aad5d8d | 2020-06-16 16:36:08 -0700 | [diff] [blame] | 9639 | if (limit_mem) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 9640 | __io_unaccount_mem(user, ring_pages(p->sq_entries, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9641 | p->cq_entries)); |
| 9642 | free_uid(user); |
| 9643 | return -ENOMEM; |
| 9644 | } |
| 9645 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9646 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 9647 | ctx->creds = get_current_cred(); |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 9648 | #ifdef CONFIG_AUDIT |
| 9649 | ctx->loginuid = current->loginuid; |
| 9650 | ctx->sessionid = current->sessionid; |
| 9651 | #endif |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9652 | ctx->sqo_task = get_task_struct(current); |
| 9653 | |
| 9654 | /* |
| 9655 | * This is just grabbed for accounting purposes. When a process exits, |
| 9656 | * the mm is exited and dropped before the files, hence we need to hang |
| 9657 | * on to this mm purely for the purposes of being able to unaccount |
| 9658 | * memory (locked/pinned vm). It's not used for anything else. |
| 9659 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9660 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9661 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9662 | |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 9663 | #ifdef CONFIG_BLK_CGROUP |
| 9664 | /* |
| 9665 | * The sq thread will belong to the original cgroup it was inited in. |
| 9666 | * If the cgroup goes offline (e.g. disabling the io controller), then |
| 9667 | * issued bios will be associated with the closest cgroup later in the |
| 9668 | * block layer. |
| 9669 | */ |
| 9670 | rcu_read_lock(); |
| 9671 | ctx->sqo_blkcg_css = blkcg_css(); |
| 9672 | ret = css_tryget_online(ctx->sqo_blkcg_css); |
| 9673 | rcu_read_unlock(); |
| 9674 | if (!ret) { |
| 9675 | /* don't init against a dying cgroup, have the user try again */ |
| 9676 | ctx->sqo_blkcg_css = NULL; |
| 9677 | ret = -ENODEV; |
| 9678 | goto err; |
| 9679 | } |
| 9680 | #endif |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9681 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9682 | /* |
| 9683 | * Account memory _before_ installing the file descriptor. Once |
| 9684 | * the descriptor is installed, it can get closed at any time. Also |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9685 | * do this before hitting the general error path, as ring freeing |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9686 | * will un-account as well. |
| 9687 | */ |
| 9688 | io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries), |
| 9689 | ACCT_LOCKED); |
| 9690 | ctx->limit_mem = limit_mem; |
| 9691 | |
| 9692 | ret = io_allocate_scq_urings(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9693 | if (ret) |
| 9694 | goto err; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9695 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9696 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9697 | if (ret) |
| 9698 | goto err; |
| 9699 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9700 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9701 | io_sq_offload_start(ctx); |
| 9702 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9703 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
| 9704 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9705 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9706 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9707 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9708 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9709 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9710 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
| 9711 | |
| 9712 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9713 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9714 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9715 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9716 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9717 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9718 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9719 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9720 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9721 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9722 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9723 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9724 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
| 9725 | IORING_FEAT_EXT_ARG; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9726 | |
| 9727 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9728 | ret = -EFAULT; |
| 9729 | goto err; |
| 9730 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9731 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9732 | file = io_uring_get_file(ctx); |
| 9733 | if (IS_ERR(file)) { |
| 9734 | ret = PTR_ERR(file); |
| 9735 | goto err; |
| 9736 | } |
| 9737 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9738 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9739 | * Install ring fd as the very last thing, so we don't risk someone |
| 9740 | * having closed it before we finish setup |
| 9741 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9742 | ret = io_uring_install_fd(ctx, file); |
| 9743 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9744 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9745 | /* fput will clean it up */ |
| 9746 | fput(file); |
| 9747 | return ret; |
| 9748 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9749 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9750 | 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] | 9751 | return ret; |
| 9752 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9753 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9754 | io_ring_ctx_wait_and_kill(ctx); |
| 9755 | return ret; |
| 9756 | } |
| 9757 | |
| 9758 | /* |
| 9759 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9760 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9761 | * params structure passed in. |
| 9762 | */ |
| 9763 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9764 | { |
| 9765 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9766 | int i; |
| 9767 | |
| 9768 | if (copy_from_user(&p, params, sizeof(p))) |
| 9769 | return -EFAULT; |
| 9770 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9771 | if (p.resv[i]) |
| 9772 | return -EINVAL; |
| 9773 | } |
| 9774 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9775 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9776 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9777 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9778 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9779 | return -EINVAL; |
| 9780 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9781 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9782 | } |
| 9783 | |
| 9784 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9785 | struct io_uring_params __user *, params) |
| 9786 | { |
| 9787 | return io_uring_setup(entries, params); |
| 9788 | } |
| 9789 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9790 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9791 | { |
| 9792 | struct io_uring_probe *p; |
| 9793 | size_t size; |
| 9794 | int i, ret; |
| 9795 | |
| 9796 | size = struct_size(p, ops, nr_args); |
| 9797 | if (size == SIZE_MAX) |
| 9798 | return -EOVERFLOW; |
| 9799 | p = kzalloc(size, GFP_KERNEL); |
| 9800 | if (!p) |
| 9801 | return -ENOMEM; |
| 9802 | |
| 9803 | ret = -EFAULT; |
| 9804 | if (copy_from_user(p, arg, size)) |
| 9805 | goto out; |
| 9806 | ret = -EINVAL; |
| 9807 | if (memchr_inv(p, 0, size)) |
| 9808 | goto out; |
| 9809 | |
| 9810 | p->last_op = IORING_OP_LAST - 1; |
| 9811 | if (nr_args > IORING_OP_LAST) |
| 9812 | nr_args = IORING_OP_LAST; |
| 9813 | |
| 9814 | for (i = 0; i < nr_args; i++) { |
| 9815 | p->ops[i].op = i; |
| 9816 | if (!io_op_defs[i].not_supported) |
| 9817 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9818 | } |
| 9819 | p->ops_len = i; |
| 9820 | |
| 9821 | ret = 0; |
| 9822 | if (copy_to_user(arg, p, size)) |
| 9823 | ret = -EFAULT; |
| 9824 | out: |
| 9825 | kfree(p); |
| 9826 | return ret; |
| 9827 | } |
| 9828 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9829 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9830 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9831 | struct io_identity *id; |
| 9832 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9833 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9834 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 9835 | if (unlikely(!id)) |
| 9836 | return -ENOMEM; |
| 9837 | |
| 9838 | io_init_identity(id); |
| 9839 | id->creds = get_current_cred(); |
| 9840 | |
| 9841 | ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); |
| 9842 | if (ret < 0) { |
| 9843 | put_cred(id->creds); |
| 9844 | kfree(id); |
| 9845 | } |
| 9846 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9847 | } |
| 9848 | |
| 9849 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 9850 | { |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9851 | struct io_identity *iod; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9852 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9853 | iod = idr_remove(&ctx->personality_idr, id); |
| 9854 | if (iod) { |
| 9855 | put_cred(iod->creds); |
| 9856 | if (refcount_dec_and_test(&iod->count)) |
| 9857 | kfree(iod); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9858 | return 0; |
| 9859 | } |
| 9860 | |
| 9861 | return -EINVAL; |
| 9862 | } |
| 9863 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9864 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9865 | unsigned int nr_args) |
| 9866 | { |
| 9867 | struct io_uring_restriction *res; |
| 9868 | size_t size; |
| 9869 | int i, ret; |
| 9870 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9871 | /* Restrictions allowed only if rings started disabled */ |
| 9872 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9873 | return -EBADFD; |
| 9874 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9875 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9876 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9877 | return -EBUSY; |
| 9878 | |
| 9879 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9880 | return -EINVAL; |
| 9881 | |
| 9882 | size = array_size(nr_args, sizeof(*res)); |
| 9883 | if (size == SIZE_MAX) |
| 9884 | return -EOVERFLOW; |
| 9885 | |
| 9886 | res = memdup_user(arg, size); |
| 9887 | if (IS_ERR(res)) |
| 9888 | return PTR_ERR(res); |
| 9889 | |
| 9890 | ret = 0; |
| 9891 | |
| 9892 | for (i = 0; i < nr_args; i++) { |
| 9893 | switch (res[i].opcode) { |
| 9894 | case IORING_RESTRICTION_REGISTER_OP: |
| 9895 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9896 | ret = -EINVAL; |
| 9897 | goto out; |
| 9898 | } |
| 9899 | |
| 9900 | __set_bit(res[i].register_op, |
| 9901 | ctx->restrictions.register_op); |
| 9902 | break; |
| 9903 | case IORING_RESTRICTION_SQE_OP: |
| 9904 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9905 | ret = -EINVAL; |
| 9906 | goto out; |
| 9907 | } |
| 9908 | |
| 9909 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9910 | break; |
| 9911 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9912 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9913 | break; |
| 9914 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9915 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9916 | break; |
| 9917 | default: |
| 9918 | ret = -EINVAL; |
| 9919 | goto out; |
| 9920 | } |
| 9921 | } |
| 9922 | |
| 9923 | out: |
| 9924 | /* Reset all restrictions if an error happened */ |
| 9925 | if (ret != 0) |
| 9926 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9927 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9928 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9929 | |
| 9930 | kfree(res); |
| 9931 | return ret; |
| 9932 | } |
| 9933 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9934 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9935 | { |
| 9936 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9937 | return -EBADFD; |
| 9938 | |
| 9939 | if (ctx->restrictions.registered) |
| 9940 | ctx->restricted = 1; |
| 9941 | |
| 9942 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9943 | |
| 9944 | io_sq_offload_start(ctx); |
| 9945 | |
| 9946 | return 0; |
| 9947 | } |
| 9948 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9949 | static bool io_register_op_must_quiesce(int op) |
| 9950 | { |
| 9951 | switch (op) { |
| 9952 | case IORING_UNREGISTER_FILES: |
| 9953 | case IORING_REGISTER_FILES_UPDATE: |
| 9954 | case IORING_REGISTER_PROBE: |
| 9955 | case IORING_REGISTER_PERSONALITY: |
| 9956 | case IORING_UNREGISTER_PERSONALITY: |
| 9957 | return false; |
| 9958 | default: |
| 9959 | return true; |
| 9960 | } |
| 9961 | } |
| 9962 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9963 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9964 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9965 | __releases(ctx->uring_lock) |
| 9966 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9967 | { |
| 9968 | int ret; |
| 9969 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9970 | /* |
| 9971 | * We're inside the ring mutex, if the ref is already dying, then |
| 9972 | * someone else killed the ctx or is already going through |
| 9973 | * io_uring_register(). |
| 9974 | */ |
| 9975 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9976 | return -ENXIO; |
| 9977 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9978 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9979 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9980 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9981 | /* |
| 9982 | * Drop uring mutex before waiting for references to exit. If |
| 9983 | * another thread is currently inside io_uring_enter() it might |
| 9984 | * need to grab the uring_lock to make progress. If we hold it |
| 9985 | * here across the drain wait, then we can deadlock. It's safe |
| 9986 | * to drop the mutex here, since no new references will come in |
| 9987 | * after we've killed the percpu ref. |
| 9988 | */ |
| 9989 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9990 | do { |
| 9991 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 9992 | if (!ret) |
| 9993 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 9994 | ret = io_run_task_work_sig(); |
| 9995 | if (ret < 0) |
| 9996 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9997 | } while (1); |
| 9998 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9999 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 10000 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10001 | if (ret) { |
| 10002 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10003 | goto out_quiesce; |
| 10004 | } |
| 10005 | } |
| 10006 | |
| 10007 | if (ctx->restricted) { |
| 10008 | if (opcode >= IORING_REGISTER_LAST) { |
| 10009 | ret = -EINVAL; |
| 10010 | goto out; |
| 10011 | } |
| 10012 | |
| 10013 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 10014 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 10015 | goto out; |
| 10016 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10017 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10018 | |
| 10019 | switch (opcode) { |
| 10020 | case IORING_REGISTER_BUFFERS: |
| 10021 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 10022 | break; |
| 10023 | case IORING_UNREGISTER_BUFFERS: |
| 10024 | ret = -EINVAL; |
| 10025 | if (arg || nr_args) |
| 10026 | break; |
| 10027 | ret = io_sqe_buffer_unregister(ctx); |
| 10028 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10029 | case IORING_REGISTER_FILES: |
| 10030 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 10031 | break; |
| 10032 | case IORING_UNREGISTER_FILES: |
| 10033 | ret = -EINVAL; |
| 10034 | if (arg || nr_args) |
| 10035 | break; |
| 10036 | ret = io_sqe_files_unregister(ctx); |
| 10037 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10038 | case IORING_REGISTER_FILES_UPDATE: |
| 10039 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 10040 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10041 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10042 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10043 | ret = -EINVAL; |
| 10044 | if (nr_args != 1) |
| 10045 | break; |
| 10046 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10047 | if (ret) |
| 10048 | break; |
| 10049 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10050 | ctx->eventfd_async = 1; |
| 10051 | else |
| 10052 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10053 | break; |
| 10054 | case IORING_UNREGISTER_EVENTFD: |
| 10055 | ret = -EINVAL; |
| 10056 | if (arg || nr_args) |
| 10057 | break; |
| 10058 | ret = io_eventfd_unregister(ctx); |
| 10059 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10060 | case IORING_REGISTER_PROBE: |
| 10061 | ret = -EINVAL; |
| 10062 | if (!arg || nr_args > 256) |
| 10063 | break; |
| 10064 | ret = io_probe(ctx, arg, nr_args); |
| 10065 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10066 | case IORING_REGISTER_PERSONALITY: |
| 10067 | ret = -EINVAL; |
| 10068 | if (arg || nr_args) |
| 10069 | break; |
| 10070 | ret = io_register_personality(ctx); |
| 10071 | break; |
| 10072 | case IORING_UNREGISTER_PERSONALITY: |
| 10073 | ret = -EINVAL; |
| 10074 | if (arg) |
| 10075 | break; |
| 10076 | ret = io_unregister_personality(ctx, nr_args); |
| 10077 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10078 | case IORING_REGISTER_ENABLE_RINGS: |
| 10079 | ret = -EINVAL; |
| 10080 | if (arg || nr_args) |
| 10081 | break; |
| 10082 | ret = io_register_enable_rings(ctx); |
| 10083 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10084 | case IORING_REGISTER_RESTRICTIONS: |
| 10085 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 10086 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10087 | default: |
| 10088 | ret = -EINVAL; |
| 10089 | break; |
| 10090 | } |
| 10091 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10092 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10093 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10094 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10095 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10096 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 10097 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10098 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10099 | return ret; |
| 10100 | } |
| 10101 | |
| 10102 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 10103 | void __user *, arg, unsigned int, nr_args) |
| 10104 | { |
| 10105 | struct io_ring_ctx *ctx; |
| 10106 | long ret = -EBADF; |
| 10107 | struct fd f; |
| 10108 | |
| 10109 | f = fdget(fd); |
| 10110 | if (!f.file) |
| 10111 | return -EBADF; |
| 10112 | |
| 10113 | ret = -EOPNOTSUPP; |
| 10114 | if (f.file->f_op != &io_uring_fops) |
| 10115 | goto out_fput; |
| 10116 | |
| 10117 | ctx = f.file->private_data; |
| 10118 | |
| 10119 | mutex_lock(&ctx->uring_lock); |
| 10120 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 10121 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10122 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 10123 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10124 | out_fput: |
| 10125 | fdput(f); |
| 10126 | return ret; |
| 10127 | } |
| 10128 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10129 | static int __init io_uring_init(void) |
| 10130 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10131 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 10132 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 10133 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 10134 | } while (0) |
| 10135 | |
| 10136 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 10137 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 10138 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 10139 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 10140 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 10141 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 10142 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 10143 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 10144 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 10145 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10146 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10147 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 10148 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 10149 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 10150 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 10151 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10152 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 10153 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10154 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 10155 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 10156 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 10157 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 10158 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 10159 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 10160 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 10161 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10162 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10163 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 10164 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 10165 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 10166 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 10167 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 10168 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 10169 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10170 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 10171 | return 0; |
| 10172 | }; |
| 10173 | __initcall(io_uring_init); |