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 | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 73 | #include <linux/namei.h> |
| 74 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 75 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 76 | #include <linux/eventpoll.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 77 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 78 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 79 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 80 | #include <linux/io_uring.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 81 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 82 | #define CREATE_TRACE_POINTS |
| 83 | #include <trace/events/io_uring.h> |
| 84 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | #include <uapi/linux/io_uring.h> |
| 86 | |
| 87 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 88 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 89 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 90 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 91 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 95 | */ |
| 96 | #define IORING_FILE_TABLE_SHIFT 9 |
| 97 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 98 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 99 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 100 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 101 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 102 | |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 103 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 104 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 105 | IOSQE_BUFFER_SELECT) |
| 106 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 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 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 190 | enum io_uring_cmd_flags { |
| 191 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 192 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 195 | struct io_mapped_ubuf { |
| 196 | u64 ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 197 | u64 ubuf_end; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 198 | struct bio_vec *bvec; |
| 199 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 200 | unsigned long acct_pages; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 203 | struct io_ring_ctx; |
| 204 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 205 | struct io_overflow_cqe { |
| 206 | struct io_uring_cqe cqe; |
| 207 | struct list_head list; |
| 208 | }; |
| 209 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 210 | struct io_fixed_file { |
| 211 | /* file * with additional FFS_* flags */ |
| 212 | unsigned long file_ptr; |
| 213 | }; |
| 214 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 215 | struct io_rsrc_put { |
| 216 | struct list_head list; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 217 | union { |
| 218 | void *rsrc; |
| 219 | struct file *file; |
| 220 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 223 | struct io_file_table { |
| 224 | /* two level table */ |
| 225 | struct io_fixed_file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 228 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 229 | struct percpu_ref refs; |
| 230 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 231 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 232 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 233 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 234 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 235 | }; |
| 236 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 237 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 238 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 239 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 240 | struct io_ring_ctx *ctx; |
| 241 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 242 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 243 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 244 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 245 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 248 | struct io_buffer { |
| 249 | struct list_head list; |
| 250 | __u64 addr; |
| 251 | __s32 len; |
| 252 | __u16 bid; |
| 253 | }; |
| 254 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 255 | struct io_restriction { |
| 256 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 257 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 258 | u8 sqe_flags_allowed; |
| 259 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 260 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 261 | }; |
| 262 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 263 | enum { |
| 264 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 265 | IO_SQ_THREAD_SHOULD_PARK, |
| 266 | }; |
| 267 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 268 | struct io_sq_data { |
| 269 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 270 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 271 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 272 | |
| 273 | /* ctx's that are using this sqd */ |
| 274 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 275 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 276 | struct task_struct *thread; |
| 277 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 278 | |
| 279 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 280 | int sq_cpu; |
| 281 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 282 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 283 | |
| 284 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 285 | struct completion exited; |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 286 | struct callback_head *park_task_work; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 287 | }; |
| 288 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 289 | #define IO_IOPOLL_BATCH 8 |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 290 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 291 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 292 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 293 | |
| 294 | struct io_comp_state { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 295 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 296 | unsigned int nr; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 297 | unsigned int locked_free_nr; |
| 298 | /* inline/task_work completion list, under ->uring_lock */ |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 299 | struct list_head free_list; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 300 | /* IRQ completion list, under ->completion_lock */ |
| 301 | struct list_head locked_free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 304 | struct io_submit_link { |
| 305 | struct io_kiocb *head; |
| 306 | struct io_kiocb *last; |
| 307 | }; |
| 308 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 309 | struct io_submit_state { |
| 310 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 311 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * io_kiocb alloc cache |
| 315 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 316 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 317 | unsigned int free_reqs; |
| 318 | |
| 319 | bool plug_started; |
| 320 | |
| 321 | /* |
| 322 | * Batch completion logic |
| 323 | */ |
| 324 | struct io_comp_state comp; |
| 325 | |
| 326 | /* |
| 327 | * File reference cache |
| 328 | */ |
| 329 | struct file *file; |
| 330 | unsigned int fd; |
| 331 | unsigned int file_refs; |
| 332 | unsigned int ios_left; |
| 333 | }; |
| 334 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 335 | struct io_ring_ctx { |
| 336 | struct { |
| 337 | struct percpu_ref refs; |
| 338 | } ____cacheline_aligned_in_smp; |
| 339 | |
| 340 | struct { |
| 341 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 342 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 343 | unsigned int drain_next: 1; |
| 344 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 345 | unsigned int restricted: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 346 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 347 | /* |
| 348 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 349 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 350 | * |
| 351 | * This indirection could e.g. be used to assign fixed |
| 352 | * io_uring_sqe entries to operations and only submit them to |
| 353 | * the queue when needed. |
| 354 | * |
| 355 | * The kernel modifies neither the indices array nor the entries |
| 356 | * array. |
| 357 | */ |
| 358 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 359 | unsigned cached_sq_head; |
| 360 | unsigned sq_entries; |
| 361 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 362 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 363 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 364 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 365 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 366 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 367 | /* hashed buffered write serialization */ |
| 368 | struct io_wq_hash *hash_map; |
| 369 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 370 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 371 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 372 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 373 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 374 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 375 | } ____cacheline_aligned_in_smp; |
| 376 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 377 | struct { |
| 378 | struct mutex uring_lock; |
| 379 | wait_queue_head_t wait; |
| 380 | } ____cacheline_aligned_in_smp; |
| 381 | |
| 382 | struct io_submit_state submit_state; |
| 383 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 384 | struct io_rings *rings; |
| 385 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 386 | /* Only used for accounting purposes */ |
| 387 | struct mm_struct *mm_account; |
| 388 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 389 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 391 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 392 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 393 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 394 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 395 | /* |
| 396 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 397 | * readers must ensure that ->refs is alive as long as the file* is |
| 398 | * used. Only updated through io_uring_register(2). |
| 399 | */ |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 400 | struct io_rsrc_data *file_data; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 401 | struct io_file_table file_table; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 402 | unsigned nr_user_files; |
| 403 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 404 | /* if used, fixed mapped user buffers */ |
| 405 | unsigned nr_user_bufs; |
| 406 | struct io_mapped_ubuf *user_bufs; |
| 407 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 408 | struct user_struct *user; |
| 409 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 410 | struct completion ref_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 411 | |
| 412 | #if defined(CONFIG_UNIX) |
| 413 | struct socket *ring_sock; |
| 414 | #endif |
| 415 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 416 | struct xarray io_buffers; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 417 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 418 | struct xarray personalities; |
| 419 | u32 pers_next; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 420 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 421 | struct { |
| 422 | unsigned cached_cq_tail; |
| 423 | unsigned cq_entries; |
| 424 | unsigned cq_mask; |
| 425 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 426 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 427 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 428 | struct wait_queue_head cq_wait; |
| 429 | struct fasync_struct *cq_fasync; |
| 430 | struct eventfd_ctx *cq_ev_fd; |
| 431 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 432 | |
| 433 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 434 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 435 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 436 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 437 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 438 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 439 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 440 | * manipulate the list, hence no extra locking is needed there. |
| 441 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 442 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 443 | struct hlist_head *cancel_hash; |
| 444 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 445 | bool poll_multi_file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 446 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 447 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 448 | struct delayed_work rsrc_put_work; |
| 449 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 450 | struct list_head rsrc_ref_list; |
| 451 | spinlock_t rsrc_ref_lock; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 452 | struct io_rsrc_node *rsrc_node; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 453 | struct io_rsrc_node *rsrc_backup_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 454 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 455 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 456 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 457 | /* exit task_work */ |
| 458 | struct callback_head *exit_task_work; |
| 459 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 460 | /* Keep this last, we don't need it for the fast path */ |
| 461 | struct work_struct exit_work; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 462 | struct list_head tctx_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 463 | }; |
| 464 | |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 465 | struct io_uring_task { |
| 466 | /* submission side */ |
| 467 | struct xarray xa; |
| 468 | struct wait_queue_head wait; |
Stefan Metzmacher | ee53fb2 | 2021-03-15 12:56:57 +0100 | [diff] [blame] | 469 | const struct io_ring_ctx *last; |
| 470 | struct io_wq *io_wq; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 471 | struct percpu_counter inflight; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 472 | atomic_t inflight_tracked; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 473 | atomic_t in_idle; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 474 | |
| 475 | spinlock_t task_lock; |
| 476 | struct io_wq_work_list task_list; |
| 477 | unsigned long task_state; |
| 478 | struct callback_head task_work; |
| 479 | }; |
| 480 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 481 | /* |
| 482 | * First field must be the file pointer in all the |
| 483 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 484 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 485 | struct io_poll_iocb { |
| 486 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 487 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 488 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 489 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 490 | bool canceled; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 491 | struct wait_queue_entry wait; |
| 492 | }; |
| 493 | |
| 494 | struct io_poll_update { |
| 495 | struct file *file; |
| 496 | u64 old_user_data; |
| 497 | u64 new_user_data; |
| 498 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 499 | bool update_events; |
| 500 | bool update_user_data; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 501 | }; |
| 502 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 503 | struct io_poll_remove { |
| 504 | struct file *file; |
| 505 | u64 addr; |
| 506 | }; |
| 507 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 508 | struct io_close { |
| 509 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 510 | int fd; |
| 511 | }; |
| 512 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 513 | struct io_timeout_data { |
| 514 | struct io_kiocb *req; |
| 515 | struct hrtimer timer; |
| 516 | struct timespec64 ts; |
| 517 | enum hrtimer_mode mode; |
| 518 | }; |
| 519 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 520 | struct io_accept { |
| 521 | struct file *file; |
| 522 | struct sockaddr __user *addr; |
| 523 | int __user *addr_len; |
| 524 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 525 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | struct io_sync { |
| 529 | struct file *file; |
| 530 | loff_t len; |
| 531 | loff_t off; |
| 532 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 533 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 534 | }; |
| 535 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 536 | struct io_cancel { |
| 537 | struct file *file; |
| 538 | u64 addr; |
| 539 | }; |
| 540 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 541 | struct io_timeout { |
| 542 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 543 | u32 off; |
| 544 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 545 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 546 | /* head of the link, used by linked timeouts only */ |
| 547 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 550 | struct io_timeout_rem { |
| 551 | struct file *file; |
| 552 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 553 | |
| 554 | /* timeout update */ |
| 555 | struct timespec64 ts; |
| 556 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 557 | }; |
| 558 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 559 | struct io_rw { |
| 560 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 561 | struct kiocb kiocb; |
| 562 | u64 addr; |
| 563 | u64 len; |
| 564 | }; |
| 565 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 566 | struct io_connect { |
| 567 | struct file *file; |
| 568 | struct sockaddr __user *addr; |
| 569 | int addr_len; |
| 570 | }; |
| 571 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 572 | struct io_sr_msg { |
| 573 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 574 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 575 | struct compat_msghdr __user *umsg_compat; |
| 576 | struct user_msghdr __user *umsg; |
| 577 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 578 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 579 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 580 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 581 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 582 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 583 | }; |
| 584 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 585 | struct io_open { |
| 586 | struct file *file; |
| 587 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 588 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 589 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 590 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 591 | }; |
| 592 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 593 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 594 | struct file *file; |
| 595 | u64 arg; |
| 596 | u32 nr_args; |
| 597 | u32 offset; |
| 598 | }; |
| 599 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 600 | struct io_fadvise { |
| 601 | struct file *file; |
| 602 | u64 offset; |
| 603 | u32 len; |
| 604 | u32 advice; |
| 605 | }; |
| 606 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 607 | struct io_madvise { |
| 608 | struct file *file; |
| 609 | u64 addr; |
| 610 | u32 len; |
| 611 | u32 advice; |
| 612 | }; |
| 613 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 614 | struct io_epoll { |
| 615 | struct file *file; |
| 616 | int epfd; |
| 617 | int op; |
| 618 | int fd; |
| 619 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 620 | }; |
| 621 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 622 | struct io_splice { |
| 623 | struct file *file_out; |
| 624 | struct file *file_in; |
| 625 | loff_t off_out; |
| 626 | loff_t off_in; |
| 627 | u64 len; |
| 628 | unsigned int flags; |
| 629 | }; |
| 630 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 631 | struct io_provide_buf { |
| 632 | struct file *file; |
| 633 | __u64 addr; |
| 634 | __s32 len; |
| 635 | __u32 bgid; |
| 636 | __u16 nbufs; |
| 637 | __u16 bid; |
| 638 | }; |
| 639 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 640 | struct io_statx { |
| 641 | struct file *file; |
| 642 | int dfd; |
| 643 | unsigned int mask; |
| 644 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 645 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 646 | struct statx __user *buffer; |
| 647 | }; |
| 648 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 649 | struct io_shutdown { |
| 650 | struct file *file; |
| 651 | int how; |
| 652 | }; |
| 653 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 654 | struct io_rename { |
| 655 | struct file *file; |
| 656 | int old_dfd; |
| 657 | int new_dfd; |
| 658 | struct filename *oldpath; |
| 659 | struct filename *newpath; |
| 660 | int flags; |
| 661 | }; |
| 662 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 663 | struct io_unlink { |
| 664 | struct file *file; |
| 665 | int dfd; |
| 666 | int flags; |
| 667 | struct filename *filename; |
| 668 | }; |
| 669 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 670 | struct io_completion { |
| 671 | struct file *file; |
| 672 | struct list_head list; |
Pavel Begunkov | 8c3f9cd | 2021-02-28 22:35:15 +0000 | [diff] [blame] | 673 | u32 cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 674 | }; |
| 675 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 676 | struct io_async_connect { |
| 677 | struct sockaddr_storage address; |
| 678 | }; |
| 679 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 680 | struct io_async_msghdr { |
| 681 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 682 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 683 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 684 | struct sockaddr __user *uaddr; |
| 685 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 686 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 687 | }; |
| 688 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 689 | struct io_async_rw { |
| 690 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 691 | const struct iovec *free_iovec; |
| 692 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 693 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 694 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 695 | }; |
| 696 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 697 | enum { |
| 698 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 699 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 700 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 701 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 702 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 703 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 704 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 705 | REQ_F_FAIL_LINK_BIT, |
| 706 | REQ_F_INFLIGHT_BIT, |
| 707 | REQ_F_CUR_POS_BIT, |
| 708 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 709 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 710 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 711 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 712 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 713 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 714 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 715 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 716 | REQ_F_DONT_REISSUE_BIT, |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 717 | REQ_F_POLL_UPDATE_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 718 | /* keep async read/write and isreg together and in order */ |
| 719 | REQ_F_ASYNC_READ_BIT, |
| 720 | REQ_F_ASYNC_WRITE_BIT, |
| 721 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 722 | |
| 723 | /* not a real bit, just to check we're not overflowing the space */ |
| 724 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | enum { |
| 728 | /* ctx owns file */ |
| 729 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 730 | /* drain existing IO first */ |
| 731 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 732 | /* linked sqes */ |
| 733 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 734 | /* doesn't sever on completion < 0 */ |
| 735 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 736 | /* IOSQE_ASYNC */ |
| 737 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 738 | /* IOSQE_BUFFER_SELECT */ |
| 739 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 740 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 741 | /* fail rest of links */ |
| 742 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 743 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 744 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 745 | /* read/write uses file position */ |
| 746 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 747 | /* must not punt to workers */ |
| 748 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 749 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 750 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 751 | /* needs cleanup */ |
| 752 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 753 | /* already went through poll handler */ |
| 754 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 755 | /* buffer already selected */ |
| 756 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 757 | /* linked timeout is active, i.e. prepared by link's head */ |
| 758 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 759 | /* completion is deferred through io_comp_state */ |
| 760 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 761 | /* caller should reissue async */ |
| 762 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 763 | /* don't attempt request reissue, see io_rw_reissue() */ |
| 764 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 765 | /* switches between poll and poll update */ |
| 766 | REQ_F_POLL_UPDATE = BIT(REQ_F_POLL_UPDATE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 767 | /* supports async reads */ |
| 768 | REQ_F_ASYNC_READ = BIT(REQ_F_ASYNC_READ_BIT), |
| 769 | /* supports async writes */ |
| 770 | REQ_F_ASYNC_WRITE = BIT(REQ_F_ASYNC_WRITE_BIT), |
| 771 | /* regular file */ |
| 772 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 773 | }; |
| 774 | |
| 775 | struct async_poll { |
| 776 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 777 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 778 | }; |
| 779 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 780 | struct io_task_work { |
| 781 | struct io_wq_work_node node; |
| 782 | task_work_func_t func; |
| 783 | }; |
| 784 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 785 | /* |
| 786 | * NOTE! Each of the iocb union members has the file pointer |
| 787 | * as the first entry in their struct definition. So you can |
| 788 | * access the file pointer through any of the sub-structs, |
| 789 | * or directly as just 'ki_filp' in this struct. |
| 790 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 791 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 792 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 793 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 794 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 795 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 796 | struct io_poll_update poll_update; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 797 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 798 | struct io_accept accept; |
| 799 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 800 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 801 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 802 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 803 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 804 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 805 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 806 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 807 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 808 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 809 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 810 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 811 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 812 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 813 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 814 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 815 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 816 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 817 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 818 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 819 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 820 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 821 | /* opcode allocated if it needs to store data for async defer */ |
| 822 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 823 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 824 | /* polled IO has completed */ |
| 825 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 826 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 827 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 828 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 829 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 830 | struct io_ring_ctx *ctx; |
| 831 | unsigned int flags; |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 832 | atomic_t refs; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 833 | struct task_struct *task; |
| 834 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 835 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 836 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 837 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 838 | |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 839 | /* used with ctx->iopoll_list with reads/writes */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 840 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 841 | union { |
| 842 | struct io_task_work io_task_work; |
| 843 | struct callback_head task_work; |
| 844 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 845 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 846 | struct hlist_node hash_node; |
| 847 | struct async_poll *apoll; |
| 848 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 849 | }; |
| 850 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 851 | struct io_tctx_node { |
| 852 | struct list_head ctx_node; |
| 853 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 854 | struct io_ring_ctx *ctx; |
| 855 | }; |
| 856 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 857 | struct io_defer_entry { |
| 858 | struct list_head list; |
| 859 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 860 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 861 | }; |
| 862 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 863 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 864 | /* needs req->file assigned */ |
| 865 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 866 | /* hash wq insertion if file is a regular file */ |
| 867 | unsigned hash_reg_file : 1; |
| 868 | /* unbound wq insertion if file is a non-regular file */ |
| 869 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 870 | /* opcode is not supported by this kernel */ |
| 871 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 872 | /* set if opcode supports polled "wait" */ |
| 873 | unsigned pollin : 1; |
| 874 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 875 | /* op supports buffer selection */ |
| 876 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 877 | /* do prep async if is going to be punted */ |
| 878 | unsigned needs_async_setup : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 879 | /* should block plug */ |
| 880 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 881 | /* size of async data needed, if any */ |
| 882 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 883 | }; |
| 884 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 885 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 886 | [IORING_OP_NOP] = {}, |
| 887 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 888 | .needs_file = 1, |
| 889 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 890 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 891 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 892 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 893 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 894 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 895 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 896 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 897 | .needs_file = 1, |
| 898 | .hash_reg_file = 1, |
| 899 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 900 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 901 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 902 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 903 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 904 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 905 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 906 | .needs_file = 1, |
| 907 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 908 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 909 | .needs_file = 1, |
| 910 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 911 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 912 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 913 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 914 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 915 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 916 | .needs_file = 1, |
| 917 | .hash_reg_file = 1, |
| 918 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 919 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 920 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 921 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 922 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 923 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 924 | .needs_file = 1, |
| 925 | .unbound_nonreg_file = 1, |
| 926 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 927 | [IORING_OP_POLL_REMOVE] = {}, |
| 928 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 929 | .needs_file = 1, |
| 930 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 931 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 932 | .needs_file = 1, |
| 933 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 934 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 935 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 936 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 937 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 938 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 939 | .needs_file = 1, |
| 940 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 941 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 942 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 943 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 944 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 945 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 946 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 947 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 948 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 949 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 950 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 951 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 952 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 953 | .needs_file = 1, |
| 954 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 955 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 956 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 957 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 958 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 959 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 960 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 961 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 962 | .needs_file = 1, |
| 963 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 964 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 965 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 966 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 967 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 968 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 969 | .needs_file = 1, |
| 970 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 971 | [IORING_OP_OPENAT] = {}, |
| 972 | [IORING_OP_CLOSE] = {}, |
| 973 | [IORING_OP_FILES_UPDATE] = {}, |
| 974 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 975 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 976 | .needs_file = 1, |
| 977 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 978 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 979 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 980 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 981 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 982 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 983 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 984 | .needs_file = 1, |
| 985 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 986 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 987 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 988 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 989 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 990 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 991 | .needs_file = 1, |
| 992 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 993 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 994 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 995 | .needs_file = 1, |
| 996 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 997 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 998 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 999 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1000 | .needs_file = 1, |
| 1001 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1002 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1003 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1004 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1005 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1006 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1007 | [IORING_OP_EPOLL_CTL] = { |
| 1008 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1009 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1010 | [IORING_OP_SPLICE] = { |
| 1011 | .needs_file = 1, |
| 1012 | .hash_reg_file = 1, |
| 1013 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1014 | }, |
| 1015 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1016 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1017 | [IORING_OP_TEE] = { |
| 1018 | .needs_file = 1, |
| 1019 | .hash_reg_file = 1, |
| 1020 | .unbound_nonreg_file = 1, |
| 1021 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1022 | [IORING_OP_SHUTDOWN] = { |
| 1023 | .needs_file = 1, |
| 1024 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1025 | [IORING_OP_RENAMEAT] = {}, |
| 1026 | [IORING_OP_UNLINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1027 | }; |
| 1028 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1029 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 1030 | static void io_uring_del_task_file(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1031 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1032 | struct task_struct *task, |
| 1033 | struct files_struct *files); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 1034 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1035 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 1036 | |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 1037 | static bool io_cqring_fill_event(struct io_kiocb *req, long res, unsigned cflags); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1038 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1039 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1040 | static void io_dismantle_req(struct io_kiocb *req); |
| 1041 | static void io_put_task(struct task_struct *task, int nr); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1042 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 1043 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1044 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1045 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1046 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1047 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1048 | static struct file *io_file_get(struct io_submit_state *state, |
| 1049 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1050 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1051 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1052 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1053 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1054 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1055 | struct io_ring_ctx *ctx); |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 1056 | static bool io_poll_remove_waitqs(struct io_kiocb *req); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1057 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1058 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1059 | static struct kmem_cache *req_cachep; |
| 1060 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1061 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1062 | |
| 1063 | struct sock *io_uring_get_socket(struct file *file) |
| 1064 | { |
| 1065 | #if defined(CONFIG_UNIX) |
| 1066 | if (file->f_op == &io_uring_fops) { |
| 1067 | struct io_ring_ctx *ctx = file->private_data; |
| 1068 | |
| 1069 | return ctx->ring_sock->sk; |
| 1070 | } |
| 1071 | #endif |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1075 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1076 | #define io_for_each_link(pos, head) \ |
| 1077 | for (pos = (head); pos; pos = pos->link) |
| 1078 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1079 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1080 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1081 | struct io_ring_ctx *ctx = req->ctx; |
| 1082 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1083 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1084 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1085 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1089 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1090 | { |
| 1091 | bool got = percpu_ref_tryget(ref); |
| 1092 | |
| 1093 | /* already at zero, wait for ->release() */ |
| 1094 | if (!got) |
| 1095 | wait_for_completion(compl); |
| 1096 | percpu_ref_resurrect(ref); |
| 1097 | if (got) |
| 1098 | percpu_ref_put(ref); |
| 1099 | } |
| 1100 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1101 | static bool io_match_task(struct io_kiocb *head, |
| 1102 | struct task_struct *task, |
| 1103 | struct files_struct *files) |
| 1104 | { |
| 1105 | struct io_kiocb *req; |
| 1106 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1107 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1108 | return false; |
| 1109 | if (!files) |
| 1110 | return true; |
| 1111 | |
| 1112 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1113 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1114 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1115 | } |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1119 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1120 | { |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 1121 | if (req->flags & REQ_F_LINK) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1122 | req->flags |= REQ_F_FAIL_LINK; |
| 1123 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1124 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1125 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1126 | { |
| 1127 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1128 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1129 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1132 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1133 | { |
| 1134 | return !req->timeout.off; |
| 1135 | } |
| 1136 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1137 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1138 | { |
| 1139 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1140 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1141 | |
| 1142 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1143 | if (!ctx) |
| 1144 | return NULL; |
| 1145 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1146 | /* |
| 1147 | * Use 5 bits less than the max cq entries, that should give us around |
| 1148 | * 32 entries per hash list if totally full and uniformly spread. |
| 1149 | */ |
| 1150 | hash_bits = ilog2(p->cq_entries); |
| 1151 | hash_bits -= 5; |
| 1152 | if (hash_bits <= 0) |
| 1153 | hash_bits = 1; |
| 1154 | ctx->cancel_hash_bits = hash_bits; |
| 1155 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1156 | GFP_KERNEL); |
| 1157 | if (!ctx->cancel_hash) |
| 1158 | goto err; |
| 1159 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1160 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1161 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1162 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1163 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1164 | |
| 1165 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1166 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1167 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1168 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1169 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1170 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1171 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1172 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1173 | mutex_init(&ctx->uring_lock); |
| 1174 | init_waitqueue_head(&ctx->wait); |
| 1175 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1176 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1177 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1178 | INIT_LIST_HEAD(&ctx->timeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1179 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1180 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1181 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1182 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1183 | INIT_LIST_HEAD(&ctx->tctx_list); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1184 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1185 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1186 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1187 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1188 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1189 | kfree(ctx); |
| 1190 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1193 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1194 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1195 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1196 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1197 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1198 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1199 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1200 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1201 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1202 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1203 | } |
| 1204 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1205 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1206 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1207 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1208 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1209 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1213 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1214 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1215 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1216 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1217 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1218 | if (!req->work.creds) |
| 1219 | req->work.creds = get_current_cred(); |
| 1220 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1221 | req->work.list.next = NULL; |
| 1222 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1223 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1224 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1225 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1226 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1227 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1228 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1229 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1230 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1231 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1232 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1233 | |
| 1234 | switch (req->opcode) { |
| 1235 | case IORING_OP_SPLICE: |
| 1236 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1237 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1238 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1239 | break; |
| 1240 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1241 | } |
| 1242 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1243 | static void io_prep_async_link(struct io_kiocb *req) |
| 1244 | { |
| 1245 | struct io_kiocb *cur; |
| 1246 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1247 | io_for_each_link(cur, req) |
| 1248 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1249 | } |
| 1250 | |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1251 | static void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1252 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1253 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1254 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1255 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1256 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1257 | BUG_ON(!tctx); |
| 1258 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1259 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1260 | /* init ->work of the whole link before punting */ |
| 1261 | io_prep_async_link(req); |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1262 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1263 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1264 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1265 | if (link) |
| 1266 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1267 | } |
| 1268 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1269 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1270 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1271 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1272 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1273 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1274 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1275 | atomic_set(&req->ctx->cq_timeouts, |
| 1276 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1277 | list_del_init(&req->timeout.list); |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 1278 | io_cqring_fill_event(req, status, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1279 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1283 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1284 | { |
| 1285 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1286 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1287 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1288 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1289 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1290 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1291 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1292 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1293 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1294 | } while (!list_empty(&ctx->defer_list)); |
| 1295 | } |
| 1296 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1297 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1298 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1299 | u32 seq; |
| 1300 | |
| 1301 | if (list_empty(&ctx->timeout_list)) |
| 1302 | return; |
| 1303 | |
| 1304 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1305 | |
| 1306 | do { |
| 1307 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1308 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1309 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1310 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1311 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1312 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1313 | |
| 1314 | /* |
| 1315 | * Since seq can easily wrap around over time, subtract |
| 1316 | * the last seq at which timeouts were flushed before comparing. |
| 1317 | * Assuming not more than 2^31-1 events have happened since, |
| 1318 | * these subtractions won't have wrapped, so we can check if |
| 1319 | * target is in [last_seq, current_seq] by comparing the two. |
| 1320 | */ |
| 1321 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1322 | events_got = seq - ctx->cq_last_tm_flush; |
| 1323 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1324 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1325 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1326 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1327 | io_kill_timeout(req, 0); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1328 | } while (!list_empty(&ctx->timeout_list)); |
| 1329 | |
| 1330 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1331 | } |
| 1332 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1333 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1334 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1335 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1336 | |
| 1337 | /* order cqe stores with ring update */ |
| 1338 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1339 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1340 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1341 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1342 | } |
| 1343 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1344 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1345 | { |
| 1346 | struct io_rings *r = ctx->rings; |
| 1347 | |
| 1348 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1349 | } |
| 1350 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1351 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1352 | { |
| 1353 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1354 | } |
| 1355 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1356 | static inline struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1357 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1358 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1359 | unsigned tail; |
| 1360 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1361 | /* |
| 1362 | * writes to the cq entry need to come after reading head; the |
| 1363 | * control dependency is enough as we're using WRITE_ONCE to |
| 1364 | * fill the cq entry |
| 1365 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1366 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1367 | return NULL; |
| 1368 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1369 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1370 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1373 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1374 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1375 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1376 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1377 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1378 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1379 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1382 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1383 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1384 | /* see waitqueue_active() comment */ |
| 1385 | smp_mb(); |
| 1386 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1387 | if (waitqueue_active(&ctx->wait)) |
| 1388 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1389 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1390 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1391 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1392 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1393 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1394 | wake_up_interruptible(&ctx->cq_wait); |
| 1395 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1396 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1397 | } |
| 1398 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1399 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1400 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1401 | /* see waitqueue_active() comment */ |
| 1402 | smp_mb(); |
| 1403 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1404 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1405 | if (waitqueue_active(&ctx->wait)) |
| 1406 | wake_up(&ctx->wait); |
| 1407 | } |
| 1408 | if (io_should_trigger_evfd(ctx)) |
| 1409 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1410 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1411 | wake_up_interruptible(&ctx->cq_wait); |
| 1412 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1413 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1416 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1417 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1418 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1419 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1420 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1421 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1422 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1423 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1424 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1425 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1426 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1427 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1428 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1429 | struct io_uring_cqe *cqe = io_get_cqring(ctx); |
| 1430 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1431 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1432 | if (!cqe && !force) |
| 1433 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1434 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1435 | struct io_overflow_cqe, list); |
| 1436 | if (cqe) |
| 1437 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1438 | else |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1439 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1440 | ++ctx->cached_cq_overflow); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1441 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1442 | list_del(&ocqe->list); |
| 1443 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1446 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1447 | if (all_flushed) { |
| 1448 | clear_bit(0, &ctx->sq_check_overflow); |
| 1449 | clear_bit(0, &ctx->cq_check_overflow); |
| 1450 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1451 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1452 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1453 | if (posted) |
| 1454 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1455 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1456 | if (posted) |
| 1457 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1458 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1461 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1462 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1463 | bool ret = true; |
| 1464 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1465 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1466 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1467 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1468 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1469 | ret = __io_cqring_overflow_flush(ctx, force); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1470 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1471 | mutex_unlock(&ctx->uring_lock); |
| 1472 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1473 | |
| 1474 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1477 | /* |
| 1478 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1479 | * see commit f958d7b528b1 for details. |
| 1480 | */ |
| 1481 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1482 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1483 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1484 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1485 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1486 | return atomic_inc_not_zero(&req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs) |
| 1490 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1491 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1492 | return atomic_sub_and_test(refs, &req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 1496 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1497 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1498 | return atomic_dec_and_test(&req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | static inline void req_ref_put(struct io_kiocb *req) |
| 1502 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1503 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static inline void req_ref_get(struct io_kiocb *req) |
| 1507 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1508 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1509 | atomic_inc(&req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1512 | static bool io_cqring_event_overflow(struct io_kiocb *req, long res, |
| 1513 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1514 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1515 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame^] | 1516 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1517 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame^] | 1518 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1519 | if (!ocqe) { |
| 1520 | /* |
| 1521 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1522 | * or cannot allocate an overflow entry, then we need to drop it |
| 1523 | * on the floor. |
| 1524 | */ |
| 1525 | WRITE_ONCE(ctx->rings->cq_overflow, ++ctx->cached_cq_overflow); |
| 1526 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1527 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame^] | 1528 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1529 | set_bit(0, &ctx->sq_check_overflow); |
| 1530 | set_bit(0, &ctx->cq_check_overflow); |
| 1531 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
| 1532 | } |
| 1533 | ocqe->cqe.user_data = req->user_data; |
| 1534 | ocqe->cqe.res = res; |
| 1535 | ocqe->cqe.flags = cflags; |
| 1536 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1537 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1540 | static inline bool __io_cqring_fill_event(struct io_kiocb *req, long res, |
| 1541 | unsigned int cflags) |
| 1542 | { |
| 1543 | struct io_ring_ctx *ctx = req->ctx; |
| 1544 | struct io_uring_cqe *cqe; |
| 1545 | |
| 1546 | trace_io_uring_complete(ctx, req->user_data, res, cflags); |
| 1547 | |
| 1548 | /* |
| 1549 | * If we can't get a cq entry, userspace overflowed the |
| 1550 | * submission (by quite a lot). Increment the overflow count in |
| 1551 | * the ring. |
| 1552 | */ |
| 1553 | cqe = io_get_cqring(ctx); |
| 1554 | if (likely(cqe)) { |
| 1555 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1556 | WRITE_ONCE(cqe->res, res); |
| 1557 | WRITE_ONCE(cqe->flags, cflags); |
| 1558 | return true; |
| 1559 | } |
| 1560 | return io_cqring_event_overflow(req, res, cflags); |
| 1561 | } |
| 1562 | |
| 1563 | /* not as hot to bloat with inlining */ |
| 1564 | static noinline bool io_cqring_fill_event(struct io_kiocb *req, long res, |
| 1565 | unsigned int cflags) |
| 1566 | { |
| 1567 | return __io_cqring_fill_event(req, res, cflags); |
| 1568 | } |
| 1569 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1570 | static void io_req_complete_post(struct io_kiocb *req, long res, |
| 1571 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1572 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1573 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1574 | unsigned long flags; |
| 1575 | |
| 1576 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1577 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * If we're the last reference to this request, add to our locked |
| 1580 | * free_list cache. |
| 1581 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1582 | if (req_ref_put_and_test(req)) { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1583 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1584 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1585 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
| 1586 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) |
| 1587 | io_disarm_next(req); |
| 1588 | if (req->link) { |
| 1589 | io_req_task_queue(req->link); |
| 1590 | req->link = NULL; |
| 1591 | } |
| 1592 | } |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1593 | io_dismantle_req(req); |
| 1594 | io_put_task(req->task, 1); |
| 1595 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1596 | cs->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1597 | } else { |
| 1598 | if (!percpu_ref_tryget(&ctx->refs)) |
| 1599 | req = NULL; |
| 1600 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1601 | io_commit_cqring(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1602 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1603 | |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1604 | if (req) { |
| 1605 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1606 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1607 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1610 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1611 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1612 | { |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1613 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
| 1614 | io_clean_op(req); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1615 | req->result = res; |
| 1616 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1617 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1618 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1619 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1620 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1621 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1622 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1623 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1624 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1625 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1626 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1627 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1628 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1629 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1630 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1631 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1634 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
| 1635 | { |
| 1636 | req_set_fail_links(req); |
| 1637 | io_put_req(req); |
| 1638 | io_req_complete_post(req, res, 0); |
| 1639 | } |
| 1640 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1641 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
| 1642 | struct io_comp_state *cs) |
| 1643 | { |
| 1644 | spin_lock_irq(&ctx->completion_lock); |
| 1645 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1646 | cs->locked_free_nr = 0; |
| 1647 | spin_unlock_irq(&ctx->completion_lock); |
| 1648 | } |
| 1649 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1650 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1651 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1652 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1653 | struct io_submit_state *state = &ctx->submit_state; |
| 1654 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1655 | int nr; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1656 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1657 | /* |
| 1658 | * If we have more than a batch's worth of requests in our IRQ side |
| 1659 | * locked cache, grab the lock and move them over to our submission |
| 1660 | * side cache. |
| 1661 | */ |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1662 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) |
| 1663 | io_flush_cached_locked_reqs(ctx, cs); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1664 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1665 | nr = state->free_reqs; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1666 | while (!list_empty(&cs->free_list)) { |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1667 | struct io_kiocb *req = list_first_entry(&cs->free_list, |
| 1668 | struct io_kiocb, compl.list); |
| 1669 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1670 | list_del(&req->compl.list); |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1671 | state->reqs[nr++] = req; |
| 1672 | if (nr == ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1673 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1674 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1675 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1676 | state->free_reqs = nr; |
| 1677 | return nr != 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1680 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1681 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1682 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1683 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1684 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1685 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1686 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1687 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1688 | int ret; |
| 1689 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1690 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1691 | goto got_req; |
| 1692 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1693 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1694 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1695 | |
| 1696 | /* |
| 1697 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1698 | * retry single alloc to be on the safe side. |
| 1699 | */ |
| 1700 | if (unlikely(ret <= 0)) { |
| 1701 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1702 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1703 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1704 | ret = 1; |
| 1705 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1706 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1707 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1708 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1709 | state->free_reqs--; |
| 1710 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1713 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1714 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1715 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1716 | fput(file); |
| 1717 | } |
| 1718 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1719 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1720 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1721 | unsigned int flags = req->flags; |
| 1722 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1723 | if (!(flags & REQ_F_FIXED_FILE)) |
| 1724 | io_put_file(req->file); |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1725 | if (flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED | |
| 1726 | REQ_F_INFLIGHT)) { |
| 1727 | io_clean_op(req); |
| 1728 | |
| 1729 | if (req->flags & REQ_F_INFLIGHT) { |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1730 | struct io_uring_task *tctx = req->task->io_uring; |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1731 | |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1732 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1733 | req->flags &= ~REQ_F_INFLIGHT; |
| 1734 | } |
| 1735 | } |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1736 | if (req->fixed_rsrc_refs) |
| 1737 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1738 | if (req->async_data) |
| 1739 | kfree(req->async_data); |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1740 | if (req->work.creds) { |
| 1741 | put_cred(req->work.creds); |
| 1742 | req->work.creds = NULL; |
| 1743 | } |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1744 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1745 | |
Pavel Begunkov | b23fcf4 | 2021-03-01 18:20:48 +0000 | [diff] [blame] | 1746 | /* must to be called somewhat shortly after putting a request */ |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1747 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1748 | { |
| 1749 | struct io_uring_task *tctx = task->io_uring; |
| 1750 | |
| 1751 | percpu_counter_sub(&tctx->inflight, nr); |
| 1752 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1753 | wake_up(&tctx->wait); |
| 1754 | put_task_struct_many(task, nr); |
| 1755 | } |
| 1756 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1757 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1758 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1759 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1760 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1761 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1762 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1763 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1764 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1765 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1766 | } |
| 1767 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1768 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1769 | { |
| 1770 | struct io_kiocb *nxt = req->link; |
| 1771 | |
| 1772 | req->link = nxt->link; |
| 1773 | nxt->link = NULL; |
| 1774 | } |
| 1775 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1776 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 1777 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1778 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1779 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1780 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 1781 | /* |
| 1782 | * Can happen if a linked timeout fired and link had been like |
| 1783 | * req -> link t-out -> link t-out [-> ...] |
| 1784 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1785 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 1786 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1787 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1788 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 1789 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1790 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 1791 | io_cqring_fill_event(link, -ECANCELED, 0); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1792 | io_put_req_deferred(link, 1); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 1793 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1794 | } |
| 1795 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 1796 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1797 | } |
| 1798 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1799 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1800 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1801 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1802 | struct io_kiocb *nxt, *link = req->link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1803 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1804 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1805 | while (link) { |
| 1806 | nxt = link->link; |
| 1807 | link->link = NULL; |
| 1808 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1809 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 1810 | io_cqring_fill_event(link, -ECANCELED, 0); |
Jens Axboe | 1575f21 | 2021-02-27 15:20:49 -0700 | [diff] [blame] | 1811 | io_put_req_deferred(link, 2); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1812 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1813 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1814 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1815 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1816 | static bool io_disarm_next(struct io_kiocb *req) |
| 1817 | __must_hold(&req->ctx->completion_lock) |
| 1818 | { |
| 1819 | bool posted = false; |
| 1820 | |
| 1821 | if (likely(req->flags & REQ_F_LINK_TIMEOUT)) |
| 1822 | posted = io_kill_linked_timeout(req); |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 1823 | if (unlikely((req->flags & REQ_F_FAIL_LINK) && |
| 1824 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1825 | posted |= (req->link != NULL); |
| 1826 | io_fail_links(req); |
| 1827 | } |
| 1828 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1829 | } |
| 1830 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1831 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1832 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1833 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1834 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1835 | /* |
| 1836 | * If LINK is set, we have dependent requests in this chain. If we |
| 1837 | * didn't fail this request, queue the first one up, moving any other |
| 1838 | * dependencies to the next request. In case of failure, fail the rest |
| 1839 | * of the chain. |
| 1840 | */ |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1841 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) { |
| 1842 | struct io_ring_ctx *ctx = req->ctx; |
| 1843 | unsigned long flags; |
| 1844 | bool posted; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1845 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1846 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1847 | posted = io_disarm_next(req); |
| 1848 | if (posted) |
| 1849 | io_commit_cqring(req->ctx); |
| 1850 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1851 | if (posted) |
| 1852 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1853 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1854 | nxt = req->link; |
| 1855 | req->link = NULL; |
| 1856 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1857 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1858 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1859 | 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] | 1860 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 1861 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1862 | return NULL; |
| 1863 | return __io_req_find_next(req); |
| 1864 | } |
| 1865 | |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1866 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
| 1867 | { |
| 1868 | if (!ctx) |
| 1869 | return; |
| 1870 | if (ctx->submit_state.comp.nr) { |
| 1871 | mutex_lock(&ctx->uring_lock); |
| 1872 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1873 | mutex_unlock(&ctx->uring_lock); |
| 1874 | } |
| 1875 | percpu_ref_put(&ctx->refs); |
| 1876 | } |
| 1877 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1878 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 1879 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1880 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1881 | struct io_wq_work_list list; |
| 1882 | struct io_wq_work_node *node; |
| 1883 | |
| 1884 | if (wq_list_empty(&tctx->task_list)) |
| 1885 | return false; |
| 1886 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1887 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1888 | list = tctx->task_list; |
| 1889 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1890 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1891 | |
| 1892 | node = list.first; |
| 1893 | while (node) { |
| 1894 | struct io_wq_work_node *next = node->next; |
| 1895 | struct io_kiocb *req; |
| 1896 | |
| 1897 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1898 | if (req->ctx != ctx) { |
| 1899 | ctx_flush_and_put(ctx); |
| 1900 | ctx = req->ctx; |
| 1901 | percpu_ref_get(&ctx->refs); |
| 1902 | } |
| 1903 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1904 | req->task_work.func(&req->task_work); |
| 1905 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1908 | ctx_flush_and_put(ctx); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1909 | return list.first != NULL; |
| 1910 | } |
| 1911 | |
| 1912 | static void tctx_task_work(struct callback_head *cb) |
| 1913 | { |
| 1914 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 1915 | |
Jens Axboe | 1d5f360 | 2021-02-26 14:54:16 -0700 | [diff] [blame] | 1916 | clear_bit(0, &tctx->task_state); |
| 1917 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1918 | while (__tctx_task_work(tctx)) |
| 1919 | cond_resched(); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1922 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1923 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1924 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1925 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1926 | enum task_work_notify_mode notify; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1927 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1928 | unsigned long flags; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1929 | int ret = 0; |
| 1930 | |
| 1931 | if (unlikely(tsk->flags & PF_EXITING)) |
| 1932 | return -ESRCH; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1933 | |
| 1934 | WARN_ON_ONCE(!tctx); |
| 1935 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1936 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1937 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1938 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1939 | |
| 1940 | /* task_work already pending, we're done */ |
| 1941 | if (test_bit(0, &tctx->task_state) || |
| 1942 | test_and_set_bit(0, &tctx->task_state)) |
| 1943 | return 0; |
| 1944 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1945 | /* |
| 1946 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1947 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1948 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1949 | * will do the job. |
| 1950 | */ |
| 1951 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
| 1952 | |
| 1953 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
| 1954 | wake_up_process(tsk); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1955 | return 0; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1956 | } |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1957 | |
| 1958 | /* |
| 1959 | * Slow path - we failed, find and delete work. if the work is not |
| 1960 | * in the list, it got run and we're fine. |
| 1961 | */ |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1962 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1963 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 1964 | if (&req->io_task_work.node == node) { |
| 1965 | wq_list_del(&tctx->task_list, node, prev); |
| 1966 | ret = 1; |
| 1967 | break; |
| 1968 | } |
| 1969 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1970 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1971 | clear_bit(0, &tctx->task_state); |
| 1972 | return ret; |
| 1973 | } |
| 1974 | |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 1975 | static bool io_run_task_work_head(struct callback_head **work_head) |
| 1976 | { |
| 1977 | struct callback_head *work, *next; |
| 1978 | bool executed = false; |
| 1979 | |
| 1980 | do { |
| 1981 | work = xchg(work_head, NULL); |
| 1982 | if (!work) |
| 1983 | break; |
| 1984 | |
| 1985 | do { |
| 1986 | next = work->next; |
| 1987 | work->func(work); |
| 1988 | work = next; |
| 1989 | cond_resched(); |
| 1990 | } while (work); |
| 1991 | executed = true; |
| 1992 | } while (1); |
| 1993 | |
| 1994 | return executed; |
| 1995 | } |
| 1996 | |
| 1997 | static void io_task_work_add_head(struct callback_head **work_head, |
| 1998 | struct callback_head *task_work) |
| 1999 | { |
| 2000 | struct callback_head *head; |
| 2001 | |
| 2002 | do { |
| 2003 | head = READ_ONCE(*work_head); |
| 2004 | task_work->next = head; |
| 2005 | } while (cmpxchg(work_head, head, task_work) != head); |
| 2006 | } |
| 2007 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2008 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2009 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2010 | { |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2011 | init_task_work(&req->task_work, cb); |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 2012 | io_task_work_add_head(&req->ctx->exit_task_work, &req->task_work); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2015 | static void io_req_task_cancel(struct callback_head *cb) |
| 2016 | { |
| 2017 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2018 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2019 | |
Pavel Begunkov | e83acd7 | 2021-02-28 22:35:09 +0000 | [diff] [blame] | 2020 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2021 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2022 | io_req_complete_failed(req, req->result); |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2023 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2027 | { |
| 2028 | struct io_ring_ctx *ctx = req->ctx; |
| 2029 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2030 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2031 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 70aacfe | 2021-03-01 13:02:15 +0000 | [diff] [blame] | 2032 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2033 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2034 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2035 | io_req_complete_failed(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2036 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2037 | } |
| 2038 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2039 | static void io_req_task_submit(struct callback_head *cb) |
| 2040 | { |
| 2041 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2042 | |
| 2043 | __io_req_task_submit(req); |
| 2044 | } |
| 2045 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2046 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2047 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2048 | req->result = ret; |
| 2049 | req->task_work.func = io_req_task_cancel; |
| 2050 | |
| 2051 | if (unlikely(io_req_task_work_add(req))) |
| 2052 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
| 2053 | } |
| 2054 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2055 | static void io_req_task_queue(struct io_kiocb *req) |
| 2056 | { |
| 2057 | req->task_work.func = io_req_task_submit; |
| 2058 | |
| 2059 | if (unlikely(io_req_task_work_add(req))) |
| 2060 | io_req_task_queue_fail(req, -ECANCELED); |
| 2061 | } |
| 2062 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2063 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2064 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2065 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2066 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2067 | if (nxt) |
| 2068 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2069 | } |
| 2070 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2071 | static void io_free_req(struct io_kiocb *req) |
| 2072 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2073 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2074 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2075 | } |
| 2076 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2077 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2078 | struct task_struct *task; |
| 2079 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2080 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2081 | }; |
| 2082 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2083 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2084 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2085 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2086 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2087 | rb->task = NULL; |
| 2088 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2089 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2090 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2091 | struct req_batch *rb) |
| 2092 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2093 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2094 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2095 | if (rb->ctx_refs) |
| 2096 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2097 | } |
| 2098 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2099 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2100 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2101 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2102 | io_queue_next(req); |
Pavel Begunkov | 9667065 | 2021-03-19 17:22:32 +0000 | [diff] [blame] | 2103 | io_dismantle_req(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2104 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2105 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2106 | if (rb->task) |
| 2107 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2108 | rb->task = req->task; |
| 2109 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2110 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2111 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2112 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2113 | |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2114 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2115 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2116 | else |
| 2117 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2118 | } |
| 2119 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2120 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2121 | struct io_ring_ctx *ctx) |
| 2122 | { |
| 2123 | int i, nr = cs->nr; |
| 2124 | struct io_kiocb *req; |
| 2125 | struct req_batch rb; |
| 2126 | |
| 2127 | io_init_req_batch(&rb); |
| 2128 | spin_lock_irq(&ctx->completion_lock); |
| 2129 | for (i = 0; i < nr; i++) { |
| 2130 | req = cs->reqs[i]; |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 2131 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2132 | } |
| 2133 | io_commit_cqring(ctx); |
| 2134 | spin_unlock_irq(&ctx->completion_lock); |
| 2135 | |
| 2136 | io_cqring_ev_posted(ctx); |
| 2137 | for (i = 0; i < nr; i++) { |
| 2138 | req = cs->reqs[i]; |
| 2139 | |
| 2140 | /* submission and completion refs */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2141 | if (req_ref_sub_and_test(req, 2)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2142 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | io_req_free_batch_finish(ctx, &rb); |
| 2146 | cs->nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2147 | } |
| 2148 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2149 | /* |
| 2150 | * Drop reference to request, return next in chain (if there is one) if this |
| 2151 | * was the last reference to this request. |
| 2152 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2153 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2154 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2155 | struct io_kiocb *nxt = NULL; |
| 2156 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2157 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2158 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2159 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2160 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2161 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2164 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2165 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2166 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2167 | io_free_req(req); |
| 2168 | } |
| 2169 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2170 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2171 | { |
| 2172 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2173 | |
| 2174 | io_free_req(req); |
| 2175 | } |
| 2176 | |
| 2177 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2178 | { |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2179 | req->task_work.func = io_put_req_deferred_cb; |
Pavel Begunkov | a05432f | 2021-03-19 17:22:38 +0000 | [diff] [blame] | 2180 | if (unlikely(io_req_task_work_add(req))) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2181 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2185 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2186 | if (req_ref_sub_and_test(req, refs)) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2187 | io_free_req_deferred(req); |
| 2188 | } |
| 2189 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2190 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2191 | { |
| 2192 | /* See comment at the top of this file */ |
| 2193 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2194 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2195 | } |
| 2196 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2197 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2198 | { |
| 2199 | struct io_rings *rings = ctx->rings; |
| 2200 | |
| 2201 | /* make sure SQ entry isn't read before tail */ |
| 2202 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2203 | } |
| 2204 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2205 | 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] | 2206 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2207 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2208 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2209 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2210 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2211 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2212 | kfree(kbuf); |
| 2213 | return cflags; |
| 2214 | } |
| 2215 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2216 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2217 | { |
| 2218 | struct io_buffer *kbuf; |
| 2219 | |
| 2220 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2221 | return io_put_kbuf(req, kbuf); |
| 2222 | } |
| 2223 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2224 | static inline bool io_run_task_work(void) |
| 2225 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2226 | /* |
| 2227 | * Not safe to run on exiting task, and the task_work handling will |
| 2228 | * not add work to such a task. |
| 2229 | */ |
| 2230 | if (unlikely(current->flags & PF_EXITING)) |
| 2231 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2232 | if (current->task_works) { |
| 2233 | __set_current_state(TASK_RUNNING); |
| 2234 | task_work_run(); |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
| 2238 | return false; |
| 2239 | } |
| 2240 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2241 | /* |
| 2242 | * Find and free completed poll iocbs |
| 2243 | */ |
| 2244 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2245 | struct list_head *done) |
| 2246 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2247 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2248 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2249 | |
| 2250 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2251 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2252 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2253 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2254 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2255 | int cflags = 0; |
| 2256 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2257 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2258 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2259 | |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2260 | if (READ_ONCE(req->result) == -EAGAIN && |
| 2261 | !(req->flags & REQ_F_DONT_REISSUE)) { |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2262 | req->iopoll_completed = 0; |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2263 | req_ref_get(req); |
| 2264 | io_queue_async_work(req); |
| 2265 | continue; |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2268 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2269 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2270 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 2271 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2272 | (*nr_events)++; |
| 2273 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2274 | if (req_ref_put_and_test(req)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2275 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2276 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2277 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2278 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2279 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2280 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2283 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2284 | long min) |
| 2285 | { |
| 2286 | struct io_kiocb *req, *tmp; |
| 2287 | LIST_HEAD(done); |
| 2288 | bool spin; |
| 2289 | int ret; |
| 2290 | |
| 2291 | /* |
| 2292 | * Only spin for completions if we don't have multiple devices hanging |
| 2293 | * off our complete list, and we're under the requested amount. |
| 2294 | */ |
| 2295 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2296 | |
| 2297 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2298 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2299 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2300 | |
| 2301 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2302 | * Move completed and retryable entries to our local lists. |
| 2303 | * If we find a request that requires polling, break out |
| 2304 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2305 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2306 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2307 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2308 | continue; |
| 2309 | } |
| 2310 | if (!list_empty(&done)) |
| 2311 | break; |
| 2312 | |
| 2313 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2314 | if (ret < 0) |
| 2315 | break; |
| 2316 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2317 | /* iopoll may have completed current req */ |
| 2318 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2319 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2320 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2321 | if (ret && spin) |
| 2322 | spin = false; |
| 2323 | ret = 0; |
| 2324 | } |
| 2325 | |
| 2326 | if (!list_empty(&done)) |
| 2327 | io_iopoll_complete(ctx, nr_events, &done); |
| 2328 | |
| 2329 | return ret; |
| 2330 | } |
| 2331 | |
| 2332 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2333 | * 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] | 2334 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2335 | * as a non-spinning completion check. |
| 2336 | */ |
| 2337 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2338 | long min) |
| 2339 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2340 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2341 | int ret; |
| 2342 | |
| 2343 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2344 | if (ret < 0) |
| 2345 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2346 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
| 2349 | |
| 2350 | return 1; |
| 2351 | } |
| 2352 | |
| 2353 | /* |
| 2354 | * We can't just wait for polled events to come to us, we have to actively |
| 2355 | * find and complete them. |
| 2356 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2357 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2358 | { |
| 2359 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2360 | return; |
| 2361 | |
| 2362 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2363 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2364 | unsigned int nr_events = 0; |
| 2365 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2366 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2367 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2368 | /* let it sleep and repeat later if can't complete a request */ |
| 2369 | if (nr_events == 0) |
| 2370 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2371 | /* |
| 2372 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2373 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2374 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2375 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2376 | if (need_resched()) { |
| 2377 | mutex_unlock(&ctx->uring_lock); |
| 2378 | cond_resched(); |
| 2379 | mutex_lock(&ctx->uring_lock); |
| 2380 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2381 | } |
| 2382 | mutex_unlock(&ctx->uring_lock); |
| 2383 | } |
| 2384 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2385 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2386 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2387 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2388 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2389 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2390 | /* |
| 2391 | * We disallow the app entering submit/complete with polling, but we |
| 2392 | * still need to lock the ring to prevent racing with polled issue |
| 2393 | * that got punted to a workqueue. |
| 2394 | */ |
| 2395 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2396 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2397 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2398 | * Don't enter poll loop if we already have events pending. |
| 2399 | * If we do, we can potentially be spinning for commands that |
| 2400 | * already triggered a CQE (eg in error). |
| 2401 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2402 | if (test_bit(0, &ctx->cq_check_overflow)) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 2403 | __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2404 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2405 | break; |
| 2406 | |
| 2407 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2408 | * If a submit got punted to a workqueue, we can have the |
| 2409 | * application entering polling for a command before it gets |
| 2410 | * issued. That app will hold the uring_lock for the duration |
| 2411 | * of the poll right here, so we need to take a breather every |
| 2412 | * now and then to ensure that the issue has a chance to add |
| 2413 | * the poll to the issued list. Otherwise we can spin here |
| 2414 | * forever, while the workqueue is stuck trying to acquire the |
| 2415 | * very same mutex. |
| 2416 | */ |
| 2417 | if (!(++iters & 7)) { |
| 2418 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2419 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2420 | mutex_lock(&ctx->uring_lock); |
| 2421 | } |
| 2422 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2423 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2424 | if (ret <= 0) |
| 2425 | break; |
| 2426 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2427 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2428 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2429 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2430 | return ret; |
| 2431 | } |
| 2432 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2433 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2434 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2435 | /* |
| 2436 | * Tell lockdep we inherited freeze protection from submission |
| 2437 | * thread. |
| 2438 | */ |
| 2439 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2440 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2441 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2442 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2443 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2447 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2448 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2449 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2450 | struct io_async_rw *rw = req->async_data; |
| 2451 | |
| 2452 | if (!rw) |
| 2453 | return !io_req_prep_async(req); |
| 2454 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ |
| 2455 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); |
| 2456 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2457 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2458 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2459 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2460 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2461 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2462 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2463 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2464 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2465 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2466 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2467 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2468 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2469 | /* |
| 2470 | * If ref is dying, we might be running poll reap from the exit work. |
| 2471 | * Don't attempt to reissue from that path, just let it fail with |
| 2472 | * -EAGAIN. |
| 2473 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2474 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2475 | return false; |
| 2476 | return true; |
| 2477 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2478 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2479 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2480 | { |
| 2481 | return false; |
| 2482 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2483 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2484 | { |
| 2485 | return false; |
| 2486 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2487 | #endif |
| 2488 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2489 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2490 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2491 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2492 | int cflags = 0; |
| 2493 | |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2494 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2495 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2496 | if (res != req->result) { |
| 2497 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2498 | io_rw_should_reissue(req)) { |
| 2499 | req->flags |= REQ_F_REISSUE; |
| 2500 | return; |
| 2501 | } |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2502 | req_set_fail_links(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2503 | } |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2504 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2505 | cflags = io_put_rw_kbuf(req); |
| 2506 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2510 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2511 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2512 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2513 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2516 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2517 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2518 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2519 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2520 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2521 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2522 | if (unlikely(res != req->result)) { |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2523 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
| 2524 | io_resubmit_prep(req))) { |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2525 | req_set_fail_links(req); |
| 2526 | req->flags |= REQ_F_DONT_REISSUE; |
| 2527 | } |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2528 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2529 | |
| 2530 | WRITE_ONCE(req->result, res); |
Jens Axboe | b9b0e0d | 2021-02-23 08:18:36 -0700 | [diff] [blame] | 2531 | /* order with io_iopoll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2532 | smp_wmb(); |
| 2533 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2538 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2539 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2540 | * accessing the kiocb cookie. |
| 2541 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2542 | 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] | 2543 | { |
| 2544 | struct io_ring_ctx *ctx = req->ctx; |
| 2545 | |
| 2546 | /* |
| 2547 | * Track whether we have multiple files in our lists. This will impact |
| 2548 | * how we do polling eventually, not spinning if we're on potentially |
| 2549 | * different devices. |
| 2550 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2551 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2552 | ctx->poll_multi_file = false; |
| 2553 | } else if (!ctx->poll_multi_file) { |
| 2554 | struct io_kiocb *list_req; |
| 2555 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2556 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2557 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2558 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2559 | ctx->poll_multi_file = true; |
| 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * For fast devices, IO may have already completed. If it has, add |
| 2564 | * it to the front so we find it first. |
| 2565 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2566 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2567 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2568 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2569 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2570 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2571 | /* |
| 2572 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2573 | * task context or in io worker task context. If current task context is |
| 2574 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2575 | */ |
| 2576 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2577 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2578 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2579 | } |
| 2580 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2581 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2582 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2583 | if (state->file_refs) { |
| 2584 | fput_many(state->file, state->file_refs); |
| 2585 | state->file_refs = 0; |
| 2586 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | /* |
| 2590 | * Get as many references to a file as we have IOs left in this submission, |
| 2591 | * assuming most submissions are for one file, or at least that each file |
| 2592 | * has more than one submission. |
| 2593 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2594 | 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] | 2595 | { |
| 2596 | if (!state) |
| 2597 | return fget(fd); |
| 2598 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2599 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2600 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2601 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2602 | return state->file; |
| 2603 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2604 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2605 | } |
| 2606 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2607 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2608 | return NULL; |
| 2609 | |
| 2610 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2611 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2612 | return state->file; |
| 2613 | } |
| 2614 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2615 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2616 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2617 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2618 | } |
| 2619 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2620 | /* |
| 2621 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2622 | * any file. For now, just ensure that anything potentially problematic is done |
| 2623 | * inline. |
| 2624 | */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2625 | static bool __io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2626 | { |
| 2627 | umode_t mode = file_inode(file)->i_mode; |
| 2628 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2629 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2630 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2631 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2632 | return true; |
| 2633 | return false; |
| 2634 | } |
| 2635 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2636 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2637 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2638 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2639 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2640 | file->f_op != &io_uring_fops) |
| 2641 | return true; |
| 2642 | return false; |
| 2643 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2644 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2645 | /* any ->read/write should understand O_NONBLOCK */ |
| 2646 | if (file->f_flags & O_NONBLOCK) |
| 2647 | return true; |
| 2648 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2649 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2650 | return false; |
| 2651 | |
| 2652 | if (rw == READ) |
| 2653 | return file->f_op->read_iter != NULL; |
| 2654 | |
| 2655 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2656 | } |
| 2657 | |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2658 | static bool io_file_supports_async(struct io_kiocb *req, int rw) |
| 2659 | { |
| 2660 | if (rw == READ && (req->flags & REQ_F_ASYNC_READ)) |
| 2661 | return true; |
| 2662 | else if (rw == WRITE && (req->flags & REQ_F_ASYNC_WRITE)) |
| 2663 | return true; |
| 2664 | |
| 2665 | return __io_file_supports_async(req->file, rw); |
| 2666 | } |
| 2667 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2668 | 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] | 2669 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2670 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2671 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2672 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2673 | unsigned ioprio; |
| 2674 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2675 | |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2676 | if (!(req->flags & REQ_F_ISREG) && S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2677 | req->flags |= REQ_F_ISREG; |
| 2678 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2679 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2680 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2681 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2682 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2683 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2684 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2685 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2686 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2687 | if (unlikely(ret)) |
| 2688 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2689 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2690 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 2691 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 2692 | req->flags |= REQ_F_NOWAIT; |
| 2693 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2694 | ioprio = READ_ONCE(sqe->ioprio); |
| 2695 | if (ioprio) { |
| 2696 | ret = ioprio_check_cap(ioprio); |
| 2697 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2698 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2699 | |
| 2700 | kiocb->ki_ioprio = ioprio; |
| 2701 | } else |
| 2702 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2703 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2704 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2705 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2706 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2707 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2708 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2709 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2710 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2711 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2712 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2713 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2714 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2715 | kiocb->ki_complete = io_complete_rw; |
| 2716 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2717 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2718 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2719 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2720 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2721 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2725 | { |
| 2726 | switch (ret) { |
| 2727 | case -EIOCBQUEUED: |
| 2728 | break; |
| 2729 | case -ERESTARTSYS: |
| 2730 | case -ERESTARTNOINTR: |
| 2731 | case -ERESTARTNOHAND: |
| 2732 | case -ERESTART_RESTARTBLOCK: |
| 2733 | /* |
| 2734 | * We can't just restart the syscall, since previously |
| 2735 | * submitted sqes may already be in progress. Just fail this |
| 2736 | * IO with EINTR. |
| 2737 | */ |
| 2738 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2739 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2740 | default: |
| 2741 | kiocb->ki_complete(kiocb, ret, 0); |
| 2742 | } |
| 2743 | } |
| 2744 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2745 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2746 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2747 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2748 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2749 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2750 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2751 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2752 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2753 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2754 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2755 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2756 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2757 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2758 | } |
| 2759 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2760 | if (req->flags & REQ_F_CUR_POS) |
| 2761 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2762 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2763 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2764 | else |
| 2765 | io_rw_done(kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2766 | |
| 2767 | if (check_reissue && req->flags & REQ_F_REISSUE) { |
| 2768 | req->flags &= ~REQ_F_REISSUE; |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2769 | if (!io_resubmit_prep(req)) { |
| 2770 | req_ref_get(req); |
| 2771 | io_queue_async_work(req); |
| 2772 | } else { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2773 | int cflags = 0; |
| 2774 | |
| 2775 | req_set_fail_links(req); |
| 2776 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2777 | cflags = io_put_rw_kbuf(req); |
| 2778 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2779 | } |
| 2780 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2781 | } |
| 2782 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2783 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2784 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2785 | struct io_ring_ctx *ctx = req->ctx; |
| 2786 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2787 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 2788 | u16 index, buf_index = req->buf_index; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2789 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2790 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2791 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2792 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2793 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2794 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2795 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2796 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2797 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2798 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2799 | return -EFAULT; |
| 2800 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 2801 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2802 | return -EFAULT; |
| 2803 | |
| 2804 | /* |
| 2805 | * May not be a start of buffer, set size appropriately |
| 2806 | * and advance us to the beginning. |
| 2807 | */ |
| 2808 | offset = buf_addr - imu->ubuf; |
| 2809 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2810 | |
| 2811 | if (offset) { |
| 2812 | /* |
| 2813 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2814 | * using the latter parts of a big fixed buffer - it iterates |
| 2815 | * over each segment manually. We can cheat a bit here, because |
| 2816 | * we know that: |
| 2817 | * |
| 2818 | * 1) it's a BVEC iter, we set it up |
| 2819 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2820 | * first and last bvec |
| 2821 | * |
| 2822 | * So just find our index, and adjust the iterator afterwards. |
| 2823 | * If the offset is within the first bvec (or the whole first |
| 2824 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2825 | * since we can just skip the first segment, which may not |
| 2826 | * be PAGE_SIZE aligned. |
| 2827 | */ |
| 2828 | const struct bio_vec *bvec = imu->bvec; |
| 2829 | |
| 2830 | if (offset <= bvec->bv_len) { |
| 2831 | iov_iter_advance(iter, offset); |
| 2832 | } else { |
| 2833 | unsigned long seg_skip; |
| 2834 | |
| 2835 | /* skip first vec */ |
| 2836 | offset -= bvec->bv_len; |
| 2837 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2838 | |
| 2839 | iter->bvec = bvec + seg_skip; |
| 2840 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2841 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2842 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2846 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2849 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2850 | { |
| 2851 | if (needs_lock) |
| 2852 | mutex_unlock(&ctx->uring_lock); |
| 2853 | } |
| 2854 | |
| 2855 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2856 | { |
| 2857 | /* |
| 2858 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2859 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2860 | * The only exception is when we've detached the request and issue it |
| 2861 | * from an async worker thread, grab the lock for that case. |
| 2862 | */ |
| 2863 | if (needs_lock) |
| 2864 | mutex_lock(&ctx->uring_lock); |
| 2865 | } |
| 2866 | |
| 2867 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2868 | int bgid, struct io_buffer *kbuf, |
| 2869 | bool needs_lock) |
| 2870 | { |
| 2871 | struct io_buffer *head; |
| 2872 | |
| 2873 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2874 | return kbuf; |
| 2875 | |
| 2876 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2877 | |
| 2878 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2879 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 2880 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2881 | if (head) { |
| 2882 | if (!list_empty(&head->list)) { |
| 2883 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2884 | list); |
| 2885 | list_del(&kbuf->list); |
| 2886 | } else { |
| 2887 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 2888 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2889 | } |
| 2890 | if (*len > kbuf->len) |
| 2891 | *len = kbuf->len; |
| 2892 | } else { |
| 2893 | kbuf = ERR_PTR(-ENOBUFS); |
| 2894 | } |
| 2895 | |
| 2896 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2897 | |
| 2898 | return kbuf; |
| 2899 | } |
| 2900 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2901 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2902 | bool needs_lock) |
| 2903 | { |
| 2904 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2905 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2906 | |
| 2907 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2908 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2909 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2910 | if (IS_ERR(kbuf)) |
| 2911 | return kbuf; |
| 2912 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2913 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2914 | return u64_to_user_ptr(kbuf->addr); |
| 2915 | } |
| 2916 | |
| 2917 | #ifdef CONFIG_COMPAT |
| 2918 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2919 | bool needs_lock) |
| 2920 | { |
| 2921 | struct compat_iovec __user *uiov; |
| 2922 | compat_ssize_t clen; |
| 2923 | void __user *buf; |
| 2924 | ssize_t len; |
| 2925 | |
| 2926 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2927 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2928 | return -EFAULT; |
| 2929 | if (__get_user(clen, &uiov->iov_len)) |
| 2930 | return -EFAULT; |
| 2931 | if (clen < 0) |
| 2932 | return -EINVAL; |
| 2933 | |
| 2934 | len = clen; |
| 2935 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2936 | if (IS_ERR(buf)) |
| 2937 | return PTR_ERR(buf); |
| 2938 | iov[0].iov_base = buf; |
| 2939 | iov[0].iov_len = (compat_size_t) len; |
| 2940 | return 0; |
| 2941 | } |
| 2942 | #endif |
| 2943 | |
| 2944 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2945 | bool needs_lock) |
| 2946 | { |
| 2947 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2948 | void __user *buf; |
| 2949 | ssize_t len; |
| 2950 | |
| 2951 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2952 | return -EFAULT; |
| 2953 | |
| 2954 | len = iov[0].iov_len; |
| 2955 | if (len < 0) |
| 2956 | return -EINVAL; |
| 2957 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2958 | if (IS_ERR(buf)) |
| 2959 | return PTR_ERR(buf); |
| 2960 | iov[0].iov_base = buf; |
| 2961 | iov[0].iov_len = len; |
| 2962 | return 0; |
| 2963 | } |
| 2964 | |
| 2965 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2966 | bool needs_lock) |
| 2967 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2968 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2969 | struct io_buffer *kbuf; |
| 2970 | |
| 2971 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2972 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2973 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2974 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2975 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 2976 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2977 | return -EINVAL; |
| 2978 | |
| 2979 | #ifdef CONFIG_COMPAT |
| 2980 | if (req->ctx->compat) |
| 2981 | return io_compat_import(req, iov, needs_lock); |
| 2982 | #endif |
| 2983 | |
| 2984 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2985 | } |
| 2986 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2987 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 2988 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2989 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2990 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2991 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2992 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2993 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2994 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2995 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2996 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2997 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2998 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2999 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3000 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3001 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3002 | return -EINVAL; |
| 3003 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3004 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3005 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3006 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3007 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3008 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3009 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3012 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3013 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3014 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3015 | } |
| 3016 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3017 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3018 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3019 | if (!ret) |
| 3020 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3021 | *iovec = NULL; |
| 3022 | return ret; |
| 3023 | } |
| 3024 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3025 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3026 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3029 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3030 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3031 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3032 | } |
| 3033 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3034 | /* |
| 3035 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3036 | * by looping over ->read() or ->write() manually. |
| 3037 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3038 | 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] | 3039 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3040 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3041 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3042 | ssize_t ret = 0; |
| 3043 | |
| 3044 | /* |
| 3045 | * Don't support polled IO through this interface, and we can't |
| 3046 | * support non-blocking either. For the latter, this just causes |
| 3047 | * the kiocb to be handled from an async context. |
| 3048 | */ |
| 3049 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3050 | return -EOPNOTSUPP; |
| 3051 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3052 | return -EAGAIN; |
| 3053 | |
| 3054 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3055 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3056 | ssize_t nr; |
| 3057 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3058 | if (!iov_iter_is_bvec(iter)) { |
| 3059 | iovec = iov_iter_iovec(iter); |
| 3060 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3061 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3062 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3063 | } |
| 3064 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3065 | if (rw == READ) { |
| 3066 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3067 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3068 | } else { |
| 3069 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3070 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | if (nr < 0) { |
| 3074 | if (!ret) |
| 3075 | ret = nr; |
| 3076 | break; |
| 3077 | } |
| 3078 | ret += nr; |
| 3079 | if (nr != iovec.iov_len) |
| 3080 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3081 | req->rw.len -= nr; |
| 3082 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3083 | iov_iter_advance(iter, nr); |
| 3084 | } |
| 3085 | |
| 3086 | return ret; |
| 3087 | } |
| 3088 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3089 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3090 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3091 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3092 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3093 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3094 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3095 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3096 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3097 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3098 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3099 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3100 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3101 | unsigned iov_off = 0; |
| 3102 | |
| 3103 | rw->iter.iov = rw->fast_iov; |
| 3104 | if (iter->iov != fast_iov) { |
| 3105 | iov_off = iter->iov - fast_iov; |
| 3106 | rw->iter.iov += iov_off; |
| 3107 | } |
| 3108 | if (rw->fast_iov != fast_iov) |
| 3109 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3110 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3111 | } else { |
| 3112 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3116 | static inline int io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3117 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3118 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3119 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3120 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3121 | } |
| 3122 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3123 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3124 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3125 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3126 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3127 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3128 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3129 | if (!req->async_data) { |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3130 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3131 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3132 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3133 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3134 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3135 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3136 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3137 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3140 | 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] | 3141 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3142 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3143 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3144 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3145 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3146 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3147 | if (unlikely(ret < 0)) |
| 3148 | return ret; |
| 3149 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3150 | iorw->bytes_done = 0; |
| 3151 | iorw->free_iovec = iov; |
| 3152 | if (iov) |
| 3153 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3154 | return 0; |
| 3155 | } |
| 3156 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3157 | 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] | 3158 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3159 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3160 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3161 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3164 | /* |
| 3165 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3166 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3167 | * This gets called when the page is unlocked, and we generally expect that to |
| 3168 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3169 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3170 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3171 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3172 | * slow path. |
| 3173 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3174 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3175 | int sync, void *arg) |
| 3176 | { |
| 3177 | struct wait_page_queue *wpq; |
| 3178 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3179 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3180 | |
| 3181 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3182 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3183 | if (!wake_page_match(wpq, key)) |
| 3184 | return 0; |
| 3185 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3186 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3187 | list_del_init(&wait->entry); |
| 3188 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3189 | /* submit ref gets dropped, acquire a new one */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 3190 | req_ref_get(req); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3191 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3192 | return 1; |
| 3193 | } |
| 3194 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3195 | /* |
| 3196 | * This controls whether a given IO request should be armed for async page |
| 3197 | * based retry. If we return false here, the request is handed to the async |
| 3198 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3199 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3200 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3201 | * will register a callback when the page is unlocked at IO completion. Through |
| 3202 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3203 | * That retry will attempt the buffered read again. The retry will generally |
| 3204 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3205 | * async worker threads for a blocking retry. |
| 3206 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3207 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3208 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3209 | struct io_async_rw *rw = req->async_data; |
| 3210 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3211 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3212 | |
| 3213 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3214 | if (req->flags & REQ_F_NOWAIT) |
| 3215 | return false; |
| 3216 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3217 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3218 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3219 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3220 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3221 | /* |
| 3222 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3223 | * support callback based unlocks |
| 3224 | */ |
| 3225 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3226 | return false; |
| 3227 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3228 | wait->wait.func = io_async_buf_func; |
| 3229 | wait->wait.private = req; |
| 3230 | wait->wait.flags = 0; |
| 3231 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3232 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3233 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3234 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3235 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3239 | { |
| 3240 | if (req->file->f_op->read_iter) |
| 3241 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3242 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3243 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3244 | else |
| 3245 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3246 | } |
| 3247 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3248 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3249 | { |
| 3250 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3251 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3252 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3253 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3254 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3255 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3256 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3257 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3258 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3259 | iovec = NULL; |
| 3260 | } else { |
| 3261 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3262 | if (ret < 0) |
| 3263 | return ret; |
| 3264 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3265 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3266 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3267 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3268 | /* Ensure we clear previously set non-block flag */ |
| 3269 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3270 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3271 | else |
| 3272 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3273 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3274 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 3275 | if (force_nonblock && !io_file_supports_async(req, READ)) { |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3276 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3277 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3278 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3279 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3280 | ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3281 | if (unlikely(ret)) { |
| 3282 | kfree(iovec); |
| 3283 | return ret; |
| 3284 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3285 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3286 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3287 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3288 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f233 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3289 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3290 | /* IOPOLL retry should happen for io-wq threads */ |
| 3291 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3292 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3293 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3294 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3295 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3296 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3297 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3298 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3299 | } else if (ret == -EIOCBQUEUED) { |
| 3300 | goto out_free; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3301 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3302 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3303 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3304 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3305 | } |
| 3306 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3307 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3308 | if (ret2) |
| 3309 | return ret2; |
| 3310 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3311 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3312 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3313 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3314 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3315 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3316 | do { |
| 3317 | io_size -= ret; |
| 3318 | rw->bytes_done += ret; |
| 3319 | /* if we can retry, do so with the callbacks armed */ |
| 3320 | if (!io_rw_should_retry(req)) { |
| 3321 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3322 | return -EAGAIN; |
| 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3327 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3328 | * desired page gets unlocked. We can also get a partial read |
| 3329 | * here, and if we do, then just retry at the new offset. |
| 3330 | */ |
| 3331 | ret = io_iter_do_read(req, iter); |
| 3332 | if (ret == -EIOCBQUEUED) |
| 3333 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3334 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3335 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3336 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3337 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3338 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3339 | out_free: |
| 3340 | /* it's faster to check here then delegate to kfree */ |
| 3341 | if (iovec) |
| 3342 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3343 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3346 | 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] | 3347 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3348 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3349 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3350 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3351 | } |
| 3352 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3353 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3354 | { |
| 3355 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3356 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3357 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3358 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3359 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3360 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3361 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3362 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3363 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3364 | iovec = NULL; |
| 3365 | } else { |
| 3366 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3367 | if (ret < 0) |
| 3368 | return ret; |
| 3369 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3370 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3371 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3372 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3373 | /* Ensure we clear previously set non-block flag */ |
| 3374 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3375 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3376 | else |
| 3377 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3378 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3379 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 3380 | if (force_nonblock && !io_file_supports_async(req, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3381 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3382 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3383 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3384 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3385 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3386 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3387 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3388 | 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] | 3389 | if (unlikely(ret)) |
| 3390 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3391 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3392 | /* |
| 3393 | * Open-code file_start_write here to grab freeze protection, |
| 3394 | * which will be released by another thread in |
| 3395 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3396 | * released so that it doesn't complain about the held lock when |
| 3397 | * we return to userspace. |
| 3398 | */ |
| 3399 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3400 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3401 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3402 | SB_FREEZE_WRITE); |
| 3403 | } |
| 3404 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3405 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3406 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3407 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3408 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3409 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3410 | else |
| 3411 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3412 | |
Pavel Begunkov | 6ad7f233 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3413 | if (req->flags & REQ_F_REISSUE) { |
| 3414 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3415 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f233 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3416 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3417 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3418 | /* |
| 3419 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3420 | * retry them without IOCB_NOWAIT. |
| 3421 | */ |
| 3422 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3423 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3424 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3425 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3426 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3427 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3428 | /* IOPOLL retry should happen for io-wq threads */ |
| 3429 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3430 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3431 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3432 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3433 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3434 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3435 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3436 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3437 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3438 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3439 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3440 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3441 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3442 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3443 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3444 | return ret; |
| 3445 | } |
| 3446 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3447 | static int io_renameat_prep(struct io_kiocb *req, |
| 3448 | const struct io_uring_sqe *sqe) |
| 3449 | { |
| 3450 | struct io_rename *ren = &req->rename; |
| 3451 | const char __user *oldf, *newf; |
| 3452 | |
| 3453 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3454 | return -EBADF; |
| 3455 | |
| 3456 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3457 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3458 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3459 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3460 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3461 | |
| 3462 | ren->oldpath = getname(oldf); |
| 3463 | if (IS_ERR(ren->oldpath)) |
| 3464 | return PTR_ERR(ren->oldpath); |
| 3465 | |
| 3466 | ren->newpath = getname(newf); |
| 3467 | if (IS_ERR(ren->newpath)) { |
| 3468 | putname(ren->oldpath); |
| 3469 | return PTR_ERR(ren->newpath); |
| 3470 | } |
| 3471 | |
| 3472 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3476 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3477 | { |
| 3478 | struct io_rename *ren = &req->rename; |
| 3479 | int ret; |
| 3480 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3481 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3482 | return -EAGAIN; |
| 3483 | |
| 3484 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3485 | ren->newpath, ren->flags); |
| 3486 | |
| 3487 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3488 | if (ret < 0) |
| 3489 | req_set_fail_links(req); |
| 3490 | io_req_complete(req, ret); |
| 3491 | return 0; |
| 3492 | } |
| 3493 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3494 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3495 | const struct io_uring_sqe *sqe) |
| 3496 | { |
| 3497 | struct io_unlink *un = &req->unlink; |
| 3498 | const char __user *fname; |
| 3499 | |
| 3500 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3501 | return -EBADF; |
| 3502 | |
| 3503 | un->dfd = READ_ONCE(sqe->fd); |
| 3504 | |
| 3505 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3506 | if (un->flags & ~AT_REMOVEDIR) |
| 3507 | return -EINVAL; |
| 3508 | |
| 3509 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3510 | un->filename = getname(fname); |
| 3511 | if (IS_ERR(un->filename)) |
| 3512 | return PTR_ERR(un->filename); |
| 3513 | |
| 3514 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3515 | return 0; |
| 3516 | } |
| 3517 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3518 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3519 | { |
| 3520 | struct io_unlink *un = &req->unlink; |
| 3521 | int ret; |
| 3522 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3523 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3524 | return -EAGAIN; |
| 3525 | |
| 3526 | if (un->flags & AT_REMOVEDIR) |
| 3527 | ret = do_rmdir(un->dfd, un->filename); |
| 3528 | else |
| 3529 | ret = do_unlinkat(un->dfd, un->filename); |
| 3530 | |
| 3531 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3532 | if (ret < 0) |
| 3533 | req_set_fail_links(req); |
| 3534 | io_req_complete(req, ret); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3538 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3539 | const struct io_uring_sqe *sqe) |
| 3540 | { |
| 3541 | #if defined(CONFIG_NET) |
| 3542 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3543 | return -EINVAL; |
| 3544 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3545 | sqe->buf_index) |
| 3546 | return -EINVAL; |
| 3547 | |
| 3548 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3549 | return 0; |
| 3550 | #else |
| 3551 | return -EOPNOTSUPP; |
| 3552 | #endif |
| 3553 | } |
| 3554 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3555 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3556 | { |
| 3557 | #if defined(CONFIG_NET) |
| 3558 | struct socket *sock; |
| 3559 | int ret; |
| 3560 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3561 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3562 | return -EAGAIN; |
| 3563 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3564 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3565 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3566 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3567 | |
| 3568 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3569 | if (ret < 0) |
| 3570 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3571 | io_req_complete(req, ret); |
| 3572 | return 0; |
| 3573 | #else |
| 3574 | return -EOPNOTSUPP; |
| 3575 | #endif |
| 3576 | } |
| 3577 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3578 | static int __io_splice_prep(struct io_kiocb *req, |
| 3579 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3580 | { |
| 3581 | struct io_splice* sp = &req->splice; |
| 3582 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3583 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3584 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3585 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3586 | |
| 3587 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3588 | sp->len = READ_ONCE(sqe->len); |
| 3589 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3590 | |
| 3591 | if (unlikely(sp->flags & ~valid_flags)) |
| 3592 | return -EINVAL; |
| 3593 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3594 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3595 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3596 | if (!sp->file_in) |
| 3597 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3598 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3599 | return 0; |
| 3600 | } |
| 3601 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3602 | static int io_tee_prep(struct io_kiocb *req, |
| 3603 | const struct io_uring_sqe *sqe) |
| 3604 | { |
| 3605 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3606 | return -EINVAL; |
| 3607 | return __io_splice_prep(req, sqe); |
| 3608 | } |
| 3609 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3610 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3611 | { |
| 3612 | struct io_splice *sp = &req->splice; |
| 3613 | struct file *in = sp->file_in; |
| 3614 | struct file *out = sp->file_out; |
| 3615 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3616 | long ret = 0; |
| 3617 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3618 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3619 | return -EAGAIN; |
| 3620 | if (sp->len) |
| 3621 | ret = do_tee(in, out, sp->len, flags); |
| 3622 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 3623 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 3624 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3625 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3626 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3627 | if (ret != sp->len) |
| 3628 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3629 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3630 | return 0; |
| 3631 | } |
| 3632 | |
| 3633 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3634 | { |
| 3635 | struct io_splice* sp = &req->splice; |
| 3636 | |
| 3637 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3638 | sp->off_out = READ_ONCE(sqe->off); |
| 3639 | return __io_splice_prep(req, sqe); |
| 3640 | } |
| 3641 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3642 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3643 | { |
| 3644 | struct io_splice *sp = &req->splice; |
| 3645 | struct file *in = sp->file_in; |
| 3646 | struct file *out = sp->file_out; |
| 3647 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3648 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3649 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3650 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3651 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3652 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3653 | |
| 3654 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3655 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3656 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3657 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3658 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3659 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 3660 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 3661 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3662 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3663 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3664 | if (ret != sp->len) |
| 3665 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3666 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3667 | return 0; |
| 3668 | } |
| 3669 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3670 | /* |
| 3671 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3672 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3673 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3674 | { |
| 3675 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3676 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3677 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3678 | return -EINVAL; |
| 3679 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3680 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3681 | return 0; |
| 3682 | } |
| 3683 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 3684 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3685 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3686 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3687 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3688 | if (!req->file) |
| 3689 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3690 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3691 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3692 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3693 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3694 | return -EINVAL; |
| 3695 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3696 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3697 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3698 | return -EINVAL; |
| 3699 | |
| 3700 | req->sync.off = READ_ONCE(sqe->off); |
| 3701 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3702 | return 0; |
| 3703 | } |
| 3704 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3705 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3706 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3707 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3708 | int ret; |
| 3709 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3710 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3711 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3712 | return -EAGAIN; |
| 3713 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3714 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3715 | end > 0 ? end : LLONG_MAX, |
| 3716 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3717 | if (ret < 0) |
| 3718 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3719 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3720 | return 0; |
| 3721 | } |
| 3722 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3723 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3724 | const struct io_uring_sqe *sqe) |
| 3725 | { |
| 3726 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3727 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3728 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3729 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3730 | |
| 3731 | req->sync.off = READ_ONCE(sqe->off); |
| 3732 | req->sync.len = READ_ONCE(sqe->addr); |
| 3733 | req->sync.mode = READ_ONCE(sqe->len); |
| 3734 | return 0; |
| 3735 | } |
| 3736 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3737 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3738 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3739 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3740 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3741 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3742 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3743 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3744 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3745 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3746 | if (ret < 0) |
| 3747 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3748 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3749 | return 0; |
| 3750 | } |
| 3751 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3752 | 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] | 3753 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3754 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3755 | int ret; |
| 3756 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3757 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3758 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3759 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3760 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3761 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3762 | /* open.how should be already initialised */ |
| 3763 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3764 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3765 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3766 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3767 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3768 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3769 | if (IS_ERR(req->open.filename)) { |
| 3770 | ret = PTR_ERR(req->open.filename); |
| 3771 | req->open.filename = NULL; |
| 3772 | return ret; |
| 3773 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3774 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3775 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3776 | return 0; |
| 3777 | } |
| 3778 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3779 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3780 | { |
| 3781 | u64 flags, mode; |
| 3782 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3783 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3784 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3785 | mode = READ_ONCE(sqe->len); |
| 3786 | flags = READ_ONCE(sqe->open_flags); |
| 3787 | req->open.how = build_open_how(flags, mode); |
| 3788 | return __io_openat_prep(req, sqe); |
| 3789 | } |
| 3790 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3791 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3792 | { |
| 3793 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3794 | size_t len; |
| 3795 | int ret; |
| 3796 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3797 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3798 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3799 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3800 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3801 | if (len < OPEN_HOW_SIZE_VER0) |
| 3802 | return -EINVAL; |
| 3803 | |
| 3804 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3805 | len); |
| 3806 | if (ret) |
| 3807 | return ret; |
| 3808 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3809 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3812 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3813 | { |
| 3814 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3815 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3816 | bool nonblock_set; |
| 3817 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3818 | int ret; |
| 3819 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3820 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3821 | if (ret) |
| 3822 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3823 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 3824 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3825 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3826 | /* |
| 3827 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 3828 | * it'll always -EAGAIN |
| 3829 | */ |
| 3830 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 3831 | return -EAGAIN; |
| 3832 | op.lookup_flags |= LOOKUP_CACHED; |
| 3833 | op.open_flag |= O_NONBLOCK; |
| 3834 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3835 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3836 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3837 | if (ret < 0) |
| 3838 | goto err; |
| 3839 | |
| 3840 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3841 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3842 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 3843 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3844 | /* |
| 3845 | * We could hang on to this 'fd', but seems like marginal |
| 3846 | * gain for something that is now known to be a slower path. |
| 3847 | * So just put it, and we'll get a new one when we retry. |
| 3848 | */ |
| 3849 | put_unused_fd(ret); |
| 3850 | return -EAGAIN; |
| 3851 | } |
| 3852 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3853 | if (IS_ERR(file)) { |
| 3854 | put_unused_fd(ret); |
| 3855 | ret = PTR_ERR(file); |
| 3856 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3857 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3858 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3859 | fsnotify_open(file); |
| 3860 | fd_install(ret, file); |
| 3861 | } |
| 3862 | err: |
| 3863 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3864 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3865 | if (ret < 0) |
| 3866 | req_set_fail_links(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 3867 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3868 | return 0; |
| 3869 | } |
| 3870 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3871 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3872 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 3873 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3874 | } |
| 3875 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3876 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3877 | const struct io_uring_sqe *sqe) |
| 3878 | { |
| 3879 | struct io_provide_buf *p = &req->pbuf; |
| 3880 | u64 tmp; |
| 3881 | |
| 3882 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3883 | return -EINVAL; |
| 3884 | |
| 3885 | tmp = READ_ONCE(sqe->fd); |
| 3886 | if (!tmp || tmp > USHRT_MAX) |
| 3887 | return -EINVAL; |
| 3888 | |
| 3889 | memset(p, 0, sizeof(*p)); |
| 3890 | p->nbufs = tmp; |
| 3891 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3892 | return 0; |
| 3893 | } |
| 3894 | |
| 3895 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3896 | int bgid, unsigned nbufs) |
| 3897 | { |
| 3898 | unsigned i = 0; |
| 3899 | |
| 3900 | /* shouldn't happen */ |
| 3901 | if (!nbufs) |
| 3902 | return 0; |
| 3903 | |
| 3904 | /* the head kbuf is the list itself */ |
| 3905 | while (!list_empty(&buf->list)) { |
| 3906 | struct io_buffer *nxt; |
| 3907 | |
| 3908 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3909 | list_del(&nxt->list); |
| 3910 | kfree(nxt); |
| 3911 | if (++i == nbufs) |
| 3912 | return i; |
| 3913 | } |
| 3914 | i++; |
| 3915 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3916 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3917 | |
| 3918 | return i; |
| 3919 | } |
| 3920 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3921 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3922 | { |
| 3923 | struct io_provide_buf *p = &req->pbuf; |
| 3924 | struct io_ring_ctx *ctx = req->ctx; |
| 3925 | struct io_buffer *head; |
| 3926 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3927 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3928 | |
| 3929 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3930 | |
| 3931 | lockdep_assert_held(&ctx->uring_lock); |
| 3932 | |
| 3933 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3934 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3935 | if (head) |
| 3936 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3937 | if (ret < 0) |
| 3938 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3939 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 3940 | /* complete before unlock, IOPOLL may need the lock */ |
| 3941 | __io_req_complete(req, issue_flags, ret, 0); |
| 3942 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3943 | return 0; |
| 3944 | } |
| 3945 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3946 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3947 | const struct io_uring_sqe *sqe) |
| 3948 | { |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 3949 | unsigned long size; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3950 | struct io_provide_buf *p = &req->pbuf; |
| 3951 | u64 tmp; |
| 3952 | |
| 3953 | if (sqe->ioprio || sqe->rw_flags) |
| 3954 | return -EINVAL; |
| 3955 | |
| 3956 | tmp = READ_ONCE(sqe->fd); |
| 3957 | if (!tmp || tmp > USHRT_MAX) |
| 3958 | return -E2BIG; |
| 3959 | p->nbufs = tmp; |
| 3960 | p->addr = READ_ONCE(sqe->addr); |
| 3961 | p->len = READ_ONCE(sqe->len); |
| 3962 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 3963 | size = (unsigned long)p->len * p->nbufs; |
| 3964 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3965 | return -EFAULT; |
| 3966 | |
| 3967 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3968 | tmp = READ_ONCE(sqe->off); |
| 3969 | if (tmp > USHRT_MAX) |
| 3970 | return -E2BIG; |
| 3971 | p->bid = tmp; |
| 3972 | return 0; |
| 3973 | } |
| 3974 | |
| 3975 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3976 | { |
| 3977 | struct io_buffer *buf; |
| 3978 | u64 addr = pbuf->addr; |
| 3979 | int i, bid = pbuf->bid; |
| 3980 | |
| 3981 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3982 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3983 | if (!buf) |
| 3984 | break; |
| 3985 | |
| 3986 | buf->addr = addr; |
| 3987 | buf->len = pbuf->len; |
| 3988 | buf->bid = bid; |
| 3989 | addr += pbuf->len; |
| 3990 | bid++; |
| 3991 | if (!*head) { |
| 3992 | INIT_LIST_HEAD(&buf->list); |
| 3993 | *head = buf; |
| 3994 | } else { |
| 3995 | list_add_tail(&buf->list, &(*head)->list); |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | return i ? i : -ENOMEM; |
| 4000 | } |
| 4001 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4002 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4003 | { |
| 4004 | struct io_provide_buf *p = &req->pbuf; |
| 4005 | struct io_ring_ctx *ctx = req->ctx; |
| 4006 | struct io_buffer *head, *list; |
| 4007 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4008 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4009 | |
| 4010 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4011 | |
| 4012 | lockdep_assert_held(&ctx->uring_lock); |
| 4013 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4014 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4015 | |
| 4016 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4017 | if (ret >= 0 && !list) { |
| 4018 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4019 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4020 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4021 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4022 | if (ret < 0) |
| 4023 | req_set_fail_links(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4024 | /* complete before unlock, IOPOLL may need the lock */ |
| 4025 | __io_req_complete(req, issue_flags, ret, 0); |
| 4026 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4027 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4028 | } |
| 4029 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4030 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4031 | const struct io_uring_sqe *sqe) |
| 4032 | { |
| 4033 | #if defined(CONFIG_EPOLL) |
| 4034 | if (sqe->ioprio || sqe->buf_index) |
| 4035 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4036 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4037 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4038 | |
| 4039 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4040 | req->epoll.op = READ_ONCE(sqe->len); |
| 4041 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4042 | |
| 4043 | if (ep_op_has_event(req->epoll.op)) { |
| 4044 | struct epoll_event __user *ev; |
| 4045 | |
| 4046 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4047 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4048 | return -EFAULT; |
| 4049 | } |
| 4050 | |
| 4051 | return 0; |
| 4052 | #else |
| 4053 | return -EOPNOTSUPP; |
| 4054 | #endif |
| 4055 | } |
| 4056 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4057 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4058 | { |
| 4059 | #if defined(CONFIG_EPOLL) |
| 4060 | struct io_epoll *ie = &req->epoll; |
| 4061 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4062 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4063 | |
| 4064 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4065 | if (force_nonblock && ret == -EAGAIN) |
| 4066 | return -EAGAIN; |
| 4067 | |
| 4068 | if (ret < 0) |
| 4069 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4070 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4071 | return 0; |
| 4072 | #else |
| 4073 | return -EOPNOTSUPP; |
| 4074 | #endif |
| 4075 | } |
| 4076 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4077 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4078 | { |
| 4079 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4080 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4081 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4082 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4083 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4084 | |
| 4085 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4086 | req->madvise.len = READ_ONCE(sqe->len); |
| 4087 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4088 | return 0; |
| 4089 | #else |
| 4090 | return -EOPNOTSUPP; |
| 4091 | #endif |
| 4092 | } |
| 4093 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4094 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4095 | { |
| 4096 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4097 | struct io_madvise *ma = &req->madvise; |
| 4098 | int ret; |
| 4099 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4100 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4101 | return -EAGAIN; |
| 4102 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4103 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4104 | if (ret < 0) |
| 4105 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4106 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4107 | return 0; |
| 4108 | #else |
| 4109 | return -EOPNOTSUPP; |
| 4110 | #endif |
| 4111 | } |
| 4112 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4113 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4114 | { |
| 4115 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4116 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4117 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4118 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4119 | |
| 4120 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4121 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4122 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4123 | return 0; |
| 4124 | } |
| 4125 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4126 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4127 | { |
| 4128 | struct io_fadvise *fa = &req->fadvise; |
| 4129 | int ret; |
| 4130 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4131 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4132 | switch (fa->advice) { |
| 4133 | case POSIX_FADV_NORMAL: |
| 4134 | case POSIX_FADV_RANDOM: |
| 4135 | case POSIX_FADV_SEQUENTIAL: |
| 4136 | break; |
| 4137 | default: |
| 4138 | return -EAGAIN; |
| 4139 | } |
| 4140 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4141 | |
| 4142 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4143 | if (ret < 0) |
| 4144 | req_set_fail_links(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4145 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4146 | return 0; |
| 4147 | } |
| 4148 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4149 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4150 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4151 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4152 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4153 | if (sqe->ioprio || sqe->buf_index) |
| 4154 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4155 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4156 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4157 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4158 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4159 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4160 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4161 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4162 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4163 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4164 | return 0; |
| 4165 | } |
| 4166 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4167 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4168 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4169 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4170 | int ret; |
| 4171 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4172 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4173 | return -EAGAIN; |
| 4174 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4175 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4176 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4177 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4178 | if (ret < 0) |
| 4179 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4180 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4181 | return 0; |
| 4182 | } |
| 4183 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4184 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4185 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4186 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4187 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4188 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4189 | sqe->rw_flags || sqe->buf_index) |
| 4190 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4191 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4192 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4193 | |
| 4194 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
| 4197 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4198 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4199 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4200 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4201 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4202 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4203 | struct file *file = NULL; |
| 4204 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4205 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4206 | spin_lock(&files->file_lock); |
| 4207 | fdt = files_fdtable(files); |
| 4208 | if (close->fd >= fdt->max_fds) { |
| 4209 | spin_unlock(&files->file_lock); |
| 4210 | goto err; |
| 4211 | } |
| 4212 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4213 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4214 | spin_unlock(&files->file_lock); |
| 4215 | file = NULL; |
| 4216 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4217 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4218 | |
| 4219 | /* if the file has a flush method, be safe and punt to async */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4220 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4221 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4222 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4223 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4224 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4225 | ret = __close_fd_get_file(close->fd, &file); |
| 4226 | spin_unlock(&files->file_lock); |
| 4227 | if (ret < 0) { |
| 4228 | if (ret == -ENOENT) |
| 4229 | ret = -EBADF; |
| 4230 | goto err; |
| 4231 | } |
| 4232 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4233 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4234 | ret = filp_close(file, current->files); |
| 4235 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4236 | if (ret < 0) |
| 4237 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4238 | if (file) |
| 4239 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4240 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4241 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4242 | } |
| 4243 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4244 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4245 | { |
| 4246 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4247 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4248 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4249 | return -EINVAL; |
| 4250 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4251 | return -EINVAL; |
| 4252 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4253 | req->sync.off = READ_ONCE(sqe->off); |
| 4254 | req->sync.len = READ_ONCE(sqe->len); |
| 4255 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4256 | return 0; |
| 4257 | } |
| 4258 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4259 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4260 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4261 | int ret; |
| 4262 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4263 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4264 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4265 | return -EAGAIN; |
| 4266 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4267 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4268 | req->sync.flags); |
| 4269 | if (ret < 0) |
| 4270 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4271 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4272 | return 0; |
| 4273 | } |
| 4274 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4275 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4276 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4277 | struct io_async_msghdr *kmsg) |
| 4278 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4279 | struct io_async_msghdr *async_msg = req->async_data; |
| 4280 | |
| 4281 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4282 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4283 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4284 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4285 | return -ENOMEM; |
| 4286 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4287 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4288 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4289 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4290 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4291 | /* if were using fast_iov, set it to the new one */ |
| 4292 | if (!async_msg->free_iov) |
| 4293 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4294 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4295 | return -EAGAIN; |
| 4296 | } |
| 4297 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4298 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4299 | struct io_async_msghdr *iomsg) |
| 4300 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4301 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4302 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4303 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4304 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4305 | } |
| 4306 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4307 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4308 | { |
| 4309 | int ret; |
| 4310 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4311 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4312 | if (!ret) |
| 4313 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4314 | return ret; |
| 4315 | } |
| 4316 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4317 | 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] | 4318 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4319 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4320 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4321 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4322 | return -EINVAL; |
| 4323 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4324 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4325 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4326 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4327 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4328 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4329 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4330 | #ifdef CONFIG_COMPAT |
| 4331 | if (req->ctx->compat) |
| 4332 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4333 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4334 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4335 | } |
| 4336 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4337 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4338 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4339 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4340 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4341 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4342 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4343 | int ret; |
| 4344 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4345 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4346 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4347 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4348 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4349 | kmsg = req->async_data; |
| 4350 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4351 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4352 | if (ret) |
| 4353 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4354 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4355 | } |
| 4356 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4357 | flags = req->sr_msg.msg_flags; |
| 4358 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4359 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4360 | if (flags & MSG_WAITALL) |
| 4361 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4362 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4363 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4364 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4365 | return io_setup_async_msg(req, kmsg); |
| 4366 | if (ret == -ERESTARTSYS) |
| 4367 | ret = -EINTR; |
| 4368 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4369 | /* fast path, check for non-NULL to avoid function call */ |
| 4370 | if (kmsg->free_iov) |
| 4371 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4372 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4373 | if (ret < min_ret) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4374 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4375 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4376 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4377 | } |
| 4378 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4379 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4380 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4381 | struct io_sr_msg *sr = &req->sr_msg; |
| 4382 | struct msghdr msg; |
| 4383 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4384 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4385 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4386 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4387 | int ret; |
| 4388 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4389 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4390 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4391 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4392 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4393 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4394 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4395 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4396 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4397 | msg.msg_name = NULL; |
| 4398 | msg.msg_control = NULL; |
| 4399 | msg.msg_controllen = 0; |
| 4400 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4401 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4402 | flags = req->sr_msg.msg_flags; |
| 4403 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4404 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4405 | if (flags & MSG_WAITALL) |
| 4406 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4407 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4408 | msg.msg_flags = flags; |
| 4409 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4410 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4411 | return -EAGAIN; |
| 4412 | if (ret == -ERESTARTSYS) |
| 4413 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4414 | |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4415 | if (ret < min_ret) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4416 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4417 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4418 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4419 | } |
| 4420 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4421 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4422 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4423 | { |
| 4424 | struct io_sr_msg *sr = &req->sr_msg; |
| 4425 | struct iovec __user *uiov; |
| 4426 | size_t iov_len; |
| 4427 | int ret; |
| 4428 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4429 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4430 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4431 | if (ret) |
| 4432 | return ret; |
| 4433 | |
| 4434 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4435 | if (iov_len > 1) |
| 4436 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4437 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4438 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4439 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4440 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4441 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4442 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4443 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4444 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4445 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4446 | if (ret > 0) |
| 4447 | ret = 0; |
| 4448 | } |
| 4449 | |
| 4450 | return ret; |
| 4451 | } |
| 4452 | |
| 4453 | #ifdef CONFIG_COMPAT |
| 4454 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4455 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4456 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4457 | struct io_sr_msg *sr = &req->sr_msg; |
| 4458 | struct compat_iovec __user *uiov; |
| 4459 | compat_uptr_t ptr; |
| 4460 | compat_size_t len; |
| 4461 | int ret; |
| 4462 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 4463 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 4464 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4465 | if (ret) |
| 4466 | return ret; |
| 4467 | |
| 4468 | uiov = compat_ptr(ptr); |
| 4469 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4470 | compat_ssize_t clen; |
| 4471 | |
| 4472 | if (len > 1) |
| 4473 | return -EINVAL; |
| 4474 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4475 | return -EFAULT; |
| 4476 | if (__get_user(clen, &uiov->iov_len)) |
| 4477 | return -EFAULT; |
| 4478 | if (clen < 0) |
| 4479 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4480 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4481 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4482 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4483 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4484 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4485 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4486 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4487 | if (ret < 0) |
| 4488 | return ret; |
| 4489 | } |
| 4490 | |
| 4491 | return 0; |
| 4492 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4493 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4494 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4495 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4496 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4497 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4498 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4499 | |
| 4500 | #ifdef CONFIG_COMPAT |
| 4501 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4502 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4503 | #endif |
| 4504 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4505 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4506 | } |
| 4507 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4508 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4509 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4510 | { |
| 4511 | struct io_sr_msg *sr = &req->sr_msg; |
| 4512 | struct io_buffer *kbuf; |
| 4513 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4514 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4515 | if (IS_ERR(kbuf)) |
| 4516 | return kbuf; |
| 4517 | |
| 4518 | sr->kbuf = kbuf; |
| 4519 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4520 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4521 | } |
| 4522 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4523 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4524 | { |
| 4525 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4526 | } |
| 4527 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4528 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4529 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4530 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4531 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4532 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4533 | if (!ret) |
| 4534 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4535 | return ret; |
| 4536 | } |
| 4537 | |
| 4538 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4539 | { |
| 4540 | struct io_sr_msg *sr = &req->sr_msg; |
| 4541 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4542 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4543 | return -EINVAL; |
| 4544 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4545 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4546 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4547 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4548 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4549 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4550 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4551 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4552 | #ifdef CONFIG_COMPAT |
| 4553 | if (req->ctx->compat) |
| 4554 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4555 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4556 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4559 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4560 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4561 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4562 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4563 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4564 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4565 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4566 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4567 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4568 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4569 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4570 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4571 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4572 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4573 | kmsg = req->async_data; |
| 4574 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4575 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4576 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4577 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4578 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4579 | } |
| 4580 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4581 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4582 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4583 | if (IS_ERR(kbuf)) |
| 4584 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4585 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4586 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4587 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4588 | 1, req->sr_msg.len); |
| 4589 | } |
| 4590 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4591 | flags = req->sr_msg.msg_flags; |
| 4592 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4593 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4594 | if (flags & MSG_WAITALL) |
| 4595 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4596 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4597 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4598 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4599 | if (force_nonblock && ret == -EAGAIN) |
| 4600 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4601 | if (ret == -ERESTARTSYS) |
| 4602 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4603 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4604 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4605 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4606 | /* fast path, check for non-NULL to avoid function call */ |
| 4607 | if (kmsg->free_iov) |
| 4608 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4609 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4610 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4611 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4612 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4613 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4614 | } |
| 4615 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4616 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4617 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4618 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4619 | struct io_sr_msg *sr = &req->sr_msg; |
| 4620 | struct msghdr msg; |
| 4621 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4622 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4623 | struct iovec iov; |
| 4624 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4625 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4626 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4627 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4628 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4629 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4630 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4631 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4632 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4633 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4634 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4635 | if (IS_ERR(kbuf)) |
| 4636 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4637 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4638 | } |
| 4639 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4640 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4641 | if (unlikely(ret)) |
| 4642 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4643 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4644 | msg.msg_name = NULL; |
| 4645 | msg.msg_control = NULL; |
| 4646 | msg.msg_controllen = 0; |
| 4647 | msg.msg_namelen = 0; |
| 4648 | msg.msg_iocb = NULL; |
| 4649 | msg.msg_flags = 0; |
| 4650 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4651 | flags = req->sr_msg.msg_flags; |
| 4652 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4653 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4654 | if (flags & MSG_WAITALL) |
| 4655 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4656 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4657 | ret = sock_recvmsg(sock, &msg, flags); |
| 4658 | if (force_nonblock && ret == -EAGAIN) |
| 4659 | return -EAGAIN; |
| 4660 | if (ret == -ERESTARTSYS) |
| 4661 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4662 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4663 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4664 | cflags = io_put_recv_kbuf(req); |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4665 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4666 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4667 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4668 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4671 | 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] | 4672 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4673 | struct io_accept *accept = &req->accept; |
| 4674 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4675 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4676 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4677 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4678 | return -EINVAL; |
| 4679 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4680 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4681 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4682 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4683 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4684 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4685 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4686 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4687 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4688 | { |
| 4689 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4690 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4691 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4692 | int ret; |
| 4693 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4694 | if (req->file->f_flags & O_NONBLOCK) |
| 4695 | req->flags |= REQ_F_NOWAIT; |
| 4696 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4697 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4698 | accept->addr_len, accept->flags, |
| 4699 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4700 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4701 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4702 | if (ret < 0) { |
| 4703 | if (ret == -ERESTARTSYS) |
| 4704 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4705 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4706 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4707 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4708 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4711 | static int io_connect_prep_async(struct io_kiocb *req) |
| 4712 | { |
| 4713 | struct io_async_connect *io = req->async_data; |
| 4714 | struct io_connect *conn = &req->connect; |
| 4715 | |
| 4716 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 4717 | } |
| 4718 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4719 | 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] | 4720 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4721 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4722 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4723 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4724 | return -EINVAL; |
| 4725 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4726 | return -EINVAL; |
| 4727 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4728 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4729 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4730 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4731 | } |
| 4732 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4733 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4734 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4735 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4736 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4737 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4738 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4739 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4740 | if (req->async_data) { |
| 4741 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4742 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4743 | ret = move_addr_to_kernel(req->connect.addr, |
| 4744 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4745 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4746 | if (ret) |
| 4747 | goto out; |
| 4748 | io = &__io; |
| 4749 | } |
| 4750 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4751 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4752 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4753 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4754 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4755 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4756 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4757 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4758 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4759 | ret = -ENOMEM; |
| 4760 | goto out; |
| 4761 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4762 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4763 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4764 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4765 | if (ret == -ERESTARTSYS) |
| 4766 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4767 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4768 | if (ret < 0) |
| 4769 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4770 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4771 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4772 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4773 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4774 | #define IO_NETOP_FN(op) \ |
| 4775 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 4776 | { \ |
| 4777 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4778 | } |
| 4779 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4780 | #define IO_NETOP_PREP(op) \ |
| 4781 | IO_NETOP_FN(op) \ |
| 4782 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 4783 | { \ |
| 4784 | return -EOPNOTSUPP; \ |
| 4785 | } \ |
| 4786 | |
| 4787 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 4788 | IO_NETOP_PREP(op) \ |
| 4789 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 4790 | { \ |
| 4791 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4792 | } |
| 4793 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4794 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 4795 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 4796 | IO_NETOP_PREP_ASYNC(connect); |
| 4797 | IO_NETOP_PREP(accept); |
| 4798 | IO_NETOP_FN(send); |
| 4799 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4800 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4801 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4802 | struct io_poll_table { |
| 4803 | struct poll_table_struct pt; |
| 4804 | struct io_kiocb *req; |
| 4805 | int error; |
| 4806 | }; |
| 4807 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4808 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4809 | __poll_t mask, task_work_func_t func) |
| 4810 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4811 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4812 | |
| 4813 | /* for instances that support it check for an event match first: */ |
| 4814 | if (mask && !(mask & poll->events)) |
| 4815 | return 0; |
| 4816 | |
| 4817 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4818 | |
| 4819 | list_del_init(&poll->wait.entry); |
| 4820 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4821 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 4822 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4823 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4824 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4825 | * If this fails, then the task is exiting. When a task exits, the |
| 4826 | * work gets canceled, so just cancel this request as well instead |
| 4827 | * of executing it. We can't safely execute it anyway, as we may not |
| 4828 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4829 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 4830 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4831 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4832 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 4833 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4834 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4835 | return 1; |
| 4836 | } |
| 4837 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4838 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4839 | __acquires(&req->ctx->completion_lock) |
| 4840 | { |
| 4841 | struct io_ring_ctx *ctx = req->ctx; |
| 4842 | |
| 4843 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4844 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4845 | |
| 4846 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4847 | } |
| 4848 | |
| 4849 | spin_lock_irq(&ctx->completion_lock); |
| 4850 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4851 | add_wait_queue(poll->head, &poll->wait); |
| 4852 | return true; |
| 4853 | } |
| 4854 | |
| 4855 | return false; |
| 4856 | } |
| 4857 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4858 | 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] | 4859 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4860 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4861 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4862 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4863 | return req->apoll->double_poll; |
| 4864 | } |
| 4865 | |
| 4866 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 4867 | { |
| 4868 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4869 | return &req->poll; |
| 4870 | return &req->apoll->poll; |
| 4871 | } |
| 4872 | |
| 4873 | static void io_poll_remove_double(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 4874 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4875 | { |
| 4876 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4877 | |
| 4878 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4879 | |
| 4880 | if (poll && poll->head) { |
| 4881 | struct wait_queue_head *head = poll->head; |
| 4882 | |
| 4883 | spin_lock(&head->lock); |
| 4884 | list_del_init(&poll->wait.entry); |
| 4885 | if (poll->wait.private) |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 4886 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4887 | poll->head = NULL; |
| 4888 | spin_unlock(&head->lock); |
| 4889 | } |
| 4890 | } |
| 4891 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4892 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 4893 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4894 | { |
| 4895 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4896 | unsigned flags = IORING_CQE_F_MORE; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4897 | int error; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4898 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4899 | if (READ_ONCE(req->poll.canceled)) { |
Jens Axboe | 45ab03b | 2021-02-23 08:19:33 -0700 | [diff] [blame] | 4900 | error = -ECANCELED; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4901 | req->poll.events |= EPOLLONESHOT; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4902 | } else { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 4903 | error = mangle_poll(mask); |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4904 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 4905 | if (req->poll.events & EPOLLONESHOT) |
| 4906 | flags = 0; |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 4907 | if (!io_cqring_fill_event(req, error, flags)) { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 4908 | io_poll_remove_waitqs(req); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4909 | req->poll.done = true; |
| 4910 | flags = 0; |
| 4911 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4912 | io_commit_cqring(ctx); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4913 | return !(flags & IORING_CQE_F_MORE); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4914 | } |
| 4915 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4916 | static void io_poll_task_func(struct callback_head *cb) |
| 4917 | { |
| 4918 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4919 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4920 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4921 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4922 | if (io_poll_rewait(req, &req->poll)) { |
| 4923 | spin_unlock_irq(&ctx->completion_lock); |
| 4924 | } else { |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 4925 | bool done; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4926 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4927 | done = io_poll_complete(req, req->result); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4928 | if (done) { |
| 4929 | hash_del(&req->hash_node); |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 4930 | } else { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4931 | req->result = 0; |
| 4932 | add_wait_queue(req->poll.head, &req->poll.wait); |
| 4933 | } |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4934 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 4935 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4936 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4937 | if (done) { |
| 4938 | nxt = io_put_req_find_next(req); |
| 4939 | if (nxt) |
| 4940 | __io_req_task_submit(nxt); |
| 4941 | } |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4942 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4946 | int sync, void *key) |
| 4947 | { |
| 4948 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4949 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4950 | __poll_t mask = key_to_poll(key); |
| 4951 | |
| 4952 | /* for instances that support it check for an event match first: */ |
| 4953 | if (mask && !(mask & poll->events)) |
| 4954 | return 0; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4955 | if (!(poll->events & EPOLLONESHOT)) |
| 4956 | return poll->wait.func(&poll->wait, mode, sync, key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4957 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 4958 | list_del_init(&wait->entry); |
| 4959 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4960 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4961 | bool done; |
| 4962 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4963 | spin_lock(&poll->head->lock); |
| 4964 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4965 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4966 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4967 | /* make sure double remove sees this as being gone */ |
| 4968 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4969 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 4970 | if (!done) { |
| 4971 | /* use wait func handler, so it matches the rq type */ |
| 4972 | poll->wait.func(&poll->wait, mode, sync, key); |
| 4973 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4974 | } |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 4975 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4976 | return 1; |
| 4977 | } |
| 4978 | |
| 4979 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4980 | wait_queue_func_t wake_func) |
| 4981 | { |
| 4982 | poll->head = NULL; |
| 4983 | poll->done = false; |
| 4984 | poll->canceled = false; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 4985 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 4986 | /* mask in events that we always want/need */ |
| 4987 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4988 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4989 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4990 | } |
| 4991 | |
| 4992 | 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] | 4993 | struct wait_queue_head *head, |
| 4994 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4995 | { |
| 4996 | struct io_kiocb *req = pt->req; |
| 4997 | |
| 4998 | /* |
| 4999 | * If poll->head is already set, it's because the file being polled |
| 5000 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 5001 | * for write). Setup a separate io_poll_iocb if this happens. |
| 5002 | */ |
| 5003 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5004 | struct io_poll_iocb *poll_one = poll; |
| 5005 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5006 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5007 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5008 | pt->error = -EINVAL; |
| 5009 | return; |
| 5010 | } |
Jens Axboe | 1c3b3e6 | 2021-02-28 16:07:30 -0700 | [diff] [blame] | 5011 | /* double add on the same waitqueue head, ignore */ |
| 5012 | if (poll->head == head) |
| 5013 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5014 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5015 | if (!poll) { |
| 5016 | pt->error = -ENOMEM; |
| 5017 | return; |
| 5018 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5019 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5020 | req_ref_get(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5021 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5022 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | pt->error = 0; |
| 5026 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5027 | |
| 5028 | if (poll->events & EPOLLEXCLUSIVE) |
| 5029 | add_wait_queue_exclusive(head, &poll->wait); |
| 5030 | else |
| 5031 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5035 | struct poll_table_struct *p) |
| 5036 | { |
| 5037 | 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] | 5038 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5039 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5040 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5041 | } |
| 5042 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5043 | static void io_async_task_func(struct callback_head *cb) |
| 5044 | { |
| 5045 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5046 | struct async_poll *apoll = req->apoll; |
| 5047 | struct io_ring_ctx *ctx = req->ctx; |
| 5048 | |
| 5049 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5050 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5051 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5052 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5053 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
Pavel Begunkov | 0ea13b4 | 2021-04-09 09:13:21 +0100 | [diff] [blame] | 5056 | hash_del(&req->hash_node); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5057 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5058 | spin_unlock_irq(&ctx->completion_lock); |
| 5059 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5060 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5061 | __io_req_task_submit(req); |
| 5062 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 5063 | io_req_complete_failed(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5064 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5065 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5066 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5070 | void *key) |
| 5071 | { |
| 5072 | struct io_kiocb *req = wait->private; |
| 5073 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5074 | |
| 5075 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5076 | key_to_poll(key)); |
| 5077 | |
| 5078 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5079 | } |
| 5080 | |
| 5081 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5082 | { |
| 5083 | struct io_ring_ctx *ctx = req->ctx; |
| 5084 | struct hlist_head *list; |
| 5085 | |
| 5086 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5087 | hlist_add_head(&req->hash_node, list); |
| 5088 | } |
| 5089 | |
| 5090 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5091 | struct io_poll_iocb *poll, |
| 5092 | struct io_poll_table *ipt, __poll_t mask, |
| 5093 | wait_queue_func_t wake_func) |
| 5094 | __acquires(&ctx->completion_lock) |
| 5095 | { |
| 5096 | struct io_ring_ctx *ctx = req->ctx; |
| 5097 | bool cancel = false; |
| 5098 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5099 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5100 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5101 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5102 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5103 | |
| 5104 | ipt->pt._key = mask; |
| 5105 | ipt->req = req; |
| 5106 | ipt->error = -EINVAL; |
| 5107 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5108 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5109 | |
| 5110 | spin_lock_irq(&ctx->completion_lock); |
| 5111 | if (likely(poll->head)) { |
| 5112 | spin_lock(&poll->head->lock); |
| 5113 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5114 | if (ipt->error) |
| 5115 | cancel = true; |
| 5116 | ipt->error = 0; |
| 5117 | mask = 0; |
| 5118 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5119 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5120 | list_del_init(&poll->wait.entry); |
| 5121 | else if (cancel) |
| 5122 | WRITE_ONCE(poll->canceled, true); |
| 5123 | else if (!poll->done) /* actually waiting for an event */ |
| 5124 | io_poll_req_insert(req); |
| 5125 | spin_unlock(&poll->head->lock); |
| 5126 | } |
| 5127 | |
| 5128 | return mask; |
| 5129 | } |
| 5130 | |
| 5131 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5132 | { |
| 5133 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5134 | struct io_ring_ctx *ctx = req->ctx; |
| 5135 | struct async_poll *apoll; |
| 5136 | struct io_poll_table ipt; |
| 5137 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5138 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5139 | |
| 5140 | if (!req->file || !file_can_poll(req->file)) |
| 5141 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5142 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5143 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5144 | if (def->pollin) |
| 5145 | rw = READ; |
| 5146 | else if (def->pollout) |
| 5147 | rw = WRITE; |
| 5148 | else |
| 5149 | return false; |
| 5150 | /* if we can't nonblock try, then no point in arming a poll handler */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 5151 | if (!io_file_supports_async(req, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5152 | return false; |
| 5153 | |
| 5154 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5155 | if (unlikely(!apoll)) |
| 5156 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5157 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5158 | |
| 5159 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5160 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5161 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5162 | mask = EPOLLONESHOT; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5163 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5164 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5165 | if (def->pollout) |
| 5166 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5167 | |
| 5168 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5169 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5170 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5171 | mask &= ~POLLIN; |
| 5172 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5173 | mask |= POLLERR | POLLPRI; |
| 5174 | |
| 5175 | ipt.pt._qproc = io_async_queue_proc; |
| 5176 | |
| 5177 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5178 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5179 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5180 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5181 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5182 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5183 | kfree(apoll); |
| 5184 | return false; |
| 5185 | } |
| 5186 | spin_unlock_irq(&ctx->completion_lock); |
| 5187 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5188 | apoll->poll.events); |
| 5189 | return true; |
| 5190 | } |
| 5191 | |
| 5192 | static bool __io_poll_remove_one(struct io_kiocb *req, |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5193 | struct io_poll_iocb *poll, bool do_cancel) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5194 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5195 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5196 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5197 | |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5198 | if (!poll->head) |
| 5199 | return false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5200 | spin_lock(&poll->head->lock); |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5201 | if (do_cancel) |
| 5202 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5203 | if (!list_empty(&poll->wait.entry)) { |
| 5204 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5205 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5206 | } |
| 5207 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5208 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5209 | return do_complete; |
| 5210 | } |
| 5211 | |
Jens Axboe | b2c3f7e | 2021-02-23 08:58:04 -0700 | [diff] [blame] | 5212 | static bool io_poll_remove_waitqs(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5213 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5214 | { |
| 5215 | bool do_complete; |
| 5216 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5217 | io_poll_remove_double(req); |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5218 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5219 | |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5220 | if (req->opcode != IORING_OP_POLL_ADD && do_complete) { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5221 | struct async_poll *apoll = req->apoll; |
| 5222 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5223 | /* non-poll requests have submit ref still */ |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5224 | req_ref_put(req); |
| 5225 | kfree(apoll->double_poll); |
| 5226 | kfree(apoll); |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5227 | } |
Jens Axboe | b2c3f7e | 2021-02-23 08:58:04 -0700 | [diff] [blame] | 5228 | return do_complete; |
| 5229 | } |
| 5230 | |
| 5231 | static bool io_poll_remove_one(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5232 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | b2c3f7e | 2021-02-23 08:58:04 -0700 | [diff] [blame] | 5233 | { |
| 5234 | bool do_complete; |
| 5235 | |
| 5236 | do_complete = io_poll_remove_waitqs(req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5237 | if (do_complete) { |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 5238 | io_cqring_fill_event(req, -ECANCELED, 0); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5239 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5240 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5241 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5247 | /* |
| 5248 | * Returns true if we found and killed one or more poll requests |
| 5249 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5250 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5251 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5252 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5253 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5254 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5255 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5256 | |
| 5257 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5258 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5259 | struct hlist_head *list; |
| 5260 | |
| 5261 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5262 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5263 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5264 | posted += io_poll_remove_one(req); |
| 5265 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5266 | } |
| 5267 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5268 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5269 | if (posted) |
| 5270 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5271 | |
| 5272 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5273 | } |
| 5274 | |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5275 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5276 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5277 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5278 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5279 | struct io_kiocb *req; |
| 5280 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5281 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5282 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5283 | if (sqe_addr != req->user_data) |
| 5284 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5285 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5286 | } |
| 5287 | |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5288 | return NULL; |
| 5289 | } |
| 5290 | |
| 5291 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5292 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5293 | { |
| 5294 | struct io_kiocb *req; |
| 5295 | |
| 5296 | req = io_poll_find(ctx, sqe_addr); |
| 5297 | if (!req) |
| 5298 | return -ENOENT; |
| 5299 | if (io_poll_remove_one(req)) |
| 5300 | return 0; |
| 5301 | |
| 5302 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5303 | } |
| 5304 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5305 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5306 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5307 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5308 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5309 | return -EINVAL; |
| 5310 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5311 | sqe->poll_events) |
| 5312 | return -EINVAL; |
| 5313 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5314 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | /* |
| 5319 | * Find a running poll command that matches one specified in sqe->addr, |
| 5320 | * and remove it if found. |
| 5321 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5322 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5323 | { |
| 5324 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5325 | int ret; |
| 5326 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5327 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5328 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5329 | spin_unlock_irq(&ctx->completion_lock); |
| 5330 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5331 | if (ret < 0) |
| 5332 | req_set_fail_links(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 5333 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5334 | return 0; |
| 5335 | } |
| 5336 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5337 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5338 | void *key) |
| 5339 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5340 | struct io_kiocb *req = wait->private; |
| 5341 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5342 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5343 | 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] | 5344 | } |
| 5345 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5346 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5347 | struct poll_table_struct *p) |
| 5348 | { |
| 5349 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5350 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5351 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
Jens Axboe | eac406c6 | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5352 | } |
| 5353 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5354 | 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] | 5355 | { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5356 | u32 events, flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5357 | |
| 5358 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5359 | return -EINVAL; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5360 | if (sqe->ioprio || sqe->buf_index) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5361 | return -EINVAL; |
| 5362 | flags = READ_ONCE(sqe->len); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5363 | if (flags & ~(IORING_POLL_ADD_MULTI | IORING_POLL_UPDATE_EVENTS | |
| 5364 | IORING_POLL_UPDATE_USER_DATA)) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5365 | return -EINVAL; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5366 | events = READ_ONCE(sqe->poll32_events); |
| 5367 | #ifdef __BIG_ENDIAN |
| 5368 | events = swahw32(events); |
| 5369 | #endif |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5370 | if (!(flags & IORING_POLL_ADD_MULTI)) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5371 | events |= EPOLLONESHOT; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5372 | events = demangle_poll(events) | |
| 5373 | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
Pavel Begunkov | 66d2d00 | 2021-04-13 02:58:39 +0100 | [diff] [blame] | 5374 | |
| 5375 | if (flags & (IORING_POLL_UPDATE_EVENTS|IORING_POLL_UPDATE_USER_DATA)) { |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5376 | struct io_poll_update *poll_upd = &req->poll_update; |
| 5377 | |
| 5378 | req->flags |= REQ_F_POLL_UPDATE; |
| 5379 | poll_upd->events = events; |
| 5380 | poll_upd->old_user_data = READ_ONCE(sqe->addr); |
| 5381 | poll_upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5382 | poll_upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
| 5383 | if (poll_upd->update_user_data) |
| 5384 | poll_upd->new_user_data = READ_ONCE(sqe->off); |
Pavel Begunkov | 66d2d00 | 2021-04-13 02:58:39 +0100 | [diff] [blame] | 5385 | } else { |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5386 | struct io_poll_iocb *poll = &req->poll; |
| 5387 | |
| 5388 | poll->events = events; |
Pavel Begunkov | 66d2d00 | 2021-04-13 02:58:39 +0100 | [diff] [blame] | 5389 | if (sqe->off || sqe->addr) |
| 5390 | return -EINVAL; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5391 | } |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5392 | return 0; |
| 5393 | } |
| 5394 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5395 | static int __io_poll_add(struct io_kiocb *req) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5396 | { |
| 5397 | struct io_poll_iocb *poll = &req->poll; |
| 5398 | struct io_ring_ctx *ctx = req->ctx; |
| 5399 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5400 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5401 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5402 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5403 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5404 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5405 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5406 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5407 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5408 | ipt.error = 0; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5409 | io_poll_complete(req, mask); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5410 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5411 | spin_unlock_irq(&ctx->completion_lock); |
| 5412 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5413 | if (mask) { |
| 5414 | io_cqring_ev_posted(ctx); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5415 | if (poll->events & EPOLLONESHOT) |
| 5416 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5417 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5418 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5419 | } |
| 5420 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5421 | static int io_poll_update(struct io_kiocb *req) |
| 5422 | { |
| 5423 | struct io_ring_ctx *ctx = req->ctx; |
| 5424 | struct io_kiocb *preq; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5425 | bool completing; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5426 | int ret; |
| 5427 | |
| 5428 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5429 | preq = io_poll_find(ctx, req->poll_update.old_user_data); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5430 | if (!preq) { |
| 5431 | ret = -ENOENT; |
| 5432 | goto err; |
| 5433 | } else if (preq->opcode != IORING_OP_POLL_ADD) { |
| 5434 | /* don't allow internal poll updates */ |
| 5435 | ret = -EACCES; |
| 5436 | goto err; |
| 5437 | } |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5438 | |
| 5439 | /* |
| 5440 | * Don't allow racy completion with singleshot, as we cannot safely |
| 5441 | * update those. For multishot, if we're racing with completion, just |
| 5442 | * let completion re-add it. |
| 5443 | */ |
| 5444 | completing = !__io_poll_remove_one(preq, &preq->poll, false); |
| 5445 | if (completing && (preq->poll.events & EPOLLONESHOT)) { |
| 5446 | ret = -EALREADY; |
| 5447 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5448 | } |
| 5449 | /* we now have a detached poll request. reissue. */ |
| 5450 | ret = 0; |
| 5451 | err: |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5452 | if (ret < 0) { |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5453 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5454 | req_set_fail_links(req); |
| 5455 | io_req_complete(req, ret); |
| 5456 | return 0; |
| 5457 | } |
| 5458 | /* only mask one event flags, keep behavior flags */ |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5459 | if (req->poll_update.update_events) { |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5460 | preq->poll.events &= ~0xffff; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5461 | preq->poll.events |= req->poll_update.events & 0xffff; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5462 | preq->poll.events |= IO_POLL_UNMASK; |
| 5463 | } |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5464 | if (req->poll_update.update_user_data) |
| 5465 | preq->user_data = req->poll_update.new_user_data; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5466 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5467 | spin_unlock_irq(&ctx->completion_lock); |
| 5468 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5469 | /* complete update request, we're done with it */ |
| 5470 | io_req_complete(req, ret); |
| 5471 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5472 | if (!completing) { |
| 5473 | ret = __io_poll_add(preq); |
| 5474 | if (ret < 0) { |
| 5475 | req_set_fail_links(preq); |
| 5476 | io_req_complete(preq, ret); |
| 5477 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5478 | } |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
| 5482 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
| 5483 | { |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5484 | if (!(req->flags & REQ_F_POLL_UPDATE)) |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5485 | return __io_poll_add(req); |
| 5486 | return io_poll_update(req); |
| 5487 | } |
| 5488 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5489 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5490 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5491 | struct io_timeout_data *data = container_of(timer, |
| 5492 | struct io_timeout_data, timer); |
| 5493 | struct io_kiocb *req = data->req; |
| 5494 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5495 | unsigned long flags; |
| 5496 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5497 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5498 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5499 | atomic_set(&req->ctx->cq_timeouts, |
| 5500 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5501 | |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 5502 | io_cqring_fill_event(req, -ETIME, 0); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5503 | io_commit_cqring(ctx); |
| 5504 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5505 | |
| 5506 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5507 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5508 | io_put_req(req); |
| 5509 | return HRTIMER_NORESTART; |
| 5510 | } |
| 5511 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5512 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5513 | __u64 user_data) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5514 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5515 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5516 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5517 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5518 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5519 | |
| 5520 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5521 | found = user_data == req->user_data; |
| 5522 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5523 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5524 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5525 | if (!found) |
| 5526 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5527 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5528 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5529 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5530 | return ERR_PTR(-EALREADY); |
| 5531 | list_del_init(&req->timeout.list); |
| 5532 | return req; |
| 5533 | } |
| 5534 | |
| 5535 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5536 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5537 | { |
| 5538 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5539 | |
| 5540 | if (IS_ERR(req)) |
| 5541 | return PTR_ERR(req); |
| 5542 | |
| 5543 | req_set_fail_links(req); |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 5544 | io_cqring_fill_event(req, -ECANCELED, 0); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5545 | io_put_req_deferred(req, 1); |
| 5546 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5547 | } |
| 5548 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5549 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5550 | struct timespec64 *ts, enum hrtimer_mode mode) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5551 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5552 | { |
| 5553 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5554 | struct io_timeout_data *data; |
| 5555 | |
| 5556 | if (IS_ERR(req)) |
| 5557 | return PTR_ERR(req); |
| 5558 | |
| 5559 | req->timeout.off = 0; /* noseq */ |
| 5560 | data = req->async_data; |
| 5561 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5562 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5563 | data->timer.function = io_timeout_fn; |
| 5564 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5565 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5566 | } |
| 5567 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5568 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5569 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5570 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5571 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5572 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5573 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5574 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5575 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5576 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5577 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5578 | return -EINVAL; |
| 5579 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5580 | tr->addr = READ_ONCE(sqe->addr); |
| 5581 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5582 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5583 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5584 | return -EINVAL; |
| 5585 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5586 | return -EFAULT; |
| 5587 | } else if (tr->flags) { |
| 5588 | /* timeout removal doesn't support flags */ |
| 5589 | return -EINVAL; |
| 5590 | } |
| 5591 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5592 | return 0; |
| 5593 | } |
| 5594 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5595 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5596 | { |
| 5597 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5598 | : HRTIMER_MODE_REL; |
| 5599 | } |
| 5600 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5601 | /* |
| 5602 | * Remove or update an existing timeout command |
| 5603 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5604 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5605 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5606 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5607 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5608 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5609 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5610 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5611 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5612 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5613 | else |
| 5614 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5615 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5616 | |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 5617 | io_cqring_fill_event(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5618 | io_commit_cqring(ctx); |
| 5619 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5620 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5621 | if (ret < 0) |
| 5622 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5623 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5624 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5625 | } |
| 5626 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5627 | 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] | 5628 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5629 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5630 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5631 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5632 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5633 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5634 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5635 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5636 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5637 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5638 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5639 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5640 | flags = READ_ONCE(sqe->timeout_flags); |
| 5641 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5642 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5643 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5644 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5645 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5646 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5647 | return -ENOMEM; |
| 5648 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5649 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5650 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5651 | |
| 5652 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5653 | return -EFAULT; |
| 5654 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5655 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5656 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
Pavel Begunkov | 2482b58 | 2021-03-25 18:32:44 +0000 | [diff] [blame] | 5657 | if (is_timeout_link) |
| 5658 | io_req_track_inflight(req); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5659 | return 0; |
| 5660 | } |
| 5661 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5662 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5663 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5664 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5665 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5666 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5667 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5668 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5669 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5670 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5671 | /* |
| 5672 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5673 | * timeout event to be satisfied. If it isn't set, then this is |
| 5674 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5675 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5676 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5677 | entry = ctx->timeout_list.prev; |
| 5678 | goto add; |
| 5679 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5680 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5681 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5682 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5683 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5684 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5685 | * This is safe because ->completion_lock is held, and submissions |
| 5686 | * and completions are never mixed in the same ->completion_lock section. |
| 5687 | */ |
| 5688 | ctx->cq_last_tm_flush = tail; |
| 5689 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5690 | /* |
| 5691 | * Insertion sort, ensuring the first entry in the list is always |
| 5692 | * the one we need first. |
| 5693 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5694 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5695 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5696 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5697 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5698 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5699 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5700 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5701 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5702 | break; |
| 5703 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5704 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5705 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5706 | data->timer.function = io_timeout_fn; |
| 5707 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5708 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5709 | return 0; |
| 5710 | } |
| 5711 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5712 | struct io_cancel_data { |
| 5713 | struct io_ring_ctx *ctx; |
| 5714 | u64 user_data; |
| 5715 | }; |
| 5716 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5717 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5718 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5719 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5720 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5721 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5722 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5723 | } |
| 5724 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5725 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 5726 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5727 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5728 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5729 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5730 | int ret = 0; |
| 5731 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5732 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5733 | return -ENOENT; |
| 5734 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5735 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5736 | switch (cancel_ret) { |
| 5737 | case IO_WQ_CANCEL_OK: |
| 5738 | ret = 0; |
| 5739 | break; |
| 5740 | case IO_WQ_CANCEL_RUNNING: |
| 5741 | ret = -EALREADY; |
| 5742 | break; |
| 5743 | case IO_WQ_CANCEL_NOTFOUND: |
| 5744 | ret = -ENOENT; |
| 5745 | break; |
| 5746 | } |
| 5747 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5748 | return ret; |
| 5749 | } |
| 5750 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5751 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5752 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5753 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5754 | { |
| 5755 | unsigned long flags; |
| 5756 | int ret; |
| 5757 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5758 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5759 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 5760 | if (ret != -ENOENT) |
| 5761 | goto done; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5762 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5763 | if (ret != -ENOENT) |
| 5764 | goto done; |
| 5765 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5766 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5767 | if (!ret) |
| 5768 | ret = success_ret; |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 5769 | io_cqring_fill_event(req, ret, 0); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5770 | io_commit_cqring(ctx); |
| 5771 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5772 | io_cqring_ev_posted(ctx); |
| 5773 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5774 | if (ret < 0) |
| 5775 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5776 | } |
| 5777 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5778 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5779 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5780 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5781 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5782 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5783 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5784 | return -EINVAL; |
| 5785 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5786 | return -EINVAL; |
| 5787 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5788 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5789 | return 0; |
| 5790 | } |
| 5791 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5792 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5793 | { |
| 5794 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5795 | u64 sqe_addr = req->cancel.addr; |
| 5796 | struct io_tctx_node *node; |
| 5797 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5798 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5799 | /* tasks should wait for their io-wq threads, so safe w/o sync */ |
| 5800 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
| 5801 | spin_lock_irq(&ctx->completion_lock); |
| 5802 | if (ret != -ENOENT) |
| 5803 | goto done; |
| 5804 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5805 | if (ret != -ENOENT) |
| 5806 | goto done; |
| 5807 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5808 | if (ret != -ENOENT) |
| 5809 | goto done; |
| 5810 | spin_unlock_irq(&ctx->completion_lock); |
| 5811 | |
| 5812 | /* slow path, try all io-wq's */ |
| 5813 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 5814 | ret = -ENOENT; |
| 5815 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 5816 | struct io_uring_task *tctx = node->task->io_uring; |
| 5817 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5818 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 5819 | if (ret != -ENOENT) |
| 5820 | break; |
| 5821 | } |
| 5822 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 5823 | |
| 5824 | spin_lock_irq(&ctx->completion_lock); |
| 5825 | done: |
Pavel Begunkov | ff642164 | 2021-04-11 01:46:32 +0100 | [diff] [blame] | 5826 | io_cqring_fill_event(req, ret, 0); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5827 | io_commit_cqring(ctx); |
| 5828 | spin_unlock_irq(&ctx->completion_lock); |
| 5829 | io_cqring_ev_posted(ctx); |
| 5830 | |
| 5831 | if (ret < 0) |
| 5832 | req_set_fail_links(req); |
| 5833 | io_put_req(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5834 | return 0; |
| 5835 | } |
| 5836 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5837 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5838 | const struct io_uring_sqe *sqe) |
| 5839 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5840 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5841 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5842 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5843 | return -EINVAL; |
| 5844 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5845 | return -EINVAL; |
| 5846 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5847 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 5848 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 5849 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5850 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5851 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5852 | return 0; |
| 5853 | } |
| 5854 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5855 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5856 | { |
| 5857 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5858 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5859 | int ret; |
| 5860 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5861 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5862 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5863 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5864 | up.offset = req->rsrc_update.offset; |
| 5865 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5866 | |
| 5867 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5868 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5869 | mutex_unlock(&ctx->uring_lock); |
| 5870 | |
| 5871 | if (ret < 0) |
| 5872 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5873 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5874 | return 0; |
| 5875 | } |
| 5876 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5877 | 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] | 5878 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5879 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5880 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5881 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5882 | case IORING_OP_READV: |
| 5883 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5884 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5885 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5886 | case IORING_OP_WRITEV: |
| 5887 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5888 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5889 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5890 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5891 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5892 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5893 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5894 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5895 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5896 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5897 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5898 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5899 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5900 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5901 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5902 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5903 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5904 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5905 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5906 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5907 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5908 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5909 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5910 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5911 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5912 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5913 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5914 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5915 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5916 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5917 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5918 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5919 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5920 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5921 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5922 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5923 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5924 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5925 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5926 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5927 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5928 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5929 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5930 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5931 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5932 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5933 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5934 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5935 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5936 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5937 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5938 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5939 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5940 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5941 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 5942 | case IORING_OP_SHUTDOWN: |
| 5943 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5944 | case IORING_OP_RENAMEAT: |
| 5945 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5946 | case IORING_OP_UNLINKAT: |
| 5947 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5948 | } |
| 5949 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5950 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5951 | req->opcode); |
| 5952 | return-EINVAL; |
| 5953 | } |
| 5954 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5955 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5956 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 5957 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 5958 | return 0; |
| 5959 | if (WARN_ON_ONCE(req->async_data)) |
| 5960 | return -EFAULT; |
| 5961 | if (io_alloc_async_data(req)) |
| 5962 | return -EAGAIN; |
| 5963 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5964 | switch (req->opcode) { |
| 5965 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5966 | return io_rw_prep_async(req, READ); |
| 5967 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5968 | return io_rw_prep_async(req, WRITE); |
| 5969 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5970 | return io_sendmsg_prep_async(req); |
| 5971 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5972 | return io_recvmsg_prep_async(req); |
| 5973 | case IORING_OP_CONNECT: |
| 5974 | return io_connect_prep_async(req); |
| 5975 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 5976 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 5977 | req->opcode); |
| 5978 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5979 | } |
| 5980 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5981 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5982 | { |
| 5983 | struct io_kiocb *pos; |
| 5984 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5985 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5986 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5987 | io_for_each_link(pos, req) |
| 5988 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5989 | |
| 5990 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5991 | return total_submitted - nr_reqs; |
| 5992 | } |
| 5993 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5994 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5995 | { |
| 5996 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5997 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5998 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5999 | u32 seq; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6000 | |
| 6001 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6002 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 6003 | !(req->flags & REQ_F_IO_DRAIN))) |
| 6004 | return 0; |
| 6005 | |
| 6006 | seq = io_get_sequence(req); |
| 6007 | /* Still a chance to pass the sequence check */ |
| 6008 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6009 | return 0; |
| 6010 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6011 | ret = io_req_prep_async(req); |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6012 | if (ret) |
| 6013 | return ret; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6014 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6015 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 6016 | if (!de) |
| 6017 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6018 | |
| 6019 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6020 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6021 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6022 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 6023 | io_queue_async_work(req); |
| 6024 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6028 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6029 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6030 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6031 | spin_unlock_irq(&ctx->completion_lock); |
| 6032 | return -EIOCBQUEUED; |
| 6033 | } |
| 6034 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6035 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6036 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6037 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 6038 | switch (req->opcode) { |
| 6039 | case IORING_OP_READV: |
| 6040 | case IORING_OP_READ_FIXED: |
| 6041 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6042 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6043 | break; |
| 6044 | case IORING_OP_RECVMSG: |
| 6045 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6046 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6047 | break; |
| 6048 | } |
| 6049 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6050 | } |
| 6051 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6052 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6053 | switch (req->opcode) { |
| 6054 | case IORING_OP_READV: |
| 6055 | case IORING_OP_READ_FIXED: |
| 6056 | case IORING_OP_READ: |
| 6057 | case IORING_OP_WRITEV: |
| 6058 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6059 | case IORING_OP_WRITE: { |
| 6060 | struct io_async_rw *io = req->async_data; |
| 6061 | if (io->free_iovec) |
| 6062 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6063 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6064 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6065 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6066 | case IORING_OP_SENDMSG: { |
| 6067 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6068 | |
| 6069 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6070 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6071 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6072 | case IORING_OP_SPLICE: |
| 6073 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6074 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6075 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6076 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6077 | case IORING_OP_OPENAT: |
| 6078 | case IORING_OP_OPENAT2: |
| 6079 | if (req->open.filename) |
| 6080 | putname(req->open.filename); |
| 6081 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6082 | case IORING_OP_RENAMEAT: |
| 6083 | putname(req->rename.oldpath); |
| 6084 | putname(req->rename.newpath); |
| 6085 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6086 | case IORING_OP_UNLINKAT: |
| 6087 | putname(req->unlink.filename); |
| 6088 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6089 | } |
| 6090 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6091 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6092 | } |
| 6093 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6094 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6095 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6096 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6097 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6098 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6099 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6100 | if (req->work.creds && req->work.creds != current_cred()) |
| 6101 | creds = override_creds(req->work.creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6102 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6103 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6104 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6105 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6106 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6107 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6108 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6109 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6110 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6111 | break; |
| 6112 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6113 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6114 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6115 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6116 | break; |
| 6117 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6118 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6119 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6120 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6121 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6122 | break; |
| 6123 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6124 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6125 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6126 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6127 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6128 | break; |
| 6129 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6130 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6131 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6132 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6133 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6134 | break; |
| 6135 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6136 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6137 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6138 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6139 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6140 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6141 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6142 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6143 | break; |
| 6144 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6145 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6146 | break; |
| 6147 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6148 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6149 | break; |
| 6150 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6151 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6152 | break; |
| 6153 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6154 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6155 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6156 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6157 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6158 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6159 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6160 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6161 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6162 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6163 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6164 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6165 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6166 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6167 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6168 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6169 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6170 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6171 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6172 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6173 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6174 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6175 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6176 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6177 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6178 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6179 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6180 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6181 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6182 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6183 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6184 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6185 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6186 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6187 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6188 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6189 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6190 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6191 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6192 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6193 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6194 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6195 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6196 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6197 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6198 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6199 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6200 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6201 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6202 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6203 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6204 | default: |
| 6205 | ret = -EINVAL; |
| 6206 | break; |
| 6207 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6208 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6209 | if (creds) |
| 6210 | revert_creds(creds); |
| 6211 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6212 | if (ret) |
| 6213 | return ret; |
| 6214 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6215 | /* If the op doesn't have a file, we're not polling for it */ |
| 6216 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6217 | const bool in_async = io_wq_current_is_worker(); |
| 6218 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6219 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6220 | if (in_async) |
| 6221 | mutex_lock(&ctx->uring_lock); |
| 6222 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6223 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6224 | |
| 6225 | if (in_async) |
| 6226 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | return 0; |
| 6230 | } |
| 6231 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6232 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6233 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6234 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6235 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6236 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6237 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6238 | timeout = io_prep_linked_timeout(req); |
| 6239 | if (timeout) |
| 6240 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6241 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6242 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6243 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6244 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6245 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6246 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6247 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6248 | /* |
| 6249 | * We can get EAGAIN for polled IO even though we're |
| 6250 | * forcing a sync submission from here, since we can't |
| 6251 | * wait for request slots on the block side. |
| 6252 | */ |
| 6253 | if (ret != -EAGAIN) |
| 6254 | break; |
| 6255 | cond_resched(); |
| 6256 | } while (1); |
| 6257 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6258 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6259 | /* avoid locking problems by failing it from a clean context */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6260 | if (ret) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6261 | /* io-wq is going to take one down */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 6262 | req_ref_get(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6263 | io_req_task_queue_fail(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6264 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6265 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6266 | |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6267 | #define FFS_ASYNC_READ 0x1UL |
| 6268 | #define FFS_ASYNC_WRITE 0x2UL |
| 6269 | #ifdef CONFIG_64BIT |
| 6270 | #define FFS_ISREG 0x4UL |
| 6271 | #else |
| 6272 | #define FFS_ISREG 0x0UL |
| 6273 | #endif |
| 6274 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) |
| 6275 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6276 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6277 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6278 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6279 | struct io_fixed_file *table_l2; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6280 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6281 | table_l2 = table->files[i >> IORING_FILE_TABLE_SHIFT]; |
| 6282 | return &table_l2[i & IORING_FILE_TABLE_MASK]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6283 | } |
| 6284 | |
| 6285 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6286 | int index) |
| 6287 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6288 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6289 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6290 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6291 | } |
| 6292 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6293 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6294 | { |
| 6295 | unsigned long file_ptr = (unsigned long) file; |
| 6296 | |
| 6297 | if (__io_file_supports_async(file, READ)) |
| 6298 | file_ptr |= FFS_ASYNC_READ; |
| 6299 | if (__io_file_supports_async(file, WRITE)) |
| 6300 | file_ptr |= FFS_ASYNC_WRITE; |
| 6301 | if (S_ISREG(file_inode(file)->i_mode)) |
| 6302 | file_ptr |= FFS_ISREG; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6303 | file_slot->file_ptr = file_ptr; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6304 | } |
| 6305 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6306 | static struct file *io_file_get(struct io_submit_state *state, |
| 6307 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6308 | { |
| 6309 | struct io_ring_ctx *ctx = req->ctx; |
| 6310 | struct file *file; |
| 6311 | |
| 6312 | if (fixed) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6313 | unsigned long file_ptr; |
| 6314 | |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6315 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6316 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6317 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6318 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6319 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6320 | file_ptr &= ~FFS_MASK; |
| 6321 | /* mask in overlapping REQ_F and FFS bits */ |
| 6322 | req->flags |= (file_ptr << REQ_F_ASYNC_READ_BIT); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 6323 | io_req_set_rsrc_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6324 | } else { |
| 6325 | trace_io_uring_file_get(ctx, fd); |
| 6326 | file = __io_file_get(state, fd); |
Jens Axboe | d44f554 | 2021-03-12 08:27:05 -0700 | [diff] [blame] | 6327 | |
| 6328 | /* we don't allow fixed io_uring files */ |
| 6329 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6330 | io_req_track_inflight(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6331 | } |
| 6332 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6333 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6334 | } |
| 6335 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6336 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6337 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6338 | struct io_timeout_data *data = container_of(timer, |
| 6339 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6340 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6341 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6342 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6343 | |
| 6344 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6345 | prev = req->timeout.head; |
| 6346 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6347 | |
| 6348 | /* |
| 6349 | * We don't expect the list to be empty, that will only happen if we |
| 6350 | * race with the completion of the linked work. |
| 6351 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 6352 | if (prev && req_ref_inc_not_zero(prev)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6353 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6354 | else |
| 6355 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6356 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6357 | |
| 6358 | if (prev) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6359 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6360 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6361 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6362 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6363 | } |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6364 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6365 | return HRTIMER_NORESTART; |
| 6366 | } |
| 6367 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6368 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6369 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6370 | struct io_ring_ctx *ctx = req->ctx; |
| 6371 | |
| 6372 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6373 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6374 | * If the back reference is NULL, then our linked request finished |
| 6375 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6376 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6377 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6378 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6379 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6380 | data->timer.function = io_link_timeout_fn; |
| 6381 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6382 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6383 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6384 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6385 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6386 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6387 | } |
| 6388 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6389 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6390 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6391 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6392 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6393 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6394 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6395 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6396 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6397 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6398 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6399 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6400 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6401 | } |
| 6402 | |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6403 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6404 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6405 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6406 | int ret; |
| 6407 | |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6408 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6409 | |
| 6410 | /* |
| 6411 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6412 | * doesn't support non-blocking read/write attempts |
| 6413 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6414 | if (likely(!ret)) { |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6415 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6416 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6417 | struct io_ring_ctx *ctx = req->ctx; |
| 6418 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6419 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6420 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6421 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6422 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6423 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6424 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6425 | } |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6426 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
| 6427 | if (!io_arm_poll_handler(req)) { |
| 6428 | /* |
| 6429 | * Queued up for async execution, worker will release |
| 6430 | * submit reference when the iocb is actually submitted. |
| 6431 | */ |
| 6432 | io_queue_async_work(req); |
| 6433 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6434 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6435 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6436 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6437 | if (linked_timeout) |
| 6438 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6439 | } |
| 6440 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6441 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6442 | { |
| 6443 | int ret; |
| 6444 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6445 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6446 | if (ret) { |
| 6447 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6448 | fail_req: |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6449 | io_req_complete_failed(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6450 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6451 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6452 | ret = io_req_prep_async(req); |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6453 | if (unlikely(ret)) |
| 6454 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6455 | io_queue_async_work(req); |
| 6456 | } else { |
Pavel Begunkov | c5eef2b94 | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6457 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6458 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6459 | } |
| 6460 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6461 | /* |
| 6462 | * Check SQE restrictions (opcode and flags). |
| 6463 | * |
| 6464 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6465 | */ |
| 6466 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6467 | struct io_kiocb *req, |
| 6468 | unsigned int sqe_flags) |
| 6469 | { |
| 6470 | if (!ctx->restricted) |
| 6471 | return true; |
| 6472 | |
| 6473 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6474 | return false; |
| 6475 | |
| 6476 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6477 | ctx->restrictions.sqe_flags_required) |
| 6478 | return false; |
| 6479 | |
| 6480 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6481 | ctx->restrictions.sqe_flags_required)) |
| 6482 | return false; |
| 6483 | |
| 6484 | return true; |
| 6485 | } |
| 6486 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6487 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6488 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6489 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6490 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6491 | unsigned int sqe_flags; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6492 | int personality, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6493 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6494 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6495 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6496 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6497 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6498 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6499 | req->file = NULL; |
| 6500 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6501 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6502 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6503 | /* one is dropped after submission, the other at completion */ |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 6504 | atomic_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6505 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6506 | req->result = 0; |
Jens Axboe | 93e68e0 | 2021-03-09 07:02:21 -0700 | [diff] [blame] | 6507 | req->work.creds = NULL; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6508 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6509 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6510 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
| 6511 | req->flags = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6512 | return -EINVAL; |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6513 | } |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6514 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6515 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6516 | return -EINVAL; |
| 6517 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6518 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6519 | return -EACCES; |
| 6520 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6521 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6522 | !io_op_defs[req->opcode].buffer_select) |
| 6523 | return -EOPNOTSUPP; |
| 6524 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6525 | personality = READ_ONCE(sqe->personality); |
| 6526 | if (personality) { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 6527 | req->work.creds = xa_load(&ctx->personalities, personality); |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6528 | if (!req->work.creds) |
| 6529 | return -EINVAL; |
| 6530 | get_cred(req->work.creds); |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6531 | } |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6532 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6533 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6534 | /* |
| 6535 | * Plug now if we have more than 1 IO left after this, and the target |
| 6536 | * is potentially a read/write to block based storage. |
| 6537 | */ |
| 6538 | if (!state->plug_started && state->ios_left > 1 && |
| 6539 | io_op_defs[req->opcode].plug) { |
| 6540 | blk_start_plug(&state->plug); |
| 6541 | state->plug_started = true; |
| 6542 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6543 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6544 | if (io_op_defs[req->opcode].needs_file) { |
| 6545 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6546 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6547 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6548 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6549 | ret = -EBADF; |
| 6550 | } |
| 6551 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6552 | state->ios_left--; |
| 6553 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6554 | } |
| 6555 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6556 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6557 | const struct io_uring_sqe *sqe) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6558 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6559 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6560 | int ret; |
| 6561 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6562 | ret = io_init_req(ctx, req, sqe); |
| 6563 | if (unlikely(ret)) { |
| 6564 | fail_req: |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6565 | if (link->head) { |
| 6566 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6567 | link->head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6568 | io_req_complete_failed(link->head, -ECANCELED); |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6569 | link->head = NULL; |
| 6570 | } |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6571 | io_req_complete_failed(req, ret); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6572 | return ret; |
| 6573 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6574 | ret = io_req_prep(req, sqe); |
| 6575 | if (unlikely(ret)) |
| 6576 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6577 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6578 | /* don't need @sqe from now on */ |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6579 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 6580 | true, ctx->flags & IORING_SETUP_SQPOLL); |
| 6581 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6582 | /* |
| 6583 | * If we already have a head request, queue this one for async |
| 6584 | * submittal once the head completes. If we don't have a head but |
| 6585 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6586 | * submitted sync once the chain is complete. If none of those |
| 6587 | * conditions are true (normal request), then just queue it. |
| 6588 | */ |
| 6589 | if (link->head) { |
| 6590 | struct io_kiocb *head = link->head; |
| 6591 | |
| 6592 | /* |
| 6593 | * Taking sequential execution of a link, draining both sides |
| 6594 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6595 | * requests in the link. So, it drains the head and the |
| 6596 | * next after the link request. The last one is done via |
| 6597 | * drain_next flag to persist the effect across calls. |
| 6598 | */ |
| 6599 | if (req->flags & REQ_F_IO_DRAIN) { |
| 6600 | head->flags |= REQ_F_IO_DRAIN; |
| 6601 | ctx->drain_next = 1; |
| 6602 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6603 | ret = io_req_prep_async(req); |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6604 | if (unlikely(ret)) |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6605 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6606 | trace_io_uring_link(ctx, req, head); |
| 6607 | link->last->link = req; |
| 6608 | link->last = req; |
| 6609 | |
| 6610 | /* last request of a link, enqueue the link */ |
| 6611 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6612 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6613 | link->head = NULL; |
| 6614 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6615 | } else { |
| 6616 | if (unlikely(ctx->drain_next)) { |
| 6617 | req->flags |= REQ_F_IO_DRAIN; |
| 6618 | ctx->drain_next = 0; |
| 6619 | } |
| 6620 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6621 | link->head = req; |
| 6622 | link->last = req; |
| 6623 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6624 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6625 | } |
| 6626 | } |
| 6627 | |
| 6628 | return 0; |
| 6629 | } |
| 6630 | |
| 6631 | /* |
| 6632 | * Batched submission is done, ensure local IO is flushed out. |
| 6633 | */ |
| 6634 | static void io_submit_state_end(struct io_submit_state *state, |
| 6635 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6636 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6637 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6638 | io_queue_sqe(state->link.head); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6639 | if (state->comp.nr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6640 | io_submit_flush_completions(&state->comp, ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6641 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6642 | blk_finish_plug(&state->plug); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6643 | io_state_file_put(state); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6644 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6645 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6646 | /* |
| 6647 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6648 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6649 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6650 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6651 | { |
| 6652 | state->plug_started = false; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6653 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6654 | /* set only head, no need to init link_last in advance */ |
| 6655 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6656 | } |
| 6657 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6658 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6659 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6660 | struct io_rings *rings = ctx->rings; |
| 6661 | |
| 6662 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6663 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6664 | * since once we write the new head, the application could |
| 6665 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 6666 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6667 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6668 | } |
| 6669 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6670 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6671 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6672 | * that is mapped by userspace. This means that care needs to be taken to |
| 6673 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 6674 | * being a good citizen. If members of the sqe are validated and then later |
| 6675 | * used, it's important that those reads are done through READ_ONCE() to |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 6676 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6677 | */ |
| 6678 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6679 | { |
| 6680 | u32 *sq_array = ctx->sq_array; |
| 6681 | unsigned head; |
| 6682 | |
| 6683 | /* |
| 6684 | * The cached sq head (or cq tail) serves two purposes: |
| 6685 | * |
| 6686 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6687 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6688 | * 2) allows the kernel side to track the head on its own, even |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 6689 | * though the application is the one updating it. |
| 6690 | */ |
| 6691 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
| 6692 | if (likely(head < ctx->sq_entries)) |
| 6693 | return &ctx->sq_sqes[head]; |
| 6694 | |
| 6695 | /* drop invalid entries */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6696 | ctx->cached_sq_dropped++; |
| 6697 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
| 6698 | return NULL; |
| 6699 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 6700 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6701 | 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] | 6702 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6703 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6704 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6705 | /* 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] | 6706 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 6707 | if (!__io_cqring_overflow_flush(ctx, false)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6708 | return -EBUSY; |
| 6709 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6710 | |
Pavel Begunkov | ee7d46d9 | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6711 | /* make sure SQ entry isn't read before tail */ |
| 6712 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6713 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6714 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6715 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6716 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6717 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6718 | refcount_add(nr, ¤t->usage); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6719 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6720 | |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6721 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6722 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6723 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6724 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6725 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6726 | if (unlikely(!req)) { |
| 6727 | if (!submitted) |
| 6728 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6729 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6730 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6731 | sqe = io_get_sqe(ctx); |
| 6732 | if (unlikely(!sqe)) { |
| 6733 | kmem_cache_free(req_cachep, req); |
| 6734 | break; |
| 6735 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6736 | /* will complete beyond this point, count as submitted */ |
| 6737 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6738 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6739 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6740 | } |
| 6741 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6742 | if (unlikely(submitted != nr)) { |
| 6743 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6744 | struct io_uring_task *tctx = current->io_uring; |
| 6745 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6746 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6747 | percpu_ref_put_many(&ctx->refs, unused); |
| 6748 | percpu_counter_sub(&tctx->inflight, unused); |
| 6749 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6750 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6751 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6752 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6753 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6754 | io_commit_sqring(ctx); |
| 6755 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6756 | return submitted; |
| 6757 | } |
| 6758 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6759 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6760 | { |
| 6761 | /* Tell userspace we may need a wakeup call */ |
| 6762 | spin_lock_irq(&ctx->completion_lock); |
| 6763 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6764 | spin_unlock_irq(&ctx->completion_lock); |
| 6765 | } |
| 6766 | |
| 6767 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6768 | { |
| 6769 | spin_lock_irq(&ctx->completion_lock); |
| 6770 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6771 | spin_unlock_irq(&ctx->completion_lock); |
| 6772 | } |
| 6773 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6774 | 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] | 6775 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6776 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6777 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6778 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6779 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6780 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6781 | if (cap_entries && to_submit > 8) |
| 6782 | to_submit = 8; |
| 6783 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6784 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6785 | unsigned nr_events = 0; |
| 6786 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6787 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6788 | if (!list_empty(&ctx->iopoll_list)) |
| 6789 | io_do_iopoll(ctx, &nr_events, 0); |
| 6790 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 6791 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 6792 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6793 | ret = io_submit_sqes(ctx, to_submit); |
| 6794 | mutex_unlock(&ctx->uring_lock); |
| 6795 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6796 | |
| 6797 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6798 | wake_up(&ctx->sqo_sq_wait); |
| 6799 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6800 | return ret; |
| 6801 | } |
| 6802 | |
| 6803 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 6804 | { |
| 6805 | struct io_ring_ctx *ctx; |
| 6806 | unsigned sq_thread_idle = 0; |
| 6807 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 6808 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6809 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6810 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6811 | } |
| 6812 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6813 | static int io_sq_thread(void *data) |
| 6814 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6815 | struct io_sq_data *sqd = data; |
| 6816 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205f | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 6817 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6818 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6819 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6820 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 6821 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6822 | set_task_comm(current, buf); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6823 | current->pf_io_worker = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6824 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6825 | if (sqd->sq_cpu != -1) |
| 6826 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 6827 | else |
| 6828 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 6829 | current->flags |= PF_NO_SETAFFINITY; |
| 6830 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6831 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6832 | while (!test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6833 | int ret; |
| 6834 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6835 | |
Jens Axboe | 82734c5 | 2021-03-29 06:52:44 -0600 | [diff] [blame] | 6836 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 6837 | signal_pending(current)) { |
| 6838 | bool did_sig = false; |
| 6839 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6840 | mutex_unlock(&sqd->lock); |
Jens Axboe | 82734c5 | 2021-03-29 06:52:44 -0600 | [diff] [blame] | 6841 | if (signal_pending(current)) { |
| 6842 | struct ksignal ksig; |
| 6843 | |
| 6844 | did_sig = get_signal(&ksig); |
| 6845 | } |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6846 | cond_resched(); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6847 | mutex_lock(&sqd->lock); |
Jens Axboe | 82734c5 | 2021-03-29 06:52:44 -0600 | [diff] [blame] | 6848 | if (did_sig) |
| 6849 | break; |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 6850 | io_run_task_work(); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 6851 | io_run_task_work_head(&sqd->park_task_work); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6852 | timeout = jiffies + sqd->sq_thread_idle; |
Pavel Begunkov | 7d41e85 | 2021-03-10 13:13:54 +0000 | [diff] [blame] | 6853 | continue; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6854 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6855 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6856 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6857 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 6858 | const struct cred *creds = NULL; |
| 6859 | |
| 6860 | if (ctx->sq_creds != current_cred()) |
| 6861 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6862 | ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 6863 | if (creds) |
| 6864 | revert_creds(creds); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6865 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 6866 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6867 | } |
| 6868 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6869 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6870 | io_run_task_work(); |
| 6871 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6872 | if (sqt_spin) |
| 6873 | timeout = jiffies + sqd->sq_thread_idle; |
| 6874 | continue; |
| 6875 | } |
| 6876 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6877 | needs_sched = true; |
| 6878 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 6879 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6880 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6881 | !list_empty_careful(&ctx->iopoll_list)) { |
| 6882 | needs_sched = false; |
| 6883 | break; |
| 6884 | } |
| 6885 | if (io_sqring_entries(ctx)) { |
| 6886 | needs_sched = false; |
| 6887 | break; |
| 6888 | } |
| 6889 | } |
| 6890 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6891 | if (needs_sched && !test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6892 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6893 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6894 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6895 | mutex_unlock(&sqd->lock); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6896 | schedule(); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6897 | mutex_lock(&sqd->lock); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6898 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6899 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6900 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6901 | |
| 6902 | finish_wait(&sqd->wait, &wait); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 6903 | io_run_task_work_head(&sqd->park_task_work); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6904 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6905 | } |
| 6906 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6907 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6908 | io_uring_cancel_sqpoll(ctx); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6909 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6910 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 6911 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6912 | mutex_unlock(&sqd->lock); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 6913 | |
| 6914 | io_run_task_work(); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 6915 | io_run_task_work_head(&sqd->park_task_work); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6916 | complete(&sqd->exited); |
| 6917 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6918 | } |
| 6919 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6920 | struct io_wait_queue { |
| 6921 | struct wait_queue_entry wq; |
| 6922 | struct io_ring_ctx *ctx; |
| 6923 | unsigned to_wait; |
| 6924 | unsigned nr_timeouts; |
| 6925 | }; |
| 6926 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6927 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6928 | { |
| 6929 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6930 | |
| 6931 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6932 | * 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] | 6933 | * started waiting. For timeouts, we always want to return to userspace, |
| 6934 | * regardless of event count. |
| 6935 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6936 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6937 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6938 | } |
| 6939 | |
| 6940 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6941 | int wake_flags, void *key) |
| 6942 | { |
| 6943 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6944 | wq); |
| 6945 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6946 | /* |
| 6947 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 6948 | * the task, and the next invocation will do it. |
| 6949 | */ |
| 6950 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 6951 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6952 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6953 | } |
| 6954 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6955 | static int io_run_task_work_sig(void) |
| 6956 | { |
| 6957 | if (io_run_task_work()) |
| 6958 | return 1; |
| 6959 | if (!signal_pending(current)) |
| 6960 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 6961 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 6962 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6963 | return -EINTR; |
| 6964 | } |
| 6965 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6966 | /* when returns >0, the caller should retry */ |
| 6967 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 6968 | struct io_wait_queue *iowq, |
| 6969 | signed long *timeout) |
| 6970 | { |
| 6971 | int ret; |
| 6972 | |
| 6973 | /* make sure we run task_work before checking for signals */ |
| 6974 | ret = io_run_task_work_sig(); |
| 6975 | if (ret || io_should_wake(iowq)) |
| 6976 | return ret; |
| 6977 | /* let the caller flush overflows, retry */ |
| 6978 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 6979 | return 1; |
| 6980 | |
| 6981 | *timeout = schedule_timeout(*timeout); |
| 6982 | return !*timeout ? -ETIME : 1; |
| 6983 | } |
| 6984 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6985 | /* |
| 6986 | * Wait until events become available, if we don't already have some. The |
| 6987 | * application must reap them itself, as they reside on the shared cq ring. |
| 6988 | */ |
| 6989 | 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] | 6990 | const sigset_t __user *sig, size_t sigsz, |
| 6991 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6992 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6993 | struct io_wait_queue iowq = { |
| 6994 | .wq = { |
| 6995 | .private = current, |
| 6996 | .func = io_wake_function, |
| 6997 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6998 | }, |
| 6999 | .ctx = ctx, |
| 7000 | .to_wait = min_events, |
| 7001 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7002 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7003 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7004 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7005 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7006 | do { |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 7007 | io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7008 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7009 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7010 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7011 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7012 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7013 | |
| 7014 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7015 | #ifdef CONFIG_COMPAT |
| 7016 | if (in_compat_syscall()) |
| 7017 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7018 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7019 | else |
| 7020 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7021 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7022 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7023 | if (ret) |
| 7024 | return ret; |
| 7025 | } |
| 7026 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7027 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7028 | struct timespec64 ts; |
| 7029 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 7030 | if (get_timespec64(&ts, uts)) |
| 7031 | return -EFAULT; |
| 7032 | timeout = timespec64_to_jiffies(&ts); |
| 7033 | } |
| 7034 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7035 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7036 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7037 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7038 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 7039 | if (!io_cqring_overflow_flush(ctx, false)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7040 | ret = -EBUSY; |
| 7041 | break; |
| 7042 | } |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7043 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7044 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7045 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 7046 | finish_wait(&ctx->wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7047 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7048 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7049 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7050 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7051 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7052 | 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] | 7053 | } |
| 7054 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7055 | static void io_free_file_tables(struct io_file_table *table, unsigned nr_files) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7056 | { |
| 7057 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); |
| 7058 | |
| 7059 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7060 | kfree(table->files[i]); |
| 7061 | kfree(table->files); |
| 7062 | table->files = NULL; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7063 | } |
| 7064 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7065 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7066 | { |
| 7067 | #if defined(CONFIG_UNIX) |
| 7068 | if (ctx->ring_sock) { |
| 7069 | struct sock *sock = ctx->ring_sock->sk; |
| 7070 | struct sk_buff *skb; |
| 7071 | |
| 7072 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7073 | kfree_skb(skb); |
| 7074 | } |
| 7075 | #else |
| 7076 | int i; |
| 7077 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7078 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7079 | struct file *file; |
| 7080 | |
| 7081 | file = io_file_from_index(ctx, i); |
| 7082 | if (file) |
| 7083 | fput(file); |
| 7084 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7085 | #endif |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7086 | io_free_file_tables(&ctx->file_table, ctx->nr_user_files); |
| 7087 | kfree(ctx->file_data); |
| 7088 | ctx->file_data = NULL; |
| 7089 | ctx->nr_user_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7090 | } |
| 7091 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7092 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7093 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7094 | spin_lock_bh(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7097 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7098 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7099 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 7100 | } |
| 7101 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7102 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7103 | { |
| 7104 | percpu_ref_exit(&ref_node->refs); |
| 7105 | kfree(ref_node); |
| 7106 | } |
| 7107 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7108 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7109 | struct io_rsrc_data *data_to_kill) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7110 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7111 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7112 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7113 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7114 | if (data_to_kill) { |
| 7115 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7116 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7117 | rsrc_node->rsrc_data = data_to_kill; |
| 7118 | io_rsrc_ref_lock(ctx); |
| 7119 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
| 7120 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7121 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7122 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7123 | percpu_ref_kill(&rsrc_node->refs); |
| 7124 | ctx->rsrc_node = NULL; |
| 7125 | } |
| 7126 | |
| 7127 | if (!ctx->rsrc_node) { |
| 7128 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7129 | ctx->rsrc_backup_node = NULL; |
| 7130 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7131 | } |
| 7132 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7133 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7134 | { |
| 7135 | if (ctx->rsrc_backup_node) |
| 7136 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7137 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7138 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7139 | } |
| 7140 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7141 | static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7142 | { |
| 7143 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7144 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7145 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7146 | if (data->quiesce) |
| 7147 | return -ENXIO; |
| 7148 | |
| 7149 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7150 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7151 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7152 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7153 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7154 | io_rsrc_node_switch(ctx, data); |
| 7155 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7156 | /* kill initial ref, already quiesced if zero */ |
| 7157 | if (atomic_dec_and_test(&data->refs)) |
| 7158 | break; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7159 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7160 | ret = wait_for_completion_interruptible(&data->done); |
| 7161 | if (!ret) |
| 7162 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7163 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7164 | atomic_inc(&data->refs); |
| 7165 | /* wait for all works potentially completing data->done */ |
| 7166 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7167 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7168 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7169 | mutex_unlock(&ctx->uring_lock); |
| 7170 | ret = io_run_task_work_sig(); |
| 7171 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7172 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7173 | data->quiesce = false; |
| 7174 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7175 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7178 | static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx, |
| 7179 | rsrc_put_fn *do_put) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7180 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7181 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7182 | |
| 7183 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7184 | if (!data) |
| 7185 | return NULL; |
| 7186 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7187 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7188 | data->ctx = ctx; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7189 | data->do_put = do_put; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7190 | init_completion(&data->done); |
| 7191 | return data; |
| 7192 | } |
| 7193 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7194 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7195 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7196 | int ret; |
| 7197 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7198 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7199 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7200 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 7201 | if (!ret) |
| 7202 | __io_sqe_files_unregister(ctx); |
| 7203 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7204 | } |
| 7205 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7206 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7207 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7208 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7209 | WARN_ON_ONCE(sqd->thread == current); |
| 7210 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7211 | /* |
| 7212 | * Do the dance but not conditional clear_bit() because it'd race with |
| 7213 | * other threads incrementing park_pending and setting the bit. |
| 7214 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7215 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7216 | if (atomic_dec_return(&sqd->park_pending)) |
| 7217 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7218 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7219 | } |
| 7220 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7221 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7222 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7223 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7224 | WARN_ON_ONCE(sqd->thread == current); |
| 7225 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7226 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7227 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7228 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7229 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7230 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7234 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7235 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7236 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7237 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7238 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7239 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7240 | if (sqd->thread) |
| 7241 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7242 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7243 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7244 | } |
| 7245 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7246 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7247 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7248 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7249 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 7250 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7251 | io_sq_thread_stop(sqd); |
| 7252 | kfree(sqd); |
| 7253 | } |
| 7254 | } |
| 7255 | |
| 7256 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7257 | { |
| 7258 | struct io_sq_data *sqd = ctx->sq_data; |
| 7259 | |
| 7260 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7261 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7262 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7263 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7264 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7265 | |
| 7266 | io_put_sq_data(sqd); |
| 7267 | ctx->sq_data = NULL; |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7268 | if (ctx->sq_creds) |
| 7269 | put_cred(ctx->sq_creds); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7270 | } |
| 7271 | } |
| 7272 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7273 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7274 | { |
| 7275 | struct io_ring_ctx *ctx_attach; |
| 7276 | struct io_sq_data *sqd; |
| 7277 | struct fd f; |
| 7278 | |
| 7279 | f = fdget(p->wq_fd); |
| 7280 | if (!f.file) |
| 7281 | return ERR_PTR(-ENXIO); |
| 7282 | if (f.file->f_op != &io_uring_fops) { |
| 7283 | fdput(f); |
| 7284 | return ERR_PTR(-EINVAL); |
| 7285 | } |
| 7286 | |
| 7287 | ctx_attach = f.file->private_data; |
| 7288 | sqd = ctx_attach->sq_data; |
| 7289 | if (!sqd) { |
| 7290 | fdput(f); |
| 7291 | return ERR_PTR(-EINVAL); |
| 7292 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7293 | if (sqd->task_tgid != current->tgid) { |
| 7294 | fdput(f); |
| 7295 | return ERR_PTR(-EPERM); |
| 7296 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7297 | |
| 7298 | refcount_inc(&sqd->refs); |
| 7299 | fdput(f); |
| 7300 | return sqd; |
| 7301 | } |
| 7302 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7303 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 7304 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7305 | { |
| 7306 | struct io_sq_data *sqd; |
| 7307 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7308 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7309 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 7310 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7311 | if (!IS_ERR(sqd)) { |
| 7312 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7313 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7314 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7315 | /* fall through for EPERM case, setup new sqd/task */ |
| 7316 | if (PTR_ERR(sqd) != -EPERM) |
| 7317 | return sqd; |
| 7318 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7319 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7320 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7321 | if (!sqd) |
| 7322 | return ERR_PTR(-ENOMEM); |
| 7323 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7324 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7325 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7326 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7327 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7328 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7329 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7330 | return sqd; |
| 7331 | } |
| 7332 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7333 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7334 | /* |
| 7335 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7336 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7337 | * loops in the file referencing. |
| 7338 | */ |
| 7339 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7340 | { |
| 7341 | struct sock *sk = ctx->ring_sock->sk; |
| 7342 | struct scm_fp_list *fpl; |
| 7343 | struct sk_buff *skb; |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7344 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7345 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7346 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7347 | if (!fpl) |
| 7348 | return -ENOMEM; |
| 7349 | |
| 7350 | skb = alloc_skb(0, GFP_KERNEL); |
| 7351 | if (!skb) { |
| 7352 | kfree(fpl); |
| 7353 | return -ENOMEM; |
| 7354 | } |
| 7355 | |
| 7356 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7357 | |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7358 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7359 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7360 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7361 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7362 | |
| 7363 | if (!file) |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7364 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7365 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7366 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7367 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7368 | } |
| 7369 | |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7370 | if (nr_files) { |
| 7371 | fpl->max = SCM_MAX_FD; |
| 7372 | fpl->count = nr_files; |
| 7373 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7374 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7375 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7376 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7377 | |
Jens Axboe | 08a45173 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7378 | for (i = 0; i < nr_files; i++) |
| 7379 | fput(fpl->fp[i]); |
| 7380 | } else { |
| 7381 | kfree_skb(skb); |
| 7382 | kfree(fpl); |
| 7383 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7384 | |
| 7385 | return 0; |
| 7386 | } |
| 7387 | |
| 7388 | /* |
| 7389 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7390 | * causes regular reference counting to break down. We rely on the UNIX |
| 7391 | * garbage collection to take care of this problem for us. |
| 7392 | */ |
| 7393 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7394 | { |
| 7395 | unsigned left, total; |
| 7396 | int ret = 0; |
| 7397 | |
| 7398 | total = 0; |
| 7399 | left = ctx->nr_user_files; |
| 7400 | while (left) { |
| 7401 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7402 | |
| 7403 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7404 | if (ret) |
| 7405 | break; |
| 7406 | left -= this_files; |
| 7407 | total += this_files; |
| 7408 | } |
| 7409 | |
| 7410 | if (!ret) |
| 7411 | return 0; |
| 7412 | |
| 7413 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7414 | struct file *file = io_file_from_index(ctx, total); |
| 7415 | |
| 7416 | if (file) |
| 7417 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7418 | total++; |
| 7419 | } |
| 7420 | |
| 7421 | return ret; |
| 7422 | } |
| 7423 | #else |
| 7424 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7425 | { |
| 7426 | return 0; |
| 7427 | } |
| 7428 | #endif |
| 7429 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7430 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7431 | { |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7432 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); |
| 7433 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7434 | table->files = kcalloc(nr_tables, sizeof(*table->files), GFP_KERNEL); |
| 7435 | if (!table->files) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7436 | return false; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7437 | |
| 7438 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7439 | unsigned int this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7440 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7441 | table->files[i] = kcalloc(this_files, sizeof(*table->files[i]), |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7442 | GFP_KERNEL); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7443 | if (!table->files[i]) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7444 | break; |
| 7445 | nr_files -= this_files; |
| 7446 | } |
| 7447 | |
| 7448 | if (i == nr_tables) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7449 | return true; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7450 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7451 | io_free_file_tables(table, nr_tables * IORING_MAX_FILES_TABLE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7452 | return false; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7453 | } |
| 7454 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 7455 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7456 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7457 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7458 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7459 | struct sock *sock = ctx->ring_sock->sk; |
| 7460 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7461 | struct sk_buff *skb; |
| 7462 | int i; |
| 7463 | |
| 7464 | __skb_queue_head_init(&list); |
| 7465 | |
| 7466 | /* |
| 7467 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7468 | * remove this entry and rearrange the file array. |
| 7469 | */ |
| 7470 | skb = skb_dequeue(head); |
| 7471 | while (skb) { |
| 7472 | struct scm_fp_list *fp; |
| 7473 | |
| 7474 | fp = UNIXCB(skb).fp; |
| 7475 | for (i = 0; i < fp->count; i++) { |
| 7476 | int left; |
| 7477 | |
| 7478 | if (fp->fp[i] != file) |
| 7479 | continue; |
| 7480 | |
| 7481 | unix_notinflight(fp->user, fp->fp[i]); |
| 7482 | left = fp->count - 1 - i; |
| 7483 | if (left) { |
| 7484 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7485 | left * sizeof(struct file *)); |
| 7486 | } |
| 7487 | fp->count--; |
| 7488 | if (!fp->count) { |
| 7489 | kfree_skb(skb); |
| 7490 | skb = NULL; |
| 7491 | } else { |
| 7492 | __skb_queue_tail(&list, skb); |
| 7493 | } |
| 7494 | fput(file); |
| 7495 | file = NULL; |
| 7496 | break; |
| 7497 | } |
| 7498 | |
| 7499 | if (!file) |
| 7500 | break; |
| 7501 | |
| 7502 | __skb_queue_tail(&list, skb); |
| 7503 | |
| 7504 | skb = skb_dequeue(head); |
| 7505 | } |
| 7506 | |
| 7507 | if (skb_peek(&list)) { |
| 7508 | spin_lock_irq(&head->lock); |
| 7509 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7510 | __skb_queue_tail(head, skb); |
| 7511 | spin_unlock_irq(&head->lock); |
| 7512 | } |
| 7513 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7514 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7515 | #endif |
| 7516 | } |
| 7517 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7518 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7519 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7520 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7521 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7522 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7523 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7524 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7525 | list_del(&prsrc->list); |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7526 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7527 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7528 | } |
| 7529 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7530 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7531 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 7532 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7533 | } |
| 7534 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7535 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7536 | { |
| 7537 | struct io_ring_ctx *ctx; |
| 7538 | struct llist_node *node; |
| 7539 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7540 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7541 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7542 | |
| 7543 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7544 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7545 | struct llist_node *next = node->next; |
| 7546 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7547 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7548 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7549 | node = next; |
| 7550 | } |
| 7551 | } |
| 7552 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7553 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7554 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7555 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7556 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7557 | bool first_add = false; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7558 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7559 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7560 | node->done = true; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7561 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7562 | while (!list_empty(&ctx->rsrc_ref_list)) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7563 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7564 | struct io_rsrc_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7565 | /* recycle ref nodes in order */ |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7566 | if (!node->done) |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7567 | break; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7568 | list_del(&node->node); |
| 7569 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7570 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7571 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7572 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7573 | if (first_add) |
| 7574 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7575 | } |
| 7576 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7577 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7578 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7579 | struct io_rsrc_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7580 | |
| 7581 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7582 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7583 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7584 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7585 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7586 | 0, GFP_KERNEL)) { |
| 7587 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7588 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7589 | } |
| 7590 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7591 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7592 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7593 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7594 | } |
| 7595 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7596 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7597 | unsigned nr_args) |
| 7598 | { |
| 7599 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7600 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 7601 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7602 | unsigned i; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7603 | struct io_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7604 | |
| 7605 | if (ctx->file_data) |
| 7606 | return -EBUSY; |
| 7607 | if (!nr_args) |
| 7608 | return -EINVAL; |
| 7609 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7610 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7611 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 7612 | if (ret) |
| 7613 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7614 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 7615 | file_data = io_rsrc_data_alloc(ctx, io_rsrc_file_put); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7616 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7617 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7618 | ctx->file_data = file_data; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 7619 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7620 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7621 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7622 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7623 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7624 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7625 | ret = -EFAULT; |
| 7626 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7627 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7628 | /* allow sparse sets */ |
| 7629 | if (fd == -1) |
| 7630 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7631 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7632 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7633 | ret = -EBADF; |
| 7634 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7635 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7636 | |
| 7637 | /* |
| 7638 | * Don't allow io_uring instances to be registered. If UNIX |
| 7639 | * isn't enabled, then this causes a reference cycle and this |
| 7640 | * instance can never get freed. If UNIX is enabled we'll |
| 7641 | * handle it just fine, but there's still no point in allowing |
| 7642 | * a ring fd as it doesn't support regular read/write anyway. |
| 7643 | */ |
| 7644 | if (file->f_op == &io_uring_fops) { |
| 7645 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7646 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7647 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7648 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7649 | } |
| 7650 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7651 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7652 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7653 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7654 | return ret; |
| 7655 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7656 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7657 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7658 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7659 | out_fput: |
| 7660 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7661 | file = io_file_from_index(ctx, i); |
| 7662 | if (file) |
| 7663 | fput(file); |
| 7664 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7665 | io_free_file_tables(&ctx->file_table, nr_args); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7666 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7667 | out_free: |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7668 | kfree(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7669 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7670 | return ret; |
| 7671 | } |
| 7672 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7673 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7674 | int index) |
| 7675 | { |
| 7676 | #if defined(CONFIG_UNIX) |
| 7677 | struct sock *sock = ctx->ring_sock->sk; |
| 7678 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7679 | struct sk_buff *skb; |
| 7680 | |
| 7681 | /* |
| 7682 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7683 | * file set. If there's no room, fall back to allocating a new skb |
| 7684 | * and filling it in. |
| 7685 | */ |
| 7686 | spin_lock_irq(&head->lock); |
| 7687 | skb = skb_peek(head); |
| 7688 | if (skb) { |
| 7689 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7690 | |
| 7691 | if (fpl->count < SCM_MAX_FD) { |
| 7692 | __skb_unlink(skb, head); |
| 7693 | spin_unlock_irq(&head->lock); |
| 7694 | fpl->fp[fpl->count] = get_file(file); |
| 7695 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7696 | fpl->count++; |
| 7697 | spin_lock_irq(&head->lock); |
| 7698 | __skb_queue_head(head, skb); |
| 7699 | } else { |
| 7700 | skb = NULL; |
| 7701 | } |
| 7702 | } |
| 7703 | spin_unlock_irq(&head->lock); |
| 7704 | |
| 7705 | if (skb) { |
| 7706 | fput(file); |
| 7707 | return 0; |
| 7708 | } |
| 7709 | |
| 7710 | return __io_sqe_files_scm(ctx, 1, index); |
| 7711 | #else |
| 7712 | return 0; |
| 7713 | #endif |
| 7714 | } |
| 7715 | |
Pavel Begunkov | e7c7837 | 2021-04-01 15:43:45 +0100 | [diff] [blame] | 7716 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, |
| 7717 | struct io_rsrc_node *node, void *rsrc) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7718 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7719 | struct io_rsrc_put *prsrc; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7720 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7721 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7722 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7723 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7724 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7725 | prsrc->rsrc = rsrc; |
Pavel Begunkov | e7c7837 | 2021-04-01 15:43:45 +0100 | [diff] [blame] | 7726 | list_add(&prsrc->list, &node->rsrc_list); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7727 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7728 | } |
| 7729 | |
| 7730 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7731 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7732 | unsigned nr_args) |
| 7733 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7734 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7735 | struct io_fixed_file *file_slot; |
| 7736 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7737 | __s32 __user *fds; |
| 7738 | int fd, i, err; |
| 7739 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7740 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7741 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7742 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7743 | return -EOVERFLOW; |
| 7744 | if (done > ctx->nr_user_files) |
| 7745 | return -EINVAL; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7746 | err = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7747 | if (err) |
| 7748 | return err; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7749 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7750 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7751 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7752 | err = 0; |
| 7753 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7754 | err = -EFAULT; |
| 7755 | break; |
| 7756 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 7757 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 7758 | continue; |
| 7759 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7760 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7761 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7762 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7763 | if (file_slot->file_ptr) { |
| 7764 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7765 | err = io_queue_rsrc_removal(data, ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7766 | if (err) |
| 7767 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7768 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7769 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7770 | } |
| 7771 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7772 | file = fget(fd); |
| 7773 | if (!file) { |
| 7774 | err = -EBADF; |
| 7775 | break; |
| 7776 | } |
| 7777 | /* |
| 7778 | * Don't allow io_uring instances to be registered. If |
| 7779 | * UNIX isn't enabled, then this causes a reference |
| 7780 | * cycle and this instance can never get freed. If UNIX |
| 7781 | * is enabled we'll handle it just fine, but there's |
| 7782 | * still no point in allowing a ring fd as it doesn't |
| 7783 | * support regular read/write anyway. |
| 7784 | */ |
| 7785 | if (file->f_op == &io_uring_fops) { |
| 7786 | fput(file); |
| 7787 | err = -EBADF; |
| 7788 | break; |
| 7789 | } |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 7790 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7791 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7792 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7793 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7794 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7795 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7796 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7797 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7798 | } |
| 7799 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7800 | if (needs_switch) |
| 7801 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7802 | return done ? done : err; |
| 7803 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7804 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7805 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7806 | unsigned nr_args) |
| 7807 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7808 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7809 | |
| 7810 | if (!ctx->file_data) |
| 7811 | return -ENXIO; |
| 7812 | if (!nr_args) |
| 7813 | return -EINVAL; |
| 7814 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7815 | return -EFAULT; |
| 7816 | if (up.resv) |
| 7817 | return -EINVAL; |
| 7818 | |
| 7819 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7820 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7821 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7822 | static struct io_wq_work *io_free_work(struct io_wq_work *work) |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7823 | { |
| 7824 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7825 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7826 | req = io_put_req_find_next(req); |
| 7827 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7828 | } |
| 7829 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 7830 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 7831 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7832 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7833 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7834 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7835 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7836 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7837 | hash = ctx->hash_map; |
| 7838 | if (!hash) { |
| 7839 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 7840 | if (!hash) |
| 7841 | return ERR_PTR(-ENOMEM); |
| 7842 | refcount_set(&hash->refs, 1); |
| 7843 | init_waitqueue_head(&hash->wait); |
| 7844 | ctx->hash_map = hash; |
| 7845 | } |
| 7846 | |
| 7847 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 7848 | data.task = task; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7849 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7850 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7851 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7852 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7853 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7854 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7855 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7856 | } |
| 7857 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7858 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 7859 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7860 | { |
| 7861 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7862 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7863 | |
| 7864 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 7865 | if (unlikely(!tctx)) |
| 7866 | return -ENOMEM; |
| 7867 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7868 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 7869 | if (unlikely(ret)) { |
| 7870 | kfree(tctx); |
| 7871 | return ret; |
| 7872 | } |
| 7873 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 7874 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7875 | if (IS_ERR(tctx->io_wq)) { |
| 7876 | ret = PTR_ERR(tctx->io_wq); |
| 7877 | percpu_counter_destroy(&tctx->inflight); |
| 7878 | kfree(tctx); |
| 7879 | return ret; |
| 7880 | } |
| 7881 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7882 | xa_init(&tctx->xa); |
| 7883 | init_waitqueue_head(&tctx->wait); |
| 7884 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 7885 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 7886 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7887 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 7888 | spin_lock_init(&tctx->task_lock); |
| 7889 | INIT_WQ_LIST(&tctx->task_list); |
| 7890 | tctx->task_state = 0; |
| 7891 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7892 | return 0; |
| 7893 | } |
| 7894 | |
| 7895 | void __io_uring_free(struct task_struct *tsk) |
| 7896 | { |
| 7897 | struct io_uring_task *tctx = tsk->io_uring; |
| 7898 | |
| 7899 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 7900 | WARN_ON_ONCE(tctx->io_wq); |
| 7901 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7902 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7903 | kfree(tctx); |
| 7904 | tsk->io_uring = NULL; |
| 7905 | } |
| 7906 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7907 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 7908 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7909 | { |
| 7910 | int ret; |
| 7911 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7912 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 7913 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 7914 | IORING_SETUP_ATTACH_WQ) { |
| 7915 | struct fd f; |
| 7916 | |
| 7917 | f = fdget(p->wq_fd); |
| 7918 | if (!f.file) |
| 7919 | return -ENXIO; |
| 7920 | if (f.file->f_op != &io_uring_fops) { |
| 7921 | fdput(f); |
| 7922 | return -EINVAL; |
| 7923 | } |
| 7924 | fdput(f); |
| 7925 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7926 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7927 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7928 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7929 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7930 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7931 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7932 | if (IS_ERR(sqd)) { |
| 7933 | ret = PTR_ERR(sqd); |
| 7934 | goto err; |
| 7935 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7936 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7937 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7938 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7939 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7940 | if (!ctx->sq_thread_idle) |
| 7941 | ctx->sq_thread_idle = HZ; |
| 7942 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7943 | ret = 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 7944 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 7945 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 7946 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7947 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 7948 | if (attached && !sqd->thread) |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7949 | ret = -ENXIO; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 7950 | io_sq_thread_unpark(sqd); |
| 7951 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 7952 | if (ret < 0) |
| 7953 | goto err; |
| 7954 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7955 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7956 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7957 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7958 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7959 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7960 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7961 | if (cpu >= nr_cpu_ids) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7962 | goto err_sqpoll; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7963 | if (!cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7964 | goto err_sqpoll; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7965 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7966 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7967 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7968 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7969 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7970 | |
| 7971 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7972 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7973 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 7974 | if (IS_ERR(tsk)) { |
| 7975 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7976 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7977 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 7978 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7979 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 7980 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7981 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7982 | if (ret) |
| 7983 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7984 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7985 | /* Can't have SQ_AFF without SQPOLL */ |
| 7986 | ret = -EINVAL; |
| 7987 | goto err; |
| 7988 | } |
| 7989 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7990 | return 0; |
| 7991 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7992 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7993 | return ret; |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7994 | err_sqpoll: |
| 7995 | complete(&ctx->sq_data->exited); |
| 7996 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7997 | } |
| 7998 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7999 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8000 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8001 | { |
| 8002 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8003 | } |
| 8004 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8005 | static inline int __io_account_mem(struct user_struct *user, |
| 8006 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8007 | { |
| 8008 | unsigned long page_limit, cur_pages, new_pages; |
| 8009 | |
| 8010 | /* Don't allow more pages than we can safely lock */ |
| 8011 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8012 | |
| 8013 | do { |
| 8014 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8015 | new_pages = cur_pages + nr_pages; |
| 8016 | if (new_pages > page_limit) |
| 8017 | return -ENOMEM; |
| 8018 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8019 | new_pages) != cur_pages); |
| 8020 | |
| 8021 | return 0; |
| 8022 | } |
| 8023 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8024 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8025 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8026 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8027 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8028 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8029 | if (ctx->mm_account) |
| 8030 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8031 | } |
| 8032 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8033 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8034 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8035 | int ret; |
| 8036 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8037 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8038 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8039 | if (ret) |
| 8040 | return ret; |
| 8041 | } |
| 8042 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8043 | if (ctx->mm_account) |
| 8044 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8045 | |
| 8046 | return 0; |
| 8047 | } |
| 8048 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8049 | static void io_mem_free(void *ptr) |
| 8050 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8051 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8052 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8053 | if (!ptr) |
| 8054 | return; |
| 8055 | |
| 8056 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8057 | if (put_page_testzero(page)) |
| 8058 | free_compound_page(page); |
| 8059 | } |
| 8060 | |
| 8061 | static void *io_mem_alloc(size_t size) |
| 8062 | { |
| 8063 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8064 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8065 | |
| 8066 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8067 | } |
| 8068 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8069 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8070 | size_t *sq_offset) |
| 8071 | { |
| 8072 | struct io_rings *rings; |
| 8073 | size_t off, sq_array_size; |
| 8074 | |
| 8075 | off = struct_size(rings, cqes, cq_entries); |
| 8076 | if (off == SIZE_MAX) |
| 8077 | return SIZE_MAX; |
| 8078 | |
| 8079 | #ifdef CONFIG_SMP |
| 8080 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8081 | if (off == 0) |
| 8082 | return SIZE_MAX; |
| 8083 | #endif |
| 8084 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8085 | if (sq_offset) |
| 8086 | *sq_offset = off; |
| 8087 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8088 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8089 | if (sq_array_size == SIZE_MAX) |
| 8090 | return SIZE_MAX; |
| 8091 | |
| 8092 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8093 | return SIZE_MAX; |
| 8094 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8095 | return off; |
| 8096 | } |
| 8097 | |
Pavel Begunkov | 7f61a1e9 | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8098 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf *imu) |
| 8099 | { |
| 8100 | unsigned int i; |
| 8101 | |
| 8102 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8103 | unpin_user_page(imu->bvec[i].bv_page); |
| 8104 | if (imu->acct_pages) |
| 8105 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8106 | kvfree(imu->bvec); |
| 8107 | imu->nr_bvecs = 0; |
| 8108 | } |
| 8109 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8110 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8111 | { |
Pavel Begunkov | 7f61a1e9 | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8112 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8113 | |
| 8114 | if (!ctx->user_bufs) |
| 8115 | return -ENXIO; |
| 8116 | |
Pavel Begunkov | 7f61a1e9 | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8117 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8118 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8119 | kfree(ctx->user_bufs); |
| 8120 | ctx->user_bufs = NULL; |
| 8121 | ctx->nr_user_bufs = 0; |
| 8122 | return 0; |
| 8123 | } |
| 8124 | |
| 8125 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8126 | void __user *arg, unsigned index) |
| 8127 | { |
| 8128 | struct iovec __user *src; |
| 8129 | |
| 8130 | #ifdef CONFIG_COMPAT |
| 8131 | if (ctx->compat) { |
| 8132 | struct compat_iovec __user *ciovs; |
| 8133 | struct compat_iovec ciov; |
| 8134 | |
| 8135 | ciovs = (struct compat_iovec __user *) arg; |
| 8136 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8137 | return -EFAULT; |
| 8138 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8139 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8140 | dst->iov_len = ciov.iov_len; |
| 8141 | return 0; |
| 8142 | } |
| 8143 | #endif |
| 8144 | src = (struct iovec __user *) arg; |
| 8145 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8146 | return -EFAULT; |
| 8147 | return 0; |
| 8148 | } |
| 8149 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8150 | /* |
| 8151 | * Not super efficient, but this is just a registration time. And we do cache |
| 8152 | * the last compound head, so generally we'll only do a full search if we don't |
| 8153 | * match that one. |
| 8154 | * |
| 8155 | * We check if the given compound head page has already been accounted, to |
| 8156 | * avoid double accounting it. This allows us to account the full size of the |
| 8157 | * page, not just the constituent pages of a huge page. |
| 8158 | */ |
| 8159 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8160 | int nr_pages, struct page *hpage) |
| 8161 | { |
| 8162 | int i, j; |
| 8163 | |
| 8164 | /* check current page array */ |
| 8165 | for (i = 0; i < nr_pages; i++) { |
| 8166 | if (!PageCompound(pages[i])) |
| 8167 | continue; |
| 8168 | if (compound_head(pages[i]) == hpage) |
| 8169 | return true; |
| 8170 | } |
| 8171 | |
| 8172 | /* check previously registered pages */ |
| 8173 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8174 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8175 | |
| 8176 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8177 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8178 | continue; |
| 8179 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8180 | return true; |
| 8181 | } |
| 8182 | } |
| 8183 | |
| 8184 | return false; |
| 8185 | } |
| 8186 | |
| 8187 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8188 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8189 | struct page **last_hpage) |
| 8190 | { |
| 8191 | int i, ret; |
| 8192 | |
| 8193 | for (i = 0; i < nr_pages; i++) { |
| 8194 | if (!PageCompound(pages[i])) { |
| 8195 | imu->acct_pages++; |
| 8196 | } else { |
| 8197 | struct page *hpage; |
| 8198 | |
| 8199 | hpage = compound_head(pages[i]); |
| 8200 | if (hpage == *last_hpage) |
| 8201 | continue; |
| 8202 | *last_hpage = hpage; |
| 8203 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8204 | continue; |
| 8205 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8206 | } |
| 8207 | } |
| 8208 | |
| 8209 | if (!imu->acct_pages) |
| 8210 | return 0; |
| 8211 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8212 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8213 | if (ret) |
| 8214 | imu->acct_pages = 0; |
| 8215 | return ret; |
| 8216 | } |
| 8217 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8218 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8219 | struct io_mapped_ubuf *imu, |
| 8220 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8221 | { |
| 8222 | struct vm_area_struct **vmas = NULL; |
| 8223 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8224 | unsigned long off, start, end, ubuf; |
| 8225 | size_t size; |
| 8226 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8227 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8228 | ubuf = (unsigned long) iov->iov_base; |
| 8229 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8230 | start = ubuf >> PAGE_SHIFT; |
| 8231 | nr_pages = end - start; |
| 8232 | |
| 8233 | ret = -ENOMEM; |
| 8234 | |
| 8235 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8236 | if (!pages) |
| 8237 | goto done; |
| 8238 | |
| 8239 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8240 | GFP_KERNEL); |
| 8241 | if (!vmas) |
| 8242 | goto done; |
| 8243 | |
| 8244 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8245 | GFP_KERNEL); |
| 8246 | if (!imu->bvec) |
| 8247 | goto done; |
| 8248 | |
| 8249 | ret = 0; |
| 8250 | mmap_read_lock(current->mm); |
| 8251 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8252 | pages, vmas); |
| 8253 | if (pret == nr_pages) { |
| 8254 | /* don't support file backed memory */ |
| 8255 | for (i = 0; i < nr_pages; i++) { |
| 8256 | struct vm_area_struct *vma = vmas[i]; |
| 8257 | |
| 8258 | if (vma->vm_file && |
| 8259 | !is_file_hugepages(vma->vm_file)) { |
| 8260 | ret = -EOPNOTSUPP; |
| 8261 | break; |
| 8262 | } |
| 8263 | } |
| 8264 | } else { |
| 8265 | ret = pret < 0 ? pret : -EFAULT; |
| 8266 | } |
| 8267 | mmap_read_unlock(current->mm); |
| 8268 | if (ret) { |
| 8269 | /* |
| 8270 | * if we did partial map, or found file backed vmas, |
| 8271 | * release any pages we did get |
| 8272 | */ |
| 8273 | if (pret > 0) |
| 8274 | unpin_user_pages(pages, pret); |
| 8275 | kvfree(imu->bvec); |
| 8276 | goto done; |
| 8277 | } |
| 8278 | |
| 8279 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8280 | if (ret) { |
| 8281 | unpin_user_pages(pages, pret); |
| 8282 | kvfree(imu->bvec); |
| 8283 | goto done; |
| 8284 | } |
| 8285 | |
| 8286 | off = ubuf & ~PAGE_MASK; |
| 8287 | size = iov->iov_len; |
| 8288 | for (i = 0; i < nr_pages; i++) { |
| 8289 | size_t vec_len; |
| 8290 | |
| 8291 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8292 | imu->bvec[i].bv_page = pages[i]; |
| 8293 | imu->bvec[i].bv_len = vec_len; |
| 8294 | imu->bvec[i].bv_offset = off; |
| 8295 | off = 0; |
| 8296 | size -= vec_len; |
| 8297 | } |
| 8298 | /* store original address for later verification */ |
| 8299 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 8300 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8301 | imu->nr_bvecs = nr_pages; |
| 8302 | ret = 0; |
| 8303 | done: |
| 8304 | kvfree(pages); |
| 8305 | kvfree(vmas); |
| 8306 | return ret; |
| 8307 | } |
| 8308 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8309 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8310 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8311 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 8312 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8313 | } |
| 8314 | |
| 8315 | static int io_buffer_validate(struct iovec *iov) |
| 8316 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 8317 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 8318 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8319 | /* |
| 8320 | * Don't impose further limits on the size and buffer |
| 8321 | * constraints here, we'll -EINVAL later when IO is |
| 8322 | * submitted if they are wrong. |
| 8323 | */ |
| 8324 | if (!iov->iov_base || !iov->iov_len) |
| 8325 | return -EFAULT; |
| 8326 | |
| 8327 | /* arbitrary limit, but we need something */ |
| 8328 | if (iov->iov_len > SZ_1G) |
| 8329 | return -EFAULT; |
| 8330 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 8331 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 8332 | return -EOVERFLOW; |
| 8333 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8334 | return 0; |
| 8335 | } |
| 8336 | |
| 8337 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8338 | unsigned int nr_args) |
| 8339 | { |
| 8340 | int i, ret; |
| 8341 | struct iovec iov; |
| 8342 | struct page *last_hpage = NULL; |
| 8343 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8344 | if (ctx->user_bufs) |
| 8345 | return -EBUSY; |
| 8346 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8347 | return -EINVAL; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8348 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8349 | if (ret) |
| 8350 | return ret; |
| 8351 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8352 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8353 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8354 | |
| 8355 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8356 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8357 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8358 | ret = io_buffer_validate(&iov); |
| 8359 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8360 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8361 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8362 | if (ret) |
| 8363 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8364 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8365 | |
| 8366 | if (ret) |
| 8367 | io_sqe_buffers_unregister(ctx); |
| 8368 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8369 | return ret; |
| 8370 | } |
| 8371 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8372 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8373 | { |
| 8374 | __s32 __user *fds = arg; |
| 8375 | int fd; |
| 8376 | |
| 8377 | if (ctx->cq_ev_fd) |
| 8378 | return -EBUSY; |
| 8379 | |
| 8380 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8381 | return -EFAULT; |
| 8382 | |
| 8383 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8384 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8385 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8386 | ctx->cq_ev_fd = NULL; |
| 8387 | return ret; |
| 8388 | } |
| 8389 | |
| 8390 | return 0; |
| 8391 | } |
| 8392 | |
| 8393 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8394 | { |
| 8395 | if (ctx->cq_ev_fd) { |
| 8396 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8397 | ctx->cq_ev_fd = NULL; |
| 8398 | return 0; |
| 8399 | } |
| 8400 | |
| 8401 | return -ENXIO; |
| 8402 | } |
| 8403 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8404 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8405 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 8406 | struct io_buffer *buf; |
| 8407 | unsigned long index; |
| 8408 | |
| 8409 | xa_for_each(&ctx->io_buffers, index, buf) |
| 8410 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8411 | } |
| 8412 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8413 | static void io_req_cache_free(struct list_head *list, struct task_struct *tsk) |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8414 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8415 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8416 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8417 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8418 | if (tsk && req->task != tsk) |
| 8419 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8420 | list_del(&req->compl.list); |
| 8421 | kmem_cache_free(req_cachep, req); |
| 8422 | } |
| 8423 | } |
| 8424 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8425 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8426 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8427 | struct io_submit_state *submit_state = &ctx->submit_state; |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8428 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8429 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8430 | mutex_lock(&ctx->uring_lock); |
| 8431 | |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8432 | if (submit_state->free_reqs) { |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8433 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8434 | submit_state->reqs); |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8435 | submit_state->free_reqs = 0; |
| 8436 | } |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8437 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 8438 | io_flush_cached_locked_reqs(ctx, cs); |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8439 | io_req_cache_free(&cs->free_list, NULL); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8440 | mutex_unlock(&ctx->uring_lock); |
| 8441 | } |
| 8442 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8443 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8444 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8445 | io_sq_thread_finish(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8446 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8447 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8448 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8449 | mmdrop(ctx->mm_account); |
| 8450 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8451 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8452 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8453 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8454 | if (ctx->file_data) { |
| 8455 | if (!atomic_dec_and_test(&ctx->file_data->refs)) |
| 8456 | wait_for_completion(&ctx->file_data->done); |
| 8457 | __io_sqe_files_unregister(ctx); |
| 8458 | } |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 8459 | if (ctx->rings) |
| 8460 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8461 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8462 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8463 | io_destroy_buffers(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8464 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8465 | /* there are no registered resources left, nobody uses it */ |
| 8466 | if (ctx->rsrc_node) |
| 8467 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8468 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8469 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8470 | flush_delayed_work(&ctx->rsrc_put_work); |
| 8471 | |
| 8472 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 8473 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8474 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8475 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d26 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8476 | if (ctx->ring_sock) { |
| 8477 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8478 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d26 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8479 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8480 | #endif |
| 8481 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8482 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8483 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8484 | |
| 8485 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8486 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8487 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8488 | if (ctx->hash_map) |
| 8489 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8490 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8491 | kfree(ctx); |
| 8492 | } |
| 8493 | |
| 8494 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8495 | { |
| 8496 | struct io_ring_ctx *ctx = file->private_data; |
| 8497 | __poll_t mask = 0; |
| 8498 | |
| 8499 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8500 | /* |
| 8501 | * synchronizes with barrier from wq_has_sleeper call in |
| 8502 | * io_commit_cqring |
| 8503 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8504 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8505 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8506 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8507 | |
| 8508 | /* |
| 8509 | * Don't flush cqring overflow list here, just do a simple check. |
| 8510 | * Otherwise there could possible be ABBA deadlock: |
| 8511 | * CPU0 CPU1 |
| 8512 | * ---- ---- |
| 8513 | * lock(&ctx->uring_lock); |
| 8514 | * lock(&ep->mtx); |
| 8515 | * lock(&ctx->uring_lock); |
| 8516 | * lock(&ep->mtx); |
| 8517 | * |
| 8518 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8519 | * pushs them to do the flush. |
| 8520 | */ |
| 8521 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8522 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8523 | |
| 8524 | return mask; |
| 8525 | } |
| 8526 | |
| 8527 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8528 | { |
| 8529 | struct io_ring_ctx *ctx = file->private_data; |
| 8530 | |
| 8531 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8532 | } |
| 8533 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8534 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8535 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8536 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8537 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 8538 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8539 | if (creds) { |
| 8540 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8541 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8542 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8543 | |
| 8544 | return -EINVAL; |
| 8545 | } |
| 8546 | |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 8547 | static inline bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8548 | { |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 8549 | return io_run_task_work_head(&ctx->exit_task_work); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8550 | } |
| 8551 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8552 | struct io_tctx_exit { |
| 8553 | struct callback_head task_work; |
| 8554 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8555 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8556 | }; |
| 8557 | |
| 8558 | static void io_tctx_exit_cb(struct callback_head *cb) |
| 8559 | { |
| 8560 | struct io_uring_task *tctx = current->io_uring; |
| 8561 | struct io_tctx_exit *work; |
| 8562 | |
| 8563 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 8564 | /* |
| 8565 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 8566 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 8567 | */ |
| 8568 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8569 | io_uring_del_task_file((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8570 | complete(&work->completion); |
| 8571 | } |
| 8572 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8573 | static void io_ring_exit_work(struct work_struct *work) |
| 8574 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8575 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 8576 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8577 | struct io_tctx_exit exit; |
| 8578 | struct io_tctx_node *node; |
| 8579 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8580 | |
Pavel Begunkov | a185f1d | 2021-03-23 10:52:38 +0000 | [diff] [blame] | 8581 | /* prevent SQPOLL from submitting new requests */ |
| 8582 | if (ctx->sq_data) { |
| 8583 | io_sq_thread_park(ctx->sq_data); |
| 8584 | list_del_init(&ctx->sqd_list); |
| 8585 | io_sqd_update_thread_idle(ctx->sq_data); |
| 8586 | io_sq_thread_unpark(ctx->sq_data); |
| 8587 | } |
| 8588 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8589 | /* |
| 8590 | * If we're doing polled IO and end up having requests being |
| 8591 | * submitted async (out-of-line), then completions can come in while |
| 8592 | * we're waiting for refs to drop. We need to reap these manually, |
| 8593 | * as nobody else will be looking for them. |
| 8594 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8595 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8596 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 8597 | |
| 8598 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8599 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8600 | |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 8601 | /* |
| 8602 | * Some may use context even when all refs and requests have been put, |
| 8603 | * and they are free to do so while still holding uring_lock or |
| 8604 | * completion_lock, see __io_req_task_submit(). Apart from other work, |
| 8605 | * this lock/unlock section also waits them to finish. |
| 8606 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8607 | mutex_lock(&ctx->uring_lock); |
| 8608 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 8609 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 8610 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8611 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 8612 | ctx_node); |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8613 | exit.ctx = ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8614 | init_completion(&exit.completion); |
| 8615 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 8616 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 8617 | if (WARN_ON_ONCE(ret)) |
| 8618 | continue; |
| 8619 | wake_up_process(node->task); |
| 8620 | |
| 8621 | mutex_unlock(&ctx->uring_lock); |
| 8622 | wait_for_completion(&exit.completion); |
| 8623 | cond_resched(); |
| 8624 | mutex_lock(&ctx->uring_lock); |
| 8625 | } |
| 8626 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 8627 | spin_lock_irq(&ctx->completion_lock); |
| 8628 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8629 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8630 | io_ring_ctx_free(ctx); |
| 8631 | } |
| 8632 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 8633 | /* Returns true if we found and killed one or more timeouts */ |
| 8634 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 8635 | struct files_struct *files) |
| 8636 | { |
| 8637 | struct io_kiocb *req, *tmp; |
| 8638 | int canceled = 0; |
| 8639 | |
| 8640 | spin_lock_irq(&ctx->completion_lock); |
| 8641 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
| 8642 | if (io_match_task(req, tsk, files)) { |
| 8643 | io_kill_timeout(req, -ECANCELED); |
| 8644 | canceled++; |
| 8645 | } |
| 8646 | } |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 8647 | if (canceled != 0) |
| 8648 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 8649 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 8650 | if (canceled != 0) |
| 8651 | io_cqring_ev_posted(ctx); |
| 8652 | return canceled != 0; |
| 8653 | } |
| 8654 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8655 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8656 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 8657 | unsigned long index; |
| 8658 | struct creds *creds; |
| 8659 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8660 | mutex_lock(&ctx->uring_lock); |
| 8661 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8662 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 8663 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 8664 | xa_for_each(&ctx->personalities, index, creds) |
| 8665 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8666 | mutex_unlock(&ctx->uring_lock); |
| 8667 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8668 | io_kill_timeouts(ctx, NULL, NULL); |
| 8669 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8670 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8671 | /* 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] | 8672 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8673 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8674 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8675 | /* |
| 8676 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8677 | * if we're exiting a ton of rings at the same time. It just adds |
| 8678 | * noise and overhead, there's no discernable change in runtime |
| 8679 | * over using system_wq. |
| 8680 | */ |
| 8681 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8682 | } |
| 8683 | |
| 8684 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8685 | { |
| 8686 | struct io_ring_ctx *ctx = file->private_data; |
| 8687 | |
| 8688 | file->private_data = NULL; |
| 8689 | io_ring_ctx_wait_and_kill(ctx); |
| 8690 | return 0; |
| 8691 | } |
| 8692 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8693 | struct io_task_cancel { |
| 8694 | struct task_struct *task; |
| 8695 | struct files_struct *files; |
| 8696 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8697 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8698 | 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] | 8699 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8700 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8701 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8702 | bool ret; |
| 8703 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8704 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8705 | unsigned long flags; |
| 8706 | struct io_ring_ctx *ctx = req->ctx; |
| 8707 | |
| 8708 | /* protect against races with linked timeouts */ |
| 8709 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8710 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8711 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8712 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8713 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8714 | } |
| 8715 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8716 | } |
| 8717 | |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8718 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8719 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8720 | struct files_struct *files) |
| 8721 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8722 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8723 | LIST_HEAD(list); |
| 8724 | |
| 8725 | spin_lock_irq(&ctx->completion_lock); |
| 8726 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8727 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8728 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8729 | break; |
| 8730 | } |
| 8731 | } |
| 8732 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8733 | if (list_empty(&list)) |
| 8734 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8735 | |
| 8736 | while (!list_empty(&list)) { |
| 8737 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8738 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 8739 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8740 | kfree(de); |
| 8741 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8742 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8743 | } |
| 8744 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 8745 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8746 | { |
| 8747 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8748 | |
| 8749 | return req->ctx == data; |
| 8750 | } |
| 8751 | |
| 8752 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
| 8753 | { |
| 8754 | struct io_tctx_node *node; |
| 8755 | enum io_wq_cancel cret; |
| 8756 | bool ret = false; |
| 8757 | |
| 8758 | mutex_lock(&ctx->uring_lock); |
| 8759 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 8760 | struct io_uring_task *tctx = node->task->io_uring; |
| 8761 | |
| 8762 | /* |
| 8763 | * io_wq will stay alive while we hold uring_lock, because it's |
| 8764 | * killed after ctx nodes, which requires to take the lock. |
| 8765 | */ |
| 8766 | if (!tctx || !tctx->io_wq) |
| 8767 | continue; |
| 8768 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 8769 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8770 | } |
| 8771 | mutex_unlock(&ctx->uring_lock); |
| 8772 | |
| 8773 | return ret; |
| 8774 | } |
| 8775 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8776 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8777 | struct task_struct *task, |
| 8778 | struct files_struct *files) |
| 8779 | { |
| 8780 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 8781 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8782 | |
| 8783 | while (1) { |
| 8784 | enum io_wq_cancel cret; |
| 8785 | bool ret = false; |
| 8786 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 8787 | if (!task) { |
| 8788 | ret |= io_uring_try_cancel_iowq(ctx); |
| 8789 | } else if (tctx && tctx->io_wq) { |
| 8790 | /* |
| 8791 | * Cancels requests of all rings, not only @ctx, but |
| 8792 | * it's fine as the task is in exit/exec. |
| 8793 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8794 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8795 | &cancel, true); |
| 8796 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8797 | } |
| 8798 | |
| 8799 | /* SQPOLL thread does its own polling */ |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 8800 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && !files) || |
| 8801 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8802 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8803 | io_iopoll_try_reap_events(ctx); |
| 8804 | ret = true; |
| 8805 | } |
| 8806 | } |
| 8807 | |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8808 | ret |= io_cancel_defer_files(ctx, task, files); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8809 | ret |= io_poll_remove_all(ctx, task, files); |
| 8810 | ret |= io_kill_timeouts(ctx, task, files); |
| 8811 | ret |= io_run_task_work(); |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8812 | ret |= io_run_ctx_fallback(ctx); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8813 | if (!ret) |
| 8814 | break; |
| 8815 | cond_resched(); |
| 8816 | } |
| 8817 | } |
| 8818 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8819 | static int __io_uring_add_task_file(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8820 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8821 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8822 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8823 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8824 | |
| 8825 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8826 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8827 | if (unlikely(ret)) |
| 8828 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8829 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8830 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8831 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 8832 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 8833 | if (!node) |
| 8834 | return -ENOMEM; |
| 8835 | node->ctx = ctx; |
| 8836 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8837 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8838 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 8839 | node, GFP_KERNEL)); |
| 8840 | if (ret) { |
| 8841 | kfree(node); |
| 8842 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8843 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8844 | |
| 8845 | mutex_lock(&ctx->uring_lock); |
| 8846 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 8847 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8848 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8849 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8850 | return 0; |
| 8851 | } |
| 8852 | |
| 8853 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8854 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8855 | */ |
| 8856 | static inline int io_uring_add_task_file(struct io_ring_ctx *ctx) |
| 8857 | { |
| 8858 | struct io_uring_task *tctx = current->io_uring; |
| 8859 | |
| 8860 | if (likely(tctx && tctx->last == ctx)) |
| 8861 | return 0; |
| 8862 | return __io_uring_add_task_file(ctx); |
| 8863 | } |
| 8864 | |
| 8865 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8866 | * Remove this io_uring_file -> task mapping. |
| 8867 | */ |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8868 | static void io_uring_del_task_file(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8869 | { |
| 8870 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8871 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8872 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 8873 | if (!tctx) |
| 8874 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8875 | node = xa_erase(&tctx->xa, index); |
| 8876 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8877 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8878 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8879 | WARN_ON_ONCE(current != node->task); |
| 8880 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 8881 | |
| 8882 | mutex_lock(&node->ctx->uring_lock); |
| 8883 | list_del(&node->ctx_node); |
| 8884 | mutex_unlock(&node->ctx->uring_lock); |
| 8885 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8886 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8887 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8888 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8889 | } |
| 8890 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8891 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8892 | { |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8893 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8894 | unsigned long index; |
| 8895 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8896 | xa_for_each(&tctx->xa, index, node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8897 | io_uring_del_task_file(index); |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8898 | if (tctx->io_wq) { |
| 8899 | io_wq_put_and_exit(tctx->io_wq); |
| 8900 | tctx->io_wq = NULL; |
| 8901 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8902 | } |
| 8903 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8904 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 368b208 | 2021-04-11 01:46:25 +0100 | [diff] [blame] | 8905 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8906 | if (tracked) |
| 8907 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8908 | return percpu_counter_sum(&tctx->inflight); |
| 8909 | } |
| 8910 | |
| 8911 | static void io_sqpoll_cancel_cb(struct callback_head *cb) |
| 8912 | { |
| 8913 | struct io_tctx_exit *work = container_of(cb, struct io_tctx_exit, task_work); |
| 8914 | struct io_ring_ctx *ctx = work->ctx; |
| 8915 | struct io_sq_data *sqd = ctx->sq_data; |
| 8916 | |
| 8917 | if (sqd->thread) |
| 8918 | io_uring_cancel_sqpoll(ctx); |
| 8919 | complete(&work->completion); |
| 8920 | } |
| 8921 | |
| 8922 | static void io_sqpoll_cancel_sync(struct io_ring_ctx *ctx) |
| 8923 | { |
| 8924 | struct io_sq_data *sqd = ctx->sq_data; |
| 8925 | struct io_tctx_exit work = { .ctx = ctx, }; |
| 8926 | struct task_struct *task; |
| 8927 | |
| 8928 | io_sq_thread_park(sqd); |
| 8929 | list_del_init(&ctx->sqd_list); |
| 8930 | io_sqd_update_thread_idle(sqd); |
| 8931 | task = sqd->thread; |
| 8932 | if (task) { |
| 8933 | init_completion(&work.completion); |
| 8934 | init_task_work(&work.task_work, io_sqpoll_cancel_cb); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 8935 | io_task_work_add_head(&sqd->park_task_work, &work.task_work); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8936 | wake_up_process(task); |
| 8937 | } |
| 8938 | io_sq_thread_unpark(sqd); |
| 8939 | |
| 8940 | if (task) |
| 8941 | wait_for_completion(&work.completion); |
| 8942 | } |
| 8943 | |
Pavel Begunkov | 368b208 | 2021-04-11 01:46:25 +0100 | [diff] [blame] | 8944 | static void io_uring_try_cancel(struct files_struct *files) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8945 | { |
| 8946 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8947 | struct io_tctx_node *node; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 8948 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8949 | |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8950 | xa_for_each(&tctx->xa, index, node) { |
| 8951 | struct io_ring_ctx *ctx = node->ctx; |
| 8952 | |
| 8953 | if (ctx->sq_data) { |
| 8954 | io_sqpoll_cancel_sync(ctx); |
| 8955 | continue; |
| 8956 | } |
Pavel Begunkov | 368b208 | 2021-04-11 01:46:25 +0100 | [diff] [blame] | 8957 | io_uring_try_cancel_requests(ctx, current, files); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8958 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8959 | } |
| 8960 | |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8961 | /* should only be called by SQPOLL task */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8962 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 8963 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8964 | struct io_sq_data *sqd = ctx->sq_data; |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8965 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8966 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8967 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8968 | |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8969 | WARN_ON_ONCE(!sqd || ctx->sq_data->thread != current); |
| 8970 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8971 | atomic_inc(&tctx->in_idle); |
| 8972 | do { |
| 8973 | /* read completions before cancelations */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8974 | inflight = tctx_inflight(tctx, false); |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8975 | if (!inflight) |
| 8976 | break; |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8977 | io_uring_try_cancel_requests(ctx, current, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8978 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8979 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8980 | /* |
| 8981 | * If we've seen completions, retry without waiting. This |
| 8982 | * avoids a race where a completion comes in before we did |
| 8983 | * prepare_to_wait(). |
| 8984 | */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8985 | if (inflight == tctx_inflight(tctx, false)) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8986 | schedule(); |
| 8987 | finish_wait(&tctx->wait, &wait); |
| 8988 | } while (1); |
| 8989 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8990 | } |
| 8991 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8992 | /* |
| 8993 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 8994 | * requests. |
| 8995 | */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8996 | void __io_uring_cancel(struct files_struct *files) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8997 | { |
| 8998 | struct io_uring_task *tctx = current->io_uring; |
| 8999 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9000 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9001 | |
| 9002 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9003 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9004 | io_uring_try_cancel(files); |
Pavel Begunkov | 5a978dc | 2021-03-27 09:59:30 +0000 | [diff] [blame] | 9005 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9006 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9007 | /* read completions before cancelations */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9008 | inflight = tctx_inflight(tctx, !!files); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9009 | if (!inflight) |
| 9010 | break; |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9011 | io_uring_try_cancel(files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9012 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 9013 | |
| 9014 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9015 | * If we've seen completions, retry without waiting. This |
| 9016 | * avoids a race where a completion comes in before we did |
| 9017 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9018 | */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9019 | if (inflight == tctx_inflight(tctx, !!files)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9020 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9021 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9022 | } while (1); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9023 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9024 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9025 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9026 | if (!files) { |
| 9027 | /* for exec all current's requests should be gone, kill tctx */ |
| 9028 | __io_uring_free(current); |
| 9029 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9030 | } |
| 9031 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9032 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9033 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9034 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9035 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9036 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9037 | struct page *page; |
| 9038 | void *ptr; |
| 9039 | |
| 9040 | switch (offset) { |
| 9041 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9042 | case IORING_OFF_CQ_RING: |
| 9043 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9044 | break; |
| 9045 | case IORING_OFF_SQES: |
| 9046 | ptr = ctx->sq_sqes; |
| 9047 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9048 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9049 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9050 | } |
| 9051 | |
| 9052 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9053 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9054 | return ERR_PTR(-EINVAL); |
| 9055 | |
| 9056 | return ptr; |
| 9057 | } |
| 9058 | |
| 9059 | #ifdef CONFIG_MMU |
| 9060 | |
| 9061 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9062 | { |
| 9063 | size_t sz = vma->vm_end - vma->vm_start; |
| 9064 | unsigned long pfn; |
| 9065 | void *ptr; |
| 9066 | |
| 9067 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9068 | if (IS_ERR(ptr)) |
| 9069 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9070 | |
| 9071 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9072 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9073 | } |
| 9074 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9075 | #else /* !CONFIG_MMU */ |
| 9076 | |
| 9077 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9078 | { |
| 9079 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9080 | } |
| 9081 | |
| 9082 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9083 | { |
| 9084 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9085 | } |
| 9086 | |
| 9087 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9088 | unsigned long addr, unsigned long len, |
| 9089 | unsigned long pgoff, unsigned long flags) |
| 9090 | { |
| 9091 | void *ptr; |
| 9092 | |
| 9093 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9094 | if (IS_ERR(ptr)) |
| 9095 | return PTR_ERR(ptr); |
| 9096 | |
| 9097 | return (unsigned long) ptr; |
| 9098 | } |
| 9099 | |
| 9100 | #endif /* !CONFIG_MMU */ |
| 9101 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9102 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9103 | { |
| 9104 | DEFINE_WAIT(wait); |
| 9105 | |
| 9106 | do { |
| 9107 | if (!io_sqring_full(ctx)) |
| 9108 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9109 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9110 | |
| 9111 | if (!io_sqring_full(ctx)) |
| 9112 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9113 | schedule(); |
| 9114 | } while (!signal_pending(current)); |
| 9115 | |
| 9116 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 9117 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9118 | } |
| 9119 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9120 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9121 | struct __kernel_timespec __user **ts, |
| 9122 | const sigset_t __user **sig) |
| 9123 | { |
| 9124 | struct io_uring_getevents_arg arg; |
| 9125 | |
| 9126 | /* |
| 9127 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9128 | * is just a pointer to the sigset_t. |
| 9129 | */ |
| 9130 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9131 | *sig = (const sigset_t __user *) argp; |
| 9132 | *ts = NULL; |
| 9133 | return 0; |
| 9134 | } |
| 9135 | |
| 9136 | /* |
| 9137 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9138 | * timespec and sigset_t pointers if good. |
| 9139 | */ |
| 9140 | if (*argsz != sizeof(arg)) |
| 9141 | return -EINVAL; |
| 9142 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9143 | return -EFAULT; |
| 9144 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9145 | *argsz = arg.sigmask_sz; |
| 9146 | *ts = u64_to_user_ptr(arg.ts); |
| 9147 | return 0; |
| 9148 | } |
| 9149 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9150 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9151 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9152 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9153 | { |
| 9154 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9155 | int submitted = 0; |
| 9156 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9157 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9158 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9159 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9160 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9161 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 9162 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9163 | return -EINVAL; |
| 9164 | |
| 9165 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9166 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9167 | return -EBADF; |
| 9168 | |
| 9169 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9170 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9171 | goto out_fput; |
| 9172 | |
| 9173 | ret = -ENXIO; |
| 9174 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9175 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9176 | goto out_fput; |
| 9177 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9178 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9179 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9180 | goto out; |
| 9181 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9182 | /* |
| 9183 | * For SQ polling, the thread will do all submissions and completions. |
| 9184 | * Just return the requested submit count, and wake the thread if |
| 9185 | * we were asked to. |
| 9186 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9187 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9188 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9189 | io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9190 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9191 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 9192 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 9193 | goto out; |
| 9194 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9195 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9196 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9197 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9198 | ret = io_sqpoll_wait_sq(ctx); |
| 9199 | if (ret) |
| 9200 | goto out; |
| 9201 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9202 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9203 | } else if (to_submit) { |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9204 | ret = io_uring_add_task_file(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9205 | if (unlikely(ret)) |
| 9206 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9207 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9208 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9209 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9210 | |
| 9211 | if (submitted != to_submit) |
| 9212 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9213 | } |
| 9214 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9215 | const sigset_t __user *sig; |
| 9216 | struct __kernel_timespec __user *ts; |
| 9217 | |
| 9218 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9219 | if (unlikely(ret)) |
| 9220 | goto out; |
| 9221 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9222 | min_complete = min(min_complete, ctx->cq_entries); |
| 9223 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9224 | /* |
| 9225 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9226 | * space applications don't need to do io completion events |
| 9227 | * polling again, they can rely on io_sq_thread to do polling |
| 9228 | * work, which can reduce cpu usage and uring_lock contention. |
| 9229 | */ |
| 9230 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9231 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9232 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9233 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9234 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9235 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9236 | } |
| 9237 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9238 | out: |
Pavel Begunkov | 6805b32e | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9239 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9240 | out_fput: |
| 9241 | fdput(f); |
| 9242 | return submitted ? submitted : ret; |
| 9243 | } |
| 9244 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9245 | #ifdef CONFIG_PROC_FS |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9246 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
| 9247 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9248 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9249 | struct user_namespace *uns = seq_user_ns(m); |
| 9250 | struct group_info *gi; |
| 9251 | kernel_cap_t cap; |
| 9252 | unsigned __capi; |
| 9253 | int g; |
| 9254 | |
| 9255 | seq_printf(m, "%5d\n", id); |
| 9256 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9257 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9258 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9259 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9260 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9261 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9262 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9263 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9264 | seq_puts(m, "\n\tGroups:\t"); |
| 9265 | gi = cred->group_info; |
| 9266 | for (g = 0; g < gi->ngroups; g++) { |
| 9267 | seq_put_decimal_ull(m, g ? " " : "", |
| 9268 | from_kgid_munged(uns, gi->gid[g])); |
| 9269 | } |
| 9270 | seq_puts(m, "\n\tCapEff:\t"); |
| 9271 | cap = cred->cap_effective; |
| 9272 | CAP_FOR_EACH_U32(__capi) |
| 9273 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9274 | seq_putc(m, '\n'); |
| 9275 | return 0; |
| 9276 | } |
| 9277 | |
| 9278 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9279 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9280 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9281 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9282 | int i; |
| 9283 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9284 | /* |
| 9285 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9286 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9287 | * cases. If we fail to get the lock, we just don't iterate any |
| 9288 | * structures that could be going away outside the io_uring mutex. |
| 9289 | */ |
| 9290 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9291 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9292 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9293 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9294 | if (!sq->thread) |
| 9295 | sq = NULL; |
| 9296 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9297 | |
| 9298 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9299 | 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] | 9300 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9301 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 9302 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9303 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9304 | if (f) |
| 9305 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9306 | else |
| 9307 | seq_printf(m, "%5u: <none>\n", i); |
| 9308 | } |
| 9309 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9310 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9311 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9312 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9313 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9314 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9315 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9316 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 9317 | unsigned long index; |
| 9318 | const struct cred *cred; |
| 9319 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9320 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9321 | xa_for_each(&ctx->personalities, index, cred) |
| 9322 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9323 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9324 | seq_printf(m, "PollList:\n"); |
| 9325 | spin_lock_irq(&ctx->completion_lock); |
| 9326 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9327 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9328 | struct io_kiocb *req; |
| 9329 | |
| 9330 | hlist_for_each_entry(req, list, hash_node) |
| 9331 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9332 | req->task->task_works != NULL); |
| 9333 | } |
| 9334 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9335 | if (has_lock) |
| 9336 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9337 | } |
| 9338 | |
| 9339 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9340 | { |
| 9341 | struct io_ring_ctx *ctx = f->private_data; |
| 9342 | |
| 9343 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9344 | __io_uring_show_fdinfo(ctx, m); |
| 9345 | percpu_ref_put(&ctx->refs); |
| 9346 | } |
| 9347 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9348 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9349 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9350 | static const struct file_operations io_uring_fops = { |
| 9351 | .release = io_uring_release, |
| 9352 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9353 | #ifndef CONFIG_MMU |
| 9354 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9355 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9356 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9357 | .poll = io_uring_poll, |
| 9358 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9359 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9360 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9361 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9362 | }; |
| 9363 | |
| 9364 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9365 | struct io_uring_params *p) |
| 9366 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9367 | struct io_rings *rings; |
| 9368 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9369 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9370 | /* make sure these are sane, as we already accounted them */ |
| 9371 | ctx->sq_entries = p->sq_entries; |
| 9372 | ctx->cq_entries = p->cq_entries; |
| 9373 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9374 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9375 | if (size == SIZE_MAX) |
| 9376 | return -EOVERFLOW; |
| 9377 | |
| 9378 | rings = io_mem_alloc(size); |
| 9379 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9380 | return -ENOMEM; |
| 9381 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9382 | ctx->rings = rings; |
| 9383 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9384 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9385 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9386 | rings->sq_ring_entries = p->sq_entries; |
| 9387 | rings->cq_ring_entries = p->cq_entries; |
| 9388 | ctx->sq_mask = rings->sq_ring_mask; |
| 9389 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9390 | |
| 9391 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9392 | if (size == SIZE_MAX) { |
| 9393 | io_mem_free(ctx->rings); |
| 9394 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9395 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9396 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9397 | |
| 9398 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9399 | if (!ctx->sq_sqes) { |
| 9400 | io_mem_free(ctx->rings); |
| 9401 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9402 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9403 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9404 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9405 | return 0; |
| 9406 | } |
| 9407 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9408 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9409 | { |
| 9410 | int ret, fd; |
| 9411 | |
| 9412 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9413 | if (fd < 0) |
| 9414 | return fd; |
| 9415 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9416 | ret = io_uring_add_task_file(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9417 | if (ret) { |
| 9418 | put_unused_fd(fd); |
| 9419 | return ret; |
| 9420 | } |
| 9421 | fd_install(fd, file); |
| 9422 | return fd; |
| 9423 | } |
| 9424 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9425 | /* |
| 9426 | * Allocate an anonymous fd, this is what constitutes the application |
| 9427 | * visible backing of an io_uring instance. The application mmaps this |
| 9428 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9429 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9430 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9431 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9432 | { |
| 9433 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9434 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9435 | int ret; |
| 9436 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9437 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9438 | &ctx->ring_sock); |
| 9439 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9440 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9441 | #endif |
| 9442 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9443 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9444 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9445 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9446 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9447 | sock_release(ctx->ring_sock); |
| 9448 | ctx->ring_sock = NULL; |
| 9449 | } else { |
| 9450 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9451 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9452 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9453 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9454 | } |
| 9455 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9456 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9457 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9458 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9459 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9460 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9461 | int ret; |
| 9462 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9463 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9464 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9465 | if (entries > IORING_MAX_ENTRIES) { |
| 9466 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9467 | return -EINVAL; |
| 9468 | entries = IORING_MAX_ENTRIES; |
| 9469 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9470 | |
| 9471 | /* |
| 9472 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9473 | * application to drive a higher depth than the size of the SQ ring, |
| 9474 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9475 | * some flexibility in overcommitting a bit. If the application has |
| 9476 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9477 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9478 | */ |
| 9479 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9480 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9481 | /* |
| 9482 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9483 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9484 | * any cq vs sq ring sizing. |
| 9485 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9486 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9487 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9488 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9489 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9490 | return -EINVAL; |
| 9491 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9492 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9493 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9494 | if (p->cq_entries < p->sq_entries) |
| 9495 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9496 | } else { |
| 9497 | p->cq_entries = 2 * p->sq_entries; |
| 9498 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9499 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9500 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9501 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9502 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9503 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9504 | if (!capable(CAP_IPC_LOCK)) |
| 9505 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9506 | |
| 9507 | /* |
| 9508 | * This is just grabbed for accounting purposes. When a process exits, |
| 9509 | * the mm is exited and dropped before the files, hence we need to hang |
| 9510 | * on to this mm purely for the purposes of being able to unaccount |
| 9511 | * memory (locked/pinned vm). It's not used for anything else. |
| 9512 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9513 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9514 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9515 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9516 | ret = io_allocate_scq_urings(ctx, p); |
| 9517 | if (ret) |
| 9518 | goto err; |
| 9519 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9520 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9521 | if (ret) |
| 9522 | goto err; |
| 9523 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9524 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9525 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9526 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9527 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9528 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9529 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9530 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9531 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9532 | |
| 9533 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9534 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9535 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9536 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9537 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9538 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9539 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9540 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9541 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9542 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9543 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9544 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9545 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Jens Axboe | 1c0aa1fa | 2021-02-20 11:55:28 -0700 | [diff] [blame] | 9546 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9547 | |
| 9548 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9549 | ret = -EFAULT; |
| 9550 | goto err; |
| 9551 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9552 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9553 | file = io_uring_get_file(ctx); |
| 9554 | if (IS_ERR(file)) { |
| 9555 | ret = PTR_ERR(file); |
| 9556 | goto err; |
| 9557 | } |
| 9558 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9559 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9560 | * Install ring fd as the very last thing, so we don't risk someone |
| 9561 | * having closed it before we finish setup |
| 9562 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9563 | ret = io_uring_install_fd(ctx, file); |
| 9564 | if (ret < 0) { |
| 9565 | /* fput will clean it up */ |
| 9566 | fput(file); |
| 9567 | return ret; |
| 9568 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9569 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9570 | 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] | 9571 | return ret; |
| 9572 | err: |
| 9573 | io_ring_ctx_wait_and_kill(ctx); |
| 9574 | return ret; |
| 9575 | } |
| 9576 | |
| 9577 | /* |
| 9578 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9579 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9580 | * params structure passed in. |
| 9581 | */ |
| 9582 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9583 | { |
| 9584 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9585 | int i; |
| 9586 | |
| 9587 | if (copy_from_user(&p, params, sizeof(p))) |
| 9588 | return -EFAULT; |
| 9589 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9590 | if (p.resv[i]) |
| 9591 | return -EINVAL; |
| 9592 | } |
| 9593 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9594 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9595 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9596 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9597 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9598 | return -EINVAL; |
| 9599 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9600 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9601 | } |
| 9602 | |
| 9603 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9604 | struct io_uring_params __user *, params) |
| 9605 | { |
| 9606 | return io_uring_setup(entries, params); |
| 9607 | } |
| 9608 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9609 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9610 | { |
| 9611 | struct io_uring_probe *p; |
| 9612 | size_t size; |
| 9613 | int i, ret; |
| 9614 | |
| 9615 | size = struct_size(p, ops, nr_args); |
| 9616 | if (size == SIZE_MAX) |
| 9617 | return -EOVERFLOW; |
| 9618 | p = kzalloc(size, GFP_KERNEL); |
| 9619 | if (!p) |
| 9620 | return -ENOMEM; |
| 9621 | |
| 9622 | ret = -EFAULT; |
| 9623 | if (copy_from_user(p, arg, size)) |
| 9624 | goto out; |
| 9625 | ret = -EINVAL; |
| 9626 | if (memchr_inv(p, 0, size)) |
| 9627 | goto out; |
| 9628 | |
| 9629 | p->last_op = IORING_OP_LAST - 1; |
| 9630 | if (nr_args > IORING_OP_LAST) |
| 9631 | nr_args = IORING_OP_LAST; |
| 9632 | |
| 9633 | for (i = 0; i < nr_args; i++) { |
| 9634 | p->ops[i].op = i; |
| 9635 | if (!io_op_defs[i].not_supported) |
| 9636 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9637 | } |
| 9638 | p->ops_len = i; |
| 9639 | |
| 9640 | ret = 0; |
| 9641 | if (copy_to_user(arg, p, size)) |
| 9642 | ret = -EFAULT; |
| 9643 | out: |
| 9644 | kfree(p); |
| 9645 | return ret; |
| 9646 | } |
| 9647 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9648 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9649 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9650 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9651 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9652 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9653 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9654 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9655 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9656 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 9657 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
| 9658 | if (!ret) |
| 9659 | return id; |
| 9660 | put_cred(creds); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9661 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9662 | } |
| 9663 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9664 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9665 | unsigned int nr_args) |
| 9666 | { |
| 9667 | struct io_uring_restriction *res; |
| 9668 | size_t size; |
| 9669 | int i, ret; |
| 9670 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9671 | /* Restrictions allowed only if rings started disabled */ |
| 9672 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9673 | return -EBADFD; |
| 9674 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9675 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9676 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9677 | return -EBUSY; |
| 9678 | |
| 9679 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9680 | return -EINVAL; |
| 9681 | |
| 9682 | size = array_size(nr_args, sizeof(*res)); |
| 9683 | if (size == SIZE_MAX) |
| 9684 | return -EOVERFLOW; |
| 9685 | |
| 9686 | res = memdup_user(arg, size); |
| 9687 | if (IS_ERR(res)) |
| 9688 | return PTR_ERR(res); |
| 9689 | |
| 9690 | ret = 0; |
| 9691 | |
| 9692 | for (i = 0; i < nr_args; i++) { |
| 9693 | switch (res[i].opcode) { |
| 9694 | case IORING_RESTRICTION_REGISTER_OP: |
| 9695 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9696 | ret = -EINVAL; |
| 9697 | goto out; |
| 9698 | } |
| 9699 | |
| 9700 | __set_bit(res[i].register_op, |
| 9701 | ctx->restrictions.register_op); |
| 9702 | break; |
| 9703 | case IORING_RESTRICTION_SQE_OP: |
| 9704 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9705 | ret = -EINVAL; |
| 9706 | goto out; |
| 9707 | } |
| 9708 | |
| 9709 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9710 | break; |
| 9711 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9712 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9713 | break; |
| 9714 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9715 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9716 | break; |
| 9717 | default: |
| 9718 | ret = -EINVAL; |
| 9719 | goto out; |
| 9720 | } |
| 9721 | } |
| 9722 | |
| 9723 | out: |
| 9724 | /* Reset all restrictions if an error happened */ |
| 9725 | if (ret != 0) |
| 9726 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9727 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9728 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9729 | |
| 9730 | kfree(res); |
| 9731 | return ret; |
| 9732 | } |
| 9733 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9734 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9735 | { |
| 9736 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9737 | return -EBADFD; |
| 9738 | |
| 9739 | if (ctx->restrictions.registered) |
| 9740 | ctx->restricted = 1; |
| 9741 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 9742 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9743 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 9744 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9745 | return 0; |
| 9746 | } |
| 9747 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9748 | static bool io_register_op_must_quiesce(int op) |
| 9749 | { |
| 9750 | switch (op) { |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 9751 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9752 | case IORING_UNREGISTER_FILES: |
| 9753 | case IORING_REGISTER_FILES_UPDATE: |
| 9754 | case IORING_REGISTER_PROBE: |
| 9755 | case IORING_REGISTER_PERSONALITY: |
| 9756 | case IORING_UNREGISTER_PERSONALITY: |
| 9757 | return false; |
| 9758 | default: |
| 9759 | return true; |
| 9760 | } |
| 9761 | } |
| 9762 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9763 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9764 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9765 | __releases(ctx->uring_lock) |
| 9766 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9767 | { |
| 9768 | int ret; |
| 9769 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9770 | /* |
| 9771 | * We're inside the ring mutex, if the ref is already dying, then |
| 9772 | * someone else killed the ctx or is already going through |
| 9773 | * io_uring_register(). |
| 9774 | */ |
| 9775 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9776 | return -ENXIO; |
| 9777 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9778 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9779 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9780 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9781 | /* |
| 9782 | * Drop uring mutex before waiting for references to exit. If |
| 9783 | * another thread is currently inside io_uring_enter() it might |
| 9784 | * need to grab the uring_lock to make progress. If we hold it |
| 9785 | * here across the drain wait, then we can deadlock. It's safe |
| 9786 | * to drop the mutex here, since no new references will come in |
| 9787 | * after we've killed the percpu ref. |
| 9788 | */ |
| 9789 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9790 | do { |
| 9791 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 9792 | if (!ret) |
| 9793 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 9794 | ret = io_run_task_work_sig(); |
| 9795 | if (ret < 0) |
| 9796 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9797 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9798 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9799 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9800 | if (ret) { |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 9801 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 9802 | return ret; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9803 | } |
| 9804 | } |
| 9805 | |
| 9806 | if (ctx->restricted) { |
| 9807 | if (opcode >= IORING_REGISTER_LAST) { |
| 9808 | ret = -EINVAL; |
| 9809 | goto out; |
| 9810 | } |
| 9811 | |
| 9812 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 9813 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9814 | goto out; |
| 9815 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9816 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9817 | |
| 9818 | switch (opcode) { |
| 9819 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9820 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9821 | break; |
| 9822 | case IORING_UNREGISTER_BUFFERS: |
| 9823 | ret = -EINVAL; |
| 9824 | if (arg || nr_args) |
| 9825 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9826 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9827 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 9828 | case IORING_REGISTER_FILES: |
| 9829 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 9830 | break; |
| 9831 | case IORING_UNREGISTER_FILES: |
| 9832 | ret = -EINVAL; |
| 9833 | if (arg || nr_args) |
| 9834 | break; |
| 9835 | ret = io_sqe_files_unregister(ctx); |
| 9836 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 9837 | case IORING_REGISTER_FILES_UPDATE: |
| 9838 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 9839 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9840 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9841 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9842 | ret = -EINVAL; |
| 9843 | if (nr_args != 1) |
| 9844 | break; |
| 9845 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9846 | if (ret) |
| 9847 | break; |
| 9848 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 9849 | ctx->eventfd_async = 1; |
| 9850 | else |
| 9851 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9852 | break; |
| 9853 | case IORING_UNREGISTER_EVENTFD: |
| 9854 | ret = -EINVAL; |
| 9855 | if (arg || nr_args) |
| 9856 | break; |
| 9857 | ret = io_eventfd_unregister(ctx); |
| 9858 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9859 | case IORING_REGISTER_PROBE: |
| 9860 | ret = -EINVAL; |
| 9861 | if (!arg || nr_args > 256) |
| 9862 | break; |
| 9863 | ret = io_probe(ctx, arg, nr_args); |
| 9864 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9865 | case IORING_REGISTER_PERSONALITY: |
| 9866 | ret = -EINVAL; |
| 9867 | if (arg || nr_args) |
| 9868 | break; |
| 9869 | ret = io_register_personality(ctx); |
| 9870 | break; |
| 9871 | case IORING_UNREGISTER_PERSONALITY: |
| 9872 | ret = -EINVAL; |
| 9873 | if (arg) |
| 9874 | break; |
| 9875 | ret = io_unregister_personality(ctx, nr_args); |
| 9876 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9877 | case IORING_REGISTER_ENABLE_RINGS: |
| 9878 | ret = -EINVAL; |
| 9879 | if (arg || nr_args) |
| 9880 | break; |
| 9881 | ret = io_register_enable_rings(ctx); |
| 9882 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9883 | case IORING_REGISTER_RESTRICTIONS: |
| 9884 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 9885 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9886 | default: |
| 9887 | ret = -EINVAL; |
| 9888 | break; |
| 9889 | } |
| 9890 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9891 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9892 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9893 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9894 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9895 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9896 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9897 | return ret; |
| 9898 | } |
| 9899 | |
| 9900 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 9901 | void __user *, arg, unsigned int, nr_args) |
| 9902 | { |
| 9903 | struct io_ring_ctx *ctx; |
| 9904 | long ret = -EBADF; |
| 9905 | struct fd f; |
| 9906 | |
| 9907 | f = fdget(fd); |
| 9908 | if (!f.file) |
| 9909 | return -EBADF; |
| 9910 | |
| 9911 | ret = -EOPNOTSUPP; |
| 9912 | if (f.file->f_op != &io_uring_fops) |
| 9913 | goto out_fput; |
| 9914 | |
| 9915 | ctx = f.file->private_data; |
| 9916 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 9917 | io_run_task_work(); |
| 9918 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9919 | mutex_lock(&ctx->uring_lock); |
| 9920 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 9921 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9922 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 9923 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9924 | out_fput: |
| 9925 | fdput(f); |
| 9926 | return ret; |
| 9927 | } |
| 9928 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9929 | static int __init io_uring_init(void) |
| 9930 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9931 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 9932 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 9933 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 9934 | } while (0) |
| 9935 | |
| 9936 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 9937 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 9938 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 9939 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 9940 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 9941 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 9942 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 9943 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 9944 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 9945 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9946 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9947 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 9948 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 9949 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 9950 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 9951 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9952 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 9953 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9954 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 9955 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 9956 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 9957 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 9958 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 9959 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 9960 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 9961 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9962 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9963 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 9964 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 9965 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9966 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9967 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 9968 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 9969 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 9970 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 9971 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9972 | return 0; |
| 9973 | }; |
| 9974 | __initcall(io_uring_init); |