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; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 217 | u64 tag; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 218 | union { |
| 219 | void *rsrc; |
| 220 | struct file *file; |
| 221 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 224 | struct io_file_table { |
| 225 | /* two level table */ |
| 226 | struct io_fixed_file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 229 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 230 | struct percpu_ref refs; |
| 231 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 232 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 233 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 234 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 235 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 236 | }; |
| 237 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 238 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 239 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 240 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 241 | struct io_ring_ctx *ctx; |
| 242 | |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 243 | u64 *tags; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 244 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 245 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 246 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 247 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 250 | struct io_buffer { |
| 251 | struct list_head list; |
| 252 | __u64 addr; |
| 253 | __s32 len; |
| 254 | __u16 bid; |
| 255 | }; |
| 256 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 257 | struct io_restriction { |
| 258 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 259 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 260 | u8 sqe_flags_allowed; |
| 261 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 262 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 263 | }; |
| 264 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 265 | enum { |
| 266 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 267 | IO_SQ_THREAD_SHOULD_PARK, |
| 268 | }; |
| 269 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 270 | struct io_sq_data { |
| 271 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 272 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 273 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 274 | |
| 275 | /* ctx's that are using this sqd */ |
| 276 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 277 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 278 | struct task_struct *thread; |
| 279 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 280 | |
| 281 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 282 | int sq_cpu; |
| 283 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 284 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 285 | |
| 286 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 287 | struct completion exited; |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 288 | struct callback_head *park_task_work; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 289 | }; |
| 290 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 291 | #define IO_IOPOLL_BATCH 8 |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 292 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 293 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 294 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 295 | |
| 296 | struct io_comp_state { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 297 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 298 | unsigned int nr; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 299 | unsigned int locked_free_nr; |
| 300 | /* inline/task_work completion list, under ->uring_lock */ |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 301 | struct list_head free_list; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 302 | /* IRQ completion list, under ->completion_lock */ |
| 303 | struct list_head locked_free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 306 | struct io_submit_link { |
| 307 | struct io_kiocb *head; |
| 308 | struct io_kiocb *last; |
| 309 | }; |
| 310 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 311 | struct io_submit_state { |
| 312 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 313 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 314 | |
| 315 | /* |
| 316 | * io_kiocb alloc cache |
| 317 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 318 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 319 | unsigned int free_reqs; |
| 320 | |
| 321 | bool plug_started; |
| 322 | |
| 323 | /* |
| 324 | * Batch completion logic |
| 325 | */ |
| 326 | struct io_comp_state comp; |
| 327 | |
| 328 | /* |
| 329 | * File reference cache |
| 330 | */ |
| 331 | struct file *file; |
| 332 | unsigned int fd; |
| 333 | unsigned int file_refs; |
| 334 | unsigned int ios_left; |
| 335 | }; |
| 336 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 337 | struct io_ring_ctx { |
| 338 | struct { |
| 339 | struct percpu_ref refs; |
| 340 | } ____cacheline_aligned_in_smp; |
| 341 | |
| 342 | struct { |
| 343 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 344 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 345 | unsigned int drain_next: 1; |
| 346 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 347 | unsigned int restricted: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 348 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 349 | /* |
| 350 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 351 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 352 | * |
| 353 | * This indirection could e.g. be used to assign fixed |
| 354 | * io_uring_sqe entries to operations and only submit them to |
| 355 | * the queue when needed. |
| 356 | * |
| 357 | * The kernel modifies neither the indices array nor the entries |
| 358 | * array. |
| 359 | */ |
| 360 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 361 | unsigned cached_sq_head; |
| 362 | unsigned sq_entries; |
| 363 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 364 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 365 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 366 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 367 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 368 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 369 | /* hashed buffered write serialization */ |
| 370 | struct io_wq_hash *hash_map; |
| 371 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 372 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 373 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 374 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 375 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 376 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 377 | } ____cacheline_aligned_in_smp; |
| 378 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 379 | struct { |
| 380 | struct mutex uring_lock; |
| 381 | wait_queue_head_t wait; |
| 382 | } ____cacheline_aligned_in_smp; |
| 383 | |
| 384 | struct io_submit_state submit_state; |
| 385 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 386 | struct io_rings *rings; |
| 387 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 388 | /* Only used for accounting purposes */ |
| 389 | struct mm_struct *mm_account; |
| 390 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 391 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 392 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 393 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 394 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 395 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 396 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 397 | /* |
| 398 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 399 | * readers must ensure that ->refs is alive as long as the file* is |
| 400 | * used. Only updated through io_uring_register(2). |
| 401 | */ |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 402 | struct io_rsrc_data *file_data; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 403 | struct io_file_table file_table; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 404 | unsigned nr_user_files; |
| 405 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 406 | /* if used, fixed mapped user buffers */ |
| 407 | unsigned nr_user_bufs; |
| 408 | struct io_mapped_ubuf *user_bufs; |
| 409 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 410 | struct user_struct *user; |
| 411 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 412 | struct completion ref_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 413 | |
| 414 | #if defined(CONFIG_UNIX) |
| 415 | struct socket *ring_sock; |
| 416 | #endif |
| 417 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 418 | struct xarray io_buffers; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 419 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 420 | struct xarray personalities; |
| 421 | u32 pers_next; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 422 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 423 | struct { |
| 424 | unsigned cached_cq_tail; |
| 425 | unsigned cq_entries; |
| 426 | unsigned cq_mask; |
| 427 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 428 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 429 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 430 | struct wait_queue_head cq_wait; |
| 431 | struct fasync_struct *cq_fasync; |
| 432 | struct eventfd_ctx *cq_ev_fd; |
| 433 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 434 | |
| 435 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 436 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 437 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 438 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 439 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 440 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 441 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 442 | * manipulate the list, hence no extra locking is needed there. |
| 443 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 444 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 445 | struct hlist_head *cancel_hash; |
| 446 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 447 | bool poll_multi_file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 448 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 449 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 450 | struct delayed_work rsrc_put_work; |
| 451 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 452 | struct list_head rsrc_ref_list; |
| 453 | spinlock_t rsrc_ref_lock; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 454 | struct io_rsrc_node *rsrc_node; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 455 | struct io_rsrc_node *rsrc_backup_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 456 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 457 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 458 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 459 | /* exit task_work */ |
| 460 | struct callback_head *exit_task_work; |
| 461 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 462 | /* Keep this last, we don't need it for the fast path */ |
| 463 | struct work_struct exit_work; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 464 | struct list_head tctx_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 467 | struct io_uring_task { |
| 468 | /* submission side */ |
| 469 | struct xarray xa; |
| 470 | struct wait_queue_head wait; |
Stefan Metzmacher | ee53fb2 | 2021-03-15 12:56:57 +0100 | [diff] [blame] | 471 | const struct io_ring_ctx *last; |
| 472 | struct io_wq *io_wq; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 473 | struct percpu_counter inflight; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 474 | atomic_t inflight_tracked; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 475 | atomic_t in_idle; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 476 | |
| 477 | spinlock_t task_lock; |
| 478 | struct io_wq_work_list task_list; |
| 479 | unsigned long task_state; |
| 480 | struct callback_head task_work; |
| 481 | }; |
| 482 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 483 | /* |
| 484 | * First field must be the file pointer in all the |
| 485 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 486 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 487 | struct io_poll_iocb { |
| 488 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 489 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 490 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 491 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 492 | bool canceled; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 493 | struct wait_queue_entry wait; |
| 494 | }; |
| 495 | |
| 496 | struct io_poll_update { |
| 497 | struct file *file; |
| 498 | u64 old_user_data; |
| 499 | u64 new_user_data; |
| 500 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 501 | bool update_events; |
| 502 | bool update_user_data; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 503 | }; |
| 504 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 505 | struct io_close { |
| 506 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 507 | int fd; |
| 508 | }; |
| 509 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 510 | struct io_timeout_data { |
| 511 | struct io_kiocb *req; |
| 512 | struct hrtimer timer; |
| 513 | struct timespec64 ts; |
| 514 | enum hrtimer_mode mode; |
| 515 | }; |
| 516 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 517 | struct io_accept { |
| 518 | struct file *file; |
| 519 | struct sockaddr __user *addr; |
| 520 | int __user *addr_len; |
| 521 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 522 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | struct io_sync { |
| 526 | struct file *file; |
| 527 | loff_t len; |
| 528 | loff_t off; |
| 529 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 530 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 533 | struct io_cancel { |
| 534 | struct file *file; |
| 535 | u64 addr; |
| 536 | }; |
| 537 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 538 | struct io_timeout { |
| 539 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 540 | u32 off; |
| 541 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 542 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 543 | /* head of the link, used by linked timeouts only */ |
| 544 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 545 | }; |
| 546 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 547 | struct io_timeout_rem { |
| 548 | struct file *file; |
| 549 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 550 | |
| 551 | /* timeout update */ |
| 552 | struct timespec64 ts; |
| 553 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 554 | }; |
| 555 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 556 | struct io_rw { |
| 557 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 558 | struct kiocb kiocb; |
| 559 | u64 addr; |
| 560 | u64 len; |
| 561 | }; |
| 562 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 563 | struct io_connect { |
| 564 | struct file *file; |
| 565 | struct sockaddr __user *addr; |
| 566 | int addr_len; |
| 567 | }; |
| 568 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 569 | struct io_sr_msg { |
| 570 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 571 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 572 | struct compat_msghdr __user *umsg_compat; |
| 573 | struct user_msghdr __user *umsg; |
| 574 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 575 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 576 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 577 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 578 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 579 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 580 | }; |
| 581 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 582 | struct io_open { |
| 583 | struct file *file; |
| 584 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 585 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 586 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 587 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 588 | }; |
| 589 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 590 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 591 | struct file *file; |
| 592 | u64 arg; |
| 593 | u32 nr_args; |
| 594 | u32 offset; |
| 595 | }; |
| 596 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 597 | struct io_fadvise { |
| 598 | struct file *file; |
| 599 | u64 offset; |
| 600 | u32 len; |
| 601 | u32 advice; |
| 602 | }; |
| 603 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 604 | struct io_madvise { |
| 605 | struct file *file; |
| 606 | u64 addr; |
| 607 | u32 len; |
| 608 | u32 advice; |
| 609 | }; |
| 610 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 611 | struct io_epoll { |
| 612 | struct file *file; |
| 613 | int epfd; |
| 614 | int op; |
| 615 | int fd; |
| 616 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 617 | }; |
| 618 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 619 | struct io_splice { |
| 620 | struct file *file_out; |
| 621 | struct file *file_in; |
| 622 | loff_t off_out; |
| 623 | loff_t off_in; |
| 624 | u64 len; |
| 625 | unsigned int flags; |
| 626 | }; |
| 627 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 628 | struct io_provide_buf { |
| 629 | struct file *file; |
| 630 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 631 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 632 | __u32 bgid; |
| 633 | __u16 nbufs; |
| 634 | __u16 bid; |
| 635 | }; |
| 636 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 637 | struct io_statx { |
| 638 | struct file *file; |
| 639 | int dfd; |
| 640 | unsigned int mask; |
| 641 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 642 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 643 | struct statx __user *buffer; |
| 644 | }; |
| 645 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 646 | struct io_shutdown { |
| 647 | struct file *file; |
| 648 | int how; |
| 649 | }; |
| 650 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 651 | struct io_rename { |
| 652 | struct file *file; |
| 653 | int old_dfd; |
| 654 | int new_dfd; |
| 655 | struct filename *oldpath; |
| 656 | struct filename *newpath; |
| 657 | int flags; |
| 658 | }; |
| 659 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 660 | struct io_unlink { |
| 661 | struct file *file; |
| 662 | int dfd; |
| 663 | int flags; |
| 664 | struct filename *filename; |
| 665 | }; |
| 666 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 667 | struct io_completion { |
| 668 | struct file *file; |
| 669 | struct list_head list; |
Pavel Begunkov | 8c3f9cd | 2021-02-28 22:35:15 +0000 | [diff] [blame] | 670 | u32 cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 671 | }; |
| 672 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 673 | struct io_async_connect { |
| 674 | struct sockaddr_storage address; |
| 675 | }; |
| 676 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 677 | struct io_async_msghdr { |
| 678 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 679 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 680 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 681 | struct sockaddr __user *uaddr; |
| 682 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 683 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 684 | }; |
| 685 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 686 | struct io_async_rw { |
| 687 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 688 | const struct iovec *free_iovec; |
| 689 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 690 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 691 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 692 | }; |
| 693 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 694 | enum { |
| 695 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 696 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 697 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 698 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 699 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 700 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 701 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 702 | REQ_F_FAIL_LINK_BIT, |
| 703 | REQ_F_INFLIGHT_BIT, |
| 704 | REQ_F_CUR_POS_BIT, |
| 705 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 706 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 707 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 708 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 709 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 710 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 711 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 712 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 713 | REQ_F_DONT_REISSUE_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 714 | /* keep async read/write and isreg together and in order */ |
| 715 | REQ_F_ASYNC_READ_BIT, |
| 716 | REQ_F_ASYNC_WRITE_BIT, |
| 717 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 718 | |
| 719 | /* not a real bit, just to check we're not overflowing the space */ |
| 720 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 721 | }; |
| 722 | |
| 723 | enum { |
| 724 | /* ctx owns file */ |
| 725 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 726 | /* drain existing IO first */ |
| 727 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 728 | /* linked sqes */ |
| 729 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 730 | /* doesn't sever on completion < 0 */ |
| 731 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 732 | /* IOSQE_ASYNC */ |
| 733 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 734 | /* IOSQE_BUFFER_SELECT */ |
| 735 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 736 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 737 | /* fail rest of links */ |
| 738 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 739 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 740 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 741 | /* read/write uses file position */ |
| 742 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 743 | /* must not punt to workers */ |
| 744 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 745 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 746 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 747 | /* needs cleanup */ |
| 748 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 749 | /* already went through poll handler */ |
| 750 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 751 | /* buffer already selected */ |
| 752 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 753 | /* linked timeout is active, i.e. prepared by link's head */ |
| 754 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 755 | /* completion is deferred through io_comp_state */ |
| 756 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 757 | /* caller should reissue async */ |
| 758 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 759 | /* don't attempt request reissue, see io_rw_reissue() */ |
| 760 | REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 761 | /* supports async reads */ |
| 762 | REQ_F_ASYNC_READ = BIT(REQ_F_ASYNC_READ_BIT), |
| 763 | /* supports async writes */ |
| 764 | REQ_F_ASYNC_WRITE = BIT(REQ_F_ASYNC_WRITE_BIT), |
| 765 | /* regular file */ |
| 766 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | struct async_poll { |
| 770 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 771 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 772 | }; |
| 773 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 774 | struct io_task_work { |
| 775 | struct io_wq_work_node node; |
| 776 | task_work_func_t func; |
| 777 | }; |
| 778 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 779 | /* |
| 780 | * NOTE! Each of the iocb union members has the file pointer |
| 781 | * as the first entry in their struct definition. So you can |
| 782 | * access the file pointer through any of the sub-structs, |
| 783 | * or directly as just 'ki_filp' in this struct. |
| 784 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 785 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 786 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 787 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 788 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 789 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 790 | struct io_poll_update poll_update; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 791 | struct io_accept accept; |
| 792 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 793 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 794 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 795 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 796 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 797 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 798 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 799 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 800 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 801 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 802 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 803 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 804 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 805 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 806 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 807 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 808 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 809 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 810 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 811 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 812 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 813 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 814 | /* opcode allocated if it needs to store data for async defer */ |
| 815 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 816 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 817 | /* polled IO has completed */ |
| 818 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 819 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 820 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 821 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 822 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 823 | struct io_ring_ctx *ctx; |
| 824 | unsigned int flags; |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 825 | atomic_t refs; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 826 | struct task_struct *task; |
| 827 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 828 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 829 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 830 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 831 | |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 832 | /* used with ctx->iopoll_list with reads/writes */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 833 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 834 | union { |
| 835 | struct io_task_work io_task_work; |
| 836 | struct callback_head task_work; |
| 837 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 838 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 839 | struct hlist_node hash_node; |
| 840 | struct async_poll *apoll; |
| 841 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 842 | }; |
| 843 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 844 | struct io_tctx_node { |
| 845 | struct list_head ctx_node; |
| 846 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 847 | struct io_ring_ctx *ctx; |
| 848 | }; |
| 849 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 850 | struct io_defer_entry { |
| 851 | struct list_head list; |
| 852 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 853 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 854 | }; |
| 855 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 856 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 857 | /* needs req->file assigned */ |
| 858 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 859 | /* hash wq insertion if file is a regular file */ |
| 860 | unsigned hash_reg_file : 1; |
| 861 | /* unbound wq insertion if file is a non-regular file */ |
| 862 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 863 | /* opcode is not supported by this kernel */ |
| 864 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 865 | /* set if opcode supports polled "wait" */ |
| 866 | unsigned pollin : 1; |
| 867 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 868 | /* op supports buffer selection */ |
| 869 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 870 | /* do prep async if is going to be punted */ |
| 871 | unsigned needs_async_setup : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 872 | /* should block plug */ |
| 873 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 874 | /* size of async data needed, if any */ |
| 875 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 876 | }; |
| 877 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 878 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 879 | [IORING_OP_NOP] = {}, |
| 880 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 881 | .needs_file = 1, |
| 882 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 883 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 884 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 885 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 886 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 887 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 888 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 889 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 890 | .needs_file = 1, |
| 891 | .hash_reg_file = 1, |
| 892 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 893 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 894 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 895 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 896 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 897 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 898 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 899 | .needs_file = 1, |
| 900 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 901 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 902 | .needs_file = 1, |
| 903 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 904 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 905 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 906 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 907 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 908 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 909 | .needs_file = 1, |
| 910 | .hash_reg_file = 1, |
| 911 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 912 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 913 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 914 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 915 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 916 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 917 | .needs_file = 1, |
| 918 | .unbound_nonreg_file = 1, |
| 919 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 920 | [IORING_OP_POLL_REMOVE] = {}, |
| 921 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 922 | .needs_file = 1, |
| 923 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 924 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 925 | .needs_file = 1, |
| 926 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 927 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 928 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 929 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 930 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 931 | [IORING_OP_RECVMSG] = { |
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 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 935 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 936 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 937 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 938 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 939 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 940 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 941 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 942 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 943 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 944 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 945 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 946 | .needs_file = 1, |
| 947 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 948 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 949 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 950 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 951 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 952 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 953 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 954 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 955 | .needs_file = 1, |
| 956 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 957 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 958 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 959 | .async_size = sizeof(struct io_async_connect), |
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_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 962 | .needs_file = 1, |
| 963 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 964 | [IORING_OP_OPENAT] = {}, |
| 965 | [IORING_OP_CLOSE] = {}, |
| 966 | [IORING_OP_FILES_UPDATE] = {}, |
| 967 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 968 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 969 | .needs_file = 1, |
| 970 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 971 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 972 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 973 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 974 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 975 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 976 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 977 | .needs_file = 1, |
| 978 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 979 | .pollout = 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_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 984 | .needs_file = 1, |
| 985 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 986 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 987 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 988 | .needs_file = 1, |
| 989 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 990 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 991 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 992 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 993 | .needs_file = 1, |
| 994 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 995 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 996 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 997 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 998 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 999 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1000 | [IORING_OP_EPOLL_CTL] = { |
| 1001 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1002 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1003 | [IORING_OP_SPLICE] = { |
| 1004 | .needs_file = 1, |
| 1005 | .hash_reg_file = 1, |
| 1006 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1007 | }, |
| 1008 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 1009 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1010 | [IORING_OP_TEE] = { |
| 1011 | .needs_file = 1, |
| 1012 | .hash_reg_file = 1, |
| 1013 | .unbound_nonreg_file = 1, |
| 1014 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1015 | [IORING_OP_SHUTDOWN] = { |
| 1016 | .needs_file = 1, |
| 1017 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1018 | [IORING_OP_RENAMEAT] = {}, |
| 1019 | [IORING_OP_UNLINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1020 | }; |
| 1021 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1022 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 1023 | static void io_uring_del_task_file(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1024 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1025 | struct task_struct *task, |
| 1026 | struct files_struct *files); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 1027 | static void io_uring_cancel_sqpoll(struct io_sq_data *sqd); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1028 | 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] | 1029 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1030 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1031 | long res, unsigned int cflags); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1032 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1033 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1034 | static void io_dismantle_req(struct io_kiocb *req); |
| 1035 | static void io_put_task(struct task_struct *task, int nr); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1036 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 1037 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1038 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1039 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1040 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1041 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1042 | static struct file *io_file_get(struct io_submit_state *state, |
| 1043 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1044 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1045 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1046 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1047 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1048 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1049 | struct io_ring_ctx *ctx); |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 1050 | static bool io_poll_remove_waitqs(struct io_kiocb *req); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1051 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1052 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1053 | static struct kmem_cache *req_cachep; |
| 1054 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1055 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1056 | |
| 1057 | struct sock *io_uring_get_socket(struct file *file) |
| 1058 | { |
| 1059 | #if defined(CONFIG_UNIX) |
| 1060 | if (file->f_op == &io_uring_fops) { |
| 1061 | struct io_ring_ctx *ctx = file->private_data; |
| 1062 | |
| 1063 | return ctx->ring_sock->sk; |
| 1064 | } |
| 1065 | #endif |
| 1066 | return NULL; |
| 1067 | } |
| 1068 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1069 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1070 | #define io_for_each_link(pos, head) \ |
| 1071 | for (pos = (head); pos; pos = pos->link) |
| 1072 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 1073 | static inline void io_req_set_rsrc_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1074 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1075 | struct io_ring_ctx *ctx = req->ctx; |
| 1076 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1077 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1078 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1079 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1083 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1084 | { |
| 1085 | bool got = percpu_ref_tryget(ref); |
| 1086 | |
| 1087 | /* already at zero, wait for ->release() */ |
| 1088 | if (!got) |
| 1089 | wait_for_completion(compl); |
| 1090 | percpu_ref_resurrect(ref); |
| 1091 | if (got) |
| 1092 | percpu_ref_put(ref); |
| 1093 | } |
| 1094 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1095 | static bool io_match_task(struct io_kiocb *head, |
| 1096 | struct task_struct *task, |
| 1097 | struct files_struct *files) |
| 1098 | { |
| 1099 | struct io_kiocb *req; |
| 1100 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1101 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1102 | return false; |
| 1103 | if (!files) |
| 1104 | return true; |
| 1105 | |
| 1106 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1107 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1108 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1109 | } |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1113 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1114 | { |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 1115 | if (req->flags & REQ_F_LINK) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1116 | req->flags |= REQ_F_FAIL_LINK; |
| 1117 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1118 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1119 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1120 | { |
| 1121 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1122 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1123 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1126 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1127 | { |
| 1128 | return !req->timeout.off; |
| 1129 | } |
| 1130 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1131 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1132 | { |
| 1133 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1134 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1135 | |
| 1136 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1137 | if (!ctx) |
| 1138 | return NULL; |
| 1139 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1140 | /* |
| 1141 | * Use 5 bits less than the max cq entries, that should give us around |
| 1142 | * 32 entries per hash list if totally full and uniformly spread. |
| 1143 | */ |
| 1144 | hash_bits = ilog2(p->cq_entries); |
| 1145 | hash_bits -= 5; |
| 1146 | if (hash_bits <= 0) |
| 1147 | hash_bits = 1; |
| 1148 | ctx->cancel_hash_bits = hash_bits; |
| 1149 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1150 | GFP_KERNEL); |
| 1151 | if (!ctx->cancel_hash) |
| 1152 | goto err; |
| 1153 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1154 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1155 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1156 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1157 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1158 | |
| 1159 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1160 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1161 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1162 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1163 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1164 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1165 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1166 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1167 | mutex_init(&ctx->uring_lock); |
| 1168 | init_waitqueue_head(&ctx->wait); |
| 1169 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1170 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1171 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1172 | INIT_LIST_HEAD(&ctx->timeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1173 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1174 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1175 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1176 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1177 | INIT_LIST_HEAD(&ctx->tctx_list); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1178 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1179 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1180 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1181 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1182 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1183 | kfree(ctx); |
| 1184 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1187 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1188 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1189 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1190 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1191 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1192 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1193 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1194 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1195 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1196 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1197 | } |
| 1198 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1199 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1200 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1201 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1202 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1203 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1207 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1208 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1209 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1210 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1211 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1212 | if (!req->work.creds) |
| 1213 | req->work.creds = get_current_cred(); |
| 1214 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1215 | req->work.list.next = NULL; |
| 1216 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1217 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1218 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1219 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1220 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1221 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1222 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1223 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1224 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1225 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1226 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1227 | |
| 1228 | switch (req->opcode) { |
| 1229 | case IORING_OP_SPLICE: |
| 1230 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1231 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1232 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1233 | break; |
| 1234 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1235 | } |
| 1236 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1237 | static void io_prep_async_link(struct io_kiocb *req) |
| 1238 | { |
| 1239 | struct io_kiocb *cur; |
| 1240 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1241 | io_for_each_link(cur, req) |
| 1242 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1243 | } |
| 1244 | |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1245 | static void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1246 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1247 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1248 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1249 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1250 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1251 | BUG_ON(!tctx); |
| 1252 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1253 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1254 | /* init ->work of the whole link before punting */ |
| 1255 | io_prep_async_link(req); |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1256 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1257 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1258 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1259 | if (link) |
| 1260 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1261 | } |
| 1262 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1263 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1264 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1265 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1266 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1267 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1268 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1269 | atomic_set(&req->ctx->cq_timeouts, |
| 1270 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1271 | list_del_init(&req->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1272 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1273 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1277 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1278 | { |
| 1279 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1280 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1281 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1282 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1283 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1284 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1285 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1286 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1287 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1288 | } while (!list_empty(&ctx->defer_list)); |
| 1289 | } |
| 1290 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1291 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1292 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1293 | u32 seq; |
| 1294 | |
| 1295 | if (list_empty(&ctx->timeout_list)) |
| 1296 | return; |
| 1297 | |
| 1298 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1299 | |
| 1300 | do { |
| 1301 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1302 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1303 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1304 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1305 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1306 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1307 | |
| 1308 | /* |
| 1309 | * Since seq can easily wrap around over time, subtract |
| 1310 | * the last seq at which timeouts were flushed before comparing. |
| 1311 | * Assuming not more than 2^31-1 events have happened since, |
| 1312 | * these subtractions won't have wrapped, so we can check if |
| 1313 | * target is in [last_seq, current_seq] by comparing the two. |
| 1314 | */ |
| 1315 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1316 | events_got = seq - ctx->cq_last_tm_flush; |
| 1317 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1318 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1319 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1320 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1321 | io_kill_timeout(req, 0); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1322 | } while (!list_empty(&ctx->timeout_list)); |
| 1323 | |
| 1324 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1325 | } |
| 1326 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1327 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1328 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1329 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1330 | |
| 1331 | /* order cqe stores with ring update */ |
| 1332 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1333 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1334 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1335 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1336 | } |
| 1337 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1338 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1339 | { |
| 1340 | struct io_rings *r = ctx->rings; |
| 1341 | |
| 1342 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1343 | } |
| 1344 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1345 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1346 | { |
| 1347 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1348 | } |
| 1349 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1350 | 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] | 1351 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1352 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1353 | unsigned tail; |
| 1354 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1355 | /* |
| 1356 | * writes to the cq entry need to come after reading head; the |
| 1357 | * control dependency is enough as we're using WRITE_ONCE to |
| 1358 | * fill the cq entry |
| 1359 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1360 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1361 | return NULL; |
| 1362 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1363 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1364 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1367 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1368 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1369 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1370 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1371 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1372 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1373 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1376 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1377 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1378 | /* see waitqueue_active() comment */ |
| 1379 | smp_mb(); |
| 1380 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1381 | if (waitqueue_active(&ctx->wait)) |
| 1382 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1383 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1384 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1385 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1386 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1387 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1388 | wake_up_interruptible(&ctx->cq_wait); |
| 1389 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1390 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1391 | } |
| 1392 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1393 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1394 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1395 | /* see waitqueue_active() comment */ |
| 1396 | smp_mb(); |
| 1397 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1398 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1399 | if (waitqueue_active(&ctx->wait)) |
| 1400 | wake_up(&ctx->wait); |
| 1401 | } |
| 1402 | if (io_should_trigger_evfd(ctx)) |
| 1403 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1404 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1405 | wake_up_interruptible(&ctx->cq_wait); |
| 1406 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1407 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1410 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1411 | 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] | 1412 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1413 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1414 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1415 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1416 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1417 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1418 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1419 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1420 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1421 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1422 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1423 | struct io_uring_cqe *cqe = io_get_cqring(ctx); |
| 1424 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1425 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1426 | if (!cqe && !force) |
| 1427 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1428 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1429 | struct io_overflow_cqe, list); |
| 1430 | if (cqe) |
| 1431 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1432 | else |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1433 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1434 | ++ctx->cached_cq_overflow); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1435 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1436 | list_del(&ocqe->list); |
| 1437 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1440 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1441 | if (all_flushed) { |
| 1442 | clear_bit(0, &ctx->sq_check_overflow); |
| 1443 | clear_bit(0, &ctx->cq_check_overflow); |
| 1444 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1445 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1446 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1447 | if (posted) |
| 1448 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1449 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1450 | if (posted) |
| 1451 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1452 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1455 | 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] | 1456 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1457 | bool ret = true; |
| 1458 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1459 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1460 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1461 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1462 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1463 | ret = __io_cqring_overflow_flush(ctx, force); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1464 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1465 | mutex_unlock(&ctx->uring_lock); |
| 1466 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1467 | |
| 1468 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1471 | /* |
| 1472 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1473 | * see commit f958d7b528b1 for details. |
| 1474 | */ |
| 1475 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1476 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1477 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1478 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1479 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1480 | return atomic_inc_not_zero(&req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs) |
| 1484 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1485 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1486 | return atomic_sub_and_test(refs, &req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 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_dec_and_test(&req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static inline void req_ref_put(struct io_kiocb *req) |
| 1496 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1497 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static inline void req_ref_get(struct io_kiocb *req) |
| 1501 | { |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 1502 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1503 | atomic_inc(&req->refs); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1506 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
| 1507 | long res, unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1508 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1509 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1510 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1511 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1512 | if (!ocqe) { |
| 1513 | /* |
| 1514 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1515 | * or cannot allocate an overflow entry, then we need to drop it |
| 1516 | * on the floor. |
| 1517 | */ |
| 1518 | WRITE_ONCE(ctx->rings->cq_overflow, ++ctx->cached_cq_overflow); |
| 1519 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1520 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1521 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1522 | set_bit(0, &ctx->sq_check_overflow); |
| 1523 | set_bit(0, &ctx->cq_check_overflow); |
| 1524 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
| 1525 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1526 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1527 | ocqe->cqe.res = res; |
| 1528 | ocqe->cqe.flags = cflags; |
| 1529 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1530 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1533 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1534 | long res, unsigned int cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1535 | { |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1536 | struct io_uring_cqe *cqe; |
| 1537 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1538 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1539 | |
| 1540 | /* |
| 1541 | * If we can't get a cq entry, userspace overflowed the |
| 1542 | * submission (by quite a lot). Increment the overflow count in |
| 1543 | * the ring. |
| 1544 | */ |
| 1545 | cqe = io_get_cqring(ctx); |
| 1546 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1547 | WRITE_ONCE(cqe->user_data, user_data); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1548 | WRITE_ONCE(cqe->res, res); |
| 1549 | WRITE_ONCE(cqe->flags, cflags); |
| 1550 | return true; |
| 1551 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1552 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /* not as hot to bloat with inlining */ |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1556 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
| 1557 | long res, unsigned int cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1558 | { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1559 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1562 | static void io_req_complete_post(struct io_kiocb *req, long res, |
| 1563 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1564 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1565 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1566 | unsigned long flags; |
| 1567 | |
| 1568 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1569 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1570 | /* |
| 1571 | * If we're the last reference to this request, add to our locked |
| 1572 | * free_list cache. |
| 1573 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1574 | if (req_ref_put_and_test(req)) { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1575 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1576 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1577 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
| 1578 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) |
| 1579 | io_disarm_next(req); |
| 1580 | if (req->link) { |
| 1581 | io_req_task_queue(req->link); |
| 1582 | req->link = NULL; |
| 1583 | } |
| 1584 | } |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1585 | io_dismantle_req(req); |
| 1586 | io_put_task(req->task, 1); |
| 1587 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1588 | cs->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1589 | } else { |
| 1590 | if (!percpu_ref_tryget(&ctx->refs)) |
| 1591 | req = NULL; |
| 1592 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1593 | io_commit_cqring(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1594 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1595 | |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1596 | if (req) { |
| 1597 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1598 | percpu_ref_put(&ctx->refs); |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1599 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1602 | static inline bool io_req_needs_clean(struct io_kiocb *req) |
| 1603 | { |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 1604 | return req->flags & (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 1605 | REQ_F_POLLED | REQ_F_INFLIGHT); |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1606 | } |
| 1607 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1608 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1609 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1610 | { |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1611 | if (io_req_needs_clean(req)) |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1612 | io_clean_op(req); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1613 | req->result = res; |
| 1614 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1615 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1616 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1617 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1618 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1619 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1620 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1621 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1622 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1623 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1624 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1625 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1626 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1627 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1628 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1629 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1632 | static void io_req_complete_failed(struct io_kiocb *req, long res) |
| 1633 | { |
| 1634 | req_set_fail_links(req); |
| 1635 | io_put_req(req); |
| 1636 | io_req_complete_post(req, res, 0); |
| 1637 | } |
| 1638 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1639 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
| 1640 | struct io_comp_state *cs) |
| 1641 | { |
| 1642 | spin_lock_irq(&ctx->completion_lock); |
| 1643 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1644 | cs->locked_free_nr = 0; |
| 1645 | spin_unlock_irq(&ctx->completion_lock); |
| 1646 | } |
| 1647 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1648 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1649 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1650 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1651 | struct io_submit_state *state = &ctx->submit_state; |
| 1652 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1653 | int nr; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1654 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1655 | /* |
| 1656 | * If we have more than a batch's worth of requests in our IRQ side |
| 1657 | * locked cache, grab the lock and move them over to our submission |
| 1658 | * side cache. |
| 1659 | */ |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1660 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) |
| 1661 | io_flush_cached_locked_reqs(ctx, cs); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1662 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1663 | nr = state->free_reqs; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1664 | while (!list_empty(&cs->free_list)) { |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1665 | struct io_kiocb *req = list_first_entry(&cs->free_list, |
| 1666 | struct io_kiocb, compl.list); |
| 1667 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1668 | list_del(&req->compl.list); |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1669 | state->reqs[nr++] = req; |
| 1670 | if (nr == ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1671 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1672 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1673 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1674 | state->free_reqs = nr; |
| 1675 | return nr != 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1678 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1679 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1680 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1681 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1682 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1683 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1684 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1685 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1686 | int ret; |
| 1687 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1688 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1689 | goto got_req; |
| 1690 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1691 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1692 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1696 | * retry single alloc to be on the safe side. |
| 1697 | */ |
| 1698 | if (unlikely(ret <= 0)) { |
| 1699 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1700 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1701 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1702 | ret = 1; |
| 1703 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1704 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1705 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1706 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1707 | state->free_reqs--; |
| 1708 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1711 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1712 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 1713 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1714 | fput(file); |
| 1715 | } |
| 1716 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1717 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1718 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1719 | unsigned int flags = req->flags; |
| 1720 | |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 1721 | if (io_req_needs_clean(req)) |
| 1722 | io_clean_op(req); |
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); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1725 | if (req->fixed_rsrc_refs) |
| 1726 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 1727 | if (req->async_data) |
| 1728 | kfree(req->async_data); |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1729 | if (req->work.creds) { |
| 1730 | put_cred(req->work.creds); |
| 1731 | req->work.creds = NULL; |
| 1732 | } |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1733 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1734 | |
Pavel Begunkov | b23fcf4 | 2021-03-01 18:20:48 +0000 | [diff] [blame] | 1735 | /* must to be called somewhat shortly after putting a request */ |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1736 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1737 | { |
| 1738 | struct io_uring_task *tctx = task->io_uring; |
| 1739 | |
| 1740 | percpu_counter_sub(&tctx->inflight, nr); |
| 1741 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1742 | wake_up(&tctx->wait); |
| 1743 | put_task_struct_many(task, nr); |
| 1744 | } |
| 1745 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1746 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1747 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1748 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1749 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1750 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1751 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1752 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1753 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1754 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1755 | } |
| 1756 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1757 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1758 | { |
| 1759 | struct io_kiocb *nxt = req->link; |
| 1760 | |
| 1761 | req->link = nxt->link; |
| 1762 | nxt->link = NULL; |
| 1763 | } |
| 1764 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1765 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 1766 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1767 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1768 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1769 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 1770 | /* |
| 1771 | * Can happen if a linked timeout fired and link had been like |
| 1772 | * req -> link t-out -> link t-out [-> ...] |
| 1773 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1774 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 1775 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1776 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1777 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 1778 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1779 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1780 | io_cqring_fill_event(link->ctx, link->user_data, |
| 1781 | -ECANCELED, 0); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1782 | io_put_req_deferred(link, 1); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 1783 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1784 | } |
| 1785 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 1786 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1787 | } |
| 1788 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1789 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1790 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1791 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1792 | struct io_kiocb *nxt, *link = req->link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1793 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1794 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1795 | while (link) { |
| 1796 | nxt = link->link; |
| 1797 | link->link = NULL; |
| 1798 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1799 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1800 | io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0); |
Jens Axboe | 1575f21 | 2021-02-27 15:20:49 -0700 | [diff] [blame] | 1801 | io_put_req_deferred(link, 2); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1802 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1803 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1804 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1805 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1806 | static bool io_disarm_next(struct io_kiocb *req) |
| 1807 | __must_hold(&req->ctx->completion_lock) |
| 1808 | { |
| 1809 | bool posted = false; |
| 1810 | |
| 1811 | if (likely(req->flags & REQ_F_LINK_TIMEOUT)) |
| 1812 | posted = io_kill_linked_timeout(req); |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 1813 | if (unlikely((req->flags & REQ_F_FAIL_LINK) && |
| 1814 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1815 | posted |= (req->link != NULL); |
| 1816 | io_fail_links(req); |
| 1817 | } |
| 1818 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1819 | } |
| 1820 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1821 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1822 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1823 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1824 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1825 | /* |
| 1826 | * If LINK is set, we have dependent requests in this chain. If we |
| 1827 | * didn't fail this request, queue the first one up, moving any other |
| 1828 | * dependencies to the next request. In case of failure, fail the rest |
| 1829 | * of the chain. |
| 1830 | */ |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1831 | if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL_LINK)) { |
| 1832 | struct io_ring_ctx *ctx = req->ctx; |
| 1833 | unsigned long flags; |
| 1834 | bool posted; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1835 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1836 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1837 | posted = io_disarm_next(req); |
| 1838 | if (posted) |
| 1839 | io_commit_cqring(req->ctx); |
| 1840 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1841 | if (posted) |
| 1842 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1843 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 1844 | nxt = req->link; |
| 1845 | req->link = NULL; |
| 1846 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1847 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1848 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1849 | 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] | 1850 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 1851 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1852 | return NULL; |
| 1853 | return __io_req_find_next(req); |
| 1854 | } |
| 1855 | |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1856 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
| 1857 | { |
| 1858 | if (!ctx) |
| 1859 | return; |
| 1860 | if (ctx->submit_state.comp.nr) { |
| 1861 | mutex_lock(&ctx->uring_lock); |
| 1862 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1863 | mutex_unlock(&ctx->uring_lock); |
| 1864 | } |
| 1865 | percpu_ref_put(&ctx->refs); |
| 1866 | } |
| 1867 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1868 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 1869 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1870 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1871 | struct io_wq_work_list list; |
| 1872 | struct io_wq_work_node *node; |
| 1873 | |
| 1874 | if (wq_list_empty(&tctx->task_list)) |
| 1875 | return false; |
| 1876 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1877 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1878 | list = tctx->task_list; |
| 1879 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1880 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1881 | |
| 1882 | node = list.first; |
| 1883 | while (node) { |
| 1884 | struct io_wq_work_node *next = node->next; |
| 1885 | struct io_kiocb *req; |
| 1886 | |
| 1887 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1888 | if (req->ctx != ctx) { |
| 1889 | ctx_flush_and_put(ctx); |
| 1890 | ctx = req->ctx; |
| 1891 | percpu_ref_get(&ctx->refs); |
| 1892 | } |
| 1893 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1894 | req->task_work.func(&req->task_work); |
| 1895 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1898 | ctx_flush_and_put(ctx); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1899 | return list.first != NULL; |
| 1900 | } |
| 1901 | |
| 1902 | static void tctx_task_work(struct callback_head *cb) |
| 1903 | { |
| 1904 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 1905 | |
Jens Axboe | 1d5f360 | 2021-02-26 14:54:16 -0700 | [diff] [blame] | 1906 | clear_bit(0, &tctx->task_state); |
| 1907 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1908 | while (__tctx_task_work(tctx)) |
| 1909 | cond_resched(); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1912 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1913 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1914 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1915 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1916 | enum task_work_notify_mode notify; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1917 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1918 | unsigned long flags; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1919 | int ret = 0; |
| 1920 | |
| 1921 | if (unlikely(tsk->flags & PF_EXITING)) |
| 1922 | return -ESRCH; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1923 | |
| 1924 | WARN_ON_ONCE(!tctx); |
| 1925 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1926 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1927 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1928 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1929 | |
| 1930 | /* task_work already pending, we're done */ |
| 1931 | if (test_bit(0, &tctx->task_state) || |
| 1932 | test_and_set_bit(0, &tctx->task_state)) |
| 1933 | return 0; |
| 1934 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1935 | /* |
| 1936 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1937 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1938 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1939 | * will do the job. |
| 1940 | */ |
| 1941 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
| 1942 | |
| 1943 | if (!task_work_add(tsk, &tctx->task_work, notify)) { |
| 1944 | wake_up_process(tsk); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1945 | return 0; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 1946 | } |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1947 | |
| 1948 | /* |
| 1949 | * Slow path - we failed, find and delete work. if the work is not |
| 1950 | * in the list, it got run and we're fine. |
| 1951 | */ |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1952 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1953 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 1954 | if (&req->io_task_work.node == node) { |
| 1955 | wq_list_del(&tctx->task_list, node, prev); |
| 1956 | ret = 1; |
| 1957 | break; |
| 1958 | } |
| 1959 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1960 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1961 | clear_bit(0, &tctx->task_state); |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 1965 | static bool io_run_task_work_head(struct callback_head **work_head) |
| 1966 | { |
| 1967 | struct callback_head *work, *next; |
| 1968 | bool executed = false; |
| 1969 | |
| 1970 | do { |
| 1971 | work = xchg(work_head, NULL); |
| 1972 | if (!work) |
| 1973 | break; |
| 1974 | |
| 1975 | do { |
| 1976 | next = work->next; |
| 1977 | work->func(work); |
| 1978 | work = next; |
| 1979 | cond_resched(); |
| 1980 | } while (work); |
| 1981 | executed = true; |
| 1982 | } while (1); |
| 1983 | |
| 1984 | return executed; |
| 1985 | } |
| 1986 | |
| 1987 | static void io_task_work_add_head(struct callback_head **work_head, |
| 1988 | struct callback_head *task_work) |
| 1989 | { |
| 1990 | struct callback_head *head; |
| 1991 | |
| 1992 | do { |
| 1993 | head = READ_ONCE(*work_head); |
| 1994 | task_work->next = head; |
| 1995 | } while (cmpxchg(work_head, head, task_work) != head); |
| 1996 | } |
| 1997 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1998 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1999 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2000 | { |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2001 | init_task_work(&req->task_work, cb); |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 2002 | 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] | 2003 | } |
| 2004 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2005 | static void io_req_task_cancel(struct callback_head *cb) |
| 2006 | { |
| 2007 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2008 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2009 | |
Pavel Begunkov | e83acd7 | 2021-02-28 22:35:09 +0000 | [diff] [blame] | 2010 | /* ctx is guaranteed to stay alive while we hold uring_lock */ |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2011 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2012 | io_req_complete_failed(req, req->result); |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2013 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2017 | { |
| 2018 | struct io_ring_ctx *ctx = req->ctx; |
| 2019 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2020 | /* 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] | 2021 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 70aacfe | 2021-03-01 13:02:15 +0000 | [diff] [blame] | 2022 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2023 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2024 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2025 | io_req_complete_failed(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2026 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2027 | } |
| 2028 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2029 | static void io_req_task_submit(struct callback_head *cb) |
| 2030 | { |
| 2031 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2032 | |
| 2033 | __io_req_task_submit(req); |
| 2034 | } |
| 2035 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2036 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2037 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2038 | req->result = ret; |
| 2039 | req->task_work.func = io_req_task_cancel; |
| 2040 | |
| 2041 | if (unlikely(io_req_task_work_add(req))) |
| 2042 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
| 2043 | } |
| 2044 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2045 | static void io_req_task_queue(struct io_kiocb *req) |
| 2046 | { |
| 2047 | req->task_work.func = io_req_task_submit; |
| 2048 | |
| 2049 | if (unlikely(io_req_task_work_add(req))) |
| 2050 | io_req_task_queue_fail(req, -ECANCELED); |
| 2051 | } |
| 2052 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2053 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2054 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2055 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2056 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2057 | if (nxt) |
| 2058 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2059 | } |
| 2060 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2061 | static void io_free_req(struct io_kiocb *req) |
| 2062 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2063 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2064 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2065 | } |
| 2066 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2067 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2068 | struct task_struct *task; |
| 2069 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2070 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2071 | }; |
| 2072 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2073 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2074 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2075 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2076 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2077 | rb->task = NULL; |
| 2078 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2079 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2080 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2081 | struct req_batch *rb) |
| 2082 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2083 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2084 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2085 | if (rb->ctx_refs) |
| 2086 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2087 | } |
| 2088 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2089 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2090 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2091 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2092 | io_queue_next(req); |
Pavel Begunkov | 9667065 | 2021-03-19 17:22:32 +0000 | [diff] [blame] | 2093 | io_dismantle_req(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2094 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2095 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2096 | if (rb->task) |
| 2097 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2098 | rb->task = req->task; |
| 2099 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2100 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2101 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2102 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2103 | |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2104 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2105 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2106 | else |
| 2107 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2108 | } |
| 2109 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2110 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2111 | struct io_ring_ctx *ctx) |
| 2112 | { |
| 2113 | int i, nr = cs->nr; |
| 2114 | struct io_kiocb *req; |
| 2115 | struct req_batch rb; |
| 2116 | |
| 2117 | io_init_req_batch(&rb); |
| 2118 | spin_lock_irq(&ctx->completion_lock); |
| 2119 | for (i = 0; i < nr; i++) { |
| 2120 | req = cs->reqs[i]; |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2121 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
| 2122 | req->compl.cflags); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2123 | } |
| 2124 | io_commit_cqring(ctx); |
| 2125 | spin_unlock_irq(&ctx->completion_lock); |
| 2126 | |
| 2127 | io_cqring_ev_posted(ctx); |
| 2128 | for (i = 0; i < nr; i++) { |
| 2129 | req = cs->reqs[i]; |
| 2130 | |
| 2131 | /* submission and completion refs */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2132 | if (req_ref_sub_and_test(req, 2)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2133 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | io_req_free_batch_finish(ctx, &rb); |
| 2137 | cs->nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2138 | } |
| 2139 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2140 | /* |
| 2141 | * Drop reference to request, return next in chain (if there is one) if this |
| 2142 | * was the last reference to this request. |
| 2143 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2144 | 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] | 2145 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2146 | struct io_kiocb *nxt = NULL; |
| 2147 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2148 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2149 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2150 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2151 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2152 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2155 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2156 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2157 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2158 | io_free_req(req); |
| 2159 | } |
| 2160 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2161 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2162 | { |
| 2163 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2164 | |
| 2165 | io_free_req(req); |
| 2166 | } |
| 2167 | |
| 2168 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2169 | { |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2170 | req->task_work.func = io_put_req_deferred_cb; |
Pavel Begunkov | a05432f | 2021-03-19 17:22:38 +0000 | [diff] [blame] | 2171 | if (unlikely(io_req_task_work_add(req))) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2172 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2176 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2177 | if (req_ref_sub_and_test(req, refs)) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2178 | io_free_req_deferred(req); |
| 2179 | } |
| 2180 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2181 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2182 | { |
| 2183 | /* See comment at the top of this file */ |
| 2184 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2185 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2186 | } |
| 2187 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2188 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2189 | { |
| 2190 | struct io_rings *rings = ctx->rings; |
| 2191 | |
| 2192 | /* make sure SQ entry isn't read before tail */ |
| 2193 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2194 | } |
| 2195 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2196 | 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] | 2197 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2198 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2199 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2200 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2201 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2202 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2203 | kfree(kbuf); |
| 2204 | return cflags; |
| 2205 | } |
| 2206 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2207 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2208 | { |
| 2209 | struct io_buffer *kbuf; |
| 2210 | |
| 2211 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2212 | return io_put_kbuf(req, kbuf); |
| 2213 | } |
| 2214 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2215 | static inline bool io_run_task_work(void) |
| 2216 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2217 | /* |
| 2218 | * Not safe to run on exiting task, and the task_work handling will |
| 2219 | * not add work to such a task. |
| 2220 | */ |
| 2221 | if (unlikely(current->flags & PF_EXITING)) |
| 2222 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2223 | if (current->task_works) { |
| 2224 | __set_current_state(TASK_RUNNING); |
| 2225 | task_work_run(); |
| 2226 | return true; |
| 2227 | } |
| 2228 | |
| 2229 | return false; |
| 2230 | } |
| 2231 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2232 | /* |
| 2233 | * Find and free completed poll iocbs |
| 2234 | */ |
| 2235 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2236 | struct list_head *done) |
| 2237 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2238 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2239 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2240 | |
| 2241 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2242 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2243 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2244 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2245 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2246 | int cflags = 0; |
| 2247 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2248 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2249 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2250 | |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2251 | if (READ_ONCE(req->result) == -EAGAIN && |
| 2252 | !(req->flags & REQ_F_DONT_REISSUE)) { |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2253 | req->iopoll_completed = 0; |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2254 | req_ref_get(req); |
| 2255 | io_queue_async_work(req); |
| 2256 | continue; |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2259 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2260 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2261 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2262 | __io_cqring_fill_event(ctx, req->user_data, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2263 | (*nr_events)++; |
| 2264 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2265 | if (req_ref_put_and_test(req)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2266 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2267 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2268 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2269 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2270 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2271 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2274 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2275 | long min) |
| 2276 | { |
| 2277 | struct io_kiocb *req, *tmp; |
| 2278 | LIST_HEAD(done); |
| 2279 | bool spin; |
| 2280 | int ret; |
| 2281 | |
| 2282 | /* |
| 2283 | * Only spin for completions if we don't have multiple devices hanging |
| 2284 | * off our complete list, and we're under the requested amount. |
| 2285 | */ |
| 2286 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2287 | |
| 2288 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2289 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2290 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2291 | |
| 2292 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2293 | * Move completed and retryable entries to our local lists. |
| 2294 | * If we find a request that requires polling, break out |
| 2295 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2296 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2297 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2298 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2299 | continue; |
| 2300 | } |
| 2301 | if (!list_empty(&done)) |
| 2302 | break; |
| 2303 | |
| 2304 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2305 | if (ret < 0) |
| 2306 | break; |
| 2307 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2308 | /* iopoll may have completed current req */ |
| 2309 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2310 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2311 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2312 | if (ret && spin) |
| 2313 | spin = false; |
| 2314 | ret = 0; |
| 2315 | } |
| 2316 | |
| 2317 | if (!list_empty(&done)) |
| 2318 | io_iopoll_complete(ctx, nr_events, &done); |
| 2319 | |
| 2320 | return ret; |
| 2321 | } |
| 2322 | |
| 2323 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2324 | * We can't just wait for polled events to come to us, we have to actively |
| 2325 | * find and complete them. |
| 2326 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2327 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2328 | { |
| 2329 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2330 | return; |
| 2331 | |
| 2332 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2333 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2334 | unsigned int nr_events = 0; |
| 2335 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2336 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2337 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2338 | /* let it sleep and repeat later if can't complete a request */ |
| 2339 | if (nr_events == 0) |
| 2340 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2341 | /* |
| 2342 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2343 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2344 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2345 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2346 | if (need_resched()) { |
| 2347 | mutex_unlock(&ctx->uring_lock); |
| 2348 | cond_resched(); |
| 2349 | mutex_lock(&ctx->uring_lock); |
| 2350 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2351 | } |
| 2352 | mutex_unlock(&ctx->uring_lock); |
| 2353 | } |
| 2354 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2355 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2356 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2357 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2358 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2359 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2360 | /* |
| 2361 | * We disallow the app entering submit/complete with polling, but we |
| 2362 | * still need to lock the ring to prevent racing with polled issue |
| 2363 | * that got punted to a workqueue. |
| 2364 | */ |
| 2365 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2366 | /* |
| 2367 | * Don't enter poll loop if we already have events pending. |
| 2368 | * If we do, we can potentially be spinning for commands that |
| 2369 | * already triggered a CQE (eg in error). |
| 2370 | */ |
| 2371 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2372 | __io_cqring_overflow_flush(ctx, false); |
| 2373 | if (io_cqring_events(ctx)) |
| 2374 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2375 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2376 | /* |
| 2377 | * If a submit got punted to a workqueue, we can have the |
| 2378 | * application entering polling for a command before it gets |
| 2379 | * issued. That app will hold the uring_lock for the duration |
| 2380 | * of the poll right here, so we need to take a breather every |
| 2381 | * now and then to ensure that the issue has a chance to add |
| 2382 | * the poll to the issued list. Otherwise we can spin here |
| 2383 | * forever, while the workqueue is stuck trying to acquire the |
| 2384 | * very same mutex. |
| 2385 | */ |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2386 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2387 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2388 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2389 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2390 | |
| 2391 | if (list_empty(&ctx->iopoll_list)) |
| 2392 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2393 | } |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2394 | ret = io_do_iopoll(ctx, &nr_events, min); |
| 2395 | } while (!ret && nr_events < min && !need_resched()); |
| 2396 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2397 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2398 | return ret; |
| 2399 | } |
| 2400 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2401 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2402 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2403 | /* |
| 2404 | * Tell lockdep we inherited freeze protection from submission |
| 2405 | * thread. |
| 2406 | */ |
| 2407 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2408 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2409 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2410 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2411 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2415 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2416 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2417 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2418 | struct io_async_rw *rw = req->async_data; |
| 2419 | |
| 2420 | if (!rw) |
| 2421 | return !io_req_prep_async(req); |
| 2422 | /* may have left rw->iter inconsistent on -EIOCBQUEUED */ |
| 2423 | iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter)); |
| 2424 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2425 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2426 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2427 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2428 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2429 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2430 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2431 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2432 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2433 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2434 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2435 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2436 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2437 | /* |
| 2438 | * If ref is dying, we might be running poll reap from the exit work. |
| 2439 | * Don't attempt to reissue from that path, just let it fail with |
| 2440 | * -EAGAIN. |
| 2441 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2442 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2443 | return false; |
| 2444 | return true; |
| 2445 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2446 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2447 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2448 | { |
| 2449 | return false; |
| 2450 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2451 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2452 | { |
| 2453 | return false; |
| 2454 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2455 | #endif |
| 2456 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2457 | 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] | 2458 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2459 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2460 | int cflags = 0; |
| 2461 | |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2462 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2463 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2464 | if (res != req->result) { |
| 2465 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2466 | io_rw_should_reissue(req)) { |
| 2467 | req->flags |= REQ_F_REISSUE; |
| 2468 | return; |
| 2469 | } |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2470 | req_set_fail_links(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2471 | } |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2472 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2473 | cflags = io_put_rw_kbuf(req); |
| 2474 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2478 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2479 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2480 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2481 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2484 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2485 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2486 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2487 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2488 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2489 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2490 | if (unlikely(res != req->result)) { |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2491 | if (!(res == -EAGAIN && io_rw_should_reissue(req) && |
| 2492 | io_resubmit_prep(req))) { |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2493 | req_set_fail_links(req); |
| 2494 | req->flags |= REQ_F_DONT_REISSUE; |
| 2495 | } |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2496 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2497 | |
| 2498 | WRITE_ONCE(req->result, res); |
Jens Axboe | b9b0e0d | 2021-02-23 08:18:36 -0700 | [diff] [blame] | 2499 | /* order with io_iopoll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2500 | smp_wmb(); |
| 2501 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | /* |
| 2505 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2506 | * Adding the kiocb to the list AFTER submission ensures that we don't |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2507 | * find it from a io_do_iopoll() thread before the issuer is done |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2508 | * accessing the kiocb cookie. |
| 2509 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2510 | 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] | 2511 | { |
| 2512 | struct io_ring_ctx *ctx = req->ctx; |
| 2513 | |
| 2514 | /* |
| 2515 | * Track whether we have multiple files in our lists. This will impact |
| 2516 | * how we do polling eventually, not spinning if we're on potentially |
| 2517 | * different devices. |
| 2518 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2519 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2520 | ctx->poll_multi_file = false; |
| 2521 | } else if (!ctx->poll_multi_file) { |
| 2522 | struct io_kiocb *list_req; |
| 2523 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2524 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2525 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2526 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2527 | ctx->poll_multi_file = true; |
| 2528 | } |
| 2529 | |
| 2530 | /* |
| 2531 | * For fast devices, IO may have already completed. If it has, add |
| 2532 | * it to the front so we find it first. |
| 2533 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2534 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2535 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2536 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2537 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2538 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2539 | /* |
| 2540 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2541 | * task context or in io worker task context. If current task context is |
| 2542 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2543 | */ |
| 2544 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2545 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2546 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2549 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2550 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2551 | if (state->file_refs) { |
| 2552 | fput_many(state->file, state->file_refs); |
| 2553 | state->file_refs = 0; |
| 2554 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* |
| 2558 | * Get as many references to a file as we have IOs left in this submission, |
| 2559 | * assuming most submissions are for one file, or at least that each file |
| 2560 | * has more than one submission. |
| 2561 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2562 | 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] | 2563 | { |
| 2564 | if (!state) |
| 2565 | return fget(fd); |
| 2566 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2567 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2568 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2569 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2570 | return state->file; |
| 2571 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2572 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2573 | } |
| 2574 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2575 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2576 | return NULL; |
| 2577 | |
| 2578 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2579 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2580 | return state->file; |
| 2581 | } |
| 2582 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2583 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2584 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2585 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2586 | } |
| 2587 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2588 | /* |
| 2589 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2590 | * any file. For now, just ensure that anything potentially problematic is done |
| 2591 | * inline. |
| 2592 | */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2593 | static bool __io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2594 | { |
| 2595 | umode_t mode = file_inode(file)->i_mode; |
| 2596 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2597 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2598 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2599 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2600 | return true; |
| 2601 | return false; |
| 2602 | } |
| 2603 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2604 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2605 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2606 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2607 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2608 | file->f_op != &io_uring_fops) |
| 2609 | return true; |
| 2610 | return false; |
| 2611 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2612 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2613 | /* any ->read/write should understand O_NONBLOCK */ |
| 2614 | if (file->f_flags & O_NONBLOCK) |
| 2615 | return true; |
| 2616 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2617 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2618 | return false; |
| 2619 | |
| 2620 | if (rw == READ) |
| 2621 | return file->f_op->read_iter != NULL; |
| 2622 | |
| 2623 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2626 | static bool io_file_supports_async(struct io_kiocb *req, int rw) |
| 2627 | { |
| 2628 | if (rw == READ && (req->flags & REQ_F_ASYNC_READ)) |
| 2629 | return true; |
| 2630 | else if (rw == WRITE && (req->flags & REQ_F_ASYNC_WRITE)) |
| 2631 | return true; |
| 2632 | |
| 2633 | return __io_file_supports_async(req->file, rw); |
| 2634 | } |
| 2635 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2636 | 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] | 2637 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2638 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2639 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2640 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2641 | unsigned ioprio; |
| 2642 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2643 | |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2644 | 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] | 2645 | req->flags |= REQ_F_ISREG; |
| 2646 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2647 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2648 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2649 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2650 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2651 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2652 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2653 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2654 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2655 | if (unlikely(ret)) |
| 2656 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2657 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2658 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 2659 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 2660 | req->flags |= REQ_F_NOWAIT; |
| 2661 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2662 | ioprio = READ_ONCE(sqe->ioprio); |
| 2663 | if (ioprio) { |
| 2664 | ret = ioprio_check_cap(ioprio); |
| 2665 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2666 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2667 | |
| 2668 | kiocb->ki_ioprio = ioprio; |
| 2669 | } else |
| 2670 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2671 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2672 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2673 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2674 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2675 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2676 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2677 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2678 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2679 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2680 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2681 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2682 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2683 | kiocb->ki_complete = io_complete_rw; |
| 2684 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2685 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2686 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2687 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2688 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2689 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2693 | { |
| 2694 | switch (ret) { |
| 2695 | case -EIOCBQUEUED: |
| 2696 | break; |
| 2697 | case -ERESTARTSYS: |
| 2698 | case -ERESTARTNOINTR: |
| 2699 | case -ERESTARTNOHAND: |
| 2700 | case -ERESTART_RESTARTBLOCK: |
| 2701 | /* |
| 2702 | * We can't just restart the syscall, since previously |
| 2703 | * submitted sqes may already be in progress. Just fail this |
| 2704 | * IO with EINTR. |
| 2705 | */ |
| 2706 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2707 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2708 | default: |
| 2709 | kiocb->ki_complete(kiocb, ret, 0); |
| 2710 | } |
| 2711 | } |
| 2712 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2713 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2714 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2715 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2716 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2717 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2718 | bool check_reissue = kiocb->ki_complete == io_complete_rw; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2719 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2720 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2721 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2722 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2723 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2724 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2725 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2726 | } |
| 2727 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2728 | if (req->flags & REQ_F_CUR_POS) |
| 2729 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2730 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2731 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2732 | else |
| 2733 | io_rw_done(kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2734 | |
| 2735 | if (check_reissue && req->flags & REQ_F_REISSUE) { |
| 2736 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 2737 | if (io_resubmit_prep(req)) { |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2738 | req_ref_get(req); |
| 2739 | io_queue_async_work(req); |
| 2740 | } else { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2741 | int cflags = 0; |
| 2742 | |
| 2743 | req_set_fail_links(req); |
| 2744 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2745 | cflags = io_put_rw_kbuf(req); |
| 2746 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2747 | } |
| 2748 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2749 | } |
| 2750 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2751 | 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] | 2752 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2753 | struct io_ring_ctx *ctx = req->ctx; |
| 2754 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2755 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 2756 | u16 index, buf_index = req->buf_index; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2757 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2758 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2759 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2760 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2761 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2762 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2763 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2764 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2765 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 2766 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2767 | return -EFAULT; |
| 2768 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 2769 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2770 | return -EFAULT; |
| 2771 | |
| 2772 | /* |
| 2773 | * May not be a start of buffer, set size appropriately |
| 2774 | * and advance us to the beginning. |
| 2775 | */ |
| 2776 | offset = buf_addr - imu->ubuf; |
| 2777 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2778 | |
| 2779 | if (offset) { |
| 2780 | /* |
| 2781 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2782 | * using the latter parts of a big fixed buffer - it iterates |
| 2783 | * over each segment manually. We can cheat a bit here, because |
| 2784 | * we know that: |
| 2785 | * |
| 2786 | * 1) it's a BVEC iter, we set it up |
| 2787 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2788 | * first and last bvec |
| 2789 | * |
| 2790 | * So just find our index, and adjust the iterator afterwards. |
| 2791 | * If the offset is within the first bvec (or the whole first |
| 2792 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2793 | * since we can just skip the first segment, which may not |
| 2794 | * be PAGE_SIZE aligned. |
| 2795 | */ |
| 2796 | const struct bio_vec *bvec = imu->bvec; |
| 2797 | |
| 2798 | if (offset <= bvec->bv_len) { |
| 2799 | iov_iter_advance(iter, offset); |
| 2800 | } else { |
| 2801 | unsigned long seg_skip; |
| 2802 | |
| 2803 | /* skip first vec */ |
| 2804 | offset -= bvec->bv_len; |
| 2805 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2806 | |
| 2807 | iter->bvec = bvec + seg_skip; |
| 2808 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2809 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2810 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2814 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2817 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2818 | { |
| 2819 | if (needs_lock) |
| 2820 | mutex_unlock(&ctx->uring_lock); |
| 2821 | } |
| 2822 | |
| 2823 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2824 | { |
| 2825 | /* |
| 2826 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2827 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2828 | * The only exception is when we've detached the request and issue it |
| 2829 | * from an async worker thread, grab the lock for that case. |
| 2830 | */ |
| 2831 | if (needs_lock) |
| 2832 | mutex_lock(&ctx->uring_lock); |
| 2833 | } |
| 2834 | |
| 2835 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2836 | int bgid, struct io_buffer *kbuf, |
| 2837 | bool needs_lock) |
| 2838 | { |
| 2839 | struct io_buffer *head; |
| 2840 | |
| 2841 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2842 | return kbuf; |
| 2843 | |
| 2844 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2845 | |
| 2846 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2847 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 2848 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2849 | if (head) { |
| 2850 | if (!list_empty(&head->list)) { |
| 2851 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2852 | list); |
| 2853 | list_del(&kbuf->list); |
| 2854 | } else { |
| 2855 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 2856 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2857 | } |
| 2858 | if (*len > kbuf->len) |
| 2859 | *len = kbuf->len; |
| 2860 | } else { |
| 2861 | kbuf = ERR_PTR(-ENOBUFS); |
| 2862 | } |
| 2863 | |
| 2864 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2865 | |
| 2866 | return kbuf; |
| 2867 | } |
| 2868 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2869 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2870 | bool needs_lock) |
| 2871 | { |
| 2872 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2873 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2874 | |
| 2875 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2876 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2877 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2878 | if (IS_ERR(kbuf)) |
| 2879 | return kbuf; |
| 2880 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2881 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2882 | return u64_to_user_ptr(kbuf->addr); |
| 2883 | } |
| 2884 | |
| 2885 | #ifdef CONFIG_COMPAT |
| 2886 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2887 | bool needs_lock) |
| 2888 | { |
| 2889 | struct compat_iovec __user *uiov; |
| 2890 | compat_ssize_t clen; |
| 2891 | void __user *buf; |
| 2892 | ssize_t len; |
| 2893 | |
| 2894 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2895 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2896 | return -EFAULT; |
| 2897 | if (__get_user(clen, &uiov->iov_len)) |
| 2898 | return -EFAULT; |
| 2899 | if (clen < 0) |
| 2900 | return -EINVAL; |
| 2901 | |
| 2902 | len = clen; |
| 2903 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2904 | if (IS_ERR(buf)) |
| 2905 | return PTR_ERR(buf); |
| 2906 | iov[0].iov_base = buf; |
| 2907 | iov[0].iov_len = (compat_size_t) len; |
| 2908 | return 0; |
| 2909 | } |
| 2910 | #endif |
| 2911 | |
| 2912 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2913 | bool needs_lock) |
| 2914 | { |
| 2915 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2916 | void __user *buf; |
| 2917 | ssize_t len; |
| 2918 | |
| 2919 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2920 | return -EFAULT; |
| 2921 | |
| 2922 | len = iov[0].iov_len; |
| 2923 | if (len < 0) |
| 2924 | return -EINVAL; |
| 2925 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2926 | if (IS_ERR(buf)) |
| 2927 | return PTR_ERR(buf); |
| 2928 | iov[0].iov_base = buf; |
| 2929 | iov[0].iov_len = len; |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
| 2933 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2934 | bool needs_lock) |
| 2935 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2936 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2937 | struct io_buffer *kbuf; |
| 2938 | |
| 2939 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2940 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2941 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2942 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2943 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 2944 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2945 | return -EINVAL; |
| 2946 | |
| 2947 | #ifdef CONFIG_COMPAT |
| 2948 | if (req->ctx->compat) |
| 2949 | return io_compat_import(req, iov, needs_lock); |
| 2950 | #endif |
| 2951 | |
| 2952 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2953 | } |
| 2954 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2955 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 2956 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2957 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2958 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2959 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2960 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2961 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2962 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2963 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2964 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2965 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2966 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2967 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2968 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2969 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2970 | return -EINVAL; |
| 2971 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2972 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2973 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2974 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 2975 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2976 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2977 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2980 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2981 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 2982 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2983 | } |
| 2984 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2985 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2986 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2987 | if (!ret) |
| 2988 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2989 | *iovec = NULL; |
| 2990 | return ret; |
| 2991 | } |
| 2992 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 2993 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 2994 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2997 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 2998 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 2999 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3000 | } |
| 3001 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3002 | /* |
| 3003 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3004 | * by looping over ->read() or ->write() manually. |
| 3005 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3006 | 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] | 3007 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3008 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3009 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3010 | ssize_t ret = 0; |
| 3011 | |
| 3012 | /* |
| 3013 | * Don't support polled IO through this interface, and we can't |
| 3014 | * support non-blocking either. For the latter, this just causes |
| 3015 | * the kiocb to be handled from an async context. |
| 3016 | */ |
| 3017 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3018 | return -EOPNOTSUPP; |
| 3019 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3020 | return -EAGAIN; |
| 3021 | |
| 3022 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3023 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3024 | ssize_t nr; |
| 3025 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3026 | if (!iov_iter_is_bvec(iter)) { |
| 3027 | iovec = iov_iter_iovec(iter); |
| 3028 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3029 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3030 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3031 | } |
| 3032 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3033 | if (rw == READ) { |
| 3034 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3035 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3036 | } else { |
| 3037 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3038 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | if (nr < 0) { |
| 3042 | if (!ret) |
| 3043 | ret = nr; |
| 3044 | break; |
| 3045 | } |
| 3046 | ret += nr; |
| 3047 | if (nr != iovec.iov_len) |
| 3048 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3049 | req->rw.len -= nr; |
| 3050 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3051 | iov_iter_advance(iter, nr); |
| 3052 | } |
| 3053 | |
| 3054 | return ret; |
| 3055 | } |
| 3056 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3057 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3058 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3059 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3060 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3061 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3062 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3063 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3064 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3065 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3066 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3067 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3068 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3069 | unsigned iov_off = 0; |
| 3070 | |
| 3071 | rw->iter.iov = rw->fast_iov; |
| 3072 | if (iter->iov != fast_iov) { |
| 3073 | iov_off = iter->iov - fast_iov; |
| 3074 | rw->iter.iov += iov_off; |
| 3075 | } |
| 3076 | if (rw->fast_iov != fast_iov) |
| 3077 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3078 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3079 | } else { |
| 3080 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3081 | } |
| 3082 | } |
| 3083 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3084 | static inline int io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3085 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3086 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3087 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3088 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3089 | } |
| 3090 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3091 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3092 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3093 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3094 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3095 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3096 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3097 | if (!req->async_data) { |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3098 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3099 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3100 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3101 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3102 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3103 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3104 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3105 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3108 | 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] | 3109 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3110 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3111 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3112 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3113 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3114 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3115 | if (unlikely(ret < 0)) |
| 3116 | return ret; |
| 3117 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3118 | iorw->bytes_done = 0; |
| 3119 | iorw->free_iovec = iov; |
| 3120 | if (iov) |
| 3121 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3122 | return 0; |
| 3123 | } |
| 3124 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3125 | 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] | 3126 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3127 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3128 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3129 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3130 | } |
| 3131 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3132 | /* |
| 3133 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3134 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3135 | * This gets called when the page is unlocked, and we generally expect that to |
| 3136 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3137 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3138 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3139 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3140 | * slow path. |
| 3141 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3142 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3143 | int sync, void *arg) |
| 3144 | { |
| 3145 | struct wait_page_queue *wpq; |
| 3146 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3147 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3148 | |
| 3149 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3150 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3151 | if (!wake_page_match(wpq, key)) |
| 3152 | return 0; |
| 3153 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3154 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3155 | list_del_init(&wait->entry); |
| 3156 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3157 | /* submit ref gets dropped, acquire a new one */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 3158 | req_ref_get(req); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3159 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3160 | return 1; |
| 3161 | } |
| 3162 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3163 | /* |
| 3164 | * This controls whether a given IO request should be armed for async page |
| 3165 | * based retry. If we return false here, the request is handed to the async |
| 3166 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3167 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3168 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3169 | * will register a callback when the page is unlocked at IO completion. Through |
| 3170 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3171 | * That retry will attempt the buffered read again. The retry will generally |
| 3172 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3173 | * async worker threads for a blocking retry. |
| 3174 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3175 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3176 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3177 | struct io_async_rw *rw = req->async_data; |
| 3178 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3179 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3180 | |
| 3181 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3182 | if (req->flags & REQ_F_NOWAIT) |
| 3183 | return false; |
| 3184 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3185 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3186 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3187 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3188 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3189 | /* |
| 3190 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3191 | * support callback based unlocks |
| 3192 | */ |
| 3193 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3194 | return false; |
| 3195 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3196 | wait->wait.func = io_async_buf_func; |
| 3197 | wait->wait.private = req; |
| 3198 | wait->wait.flags = 0; |
| 3199 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3200 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3201 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3202 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3203 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3207 | { |
| 3208 | if (req->file->f_op->read_iter) |
| 3209 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3210 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3211 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3212 | else |
| 3213 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3214 | } |
| 3215 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3216 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3217 | { |
| 3218 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3219 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3220 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3221 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3222 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3223 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3224 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3225 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3226 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3227 | iovec = NULL; |
| 3228 | } else { |
| 3229 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3230 | if (ret < 0) |
| 3231 | return ret; |
| 3232 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3233 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3234 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3235 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3236 | /* Ensure we clear previously set non-block flag */ |
| 3237 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3238 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3239 | else |
| 3240 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3241 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3242 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 3243 | if (force_nonblock && !io_file_supports_async(req, READ)) { |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3244 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3245 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3246 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3247 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3248 | 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] | 3249 | if (unlikely(ret)) { |
| 3250 | kfree(iovec); |
| 3251 | return ret; |
| 3252 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3253 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3254 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3255 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3256 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3257 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3258 | /* IOPOLL retry should happen for io-wq threads */ |
| 3259 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3260 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3261 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3262 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3263 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3264 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3265 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3266 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3267 | } else if (ret == -EIOCBQUEUED) { |
| 3268 | goto out_free; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3269 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3270 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3271 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3272 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3273 | } |
| 3274 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3275 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3276 | if (ret2) |
| 3277 | return ret2; |
| 3278 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3279 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3280 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3281 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3282 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3283 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3284 | do { |
| 3285 | io_size -= ret; |
| 3286 | rw->bytes_done += ret; |
| 3287 | /* if we can retry, do so with the callbacks armed */ |
| 3288 | if (!io_rw_should_retry(req)) { |
| 3289 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3290 | return -EAGAIN; |
| 3291 | } |
| 3292 | |
| 3293 | /* |
| 3294 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3295 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3296 | * desired page gets unlocked. We can also get a partial read |
| 3297 | * here, and if we do, then just retry at the new offset. |
| 3298 | */ |
| 3299 | ret = io_iter_do_read(req, iter); |
| 3300 | if (ret == -EIOCBQUEUED) |
| 3301 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3302 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3303 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3304 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3305 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3306 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3307 | out_free: |
| 3308 | /* it's faster to check here then delegate to kfree */ |
| 3309 | if (iovec) |
| 3310 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3311 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3314 | 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] | 3315 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3316 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3317 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3318 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3321 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3322 | { |
| 3323 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3324 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3325 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3326 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3327 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3328 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3329 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3330 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3331 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3332 | iovec = NULL; |
| 3333 | } else { |
| 3334 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3335 | if (ret < 0) |
| 3336 | return ret; |
| 3337 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3338 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3339 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3340 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3341 | /* Ensure we clear previously set non-block flag */ |
| 3342 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3343 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3344 | else |
| 3345 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3346 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3347 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 3348 | if (force_nonblock && !io_file_supports_async(req, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3349 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3350 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3351 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3352 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3353 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3354 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3355 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3356 | 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] | 3357 | if (unlikely(ret)) |
| 3358 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3359 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3360 | /* |
| 3361 | * Open-code file_start_write here to grab freeze protection, |
| 3362 | * which will be released by another thread in |
| 3363 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3364 | * released so that it doesn't complain about the held lock when |
| 3365 | * we return to userspace. |
| 3366 | */ |
| 3367 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3368 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3369 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3370 | SB_FREEZE_WRITE); |
| 3371 | } |
| 3372 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3373 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3374 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3375 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3376 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3377 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3378 | else |
| 3379 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3380 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3381 | if (req->flags & REQ_F_REISSUE) { |
| 3382 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3383 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3384 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3385 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3386 | /* |
| 3387 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3388 | * retry them without IOCB_NOWAIT. |
| 3389 | */ |
| 3390 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3391 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3392 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3393 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3394 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3395 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3396 | /* IOPOLL retry should happen for io-wq threads */ |
| 3397 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3398 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3399 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3400 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3401 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3402 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3403 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3404 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3405 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3406 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3407 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3408 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3409 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3410 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3411 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3412 | return ret; |
| 3413 | } |
| 3414 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3415 | static int io_renameat_prep(struct io_kiocb *req, |
| 3416 | const struct io_uring_sqe *sqe) |
| 3417 | { |
| 3418 | struct io_rename *ren = &req->rename; |
| 3419 | const char __user *oldf, *newf; |
| 3420 | |
| 3421 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3422 | return -EBADF; |
| 3423 | |
| 3424 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3425 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3426 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3427 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3428 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3429 | |
| 3430 | ren->oldpath = getname(oldf); |
| 3431 | if (IS_ERR(ren->oldpath)) |
| 3432 | return PTR_ERR(ren->oldpath); |
| 3433 | |
| 3434 | ren->newpath = getname(newf); |
| 3435 | if (IS_ERR(ren->newpath)) { |
| 3436 | putname(ren->oldpath); |
| 3437 | return PTR_ERR(ren->newpath); |
| 3438 | } |
| 3439 | |
| 3440 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3441 | return 0; |
| 3442 | } |
| 3443 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3444 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3445 | { |
| 3446 | struct io_rename *ren = &req->rename; |
| 3447 | int ret; |
| 3448 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3449 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3450 | return -EAGAIN; |
| 3451 | |
| 3452 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3453 | ren->newpath, ren->flags); |
| 3454 | |
| 3455 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3456 | if (ret < 0) |
| 3457 | req_set_fail_links(req); |
| 3458 | io_req_complete(req, ret); |
| 3459 | return 0; |
| 3460 | } |
| 3461 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3462 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3463 | const struct io_uring_sqe *sqe) |
| 3464 | { |
| 3465 | struct io_unlink *un = &req->unlink; |
| 3466 | const char __user *fname; |
| 3467 | |
| 3468 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3469 | return -EBADF; |
| 3470 | |
| 3471 | un->dfd = READ_ONCE(sqe->fd); |
| 3472 | |
| 3473 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3474 | if (un->flags & ~AT_REMOVEDIR) |
| 3475 | return -EINVAL; |
| 3476 | |
| 3477 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3478 | un->filename = getname(fname); |
| 3479 | if (IS_ERR(un->filename)) |
| 3480 | return PTR_ERR(un->filename); |
| 3481 | |
| 3482 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3483 | return 0; |
| 3484 | } |
| 3485 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3486 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3487 | { |
| 3488 | struct io_unlink *un = &req->unlink; |
| 3489 | int ret; |
| 3490 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3491 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3492 | return -EAGAIN; |
| 3493 | |
| 3494 | if (un->flags & AT_REMOVEDIR) |
| 3495 | ret = do_rmdir(un->dfd, un->filename); |
| 3496 | else |
| 3497 | ret = do_unlinkat(un->dfd, un->filename); |
| 3498 | |
| 3499 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3500 | if (ret < 0) |
| 3501 | req_set_fail_links(req); |
| 3502 | io_req_complete(req, ret); |
| 3503 | return 0; |
| 3504 | } |
| 3505 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3506 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3507 | const struct io_uring_sqe *sqe) |
| 3508 | { |
| 3509 | #if defined(CONFIG_NET) |
| 3510 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3511 | return -EINVAL; |
| 3512 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3513 | sqe->buf_index) |
| 3514 | return -EINVAL; |
| 3515 | |
| 3516 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3517 | return 0; |
| 3518 | #else |
| 3519 | return -EOPNOTSUPP; |
| 3520 | #endif |
| 3521 | } |
| 3522 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3523 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3524 | { |
| 3525 | #if defined(CONFIG_NET) |
| 3526 | struct socket *sock; |
| 3527 | int ret; |
| 3528 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3529 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3530 | return -EAGAIN; |
| 3531 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3532 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3533 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3534 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3535 | |
| 3536 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3537 | if (ret < 0) |
| 3538 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3539 | io_req_complete(req, ret); |
| 3540 | return 0; |
| 3541 | #else |
| 3542 | return -EOPNOTSUPP; |
| 3543 | #endif |
| 3544 | } |
| 3545 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3546 | static int __io_splice_prep(struct io_kiocb *req, |
| 3547 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3548 | { |
| 3549 | struct io_splice* sp = &req->splice; |
| 3550 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3551 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3552 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3553 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3554 | |
| 3555 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3556 | sp->len = READ_ONCE(sqe->len); |
| 3557 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3558 | |
| 3559 | if (unlikely(sp->flags & ~valid_flags)) |
| 3560 | return -EINVAL; |
| 3561 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3562 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3563 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3564 | if (!sp->file_in) |
| 3565 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3566 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3567 | return 0; |
| 3568 | } |
| 3569 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3570 | static int io_tee_prep(struct io_kiocb *req, |
| 3571 | const struct io_uring_sqe *sqe) |
| 3572 | { |
| 3573 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3574 | return -EINVAL; |
| 3575 | return __io_splice_prep(req, sqe); |
| 3576 | } |
| 3577 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3578 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3579 | { |
| 3580 | struct io_splice *sp = &req->splice; |
| 3581 | struct file *in = sp->file_in; |
| 3582 | struct file *out = sp->file_out; |
| 3583 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3584 | long ret = 0; |
| 3585 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3586 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3587 | return -EAGAIN; |
| 3588 | if (sp->len) |
| 3589 | ret = do_tee(in, out, sp->len, flags); |
| 3590 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 3591 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 3592 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3593 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3594 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3595 | if (ret != sp->len) |
| 3596 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3597 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3598 | return 0; |
| 3599 | } |
| 3600 | |
| 3601 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3602 | { |
| 3603 | struct io_splice* sp = &req->splice; |
| 3604 | |
| 3605 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3606 | sp->off_out = READ_ONCE(sqe->off); |
| 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_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +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 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3617 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3618 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3619 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3620 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3621 | |
| 3622 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3623 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3624 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3625 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3626 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3627 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 3628 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 3629 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3630 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3631 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3632 | if (ret != sp->len) |
| 3633 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3634 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3635 | return 0; |
| 3636 | } |
| 3637 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3638 | /* |
| 3639 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3640 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3641 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3642 | { |
| 3643 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3644 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3645 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3646 | return -EINVAL; |
| 3647 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3648 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3649 | return 0; |
| 3650 | } |
| 3651 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 3652 | 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] | 3653 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3654 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3655 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3656 | if (!req->file) |
| 3657 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3658 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3659 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3660 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3661 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3662 | return -EINVAL; |
| 3663 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3664 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3665 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3666 | return -EINVAL; |
| 3667 | |
| 3668 | req->sync.off = READ_ONCE(sqe->off); |
| 3669 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3670 | return 0; |
| 3671 | } |
| 3672 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3673 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3674 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3675 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3676 | int ret; |
| 3677 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3678 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3679 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3680 | return -EAGAIN; |
| 3681 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3682 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3683 | end > 0 ? end : LLONG_MAX, |
| 3684 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3685 | if (ret < 0) |
| 3686 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3687 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3688 | return 0; |
| 3689 | } |
| 3690 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3691 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3692 | const struct io_uring_sqe *sqe) |
| 3693 | { |
| 3694 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3695 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3696 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3697 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3698 | |
| 3699 | req->sync.off = READ_ONCE(sqe->off); |
| 3700 | req->sync.len = READ_ONCE(sqe->addr); |
| 3701 | req->sync.mode = READ_ONCE(sqe->len); |
| 3702 | return 0; |
| 3703 | } |
| 3704 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3705 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3706 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3707 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3708 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3709 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3710 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3711 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3712 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3713 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3714 | if (ret < 0) |
| 3715 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3716 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3717 | return 0; |
| 3718 | } |
| 3719 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3720 | 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] | 3721 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3722 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3723 | int ret; |
| 3724 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3725 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3726 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3727 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3728 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3729 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3730 | /* open.how should be already initialised */ |
| 3731 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3732 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3733 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3734 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3735 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3736 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3737 | if (IS_ERR(req->open.filename)) { |
| 3738 | ret = PTR_ERR(req->open.filename); |
| 3739 | req->open.filename = NULL; |
| 3740 | return ret; |
| 3741 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3742 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3743 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3744 | return 0; |
| 3745 | } |
| 3746 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3747 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3748 | { |
| 3749 | u64 flags, mode; |
| 3750 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3751 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3752 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3753 | mode = READ_ONCE(sqe->len); |
| 3754 | flags = READ_ONCE(sqe->open_flags); |
| 3755 | req->open.how = build_open_how(flags, mode); |
| 3756 | return __io_openat_prep(req, sqe); |
| 3757 | } |
| 3758 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3759 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3760 | { |
| 3761 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3762 | size_t len; |
| 3763 | int ret; |
| 3764 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3765 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3766 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3767 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3768 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3769 | if (len < OPEN_HOW_SIZE_VER0) |
| 3770 | return -EINVAL; |
| 3771 | |
| 3772 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3773 | len); |
| 3774 | if (ret) |
| 3775 | return ret; |
| 3776 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3777 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3780 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3781 | { |
| 3782 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3783 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3784 | bool nonblock_set; |
| 3785 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3786 | int ret; |
| 3787 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3788 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3789 | if (ret) |
| 3790 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3791 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 3792 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3793 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3794 | /* |
| 3795 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 3796 | * it'll always -EAGAIN |
| 3797 | */ |
| 3798 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 3799 | return -EAGAIN; |
| 3800 | op.lookup_flags |= LOOKUP_CACHED; |
| 3801 | op.open_flag |= O_NONBLOCK; |
| 3802 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3803 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3804 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3805 | if (ret < 0) |
| 3806 | goto err; |
| 3807 | |
| 3808 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3809 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3810 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 3811 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3812 | /* |
| 3813 | * We could hang on to this 'fd', but seems like marginal |
| 3814 | * gain for something that is now known to be a slower path. |
| 3815 | * So just put it, and we'll get a new one when we retry. |
| 3816 | */ |
| 3817 | put_unused_fd(ret); |
| 3818 | return -EAGAIN; |
| 3819 | } |
| 3820 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3821 | if (IS_ERR(file)) { |
| 3822 | put_unused_fd(ret); |
| 3823 | ret = PTR_ERR(file); |
| 3824 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3825 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3826 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3827 | fsnotify_open(file); |
| 3828 | fd_install(ret, file); |
| 3829 | } |
| 3830 | err: |
| 3831 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3832 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3833 | if (ret < 0) |
| 3834 | req_set_fail_links(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 3835 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3836 | return 0; |
| 3837 | } |
| 3838 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3839 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3840 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 3841 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3842 | } |
| 3843 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3844 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3845 | const struct io_uring_sqe *sqe) |
| 3846 | { |
| 3847 | struct io_provide_buf *p = &req->pbuf; |
| 3848 | u64 tmp; |
| 3849 | |
| 3850 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3851 | return -EINVAL; |
| 3852 | |
| 3853 | tmp = READ_ONCE(sqe->fd); |
| 3854 | if (!tmp || tmp > USHRT_MAX) |
| 3855 | return -EINVAL; |
| 3856 | |
| 3857 | memset(p, 0, sizeof(*p)); |
| 3858 | p->nbufs = tmp; |
| 3859 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3860 | return 0; |
| 3861 | } |
| 3862 | |
| 3863 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3864 | int bgid, unsigned nbufs) |
| 3865 | { |
| 3866 | unsigned i = 0; |
| 3867 | |
| 3868 | /* shouldn't happen */ |
| 3869 | if (!nbufs) |
| 3870 | return 0; |
| 3871 | |
| 3872 | /* the head kbuf is the list itself */ |
| 3873 | while (!list_empty(&buf->list)) { |
| 3874 | struct io_buffer *nxt; |
| 3875 | |
| 3876 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3877 | list_del(&nxt->list); |
| 3878 | kfree(nxt); |
| 3879 | if (++i == nbufs) |
| 3880 | return i; |
| 3881 | } |
| 3882 | i++; |
| 3883 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3884 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3885 | |
| 3886 | return i; |
| 3887 | } |
| 3888 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3889 | 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] | 3890 | { |
| 3891 | struct io_provide_buf *p = &req->pbuf; |
| 3892 | struct io_ring_ctx *ctx = req->ctx; |
| 3893 | struct io_buffer *head; |
| 3894 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3895 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3896 | |
| 3897 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3898 | |
| 3899 | lockdep_assert_held(&ctx->uring_lock); |
| 3900 | |
| 3901 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3902 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3903 | if (head) |
| 3904 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3905 | if (ret < 0) |
| 3906 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3907 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 3908 | /* complete before unlock, IOPOLL may need the lock */ |
| 3909 | __io_req_complete(req, issue_flags, ret, 0); |
| 3910 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3911 | return 0; |
| 3912 | } |
| 3913 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3914 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3915 | const struct io_uring_sqe *sqe) |
| 3916 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 3917 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3918 | struct io_provide_buf *p = &req->pbuf; |
| 3919 | u64 tmp; |
| 3920 | |
| 3921 | if (sqe->ioprio || sqe->rw_flags) |
| 3922 | return -EINVAL; |
| 3923 | |
| 3924 | tmp = READ_ONCE(sqe->fd); |
| 3925 | if (!tmp || tmp > USHRT_MAX) |
| 3926 | return -E2BIG; |
| 3927 | p->nbufs = tmp; |
| 3928 | p->addr = READ_ONCE(sqe->addr); |
| 3929 | p->len = READ_ONCE(sqe->len); |
| 3930 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 3931 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 3932 | &size)) |
| 3933 | return -EOVERFLOW; |
| 3934 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 3935 | return -EOVERFLOW; |
| 3936 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 3937 | size = (unsigned long)p->len * p->nbufs; |
| 3938 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3939 | return -EFAULT; |
| 3940 | |
| 3941 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3942 | tmp = READ_ONCE(sqe->off); |
| 3943 | if (tmp > USHRT_MAX) |
| 3944 | return -E2BIG; |
| 3945 | p->bid = tmp; |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
| 3949 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3950 | { |
| 3951 | struct io_buffer *buf; |
| 3952 | u64 addr = pbuf->addr; |
| 3953 | int i, bid = pbuf->bid; |
| 3954 | |
| 3955 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3956 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3957 | if (!buf) |
| 3958 | break; |
| 3959 | |
| 3960 | buf->addr = addr; |
| 3961 | buf->len = pbuf->len; |
| 3962 | buf->bid = bid; |
| 3963 | addr += pbuf->len; |
| 3964 | bid++; |
| 3965 | if (!*head) { |
| 3966 | INIT_LIST_HEAD(&buf->list); |
| 3967 | *head = buf; |
| 3968 | } else { |
| 3969 | list_add_tail(&buf->list, &(*head)->list); |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | return i ? i : -ENOMEM; |
| 3974 | } |
| 3975 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3976 | 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] | 3977 | { |
| 3978 | struct io_provide_buf *p = &req->pbuf; |
| 3979 | struct io_ring_ctx *ctx = req->ctx; |
| 3980 | struct io_buffer *head, *list; |
| 3981 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3982 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3983 | |
| 3984 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3985 | |
| 3986 | lockdep_assert_held(&ctx->uring_lock); |
| 3987 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3988 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3989 | |
| 3990 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3991 | if (ret >= 0 && !list) { |
| 3992 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 3993 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3994 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3995 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3996 | if (ret < 0) |
| 3997 | req_set_fail_links(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 3998 | /* complete before unlock, IOPOLL may need the lock */ |
| 3999 | __io_req_complete(req, issue_flags, ret, 0); |
| 4000 | io_ring_submit_unlock(ctx, !force_nonblock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4001 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4002 | } |
| 4003 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4004 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4005 | const struct io_uring_sqe *sqe) |
| 4006 | { |
| 4007 | #if defined(CONFIG_EPOLL) |
| 4008 | if (sqe->ioprio || sqe->buf_index) |
| 4009 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4010 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4011 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4012 | |
| 4013 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4014 | req->epoll.op = READ_ONCE(sqe->len); |
| 4015 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4016 | |
| 4017 | if (ep_op_has_event(req->epoll.op)) { |
| 4018 | struct epoll_event __user *ev; |
| 4019 | |
| 4020 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4021 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4022 | return -EFAULT; |
| 4023 | } |
| 4024 | |
| 4025 | return 0; |
| 4026 | #else |
| 4027 | return -EOPNOTSUPP; |
| 4028 | #endif |
| 4029 | } |
| 4030 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4031 | 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] | 4032 | { |
| 4033 | #if defined(CONFIG_EPOLL) |
| 4034 | struct io_epoll *ie = &req->epoll; |
| 4035 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4036 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4037 | |
| 4038 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4039 | if (force_nonblock && ret == -EAGAIN) |
| 4040 | return -EAGAIN; |
| 4041 | |
| 4042 | if (ret < 0) |
| 4043 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4044 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4045 | return 0; |
| 4046 | #else |
| 4047 | return -EOPNOTSUPP; |
| 4048 | #endif |
| 4049 | } |
| 4050 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4051 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4052 | { |
| 4053 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4054 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4055 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4056 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4057 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4058 | |
| 4059 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4060 | req->madvise.len = READ_ONCE(sqe->len); |
| 4061 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4062 | return 0; |
| 4063 | #else |
| 4064 | return -EOPNOTSUPP; |
| 4065 | #endif |
| 4066 | } |
| 4067 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4068 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4069 | { |
| 4070 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4071 | struct io_madvise *ma = &req->madvise; |
| 4072 | int ret; |
| 4073 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4074 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4075 | return -EAGAIN; |
| 4076 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4077 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4078 | if (ret < 0) |
| 4079 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4080 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4081 | return 0; |
| 4082 | #else |
| 4083 | return -EOPNOTSUPP; |
| 4084 | #endif |
| 4085 | } |
| 4086 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4087 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4088 | { |
| 4089 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4090 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4091 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4092 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4093 | |
| 4094 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4095 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4096 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4097 | return 0; |
| 4098 | } |
| 4099 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4100 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4101 | { |
| 4102 | struct io_fadvise *fa = &req->fadvise; |
| 4103 | int ret; |
| 4104 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4105 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4106 | switch (fa->advice) { |
| 4107 | case POSIX_FADV_NORMAL: |
| 4108 | case POSIX_FADV_RANDOM: |
| 4109 | case POSIX_FADV_SEQUENTIAL: |
| 4110 | break; |
| 4111 | default: |
| 4112 | return -EAGAIN; |
| 4113 | } |
| 4114 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4115 | |
| 4116 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4117 | if (ret < 0) |
| 4118 | req_set_fail_links(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4119 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4120 | return 0; |
| 4121 | } |
| 4122 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4123 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4124 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4125 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4126 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4127 | if (sqe->ioprio || sqe->buf_index) |
| 4128 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4129 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4130 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4131 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4132 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4133 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4134 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4135 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4136 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4137 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4138 | return 0; |
| 4139 | } |
| 4140 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4141 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4142 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4143 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4144 | int ret; |
| 4145 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4146 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4147 | return -EAGAIN; |
| 4148 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4149 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4150 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4151 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4152 | if (ret < 0) |
| 4153 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4154 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4155 | return 0; |
| 4156 | } |
| 4157 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4158 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4159 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4160 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4161 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4162 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4163 | sqe->rw_flags || sqe->buf_index) |
| 4164 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4165 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4166 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4167 | |
| 4168 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4169 | return 0; |
| 4170 | } |
| 4171 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4172 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4173 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4174 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4175 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4176 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4177 | struct file *file = NULL; |
| 4178 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4179 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4180 | spin_lock(&files->file_lock); |
| 4181 | fdt = files_fdtable(files); |
| 4182 | if (close->fd >= fdt->max_fds) { |
| 4183 | spin_unlock(&files->file_lock); |
| 4184 | goto err; |
| 4185 | } |
| 4186 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4187 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4188 | spin_unlock(&files->file_lock); |
| 4189 | file = NULL; |
| 4190 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4191 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4192 | |
| 4193 | /* 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] | 4194 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4195 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4196 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4197 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4198 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4199 | ret = __close_fd_get_file(close->fd, &file); |
| 4200 | spin_unlock(&files->file_lock); |
| 4201 | if (ret < 0) { |
| 4202 | if (ret == -ENOENT) |
| 4203 | ret = -EBADF; |
| 4204 | goto err; |
| 4205 | } |
| 4206 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4207 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4208 | ret = filp_close(file, current->files); |
| 4209 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4210 | if (ret < 0) |
| 4211 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4212 | if (file) |
| 4213 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4214 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4215 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4218 | 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] | 4219 | { |
| 4220 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4221 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4222 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4223 | return -EINVAL; |
| 4224 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4225 | return -EINVAL; |
| 4226 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4227 | req->sync.off = READ_ONCE(sqe->off); |
| 4228 | req->sync.len = READ_ONCE(sqe->len); |
| 4229 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4230 | return 0; |
| 4231 | } |
| 4232 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4233 | 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] | 4234 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4235 | int ret; |
| 4236 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4237 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4238 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4239 | return -EAGAIN; |
| 4240 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4241 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4242 | req->sync.flags); |
| 4243 | if (ret < 0) |
| 4244 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4245 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4246 | return 0; |
| 4247 | } |
| 4248 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4249 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4250 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4251 | struct io_async_msghdr *kmsg) |
| 4252 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4253 | struct io_async_msghdr *async_msg = req->async_data; |
| 4254 | |
| 4255 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4256 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4257 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4258 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4259 | return -ENOMEM; |
| 4260 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4261 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4262 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4263 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4264 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4265 | /* if were using fast_iov, set it to the new one */ |
| 4266 | if (!async_msg->free_iov) |
| 4267 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4268 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4269 | return -EAGAIN; |
| 4270 | } |
| 4271 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4272 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4273 | struct io_async_msghdr *iomsg) |
| 4274 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4275 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4276 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4277 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4278 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4279 | } |
| 4280 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4281 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4282 | { |
| 4283 | int ret; |
| 4284 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4285 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4286 | if (!ret) |
| 4287 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4288 | return ret; |
| 4289 | } |
| 4290 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4291 | 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] | 4292 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4293 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4294 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4295 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4296 | return -EINVAL; |
| 4297 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4298 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4299 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4300 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4301 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4302 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4303 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4304 | #ifdef CONFIG_COMPAT |
| 4305 | if (req->ctx->compat) |
| 4306 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4307 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4308 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4309 | } |
| 4310 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4311 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4312 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4313 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4314 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4315 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4316 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4317 | int ret; |
| 4318 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4319 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4320 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4321 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4322 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4323 | kmsg = req->async_data; |
| 4324 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4325 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4326 | if (ret) |
| 4327 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4328 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4329 | } |
| 4330 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4331 | flags = req->sr_msg.msg_flags; |
| 4332 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4333 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4334 | if (flags & MSG_WAITALL) |
| 4335 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4336 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4337 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4338 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4339 | return io_setup_async_msg(req, kmsg); |
| 4340 | if (ret == -ERESTARTSYS) |
| 4341 | ret = -EINTR; |
| 4342 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4343 | /* fast path, check for non-NULL to avoid function call */ |
| 4344 | if (kmsg->free_iov) |
| 4345 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4346 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4347 | if (ret < min_ret) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4348 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4349 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4350 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4353 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4354 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4355 | struct io_sr_msg *sr = &req->sr_msg; |
| 4356 | struct msghdr msg; |
| 4357 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4358 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4359 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4360 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4361 | int ret; |
| 4362 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4363 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4364 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4365 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4366 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4367 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4368 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4369 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4370 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4371 | msg.msg_name = NULL; |
| 4372 | msg.msg_control = NULL; |
| 4373 | msg.msg_controllen = 0; |
| 4374 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4375 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4376 | flags = req->sr_msg.msg_flags; |
| 4377 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4378 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4379 | if (flags & MSG_WAITALL) |
| 4380 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4381 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4382 | msg.msg_flags = flags; |
| 4383 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4384 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4385 | return -EAGAIN; |
| 4386 | if (ret == -ERESTARTSYS) |
| 4387 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4388 | |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4389 | if (ret < min_ret) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4390 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4391 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4392 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4393 | } |
| 4394 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4395 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4396 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4397 | { |
| 4398 | struct io_sr_msg *sr = &req->sr_msg; |
| 4399 | struct iovec __user *uiov; |
| 4400 | size_t iov_len; |
| 4401 | int ret; |
| 4402 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4403 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4404 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4405 | if (ret) |
| 4406 | return ret; |
| 4407 | |
| 4408 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4409 | if (iov_len > 1) |
| 4410 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4411 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4412 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4413 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4414 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4415 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4416 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4417 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4418 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4419 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4420 | if (ret > 0) |
| 4421 | ret = 0; |
| 4422 | } |
| 4423 | |
| 4424 | return ret; |
| 4425 | } |
| 4426 | |
| 4427 | #ifdef CONFIG_COMPAT |
| 4428 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4429 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4430 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4431 | struct io_sr_msg *sr = &req->sr_msg; |
| 4432 | struct compat_iovec __user *uiov; |
| 4433 | compat_uptr_t ptr; |
| 4434 | compat_size_t len; |
| 4435 | int ret; |
| 4436 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 4437 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 4438 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4439 | if (ret) |
| 4440 | return ret; |
| 4441 | |
| 4442 | uiov = compat_ptr(ptr); |
| 4443 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4444 | compat_ssize_t clen; |
| 4445 | |
| 4446 | if (len > 1) |
| 4447 | return -EINVAL; |
| 4448 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4449 | return -EFAULT; |
| 4450 | if (__get_user(clen, &uiov->iov_len)) |
| 4451 | return -EFAULT; |
| 4452 | if (clen < 0) |
| 4453 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4454 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4455 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4456 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4457 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4458 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4459 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4460 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4461 | if (ret < 0) |
| 4462 | return ret; |
| 4463 | } |
| 4464 | |
| 4465 | return 0; |
| 4466 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4467 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4468 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4469 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4470 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4471 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4472 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4473 | |
| 4474 | #ifdef CONFIG_COMPAT |
| 4475 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4476 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4477 | #endif |
| 4478 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4479 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4480 | } |
| 4481 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4482 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4483 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4484 | { |
| 4485 | struct io_sr_msg *sr = &req->sr_msg; |
| 4486 | struct io_buffer *kbuf; |
| 4487 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4488 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4489 | if (IS_ERR(kbuf)) |
| 4490 | return kbuf; |
| 4491 | |
| 4492 | sr->kbuf = kbuf; |
| 4493 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4494 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4495 | } |
| 4496 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4497 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4498 | { |
| 4499 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4500 | } |
| 4501 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4502 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4503 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4504 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4505 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4506 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4507 | if (!ret) |
| 4508 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4509 | return ret; |
| 4510 | } |
| 4511 | |
| 4512 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4513 | { |
| 4514 | struct io_sr_msg *sr = &req->sr_msg; |
| 4515 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4516 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4517 | return -EINVAL; |
| 4518 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4519 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4520 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4521 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4522 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4523 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4524 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4525 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4526 | #ifdef CONFIG_COMPAT |
| 4527 | if (req->ctx->compat) |
| 4528 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4529 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4530 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4531 | } |
| 4532 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4533 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4534 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4535 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4536 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4537 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4538 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4539 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4540 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4541 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4542 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4543 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4544 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4545 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4546 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4547 | kmsg = req->async_data; |
| 4548 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4549 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4550 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4551 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4552 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4553 | } |
| 4554 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4555 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4556 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4557 | if (IS_ERR(kbuf)) |
| 4558 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4559 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4560 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4561 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4562 | 1, req->sr_msg.len); |
| 4563 | } |
| 4564 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4565 | flags = req->sr_msg.msg_flags; |
| 4566 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4567 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4568 | if (flags & MSG_WAITALL) |
| 4569 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4570 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4571 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4572 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4573 | if (force_nonblock && ret == -EAGAIN) |
| 4574 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4575 | if (ret == -ERESTARTSYS) |
| 4576 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4577 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4578 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4579 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4580 | /* fast path, check for non-NULL to avoid function call */ |
| 4581 | if (kmsg->free_iov) |
| 4582 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4583 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4584 | 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] | 4585 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4586 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4587 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4588 | } |
| 4589 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4590 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4591 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4592 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4593 | struct io_sr_msg *sr = &req->sr_msg; |
| 4594 | struct msghdr msg; |
| 4595 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4596 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4597 | struct iovec iov; |
| 4598 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4599 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4600 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4601 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4602 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4603 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4604 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4605 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4606 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4607 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4608 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4609 | if (IS_ERR(kbuf)) |
| 4610 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4611 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4612 | } |
| 4613 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4614 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4615 | if (unlikely(ret)) |
| 4616 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4617 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4618 | msg.msg_name = NULL; |
| 4619 | msg.msg_control = NULL; |
| 4620 | msg.msg_controllen = 0; |
| 4621 | msg.msg_namelen = 0; |
| 4622 | msg.msg_iocb = NULL; |
| 4623 | msg.msg_flags = 0; |
| 4624 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4625 | flags = req->sr_msg.msg_flags; |
| 4626 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4627 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4628 | if (flags & MSG_WAITALL) |
| 4629 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4630 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4631 | ret = sock_recvmsg(sock, &msg, flags); |
| 4632 | if (force_nonblock && ret == -EAGAIN) |
| 4633 | return -EAGAIN; |
| 4634 | if (ret == -ERESTARTSYS) |
| 4635 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4636 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4637 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4638 | cflags = io_put_recv_kbuf(req); |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4639 | 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] | 4640 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4641 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4642 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4645 | 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] | 4646 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4647 | struct io_accept *accept = &req->accept; |
| 4648 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4649 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4650 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4651 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4652 | return -EINVAL; |
| 4653 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4654 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4655 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4656 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4657 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4658 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4659 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4660 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4661 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4662 | { |
| 4663 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4664 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4665 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4666 | int ret; |
| 4667 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4668 | if (req->file->f_flags & O_NONBLOCK) |
| 4669 | req->flags |= REQ_F_NOWAIT; |
| 4670 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4671 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4672 | accept->addr_len, accept->flags, |
| 4673 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4674 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4675 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4676 | if (ret < 0) { |
| 4677 | if (ret == -ERESTARTSYS) |
| 4678 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4679 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4680 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4681 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4682 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4685 | static int io_connect_prep_async(struct io_kiocb *req) |
| 4686 | { |
| 4687 | struct io_async_connect *io = req->async_data; |
| 4688 | struct io_connect *conn = &req->connect; |
| 4689 | |
| 4690 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 4691 | } |
| 4692 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4693 | 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] | 4694 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4695 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4696 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4697 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4698 | return -EINVAL; |
| 4699 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4700 | return -EINVAL; |
| 4701 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4702 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4703 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4704 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4707 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4708 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4709 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4710 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4711 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4712 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4713 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4714 | if (req->async_data) { |
| 4715 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4716 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4717 | ret = move_addr_to_kernel(req->connect.addr, |
| 4718 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4719 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4720 | if (ret) |
| 4721 | goto out; |
| 4722 | io = &__io; |
| 4723 | } |
| 4724 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4725 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4726 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4727 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4728 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4729 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4730 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4731 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4732 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4733 | ret = -ENOMEM; |
| 4734 | goto out; |
| 4735 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4736 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4737 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4738 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4739 | if (ret == -ERESTARTSYS) |
| 4740 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4741 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4742 | if (ret < 0) |
| 4743 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4744 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4745 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4746 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4747 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4748 | #define IO_NETOP_FN(op) \ |
| 4749 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 4750 | { \ |
| 4751 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4752 | } |
| 4753 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4754 | #define IO_NETOP_PREP(op) \ |
| 4755 | IO_NETOP_FN(op) \ |
| 4756 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 4757 | { \ |
| 4758 | return -EOPNOTSUPP; \ |
| 4759 | } \ |
| 4760 | |
| 4761 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 4762 | IO_NETOP_PREP(op) \ |
| 4763 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 4764 | { \ |
| 4765 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4766 | } |
| 4767 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4768 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 4769 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 4770 | IO_NETOP_PREP_ASYNC(connect); |
| 4771 | IO_NETOP_PREP(accept); |
| 4772 | IO_NETOP_FN(send); |
| 4773 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4774 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4775 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4776 | struct io_poll_table { |
| 4777 | struct poll_table_struct pt; |
| 4778 | struct io_kiocb *req; |
| 4779 | int error; |
| 4780 | }; |
| 4781 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4782 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4783 | __poll_t mask, task_work_func_t func) |
| 4784 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4785 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4786 | |
| 4787 | /* for instances that support it check for an event match first: */ |
| 4788 | if (mask && !(mask & poll->events)) |
| 4789 | return 0; |
| 4790 | |
| 4791 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4792 | |
| 4793 | list_del_init(&poll->wait.entry); |
| 4794 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4795 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 4796 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4797 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4798 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4799 | * If this fails, then the task is exiting. When a task exits, the |
| 4800 | * work gets canceled, so just cancel this request as well instead |
| 4801 | * of executing it. We can't safely execute it anyway, as we may not |
| 4802 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4803 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 4804 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4805 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4806 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 4807 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4808 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4809 | return 1; |
| 4810 | } |
| 4811 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4812 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4813 | __acquires(&req->ctx->completion_lock) |
| 4814 | { |
| 4815 | struct io_ring_ctx *ctx = req->ctx; |
| 4816 | |
| 4817 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4818 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4819 | |
| 4820 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4821 | } |
| 4822 | |
| 4823 | spin_lock_irq(&ctx->completion_lock); |
| 4824 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4825 | add_wait_queue(poll->head, &poll->wait); |
| 4826 | return true; |
| 4827 | } |
| 4828 | |
| 4829 | return false; |
| 4830 | } |
| 4831 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4832 | 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] | 4833 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4834 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4835 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4836 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4837 | return req->apoll->double_poll; |
| 4838 | } |
| 4839 | |
| 4840 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 4841 | { |
| 4842 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4843 | return &req->poll; |
| 4844 | return &req->apoll->poll; |
| 4845 | } |
| 4846 | |
| 4847 | static void io_poll_remove_double(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 4848 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4849 | { |
| 4850 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4851 | |
| 4852 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4853 | |
| 4854 | if (poll && poll->head) { |
| 4855 | struct wait_queue_head *head = poll->head; |
| 4856 | |
| 4857 | spin_lock(&head->lock); |
| 4858 | list_del_init(&poll->wait.entry); |
| 4859 | if (poll->wait.private) |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 4860 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4861 | poll->head = NULL; |
| 4862 | spin_unlock(&head->lock); |
| 4863 | } |
| 4864 | } |
| 4865 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4866 | static bool io_poll_complete(struct io_kiocb *req, __poll_t mask) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 4867 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4868 | { |
| 4869 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4870 | unsigned flags = IORING_CQE_F_MORE; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4871 | int error; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4872 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4873 | if (READ_ONCE(req->poll.canceled)) { |
Jens Axboe | 45ab03b | 2021-02-23 08:19:33 -0700 | [diff] [blame] | 4874 | error = -ECANCELED; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4875 | req->poll.events |= EPOLLONESHOT; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4876 | } else { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 4877 | error = mangle_poll(mask); |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4878 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 4879 | if (req->poll.events & EPOLLONESHOT) |
| 4880 | flags = 0; |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 4881 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 4882 | io_poll_remove_waitqs(req); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4883 | req->poll.done = true; |
| 4884 | flags = 0; |
| 4885 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4886 | io_commit_cqring(ctx); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4887 | return !(flags & IORING_CQE_F_MORE); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4888 | } |
| 4889 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4890 | static void io_poll_task_func(struct callback_head *cb) |
| 4891 | { |
| 4892 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4893 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4894 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4895 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4896 | if (io_poll_rewait(req, &req->poll)) { |
| 4897 | spin_unlock_irq(&ctx->completion_lock); |
| 4898 | } else { |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 4899 | bool done; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4900 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 4901 | done = io_poll_complete(req, req->result); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4902 | if (done) { |
| 4903 | hash_del(&req->hash_node); |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 4904 | } else { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4905 | req->result = 0; |
| 4906 | add_wait_queue(req->poll.head, &req->poll.wait); |
| 4907 | } |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4908 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 4909 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4910 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4911 | if (done) { |
| 4912 | nxt = io_put_req_find_next(req); |
| 4913 | if (nxt) |
| 4914 | __io_req_task_submit(nxt); |
| 4915 | } |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4916 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4920 | int sync, void *key) |
| 4921 | { |
| 4922 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4923 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4924 | __poll_t mask = key_to_poll(key); |
| 4925 | |
| 4926 | /* for instances that support it check for an event match first: */ |
| 4927 | if (mask && !(mask & poll->events)) |
| 4928 | return 0; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 4929 | if (!(poll->events & EPOLLONESHOT)) |
| 4930 | return poll->wait.func(&poll->wait, mode, sync, key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4931 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 4932 | list_del_init(&wait->entry); |
| 4933 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4934 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4935 | bool done; |
| 4936 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4937 | spin_lock(&poll->head->lock); |
| 4938 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4939 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4940 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4941 | /* make sure double remove sees this as being gone */ |
| 4942 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4943 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 4944 | if (!done) { |
| 4945 | /* use wait func handler, so it matches the rq type */ |
| 4946 | poll->wait.func(&poll->wait, mode, sync, key); |
| 4947 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4948 | } |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 4949 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4950 | return 1; |
| 4951 | } |
| 4952 | |
| 4953 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4954 | wait_queue_func_t wake_func) |
| 4955 | { |
| 4956 | poll->head = NULL; |
| 4957 | poll->done = false; |
| 4958 | poll->canceled = false; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 4959 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 4960 | /* mask in events that we always want/need */ |
| 4961 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4962 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4963 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4964 | } |
| 4965 | |
| 4966 | 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] | 4967 | struct wait_queue_head *head, |
| 4968 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4969 | { |
| 4970 | struct io_kiocb *req = pt->req; |
| 4971 | |
| 4972 | /* |
| 4973 | * If poll->head is already set, it's because the file being polled |
| 4974 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4975 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4976 | */ |
| 4977 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 4978 | struct io_poll_iocb *poll_one = poll; |
| 4979 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4980 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4981 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4982 | pt->error = -EINVAL; |
| 4983 | return; |
| 4984 | } |
Jens Axboe | ea6a693d | 2021-04-15 09:47:13 -0600 | [diff] [blame] | 4985 | /* |
| 4986 | * Can't handle multishot for double wait for now, turn it |
| 4987 | * into one-shot mode. |
| 4988 | */ |
| 4989 | if (!(req->poll.events & EPOLLONESHOT)) |
| 4990 | req->poll.events |= EPOLLONESHOT; |
Jens Axboe | 1c3b3e6 | 2021-02-28 16:07:30 -0700 | [diff] [blame] | 4991 | /* double add on the same waitqueue head, ignore */ |
| 4992 | if (poll->head == head) |
| 4993 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4994 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4995 | if (!poll) { |
| 4996 | pt->error = -ENOMEM; |
| 4997 | return; |
| 4998 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 4999 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5000 | req_ref_get(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5001 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5002 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | pt->error = 0; |
| 5006 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5007 | |
| 5008 | if (poll->events & EPOLLEXCLUSIVE) |
| 5009 | add_wait_queue_exclusive(head, &poll->wait); |
| 5010 | else |
| 5011 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5015 | struct poll_table_struct *p) |
| 5016 | { |
| 5017 | 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] | 5018 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5019 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5020 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5021 | } |
| 5022 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5023 | static void io_async_task_func(struct callback_head *cb) |
| 5024 | { |
| 5025 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5026 | struct async_poll *apoll = req->apoll; |
| 5027 | struct io_ring_ctx *ctx = req->ctx; |
| 5028 | |
| 5029 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5030 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5031 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5032 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5033 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5034 | } |
| 5035 | |
Pavel Begunkov | 0ea13b4 | 2021-04-09 09:13:21 +0100 | [diff] [blame] | 5036 | hash_del(&req->hash_node); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5037 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5038 | spin_unlock_irq(&ctx->completion_lock); |
| 5039 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5040 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5041 | __io_req_task_submit(req); |
| 5042 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 5043 | io_req_complete_failed(req, -ECANCELED); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5047 | void *key) |
| 5048 | { |
| 5049 | struct io_kiocb *req = wait->private; |
| 5050 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5051 | |
| 5052 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5053 | key_to_poll(key)); |
| 5054 | |
| 5055 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5056 | } |
| 5057 | |
| 5058 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5059 | { |
| 5060 | struct io_ring_ctx *ctx = req->ctx; |
| 5061 | struct hlist_head *list; |
| 5062 | |
| 5063 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5064 | hlist_add_head(&req->hash_node, list); |
| 5065 | } |
| 5066 | |
| 5067 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5068 | struct io_poll_iocb *poll, |
| 5069 | struct io_poll_table *ipt, __poll_t mask, |
| 5070 | wait_queue_func_t wake_func) |
| 5071 | __acquires(&ctx->completion_lock) |
| 5072 | { |
| 5073 | struct io_ring_ctx *ctx = req->ctx; |
| 5074 | bool cancel = false; |
| 5075 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5076 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5077 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5078 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5079 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5080 | |
| 5081 | ipt->pt._key = mask; |
| 5082 | ipt->req = req; |
| 5083 | ipt->error = -EINVAL; |
| 5084 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5085 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5086 | |
| 5087 | spin_lock_irq(&ctx->completion_lock); |
| 5088 | if (likely(poll->head)) { |
| 5089 | spin_lock(&poll->head->lock); |
| 5090 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5091 | if (ipt->error) |
| 5092 | cancel = true; |
| 5093 | ipt->error = 0; |
| 5094 | mask = 0; |
| 5095 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5096 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5097 | list_del_init(&poll->wait.entry); |
| 5098 | else if (cancel) |
| 5099 | WRITE_ONCE(poll->canceled, true); |
| 5100 | else if (!poll->done) /* actually waiting for an event */ |
| 5101 | io_poll_req_insert(req); |
| 5102 | spin_unlock(&poll->head->lock); |
| 5103 | } |
| 5104 | |
| 5105 | return mask; |
| 5106 | } |
| 5107 | |
| 5108 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5109 | { |
| 5110 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5111 | struct io_ring_ctx *ctx = req->ctx; |
| 5112 | struct async_poll *apoll; |
| 5113 | struct io_poll_table ipt; |
| 5114 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5115 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5116 | |
| 5117 | if (!req->file || !file_can_poll(req->file)) |
| 5118 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5119 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5120 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5121 | if (def->pollin) |
| 5122 | rw = READ; |
| 5123 | else if (def->pollout) |
| 5124 | rw = WRITE; |
| 5125 | else |
| 5126 | return false; |
| 5127 | /* 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] | 5128 | if (!io_file_supports_async(req, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5129 | return false; |
| 5130 | |
| 5131 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5132 | if (unlikely(!apoll)) |
| 5133 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5134 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5135 | |
| 5136 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5137 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5138 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5139 | mask = EPOLLONESHOT; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5140 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5141 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5142 | if (def->pollout) |
| 5143 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5144 | |
| 5145 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5146 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5147 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5148 | mask &= ~POLLIN; |
| 5149 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5150 | mask |= POLLERR | POLLPRI; |
| 5151 | |
| 5152 | ipt.pt._qproc = io_async_queue_proc; |
| 5153 | |
| 5154 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5155 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5156 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5157 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5158 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5159 | return false; |
| 5160 | } |
| 5161 | spin_unlock_irq(&ctx->completion_lock); |
| 5162 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5163 | apoll->poll.events); |
| 5164 | return true; |
| 5165 | } |
| 5166 | |
| 5167 | static bool __io_poll_remove_one(struct io_kiocb *req, |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5168 | struct io_poll_iocb *poll, bool do_cancel) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5169 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5170 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5171 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5172 | |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5173 | if (!poll->head) |
| 5174 | return false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5175 | spin_lock(&poll->head->lock); |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5176 | if (do_cancel) |
| 5177 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5178 | if (!list_empty(&poll->wait.entry)) { |
| 5179 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5180 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5181 | } |
| 5182 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5183 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5184 | return do_complete; |
| 5185 | } |
| 5186 | |
Jens Axboe | b2c3f7e | 2021-02-23 08:58:04 -0700 | [diff] [blame] | 5187 | static bool io_poll_remove_waitqs(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5188 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5189 | { |
| 5190 | bool do_complete; |
| 5191 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5192 | io_poll_remove_double(req); |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5193 | 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] | 5194 | |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5195 | if (req->opcode != IORING_OP_POLL_ADD && do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5196 | /* non-poll requests have submit ref still */ |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5197 | req_ref_put(req); |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5198 | } |
Jens Axboe | b2c3f7e | 2021-02-23 08:58:04 -0700 | [diff] [blame] | 5199 | return do_complete; |
| 5200 | } |
| 5201 | |
| 5202 | static bool io_poll_remove_one(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5203 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | b2c3f7e | 2021-02-23 08:58:04 -0700 | [diff] [blame] | 5204 | { |
| 5205 | bool do_complete; |
| 5206 | |
| 5207 | do_complete = io_poll_remove_waitqs(req); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5208 | if (do_complete) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5209 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5210 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5211 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5212 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5216 | } |
| 5217 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5218 | /* |
| 5219 | * Returns true if we found and killed one or more poll requests |
| 5220 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5221 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5222 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5223 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5224 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5225 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5226 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5227 | |
| 5228 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5229 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5230 | struct hlist_head *list; |
| 5231 | |
| 5232 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5233 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5234 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5235 | posted += io_poll_remove_one(req); |
| 5236 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5237 | } |
| 5238 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5239 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5240 | if (posted) |
| 5241 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5242 | |
| 5243 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5244 | } |
| 5245 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5246 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5247 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5248 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5249 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5250 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5251 | struct io_kiocb *req; |
| 5252 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5253 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5254 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5255 | if (sqe_addr != req->user_data) |
| 5256 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5257 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 5258 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5259 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5260 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5261 | return NULL; |
| 5262 | } |
| 5263 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5264 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5265 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5266 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5267 | { |
| 5268 | struct io_kiocb *req; |
| 5269 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5270 | req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5271 | if (!req) |
| 5272 | return -ENOENT; |
| 5273 | if (io_poll_remove_one(req)) |
| 5274 | return 0; |
| 5275 | |
| 5276 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5277 | } |
| 5278 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5279 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 5280 | unsigned int flags) |
| 5281 | { |
| 5282 | u32 events; |
| 5283 | |
| 5284 | events = READ_ONCE(sqe->poll32_events); |
| 5285 | #ifdef __BIG_ENDIAN |
| 5286 | events = swahw32(events); |
| 5287 | #endif |
| 5288 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 5289 | events |= EPOLLONESHOT; |
| 5290 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 5291 | } |
| 5292 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5293 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5294 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5295 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5296 | struct io_poll_update *upd = &req->poll_update; |
| 5297 | u32 flags; |
| 5298 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5299 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5300 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5301 | if (sqe->ioprio || sqe->buf_index) |
| 5302 | return -EINVAL; |
| 5303 | flags = READ_ONCE(sqe->len); |
| 5304 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 5305 | IORING_POLL_ADD_MULTI)) |
| 5306 | return -EINVAL; |
| 5307 | /* meaningless without update */ |
| 5308 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5309 | return -EINVAL; |
| 5310 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5311 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 5312 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5313 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5314 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5315 | upd->new_user_data = READ_ONCE(sqe->off); |
| 5316 | if (!upd->update_user_data && upd->new_user_data) |
| 5317 | return -EINVAL; |
| 5318 | if (upd->update_events) |
| 5319 | upd->events = io_poll_parse_events(sqe, flags); |
| 5320 | else if (sqe->poll32_events) |
| 5321 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5322 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5323 | return 0; |
| 5324 | } |
| 5325 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5326 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5327 | void *key) |
| 5328 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5329 | struct io_kiocb *req = wait->private; |
| 5330 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5331 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5332 | 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] | 5333 | } |
| 5334 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5335 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5336 | struct poll_table_struct *p) |
| 5337 | { |
| 5338 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5339 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5340 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5341 | } |
| 5342 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5343 | 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] | 5344 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5345 | struct io_poll_iocb *poll = &req->poll; |
| 5346 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5347 | |
| 5348 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5349 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5350 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5351 | return -EINVAL; |
| 5352 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5353 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5354 | return -EINVAL; |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5355 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5356 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5357 | return 0; |
| 5358 | } |
| 5359 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5360 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5361 | { |
| 5362 | struct io_poll_iocb *poll = &req->poll; |
| 5363 | struct io_ring_ctx *ctx = req->ctx; |
| 5364 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5365 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5366 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5367 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5368 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5369 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5370 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5371 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5372 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5373 | ipt.error = 0; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5374 | io_poll_complete(req, mask); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5375 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5376 | spin_unlock_irq(&ctx->completion_lock); |
| 5377 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5378 | if (mask) { |
| 5379 | io_cqring_ev_posted(ctx); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5380 | if (poll->events & EPOLLONESHOT) |
| 5381 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5382 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5383 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5384 | } |
| 5385 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5386 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5387 | { |
| 5388 | struct io_ring_ctx *ctx = req->ctx; |
| 5389 | struct io_kiocb *preq; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5390 | bool completing; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5391 | int ret; |
| 5392 | |
| 5393 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5394 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5395 | if (!preq) { |
| 5396 | ret = -ENOENT; |
| 5397 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5398 | } |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5399 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5400 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
| 5401 | completing = true; |
| 5402 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; |
| 5403 | goto err; |
| 5404 | } |
| 5405 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5406 | /* |
| 5407 | * Don't allow racy completion with singleshot, as we cannot safely |
| 5408 | * update those. For multishot, if we're racing with completion, just |
| 5409 | * let completion re-add it. |
| 5410 | */ |
| 5411 | completing = !__io_poll_remove_one(preq, &preq->poll, false); |
| 5412 | if (completing && (preq->poll.events & EPOLLONESHOT)) { |
| 5413 | ret = -EALREADY; |
| 5414 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5415 | } |
| 5416 | /* we now have a detached poll request. reissue. */ |
| 5417 | ret = 0; |
| 5418 | err: |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5419 | if (ret < 0) { |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5420 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5421 | req_set_fail_links(req); |
| 5422 | io_req_complete(req, ret); |
| 5423 | return 0; |
| 5424 | } |
| 5425 | /* only mask one event flags, keep behavior flags */ |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5426 | if (req->poll_update.update_events) { |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5427 | preq->poll.events &= ~0xffff; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5428 | preq->poll.events |= req->poll_update.events & 0xffff; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5429 | preq->poll.events |= IO_POLL_UNMASK; |
| 5430 | } |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5431 | if (req->poll_update.update_user_data) |
| 5432 | preq->user_data = req->poll_update.new_user_data; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5433 | spin_unlock_irq(&ctx->completion_lock); |
| 5434 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5435 | /* complete update request, we're done with it */ |
| 5436 | io_req_complete(req, ret); |
| 5437 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5438 | if (!completing) { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5439 | ret = io_poll_add(preq, issue_flags); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5440 | if (ret < 0) { |
| 5441 | req_set_fail_links(preq); |
| 5442 | io_req_complete(preq, ret); |
| 5443 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5444 | } |
| 5445 | return 0; |
| 5446 | } |
| 5447 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5448 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5449 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5450 | struct io_timeout_data *data = container_of(timer, |
| 5451 | struct io_timeout_data, timer); |
| 5452 | struct io_kiocb *req = data->req; |
| 5453 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5454 | unsigned long flags; |
| 5455 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5456 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5457 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5458 | atomic_set(&req->ctx->cq_timeouts, |
| 5459 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5460 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5461 | io_cqring_fill_event(ctx, req->user_data, -ETIME, 0); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5462 | io_commit_cqring(ctx); |
| 5463 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5464 | |
| 5465 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5466 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5467 | io_put_req(req); |
| 5468 | return HRTIMER_NORESTART; |
| 5469 | } |
| 5470 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5471 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5472 | __u64 user_data) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5473 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5474 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5475 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5476 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5477 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5478 | |
| 5479 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5480 | found = user_data == req->user_data; |
| 5481 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5482 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5483 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5484 | if (!found) |
| 5485 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5486 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5487 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5488 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5489 | return ERR_PTR(-EALREADY); |
| 5490 | list_del_init(&req->timeout.list); |
| 5491 | return req; |
| 5492 | } |
| 5493 | |
| 5494 | 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] | 5495 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5496 | { |
| 5497 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5498 | |
| 5499 | if (IS_ERR(req)) |
| 5500 | return PTR_ERR(req); |
| 5501 | |
| 5502 | req_set_fail_links(req); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5503 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5504 | io_put_req_deferred(req, 1); |
| 5505 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5506 | } |
| 5507 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5508 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5509 | struct timespec64 *ts, enum hrtimer_mode mode) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5510 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5511 | { |
| 5512 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5513 | struct io_timeout_data *data; |
| 5514 | |
| 5515 | if (IS_ERR(req)) |
| 5516 | return PTR_ERR(req); |
| 5517 | |
| 5518 | req->timeout.off = 0; /* noseq */ |
| 5519 | data = req->async_data; |
| 5520 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5521 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5522 | data->timer.function = io_timeout_fn; |
| 5523 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5524 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5525 | } |
| 5526 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5527 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5528 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5529 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5530 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5531 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5532 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5533 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5534 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5535 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5536 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5537 | return -EINVAL; |
| 5538 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5539 | tr->addr = READ_ONCE(sqe->addr); |
| 5540 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5541 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5542 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5543 | return -EINVAL; |
| 5544 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5545 | return -EFAULT; |
| 5546 | } else if (tr->flags) { |
| 5547 | /* timeout removal doesn't support flags */ |
| 5548 | return -EINVAL; |
| 5549 | } |
| 5550 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5551 | return 0; |
| 5552 | } |
| 5553 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5554 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5555 | { |
| 5556 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5557 | : HRTIMER_MODE_REL; |
| 5558 | } |
| 5559 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5560 | /* |
| 5561 | * Remove or update an existing timeout command |
| 5562 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5563 | 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] | 5564 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5565 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5566 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5567 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5568 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5569 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5570 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5571 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5572 | else |
| 5573 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5574 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5575 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5576 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5577 | io_commit_cqring(ctx); |
| 5578 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5579 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5580 | if (ret < 0) |
| 5581 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5582 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5583 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5584 | } |
| 5585 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5586 | 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] | 5587 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5588 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5589 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5590 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5591 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5592 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5593 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5594 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5595 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5596 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5597 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5598 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5599 | flags = READ_ONCE(sqe->timeout_flags); |
| 5600 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5601 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5602 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5603 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5604 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5605 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5606 | return -ENOMEM; |
| 5607 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5608 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5609 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5610 | |
| 5611 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5612 | return -EFAULT; |
| 5613 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5614 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5615 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
Pavel Begunkov | 2482b58 | 2021-03-25 18:32:44 +0000 | [diff] [blame] | 5616 | if (is_timeout_link) |
| 5617 | io_req_track_inflight(req); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5618 | return 0; |
| 5619 | } |
| 5620 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5621 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5622 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5623 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5624 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5625 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5626 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5627 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5628 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5629 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5630 | /* |
| 5631 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5632 | * timeout event to be satisfied. If it isn't set, then this is |
| 5633 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5634 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5635 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5636 | entry = ctx->timeout_list.prev; |
| 5637 | goto add; |
| 5638 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5639 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5640 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5641 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5642 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5643 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5644 | * This is safe because ->completion_lock is held, and submissions |
| 5645 | * and completions are never mixed in the same ->completion_lock section. |
| 5646 | */ |
| 5647 | ctx->cq_last_tm_flush = tail; |
| 5648 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5649 | /* |
| 5650 | * Insertion sort, ensuring the first entry in the list is always |
| 5651 | * the one we need first. |
| 5652 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5653 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5654 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5655 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5656 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5657 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5658 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5659 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5660 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5661 | break; |
| 5662 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5663 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5664 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5665 | data->timer.function = io_timeout_fn; |
| 5666 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5667 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5668 | return 0; |
| 5669 | } |
| 5670 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5671 | struct io_cancel_data { |
| 5672 | struct io_ring_ctx *ctx; |
| 5673 | u64 user_data; |
| 5674 | }; |
| 5675 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5676 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5677 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5678 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5679 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5680 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5681 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5682 | } |
| 5683 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5684 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 5685 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5686 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5687 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5688 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5689 | int ret = 0; |
| 5690 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5691 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5692 | return -ENOENT; |
| 5693 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5694 | 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] | 5695 | switch (cancel_ret) { |
| 5696 | case IO_WQ_CANCEL_OK: |
| 5697 | ret = 0; |
| 5698 | break; |
| 5699 | case IO_WQ_CANCEL_RUNNING: |
| 5700 | ret = -EALREADY; |
| 5701 | break; |
| 5702 | case IO_WQ_CANCEL_NOTFOUND: |
| 5703 | ret = -ENOENT; |
| 5704 | break; |
| 5705 | } |
| 5706 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5707 | return ret; |
| 5708 | } |
| 5709 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5710 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5711 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5712 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5713 | { |
| 5714 | unsigned long flags; |
| 5715 | int ret; |
| 5716 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 5717 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5718 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 5719 | if (ret != -ENOENT) |
| 5720 | goto done; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5721 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5722 | if (ret != -ENOENT) |
| 5723 | goto done; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5724 | ret = io_poll_cancel(ctx, sqe_addr, false); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5725 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5726 | if (!ret) |
| 5727 | ret = success_ret; |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5728 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5729 | io_commit_cqring(ctx); |
| 5730 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5731 | io_cqring_ev_posted(ctx); |
| 5732 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5733 | if (ret < 0) |
| 5734 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5735 | } |
| 5736 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5737 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5738 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5739 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5740 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5741 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5742 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5743 | return -EINVAL; |
| 5744 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5745 | return -EINVAL; |
| 5746 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5747 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5748 | return 0; |
| 5749 | } |
| 5750 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5751 | 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] | 5752 | { |
| 5753 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5754 | u64 sqe_addr = req->cancel.addr; |
| 5755 | struct io_tctx_node *node; |
| 5756 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5757 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5758 | /* tasks should wait for their io-wq threads, so safe w/o sync */ |
| 5759 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
| 5760 | spin_lock_irq(&ctx->completion_lock); |
| 5761 | if (ret != -ENOENT) |
| 5762 | goto done; |
| 5763 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5764 | if (ret != -ENOENT) |
| 5765 | goto done; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5766 | ret = io_poll_cancel(ctx, sqe_addr, false); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5767 | if (ret != -ENOENT) |
| 5768 | goto done; |
| 5769 | spin_unlock_irq(&ctx->completion_lock); |
| 5770 | |
| 5771 | /* slow path, try all io-wq's */ |
| 5772 | io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 5773 | ret = -ENOENT; |
| 5774 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 5775 | struct io_uring_task *tctx = node->task->io_uring; |
| 5776 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5777 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 5778 | if (ret != -ENOENT) |
| 5779 | break; |
| 5780 | } |
| 5781 | io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK)); |
| 5782 | |
| 5783 | spin_lock_irq(&ctx->completion_lock); |
| 5784 | done: |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5785 | io_cqring_fill_event(ctx, req->user_data, ret, 0); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 5786 | io_commit_cqring(ctx); |
| 5787 | spin_unlock_irq(&ctx->completion_lock); |
| 5788 | io_cqring_ev_posted(ctx); |
| 5789 | |
| 5790 | if (ret < 0) |
| 5791 | req_set_fail_links(req); |
| 5792 | io_put_req(req); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5793 | return 0; |
| 5794 | } |
| 5795 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5796 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5797 | const struct io_uring_sqe *sqe) |
| 5798 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5799 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5800 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5801 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5802 | return -EINVAL; |
| 5803 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5804 | return -EINVAL; |
| 5805 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5806 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 5807 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 5808 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5809 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5810 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5811 | return 0; |
| 5812 | } |
| 5813 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5814 | 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] | 5815 | { |
| 5816 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5817 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5818 | int ret; |
| 5819 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5820 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5821 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5822 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5823 | up.offset = req->rsrc_update.offset; |
| 5824 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5825 | |
| 5826 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5827 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5828 | mutex_unlock(&ctx->uring_lock); |
| 5829 | |
| 5830 | if (ret < 0) |
| 5831 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5832 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5833 | return 0; |
| 5834 | } |
| 5835 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5836 | 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] | 5837 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5838 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5839 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5840 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5841 | case IORING_OP_READV: |
| 5842 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5843 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5844 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5845 | case IORING_OP_WRITEV: |
| 5846 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5847 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5848 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5849 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5850 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5851 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5852 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5853 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5854 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5855 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5856 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5857 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5858 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5859 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5860 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5861 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5862 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5863 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5864 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5865 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5866 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5867 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5868 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5869 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5870 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5871 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5872 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5873 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5874 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5875 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5876 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5877 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5878 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5879 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5880 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5881 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5882 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5883 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5884 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5885 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5886 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5887 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5888 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5889 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5890 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5891 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5892 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5893 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5894 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5895 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5896 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5897 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5898 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5899 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5900 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 5901 | case IORING_OP_SHUTDOWN: |
| 5902 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5903 | case IORING_OP_RENAMEAT: |
| 5904 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5905 | case IORING_OP_UNLINKAT: |
| 5906 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5907 | } |
| 5908 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5909 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5910 | req->opcode); |
| 5911 | return-EINVAL; |
| 5912 | } |
| 5913 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5914 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5915 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 5916 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 5917 | return 0; |
| 5918 | if (WARN_ON_ONCE(req->async_data)) |
| 5919 | return -EFAULT; |
| 5920 | if (io_alloc_async_data(req)) |
| 5921 | return -EAGAIN; |
| 5922 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5923 | switch (req->opcode) { |
| 5924 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5925 | return io_rw_prep_async(req, READ); |
| 5926 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5927 | return io_rw_prep_async(req, WRITE); |
| 5928 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5929 | return io_sendmsg_prep_async(req); |
| 5930 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5931 | return io_recvmsg_prep_async(req); |
| 5932 | case IORING_OP_CONNECT: |
| 5933 | return io_connect_prep_async(req); |
| 5934 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 5935 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 5936 | req->opcode); |
| 5937 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5938 | } |
| 5939 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5940 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5941 | { |
| 5942 | struct io_kiocb *pos; |
| 5943 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5944 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5945 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5946 | io_for_each_link(pos, req) |
| 5947 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5948 | |
| 5949 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5950 | return total_submitted - nr_reqs; |
| 5951 | } |
| 5952 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5953 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5954 | { |
| 5955 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5956 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5957 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5958 | u32 seq; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5959 | |
| 5960 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5961 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 5962 | !(req->flags & REQ_F_IO_DRAIN))) |
| 5963 | return 0; |
| 5964 | |
| 5965 | seq = io_get_sequence(req); |
| 5966 | /* Still a chance to pass the sequence check */ |
| 5967 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5968 | return 0; |
| 5969 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 5970 | ret = io_req_prep_async(req); |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5971 | if (ret) |
| 5972 | return ret; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 5973 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5974 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 5975 | if (!de) |
| 5976 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5977 | |
| 5978 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5979 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5980 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5981 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 5982 | io_queue_async_work(req); |
| 5983 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5987 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5988 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5989 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5990 | spin_unlock_irq(&ctx->completion_lock); |
| 5991 | return -EIOCBQUEUED; |
| 5992 | } |
| 5993 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 5994 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5995 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5996 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 5997 | switch (req->opcode) { |
| 5998 | case IORING_OP_READV: |
| 5999 | case IORING_OP_READ_FIXED: |
| 6000 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6001 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6002 | break; |
| 6003 | case IORING_OP_RECVMSG: |
| 6004 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 6005 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6006 | break; |
| 6007 | } |
| 6008 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6009 | } |
| 6010 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6011 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6012 | switch (req->opcode) { |
| 6013 | case IORING_OP_READV: |
| 6014 | case IORING_OP_READ_FIXED: |
| 6015 | case IORING_OP_READ: |
| 6016 | case IORING_OP_WRITEV: |
| 6017 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6018 | case IORING_OP_WRITE: { |
| 6019 | struct io_async_rw *io = req->async_data; |
| 6020 | if (io->free_iovec) |
| 6021 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6022 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6023 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6024 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6025 | case IORING_OP_SENDMSG: { |
| 6026 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6027 | |
| 6028 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6029 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6030 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6031 | case IORING_OP_SPLICE: |
| 6032 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6033 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6034 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6035 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6036 | case IORING_OP_OPENAT: |
| 6037 | case IORING_OP_OPENAT2: |
| 6038 | if (req->open.filename) |
| 6039 | putname(req->open.filename); |
| 6040 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6041 | case IORING_OP_RENAMEAT: |
| 6042 | putname(req->rename.oldpath); |
| 6043 | putname(req->rename.newpath); |
| 6044 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6045 | case IORING_OP_UNLINKAT: |
| 6046 | putname(req->unlink.filename); |
| 6047 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6048 | } |
| 6049 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 6050 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6051 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6052 | kfree(req->apoll->double_poll); |
| 6053 | kfree(req->apoll); |
| 6054 | req->apoll = NULL; |
| 6055 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6056 | if (req->flags & REQ_F_INFLIGHT) { |
| 6057 | struct io_uring_task *tctx = req->task->io_uring; |
| 6058 | |
| 6059 | atomic_dec(&tctx->inflight_tracked); |
| 6060 | req->flags &= ~REQ_F_INFLIGHT; |
| 6061 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6062 | } |
| 6063 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6064 | 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] | 6065 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6066 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6067 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6068 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6069 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6070 | if (req->work.creds && req->work.creds != current_cred()) |
| 6071 | creds = override_creds(req->work.creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6072 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6073 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6074 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6075 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6076 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6077 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6078 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6079 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6080 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6081 | break; |
| 6082 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6083 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6084 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6085 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6086 | break; |
| 6087 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6088 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6089 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6090 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6091 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6092 | break; |
| 6093 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6094 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6095 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6096 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6097 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6098 | break; |
| 6099 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6100 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6101 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6102 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6103 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6104 | break; |
| 6105 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6106 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6107 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6108 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6109 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6110 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6111 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6112 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6113 | break; |
| 6114 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6115 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6116 | break; |
| 6117 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6118 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6119 | break; |
| 6120 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6121 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6122 | break; |
| 6123 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6124 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6125 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6126 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6127 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6128 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6129 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6130 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6131 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6132 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6133 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6134 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6135 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6136 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6137 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6138 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6139 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6140 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6141 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6142 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6143 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6144 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6145 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6146 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6147 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6148 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6149 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6150 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6151 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6152 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6153 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6154 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6155 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6156 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6157 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6158 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6159 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6160 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6161 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6162 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6163 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6164 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6165 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6166 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6167 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6168 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6169 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6170 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6171 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6172 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6173 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6174 | default: |
| 6175 | ret = -EINVAL; |
| 6176 | break; |
| 6177 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6178 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6179 | if (creds) |
| 6180 | revert_creds(creds); |
| 6181 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6182 | if (ret) |
| 6183 | return ret; |
| 6184 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6185 | /* If the op doesn't have a file, we're not polling for it */ |
| 6186 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6187 | const bool in_async = io_wq_current_is_worker(); |
| 6188 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6189 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6190 | if (in_async) |
| 6191 | mutex_lock(&ctx->uring_lock); |
| 6192 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6193 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6194 | |
| 6195 | if (in_async) |
| 6196 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | return 0; |
| 6200 | } |
| 6201 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6202 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6203 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6204 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6205 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6206 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6207 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6208 | timeout = io_prep_linked_timeout(req); |
| 6209 | if (timeout) |
| 6210 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6211 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6212 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6213 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6214 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6215 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6216 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6217 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6218 | /* |
| 6219 | * We can get EAGAIN for polled IO even though we're |
| 6220 | * forcing a sync submission from here, since we can't |
| 6221 | * wait for request slots on the block side. |
| 6222 | */ |
| 6223 | if (ret != -EAGAIN) |
| 6224 | break; |
| 6225 | cond_resched(); |
| 6226 | } while (1); |
| 6227 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6228 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6229 | /* avoid locking problems by failing it from a clean context */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6230 | if (ret) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6231 | /* io-wq is going to take one down */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 6232 | req_ref_get(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6233 | io_req_task_queue_fail(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6234 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6235 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6236 | |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6237 | #define FFS_ASYNC_READ 0x1UL |
| 6238 | #define FFS_ASYNC_WRITE 0x2UL |
| 6239 | #ifdef CONFIG_64BIT |
| 6240 | #define FFS_ISREG 0x4UL |
| 6241 | #else |
| 6242 | #define FFS_ISREG 0x0UL |
| 6243 | #endif |
| 6244 | #define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG) |
| 6245 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6246 | 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] | 6247 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6248 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6249 | struct io_fixed_file *table_l2; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6250 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6251 | table_l2 = table->files[i >> IORING_FILE_TABLE_SHIFT]; |
| 6252 | return &table_l2[i & IORING_FILE_TABLE_MASK]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
| 6255 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6256 | int index) |
| 6257 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6258 | 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] | 6259 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6260 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6261 | } |
| 6262 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6263 | 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] | 6264 | { |
| 6265 | unsigned long file_ptr = (unsigned long) file; |
| 6266 | |
| 6267 | if (__io_file_supports_async(file, READ)) |
| 6268 | file_ptr |= FFS_ASYNC_READ; |
| 6269 | if (__io_file_supports_async(file, WRITE)) |
| 6270 | file_ptr |= FFS_ASYNC_WRITE; |
| 6271 | if (S_ISREG(file_inode(file)->i_mode)) |
| 6272 | file_ptr |= FFS_ISREG; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6273 | file_slot->file_ptr = file_ptr; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6274 | } |
| 6275 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6276 | static struct file *io_file_get(struct io_submit_state *state, |
| 6277 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6278 | { |
| 6279 | struct io_ring_ctx *ctx = req->ctx; |
| 6280 | struct file *file; |
| 6281 | |
| 6282 | if (fixed) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6283 | unsigned long file_ptr; |
| 6284 | |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6285 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6286 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6287 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6288 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 6289 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6290 | file_ptr &= ~FFS_MASK; |
| 6291 | /* mask in overlapping REQ_F and FFS bits */ |
| 6292 | req->flags |= (file_ptr << REQ_F_ASYNC_READ_BIT); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 6293 | io_req_set_rsrc_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6294 | } else { |
| 6295 | trace_io_uring_file_get(ctx, fd); |
| 6296 | file = __io_file_get(state, fd); |
Jens Axboe | d44f554 | 2021-03-12 08:27:05 -0700 | [diff] [blame] | 6297 | |
| 6298 | /* we don't allow fixed io_uring files */ |
| 6299 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6300 | io_req_track_inflight(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6301 | } |
| 6302 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6303 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6304 | } |
| 6305 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6306 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6307 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6308 | struct io_timeout_data *data = container_of(timer, |
| 6309 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6310 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6311 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6312 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6313 | |
| 6314 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6315 | prev = req->timeout.head; |
| 6316 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6317 | |
| 6318 | /* |
| 6319 | * We don't expect the list to be empty, that will only happen if we |
| 6320 | * race with the completion of the linked work. |
| 6321 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 6322 | if (prev && req_ref_inc_not_zero(prev)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6323 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6324 | else |
| 6325 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6326 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6327 | |
| 6328 | if (prev) { |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6329 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6330 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6331 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6332 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6333 | } |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6334 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6335 | return HRTIMER_NORESTART; |
| 6336 | } |
| 6337 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6338 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6339 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6340 | struct io_ring_ctx *ctx = req->ctx; |
| 6341 | |
| 6342 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6343 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6344 | * If the back reference is NULL, then our linked request finished |
| 6345 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6346 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6347 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6348 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6349 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6350 | data->timer.function = io_link_timeout_fn; |
| 6351 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6352 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6353 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6354 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6355 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6356 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6357 | } |
| 6358 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6359 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6360 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6361 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6362 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6363 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6364 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6365 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6366 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6367 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6368 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6369 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6370 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6371 | } |
| 6372 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6373 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6374 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6375 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6376 | int ret; |
| 6377 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6378 | 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] | 6379 | |
| 6380 | /* |
| 6381 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6382 | * doesn't support non-blocking read/write attempts |
| 6383 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6384 | if (likely(!ret)) { |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6385 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6386 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6387 | struct io_ring_ctx *ctx = req->ctx; |
| 6388 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6389 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6390 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6391 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6392 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6393 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6394 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6395 | } |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 6396 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
| 6397 | if (!io_arm_poll_handler(req)) { |
| 6398 | /* |
| 6399 | * Queued up for async execution, worker will release |
| 6400 | * submit reference when the iocb is actually submitted. |
| 6401 | */ |
| 6402 | io_queue_async_work(req); |
| 6403 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6404 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6405 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6406 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6407 | if (linked_timeout) |
| 6408 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6409 | } |
| 6410 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6411 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6412 | { |
| 6413 | int ret; |
| 6414 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6415 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6416 | if (ret) { |
| 6417 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6418 | fail_req: |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6419 | io_req_complete_failed(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6420 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6421 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6422 | ret = io_req_prep_async(req); |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6423 | if (unlikely(ret)) |
| 6424 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6425 | io_queue_async_work(req); |
| 6426 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6427 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6428 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6429 | } |
| 6430 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6431 | /* |
| 6432 | * Check SQE restrictions (opcode and flags). |
| 6433 | * |
| 6434 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6435 | */ |
| 6436 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6437 | struct io_kiocb *req, |
| 6438 | unsigned int sqe_flags) |
| 6439 | { |
| 6440 | if (!ctx->restricted) |
| 6441 | return true; |
| 6442 | |
| 6443 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6444 | return false; |
| 6445 | |
| 6446 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6447 | ctx->restrictions.sqe_flags_required) |
| 6448 | return false; |
| 6449 | |
| 6450 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6451 | ctx->restrictions.sqe_flags_required)) |
| 6452 | return false; |
| 6453 | |
| 6454 | return true; |
| 6455 | } |
| 6456 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6457 | 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] | 6458 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6459 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6460 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6461 | unsigned int sqe_flags; |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6462 | int personality, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6463 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6464 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6465 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6466 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6467 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6468 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6469 | req->file = NULL; |
| 6470 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6471 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6472 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6473 | /* one is dropped after submission, the other at completion */ |
Jens Axboe | abc54d6 | 2021-02-24 13:32:30 -0700 | [diff] [blame] | 6474 | atomic_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6475 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6476 | req->result = 0; |
Jens Axboe | 93e68e0 | 2021-03-09 07:02:21 -0700 | [diff] [blame] | 6477 | req->work.creds = NULL; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6478 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6479 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6480 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
| 6481 | req->flags = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6482 | return -EINVAL; |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6483 | } |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6484 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6485 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6486 | return -EINVAL; |
| 6487 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6488 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6489 | return -EACCES; |
| 6490 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6491 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6492 | !io_op_defs[req->opcode].buffer_select) |
| 6493 | return -EOPNOTSUPP; |
| 6494 | |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6495 | personality = READ_ONCE(sqe->personality); |
| 6496 | if (personality) { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 6497 | req->work.creds = xa_load(&ctx->personalities, personality); |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6498 | if (!req->work.creds) |
| 6499 | return -EINVAL; |
| 6500 | get_cred(req->work.creds); |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 6501 | } |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6502 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6503 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6504 | /* |
| 6505 | * Plug now if we have more than 1 IO left after this, and the target |
| 6506 | * is potentially a read/write to block based storage. |
| 6507 | */ |
| 6508 | if (!state->plug_started && state->ios_left > 1 && |
| 6509 | io_op_defs[req->opcode].plug) { |
| 6510 | blk_start_plug(&state->plug); |
| 6511 | state->plug_started = true; |
| 6512 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6513 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6514 | if (io_op_defs[req->opcode].needs_file) { |
| 6515 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6516 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6517 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6518 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6519 | ret = -EBADF; |
| 6520 | } |
| 6521 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6522 | state->ios_left--; |
| 6523 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6524 | } |
| 6525 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6526 | 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] | 6527 | const struct io_uring_sqe *sqe) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6528 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6529 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6530 | int ret; |
| 6531 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6532 | ret = io_init_req(ctx, req, sqe); |
| 6533 | if (unlikely(ret)) { |
| 6534 | fail_req: |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6535 | if (link->head) { |
| 6536 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6537 | link->head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6538 | io_req_complete_failed(link->head, -ECANCELED); |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6539 | link->head = NULL; |
| 6540 | } |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 6541 | io_req_complete_failed(req, ret); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6542 | return ret; |
| 6543 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6544 | ret = io_req_prep(req, sqe); |
| 6545 | if (unlikely(ret)) |
| 6546 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6547 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6548 | /* don't need @sqe from now on */ |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6549 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 6550 | true, ctx->flags & IORING_SETUP_SQPOLL); |
| 6551 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6552 | /* |
| 6553 | * If we already have a head request, queue this one for async |
| 6554 | * submittal once the head completes. If we don't have a head but |
| 6555 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6556 | * submitted sync once the chain is complete. If none of those |
| 6557 | * conditions are true (normal request), then just queue it. |
| 6558 | */ |
| 6559 | if (link->head) { |
| 6560 | struct io_kiocb *head = link->head; |
| 6561 | |
| 6562 | /* |
| 6563 | * Taking sequential execution of a link, draining both sides |
| 6564 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6565 | * requests in the link. So, it drains the head and the |
| 6566 | * next after the link request. The last one is done via |
| 6567 | * drain_next flag to persist the effect across calls. |
| 6568 | */ |
| 6569 | if (req->flags & REQ_F_IO_DRAIN) { |
| 6570 | head->flags |= REQ_F_IO_DRAIN; |
| 6571 | ctx->drain_next = 1; |
| 6572 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6573 | ret = io_req_prep_async(req); |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6574 | if (unlikely(ret)) |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6575 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6576 | trace_io_uring_link(ctx, req, head); |
| 6577 | link->last->link = req; |
| 6578 | link->last = req; |
| 6579 | |
| 6580 | /* last request of a link, enqueue the link */ |
| 6581 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6582 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6583 | link->head = NULL; |
| 6584 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6585 | } else { |
| 6586 | if (unlikely(ctx->drain_next)) { |
| 6587 | req->flags |= REQ_F_IO_DRAIN; |
| 6588 | ctx->drain_next = 0; |
| 6589 | } |
| 6590 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6591 | link->head = req; |
| 6592 | link->last = req; |
| 6593 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6594 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6595 | } |
| 6596 | } |
| 6597 | |
| 6598 | return 0; |
| 6599 | } |
| 6600 | |
| 6601 | /* |
| 6602 | * Batched submission is done, ensure local IO is flushed out. |
| 6603 | */ |
| 6604 | static void io_submit_state_end(struct io_submit_state *state, |
| 6605 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6606 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6607 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6608 | io_queue_sqe(state->link.head); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6609 | if (state->comp.nr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6610 | io_submit_flush_completions(&state->comp, ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6611 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6612 | blk_finish_plug(&state->plug); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6613 | io_state_file_put(state); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6614 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6615 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6616 | /* |
| 6617 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6618 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6619 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6620 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6621 | { |
| 6622 | state->plug_started = false; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6623 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6624 | /* set only head, no need to init link_last in advance */ |
| 6625 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6626 | } |
| 6627 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6628 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6629 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6630 | struct io_rings *rings = ctx->rings; |
| 6631 | |
| 6632 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6633 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6634 | * since once we write the new head, the application could |
| 6635 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 6636 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6637 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6638 | } |
| 6639 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6640 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6641 | * 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] | 6642 | * that is mapped by userspace. This means that care needs to be taken to |
| 6643 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 6644 | * being a good citizen. If members of the sqe are validated and then later |
| 6645 | * 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] | 6646 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6647 | */ |
| 6648 | 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] | 6649 | { |
| 6650 | u32 *sq_array = ctx->sq_array; |
| 6651 | unsigned head; |
| 6652 | |
| 6653 | /* |
| 6654 | * The cached sq head (or cq tail) serves two purposes: |
| 6655 | * |
| 6656 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6657 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6658 | * 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] | 6659 | * though the application is the one updating it. |
| 6660 | */ |
| 6661 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
| 6662 | if (likely(head < ctx->sq_entries)) |
| 6663 | return &ctx->sq_sqes[head]; |
| 6664 | |
| 6665 | /* drop invalid entries */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6666 | ctx->cached_sq_dropped++; |
| 6667 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
| 6668 | return NULL; |
| 6669 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 6670 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6671 | 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] | 6672 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6673 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6674 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6675 | /* make sure SQ entry isn't read before tail */ |
| 6676 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6677 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6678 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6679 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6680 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6681 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6682 | refcount_add(nr, ¤t->usage); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6683 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6684 | |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6685 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6686 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6687 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6688 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6689 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6690 | if (unlikely(!req)) { |
| 6691 | if (!submitted) |
| 6692 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6693 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6694 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6695 | sqe = io_get_sqe(ctx); |
| 6696 | if (unlikely(!sqe)) { |
| 6697 | kmem_cache_free(req_cachep, req); |
| 6698 | break; |
| 6699 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6700 | /* will complete beyond this point, count as submitted */ |
| 6701 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6702 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6703 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6704 | } |
| 6705 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6706 | if (unlikely(submitted != nr)) { |
| 6707 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6708 | struct io_uring_task *tctx = current->io_uring; |
| 6709 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6710 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6711 | percpu_ref_put_many(&ctx->refs, unused); |
| 6712 | percpu_counter_sub(&tctx->inflight, unused); |
| 6713 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6714 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6715 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6716 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6717 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6718 | io_commit_sqring(ctx); |
| 6719 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6720 | return submitted; |
| 6721 | } |
| 6722 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6723 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6724 | { |
| 6725 | /* Tell userspace we may need a wakeup call */ |
| 6726 | spin_lock_irq(&ctx->completion_lock); |
| 6727 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6728 | spin_unlock_irq(&ctx->completion_lock); |
| 6729 | } |
| 6730 | |
| 6731 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6732 | { |
| 6733 | spin_lock_irq(&ctx->completion_lock); |
| 6734 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6735 | spin_unlock_irq(&ctx->completion_lock); |
| 6736 | } |
| 6737 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6738 | 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] | 6739 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6740 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6741 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6742 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6743 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6744 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6745 | if (cap_entries && to_submit > 8) |
| 6746 | to_submit = 8; |
| 6747 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6748 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6749 | unsigned nr_events = 0; |
| 6750 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6751 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6752 | if (!list_empty(&ctx->iopoll_list)) |
| 6753 | io_do_iopoll(ctx, &nr_events, 0); |
| 6754 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 6755 | /* |
| 6756 | * Don't submit if refs are dying, good for io_uring_register(), |
| 6757 | * but also it is relied upon by io_ring_exit_work() |
| 6758 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 6759 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 6760 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6761 | ret = io_submit_sqes(ctx, to_submit); |
| 6762 | mutex_unlock(&ctx->uring_lock); |
| 6763 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6764 | |
| 6765 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6766 | wake_up(&ctx->sqo_sq_wait); |
| 6767 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6768 | return ret; |
| 6769 | } |
| 6770 | |
| 6771 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 6772 | { |
| 6773 | struct io_ring_ctx *ctx; |
| 6774 | unsigned sq_thread_idle = 0; |
| 6775 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 6776 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6777 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6778 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6779 | } |
| 6780 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6781 | static int io_sq_thread(void *data) |
| 6782 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6783 | struct io_sq_data *sqd = data; |
| 6784 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 6785 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6786 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6787 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6788 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 6789 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6790 | set_task_comm(current, buf); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6791 | current->pf_io_worker = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6792 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6793 | if (sqd->sq_cpu != -1) |
| 6794 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 6795 | else |
| 6796 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 6797 | current->flags |= PF_NO_SETAFFINITY; |
| 6798 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6799 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6800 | while (!test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6801 | int ret; |
| 6802 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6803 | |
Jens Axboe | 82734c5 | 2021-03-29 06:52:44 -0600 | [diff] [blame] | 6804 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 6805 | signal_pending(current)) { |
| 6806 | bool did_sig = false; |
| 6807 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6808 | mutex_unlock(&sqd->lock); |
Jens Axboe | 82734c5 | 2021-03-29 06:52:44 -0600 | [diff] [blame] | 6809 | if (signal_pending(current)) { |
| 6810 | struct ksignal ksig; |
| 6811 | |
| 6812 | did_sig = get_signal(&ksig); |
| 6813 | } |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6814 | cond_resched(); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 6815 | mutex_lock(&sqd->lock); |
Jens Axboe | 82734c5 | 2021-03-29 06:52:44 -0600 | [diff] [blame] | 6816 | if (did_sig) |
| 6817 | break; |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 6818 | io_run_task_work(); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 6819 | io_run_task_work_head(&sqd->park_task_work); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6820 | timeout = jiffies + sqd->sq_thread_idle; |
Pavel Begunkov | 7d41e85 | 2021-03-10 13:13:54 +0000 | [diff] [blame] | 6821 | continue; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6822 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6823 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6824 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6825 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 6826 | const struct cred *creds = NULL; |
| 6827 | |
| 6828 | if (ctx->sq_creds != current_cred()) |
| 6829 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6830 | ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 6831 | if (creds) |
| 6832 | revert_creds(creds); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6833 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 6834 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6835 | } |
| 6836 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6837 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6838 | io_run_task_work(); |
| 6839 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6840 | if (sqt_spin) |
| 6841 | timeout = jiffies + sqd->sq_thread_idle; |
| 6842 | continue; |
| 6843 | } |
| 6844 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6845 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 6846 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6847 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6848 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6849 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 6850 | needs_sched = true; |
| 6851 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6852 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6853 | !list_empty_careful(&ctx->iopoll_list)) { |
| 6854 | needs_sched = false; |
| 6855 | break; |
| 6856 | } |
| 6857 | if (io_sqring_entries(ctx)) { |
| 6858 | needs_sched = false; |
| 6859 | break; |
| 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | if (needs_sched) { |
| 6864 | mutex_unlock(&sqd->lock); |
| 6865 | schedule(); |
| 6866 | mutex_lock(&sqd->lock); |
| 6867 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6868 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6869 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6870 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6871 | |
| 6872 | finish_wait(&sqd->wait, &wait); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 6873 | io_run_task_work_head(&sqd->park_task_work); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6874 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6875 | } |
| 6876 | |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 6877 | io_uring_cancel_sqpoll(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6878 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 6879 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 6880 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 6881 | io_run_task_work(); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 6882 | io_run_task_work_head(&sqd->park_task_work); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 6883 | mutex_unlock(&sqd->lock); |
| 6884 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6885 | complete(&sqd->exited); |
| 6886 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6887 | } |
| 6888 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6889 | struct io_wait_queue { |
| 6890 | struct wait_queue_entry wq; |
| 6891 | struct io_ring_ctx *ctx; |
| 6892 | unsigned to_wait; |
| 6893 | unsigned nr_timeouts; |
| 6894 | }; |
| 6895 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6896 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6897 | { |
| 6898 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6899 | |
| 6900 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6901 | * 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] | 6902 | * started waiting. For timeouts, we always want to return to userspace, |
| 6903 | * regardless of event count. |
| 6904 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6905 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6906 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6907 | } |
| 6908 | |
| 6909 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6910 | int wake_flags, void *key) |
| 6911 | { |
| 6912 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6913 | wq); |
| 6914 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6915 | /* |
| 6916 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 6917 | * the task, and the next invocation will do it. |
| 6918 | */ |
| 6919 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 6920 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6921 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6922 | } |
| 6923 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6924 | static int io_run_task_work_sig(void) |
| 6925 | { |
| 6926 | if (io_run_task_work()) |
| 6927 | return 1; |
| 6928 | if (!signal_pending(current)) |
| 6929 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 6930 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 6931 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6932 | return -EINTR; |
| 6933 | } |
| 6934 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6935 | /* when returns >0, the caller should retry */ |
| 6936 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 6937 | struct io_wait_queue *iowq, |
| 6938 | signed long *timeout) |
| 6939 | { |
| 6940 | int ret; |
| 6941 | |
| 6942 | /* make sure we run task_work before checking for signals */ |
| 6943 | ret = io_run_task_work_sig(); |
| 6944 | if (ret || io_should_wake(iowq)) |
| 6945 | return ret; |
| 6946 | /* let the caller flush overflows, retry */ |
| 6947 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 6948 | return 1; |
| 6949 | |
| 6950 | *timeout = schedule_timeout(*timeout); |
| 6951 | return !*timeout ? -ETIME : 1; |
| 6952 | } |
| 6953 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6954 | /* |
| 6955 | * Wait until events become available, if we don't already have some. The |
| 6956 | * application must reap them itself, as they reside on the shared cq ring. |
| 6957 | */ |
| 6958 | 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] | 6959 | const sigset_t __user *sig, size_t sigsz, |
| 6960 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6961 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6962 | struct io_wait_queue iowq = { |
| 6963 | .wq = { |
| 6964 | .private = current, |
| 6965 | .func = io_wake_function, |
| 6966 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6967 | }, |
| 6968 | .ctx = ctx, |
| 6969 | .to_wait = min_events, |
| 6970 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6971 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6972 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 6973 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6974 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6975 | do { |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 6976 | io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6977 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6978 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6979 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6980 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6981 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6982 | |
| 6983 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6984 | #ifdef CONFIG_COMPAT |
| 6985 | if (in_compat_syscall()) |
| 6986 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6987 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6988 | else |
| 6989 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6990 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6991 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6992 | if (ret) |
| 6993 | return ret; |
| 6994 | } |
| 6995 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6996 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6997 | struct timespec64 ts; |
| 6998 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6999 | if (get_timespec64(&ts, uts)) |
| 7000 | return -EFAULT; |
| 7001 | timeout = timespec64_to_jiffies(&ts); |
| 7002 | } |
| 7003 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7004 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7005 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7006 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7007 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 7008 | if (!io_cqring_overflow_flush(ctx, false)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7009 | ret = -EBUSY; |
| 7010 | break; |
| 7011 | } |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7012 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 7013 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7014 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 7015 | finish_wait(&ctx->wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7016 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7017 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7018 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7019 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7020 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7021 | 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] | 7022 | } |
| 7023 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7024 | 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] | 7025 | { |
| 7026 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); |
| 7027 | |
| 7028 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7029 | kfree(table->files[i]); |
| 7030 | kfree(table->files); |
| 7031 | table->files = NULL; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7032 | } |
| 7033 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7034 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7035 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7036 | spin_lock_bh(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7039 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7040 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7041 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 7042 | } |
| 7043 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7044 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7045 | { |
| 7046 | percpu_ref_exit(&ref_node->refs); |
| 7047 | kfree(ref_node); |
| 7048 | } |
| 7049 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7050 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7051 | struct io_rsrc_data *data_to_kill) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7052 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7053 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7054 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7055 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7056 | if (data_to_kill) { |
| 7057 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7058 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7059 | rsrc_node->rsrc_data = data_to_kill; |
| 7060 | io_rsrc_ref_lock(ctx); |
| 7061 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
| 7062 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7063 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7064 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7065 | percpu_ref_kill(&rsrc_node->refs); |
| 7066 | ctx->rsrc_node = NULL; |
| 7067 | } |
| 7068 | |
| 7069 | if (!ctx->rsrc_node) { |
| 7070 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7071 | ctx->rsrc_backup_node = NULL; |
| 7072 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7073 | } |
| 7074 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7075 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7076 | { |
| 7077 | if (ctx->rsrc_backup_node) |
| 7078 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7079 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7080 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7081 | } |
| 7082 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7083 | 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] | 7084 | { |
| 7085 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7086 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7087 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7088 | if (data->quiesce) |
| 7089 | return -ENXIO; |
| 7090 | |
| 7091 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7092 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7093 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7094 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7095 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7096 | io_rsrc_node_switch(ctx, data); |
| 7097 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7098 | /* kill initial ref, already quiesced if zero */ |
| 7099 | if (atomic_dec_and_test(&data->refs)) |
| 7100 | break; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7101 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7102 | ret = wait_for_completion_interruptible(&data->done); |
| 7103 | if (!ret) |
| 7104 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7105 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7106 | atomic_inc(&data->refs); |
| 7107 | /* wait for all works potentially completing data->done */ |
| 7108 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7109 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7110 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7111 | mutex_unlock(&ctx->uring_lock); |
| 7112 | ret = io_run_task_work_sig(); |
| 7113 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7114 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7115 | data->quiesce = false; |
| 7116 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7117 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7118 | } |
| 7119 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7120 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 7121 | { |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7122 | kvfree(data->tags); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7123 | kfree(data); |
| 7124 | } |
| 7125 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7126 | static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx, |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7127 | rsrc_put_fn *do_put, |
| 7128 | unsigned nr) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7129 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7130 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7131 | |
| 7132 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7133 | if (!data) |
| 7134 | return NULL; |
| 7135 | |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7136 | data->tags = kvcalloc(nr, sizeof(*data->tags), GFP_KERNEL); |
| 7137 | if (!data->tags) { |
| 7138 | kfree(data); |
| 7139 | return NULL; |
| 7140 | } |
| 7141 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7142 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7143 | data->ctx = ctx; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7144 | data->do_put = do_put; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7145 | init_completion(&data->done); |
| 7146 | return data; |
| 7147 | } |
| 7148 | |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 7149 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7150 | { |
| 7151 | #if defined(CONFIG_UNIX) |
| 7152 | if (ctx->ring_sock) { |
| 7153 | struct sock *sock = ctx->ring_sock->sk; |
| 7154 | struct sk_buff *skb; |
| 7155 | |
| 7156 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7157 | kfree_skb(skb); |
| 7158 | } |
| 7159 | #else |
| 7160 | int i; |
| 7161 | |
| 7162 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7163 | struct file *file; |
| 7164 | |
| 7165 | file = io_file_from_index(ctx, i); |
| 7166 | if (file) |
| 7167 | fput(file); |
| 7168 | } |
| 7169 | #endif |
| 7170 | io_free_file_tables(&ctx->file_table, ctx->nr_user_files); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7171 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 7172 | ctx->file_data = NULL; |
| 7173 | ctx->nr_user_files = 0; |
| 7174 | } |
| 7175 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7176 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7177 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7178 | int ret; |
| 7179 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7180 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7181 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7182 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 7183 | if (!ret) |
| 7184 | __io_sqe_files_unregister(ctx); |
| 7185 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7186 | } |
| 7187 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7188 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7189 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7190 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7191 | WARN_ON_ONCE(sqd->thread == current); |
| 7192 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7193 | /* |
| 7194 | * Do the dance but not conditional clear_bit() because it'd race with |
| 7195 | * other threads incrementing park_pending and setting the bit. |
| 7196 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7197 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7198 | if (atomic_dec_return(&sqd->park_pending)) |
| 7199 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7200 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7201 | } |
| 7202 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7203 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7204 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7205 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7206 | WARN_ON_ONCE(sqd->thread == current); |
| 7207 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7208 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7209 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7210 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7211 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7212 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7213 | } |
| 7214 | |
| 7215 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7216 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7217 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7218 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7219 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7220 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7221 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7222 | if (sqd->thread) |
| 7223 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7224 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7225 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7226 | } |
| 7227 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7228 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7229 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7230 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7231 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 7232 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7233 | io_sq_thread_stop(sqd); |
| 7234 | kfree(sqd); |
| 7235 | } |
| 7236 | } |
| 7237 | |
| 7238 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7239 | { |
| 7240 | struct io_sq_data *sqd = ctx->sq_data; |
| 7241 | |
| 7242 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7243 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7244 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7245 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7246 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7247 | |
| 7248 | io_put_sq_data(sqd); |
| 7249 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7250 | } |
| 7251 | } |
| 7252 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7253 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7254 | { |
| 7255 | struct io_ring_ctx *ctx_attach; |
| 7256 | struct io_sq_data *sqd; |
| 7257 | struct fd f; |
| 7258 | |
| 7259 | f = fdget(p->wq_fd); |
| 7260 | if (!f.file) |
| 7261 | return ERR_PTR(-ENXIO); |
| 7262 | if (f.file->f_op != &io_uring_fops) { |
| 7263 | fdput(f); |
| 7264 | return ERR_PTR(-EINVAL); |
| 7265 | } |
| 7266 | |
| 7267 | ctx_attach = f.file->private_data; |
| 7268 | sqd = ctx_attach->sq_data; |
| 7269 | if (!sqd) { |
| 7270 | fdput(f); |
| 7271 | return ERR_PTR(-EINVAL); |
| 7272 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7273 | if (sqd->task_tgid != current->tgid) { |
| 7274 | fdput(f); |
| 7275 | return ERR_PTR(-EPERM); |
| 7276 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7277 | |
| 7278 | refcount_inc(&sqd->refs); |
| 7279 | fdput(f); |
| 7280 | return sqd; |
| 7281 | } |
| 7282 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7283 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 7284 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7285 | { |
| 7286 | struct io_sq_data *sqd; |
| 7287 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7288 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7289 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 7290 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7291 | if (!IS_ERR(sqd)) { |
| 7292 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7293 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7294 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7295 | /* fall through for EPERM case, setup new sqd/task */ |
| 7296 | if (PTR_ERR(sqd) != -EPERM) |
| 7297 | return sqd; |
| 7298 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7299 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7300 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7301 | if (!sqd) |
| 7302 | return ERR_PTR(-ENOMEM); |
| 7303 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7304 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7305 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7306 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7307 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7308 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7309 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7310 | return sqd; |
| 7311 | } |
| 7312 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7313 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7314 | /* |
| 7315 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7316 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7317 | * loops in the file referencing. |
| 7318 | */ |
| 7319 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7320 | { |
| 7321 | struct sock *sk = ctx->ring_sock->sk; |
| 7322 | struct scm_fp_list *fpl; |
| 7323 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7324 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7325 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7326 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7327 | if (!fpl) |
| 7328 | return -ENOMEM; |
| 7329 | |
| 7330 | skb = alloc_skb(0, GFP_KERNEL); |
| 7331 | if (!skb) { |
| 7332 | kfree(fpl); |
| 7333 | return -ENOMEM; |
| 7334 | } |
| 7335 | |
| 7336 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7337 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7338 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7339 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7340 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7341 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7342 | |
| 7343 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7344 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7345 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7346 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7347 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7348 | } |
| 7349 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7350 | if (nr_files) { |
| 7351 | fpl->max = SCM_MAX_FD; |
| 7352 | fpl->count = nr_files; |
| 7353 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7354 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7355 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7356 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7357 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7358 | for (i = 0; i < nr_files; i++) |
| 7359 | fput(fpl->fp[i]); |
| 7360 | } else { |
| 7361 | kfree_skb(skb); |
| 7362 | kfree(fpl); |
| 7363 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7364 | |
| 7365 | return 0; |
| 7366 | } |
| 7367 | |
| 7368 | /* |
| 7369 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7370 | * causes regular reference counting to break down. We rely on the UNIX |
| 7371 | * garbage collection to take care of this problem for us. |
| 7372 | */ |
| 7373 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7374 | { |
| 7375 | unsigned left, total; |
| 7376 | int ret = 0; |
| 7377 | |
| 7378 | total = 0; |
| 7379 | left = ctx->nr_user_files; |
| 7380 | while (left) { |
| 7381 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7382 | |
| 7383 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7384 | if (ret) |
| 7385 | break; |
| 7386 | left -= this_files; |
| 7387 | total += this_files; |
| 7388 | } |
| 7389 | |
| 7390 | if (!ret) |
| 7391 | return 0; |
| 7392 | |
| 7393 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7394 | struct file *file = io_file_from_index(ctx, total); |
| 7395 | |
| 7396 | if (file) |
| 7397 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7398 | total++; |
| 7399 | } |
| 7400 | |
| 7401 | return ret; |
| 7402 | } |
| 7403 | #else |
| 7404 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7405 | { |
| 7406 | return 0; |
| 7407 | } |
| 7408 | #endif |
| 7409 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7410 | 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] | 7411 | { |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7412 | unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE); |
| 7413 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7414 | table->files = kcalloc(nr_tables, sizeof(*table->files), GFP_KERNEL); |
| 7415 | if (!table->files) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7416 | return false; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7417 | |
| 7418 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7419 | unsigned int this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7420 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7421 | table->files[i] = kcalloc(this_files, sizeof(*table->files[i]), |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7422 | GFP_KERNEL); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7423 | if (!table->files[i]) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7424 | break; |
| 7425 | nr_files -= this_files; |
| 7426 | } |
| 7427 | |
| 7428 | if (i == nr_tables) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7429 | return true; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7430 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7431 | io_free_file_tables(table, nr_tables * IORING_MAX_FILES_TABLE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7432 | return false; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7433 | } |
| 7434 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 7435 | 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] | 7436 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7437 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7438 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7439 | struct sock *sock = ctx->ring_sock->sk; |
| 7440 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7441 | struct sk_buff *skb; |
| 7442 | int i; |
| 7443 | |
| 7444 | __skb_queue_head_init(&list); |
| 7445 | |
| 7446 | /* |
| 7447 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7448 | * remove this entry and rearrange the file array. |
| 7449 | */ |
| 7450 | skb = skb_dequeue(head); |
| 7451 | while (skb) { |
| 7452 | struct scm_fp_list *fp; |
| 7453 | |
| 7454 | fp = UNIXCB(skb).fp; |
| 7455 | for (i = 0; i < fp->count; i++) { |
| 7456 | int left; |
| 7457 | |
| 7458 | if (fp->fp[i] != file) |
| 7459 | continue; |
| 7460 | |
| 7461 | unix_notinflight(fp->user, fp->fp[i]); |
| 7462 | left = fp->count - 1 - i; |
| 7463 | if (left) { |
| 7464 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7465 | left * sizeof(struct file *)); |
| 7466 | } |
| 7467 | fp->count--; |
| 7468 | if (!fp->count) { |
| 7469 | kfree_skb(skb); |
| 7470 | skb = NULL; |
| 7471 | } else { |
| 7472 | __skb_queue_tail(&list, skb); |
| 7473 | } |
| 7474 | fput(file); |
| 7475 | file = NULL; |
| 7476 | break; |
| 7477 | } |
| 7478 | |
| 7479 | if (!file) |
| 7480 | break; |
| 7481 | |
| 7482 | __skb_queue_tail(&list, skb); |
| 7483 | |
| 7484 | skb = skb_dequeue(head); |
| 7485 | } |
| 7486 | |
| 7487 | if (skb_peek(&list)) { |
| 7488 | spin_lock_irq(&head->lock); |
| 7489 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7490 | __skb_queue_tail(head, skb); |
| 7491 | spin_unlock_irq(&head->lock); |
| 7492 | } |
| 7493 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7494 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7495 | #endif |
| 7496 | } |
| 7497 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7498 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7499 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7500 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7501 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7502 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7503 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7504 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7505 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7506 | |
| 7507 | if (prsrc->tag) { |
| 7508 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
| 7509 | unsigned long flags; |
| 7510 | |
| 7511 | io_ring_submit_lock(ctx, lock_ring); |
| 7512 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 7513 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
| 7514 | io_commit_cqring(ctx); |
| 7515 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 7516 | io_cqring_ev_posted(ctx); |
| 7517 | io_ring_submit_unlock(ctx, lock_ring); |
| 7518 | } |
| 7519 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 7520 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7521 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7522 | } |
| 7523 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7524 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7525 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 7526 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7527 | } |
| 7528 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7529 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7530 | { |
| 7531 | struct io_ring_ctx *ctx; |
| 7532 | struct llist_node *node; |
| 7533 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7534 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7535 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7536 | |
| 7537 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7538 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7539 | struct llist_node *next = node->next; |
| 7540 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7541 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7542 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7543 | node = next; |
| 7544 | } |
| 7545 | } |
| 7546 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7547 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7548 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7549 | 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] | 7550 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7551 | bool first_add = false; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7552 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7553 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7554 | node->done = true; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7555 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7556 | while (!list_empty(&ctx->rsrc_ref_list)) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7557 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7558 | struct io_rsrc_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7559 | /* recycle ref nodes in order */ |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7560 | if (!node->done) |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7561 | break; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7562 | list_del(&node->node); |
| 7563 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7564 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7565 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7566 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7567 | if (first_add) |
| 7568 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7569 | } |
| 7570 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7571 | 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] | 7572 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7573 | struct io_rsrc_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7574 | |
| 7575 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7576 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7577 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7578 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7579 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7580 | 0, GFP_KERNEL)) { |
| 7581 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7582 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7583 | } |
| 7584 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7585 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7586 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7587 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7588 | } |
| 7589 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7590 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7591 | unsigned nr_args) |
| 7592 | { |
| 7593 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7594 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 7595 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7596 | unsigned i; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7597 | struct io_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7598 | |
| 7599 | if (ctx->file_data) |
| 7600 | return -EBUSY; |
| 7601 | if (!nr_args) |
| 7602 | return -EINVAL; |
| 7603 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7604 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7605 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 7606 | if (ret) |
| 7607 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7608 | |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7609 | file_data = io_rsrc_data_alloc(ctx, io_rsrc_file_put, nr_args); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7610 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7611 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7612 | ctx->file_data = file_data; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 7613 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7614 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7615 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7616 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7617 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7618 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7619 | ret = -EFAULT; |
| 7620 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7621 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7622 | /* allow sparse sets */ |
| 7623 | if (fd == -1) |
| 7624 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7625 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7626 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7627 | ret = -EBADF; |
| 7628 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7629 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7630 | |
| 7631 | /* |
| 7632 | * Don't allow io_uring instances to be registered. If UNIX |
| 7633 | * isn't enabled, then this causes a reference cycle and this |
| 7634 | * instance can never get freed. If UNIX is enabled we'll |
| 7635 | * handle it just fine, but there's still no point in allowing |
| 7636 | * a ring fd as it doesn't support regular read/write anyway. |
| 7637 | */ |
| 7638 | if (file->f_op == &io_uring_fops) { |
| 7639 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7640 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7641 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7642 | 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] | 7643 | } |
| 7644 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7645 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7646 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7647 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7648 | return ret; |
| 7649 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7650 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7651 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7652 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7653 | out_fput: |
| 7654 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7655 | file = io_file_from_index(ctx, i); |
| 7656 | if (file) |
| 7657 | fput(file); |
| 7658 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7659 | io_free_file_tables(&ctx->file_table, nr_args); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7660 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7661 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7662 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7663 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7664 | return ret; |
| 7665 | } |
| 7666 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7667 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7668 | int index) |
| 7669 | { |
| 7670 | #if defined(CONFIG_UNIX) |
| 7671 | struct sock *sock = ctx->ring_sock->sk; |
| 7672 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7673 | struct sk_buff *skb; |
| 7674 | |
| 7675 | /* |
| 7676 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7677 | * file set. If there's no room, fall back to allocating a new skb |
| 7678 | * and filling it in. |
| 7679 | */ |
| 7680 | spin_lock_irq(&head->lock); |
| 7681 | skb = skb_peek(head); |
| 7682 | if (skb) { |
| 7683 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7684 | |
| 7685 | if (fpl->count < SCM_MAX_FD) { |
| 7686 | __skb_unlink(skb, head); |
| 7687 | spin_unlock_irq(&head->lock); |
| 7688 | fpl->fp[fpl->count] = get_file(file); |
| 7689 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7690 | fpl->count++; |
| 7691 | spin_lock_irq(&head->lock); |
| 7692 | __skb_queue_head(head, skb); |
| 7693 | } else { |
| 7694 | skb = NULL; |
| 7695 | } |
| 7696 | } |
| 7697 | spin_unlock_irq(&head->lock); |
| 7698 | |
| 7699 | if (skb) { |
| 7700 | fput(file); |
| 7701 | return 0; |
| 7702 | } |
| 7703 | |
| 7704 | return __io_sqe_files_scm(ctx, 1, index); |
| 7705 | #else |
| 7706 | return 0; |
| 7707 | #endif |
| 7708 | } |
| 7709 | |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7710 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
Pavel Begunkov | e7c7837 | 2021-04-01 15:43:45 +0100 | [diff] [blame] | 7711 | struct io_rsrc_node *node, void *rsrc) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7712 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7713 | struct io_rsrc_put *prsrc; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7714 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7715 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7716 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7717 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7718 | |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7719 | prsrc->tag = data->tags[idx]; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7720 | prsrc->rsrc = rsrc; |
Pavel Begunkov | e7c7837 | 2021-04-01 15:43:45 +0100 | [diff] [blame] | 7721 | list_add(&prsrc->list, &node->rsrc_list); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7722 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7726 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7727 | unsigned nr_args) |
| 7728 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7729 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7730 | struct io_fixed_file *file_slot; |
| 7731 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7732 | __s32 __user *fds; |
| 7733 | int fd, i, err; |
| 7734 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7735 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7736 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7737 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7738 | return -EOVERFLOW; |
| 7739 | if (done > ctx->nr_user_files) |
| 7740 | return -EINVAL; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7741 | err = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7742 | if (err) |
| 7743 | return err; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7744 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7745 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7746 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7747 | err = 0; |
| 7748 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7749 | err = -EFAULT; |
| 7750 | break; |
| 7751 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 7752 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 7753 | continue; |
| 7754 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7755 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 7756 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7757 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7758 | if (file_slot->file_ptr) { |
| 7759 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame^] | 7760 | err = io_queue_rsrc_removal(data, up->offset + done, |
| 7761 | ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7762 | if (err) |
| 7763 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7764 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7765 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7766 | } |
| 7767 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7768 | file = fget(fd); |
| 7769 | if (!file) { |
| 7770 | err = -EBADF; |
| 7771 | break; |
| 7772 | } |
| 7773 | /* |
| 7774 | * Don't allow io_uring instances to be registered. If |
| 7775 | * UNIX isn't enabled, then this causes a reference |
| 7776 | * cycle and this instance can never get freed. If UNIX |
| 7777 | * is enabled we'll handle it just fine, but there's |
| 7778 | * still no point in allowing a ring fd as it doesn't |
| 7779 | * support regular read/write anyway. |
| 7780 | */ |
| 7781 | if (file->f_op == &io_uring_fops) { |
| 7782 | fput(file); |
| 7783 | err = -EBADF; |
| 7784 | break; |
| 7785 | } |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 7786 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7787 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7788 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 7789 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7790 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7791 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7792 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7793 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7794 | } |
| 7795 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7796 | if (needs_switch) |
| 7797 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7798 | return done ? done : err; |
| 7799 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7800 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7801 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7802 | unsigned nr_args) |
| 7803 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7804 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7805 | |
| 7806 | if (!ctx->file_data) |
| 7807 | return -ENXIO; |
| 7808 | if (!nr_args) |
| 7809 | return -EINVAL; |
| 7810 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7811 | return -EFAULT; |
| 7812 | if (up.resv) |
| 7813 | return -EINVAL; |
| 7814 | |
| 7815 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7816 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7817 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7818 | 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] | 7819 | { |
| 7820 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7821 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7822 | req = io_put_req_find_next(req); |
| 7823 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7824 | } |
| 7825 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 7826 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 7827 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7828 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7829 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7830 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7831 | unsigned int concurrency; |
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 | hash = ctx->hash_map; |
| 7834 | if (!hash) { |
| 7835 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 7836 | if (!hash) |
| 7837 | return ERR_PTR(-ENOMEM); |
| 7838 | refcount_set(&hash->refs, 1); |
| 7839 | init_waitqueue_head(&hash->wait); |
| 7840 | ctx->hash_map = hash; |
| 7841 | } |
| 7842 | |
| 7843 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 7844 | data.task = task; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7845 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7846 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7847 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7848 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7849 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7850 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7851 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7852 | } |
| 7853 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7854 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 7855 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7856 | { |
| 7857 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7858 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7859 | |
| 7860 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 7861 | if (unlikely(!tctx)) |
| 7862 | return -ENOMEM; |
| 7863 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7864 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 7865 | if (unlikely(ret)) { |
| 7866 | kfree(tctx); |
| 7867 | return ret; |
| 7868 | } |
| 7869 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 7870 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7871 | if (IS_ERR(tctx->io_wq)) { |
| 7872 | ret = PTR_ERR(tctx->io_wq); |
| 7873 | percpu_counter_destroy(&tctx->inflight); |
| 7874 | kfree(tctx); |
| 7875 | return ret; |
| 7876 | } |
| 7877 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7878 | xa_init(&tctx->xa); |
| 7879 | init_waitqueue_head(&tctx->wait); |
| 7880 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 7881 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 7882 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7883 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 7884 | spin_lock_init(&tctx->task_lock); |
| 7885 | INIT_WQ_LIST(&tctx->task_list); |
| 7886 | tctx->task_state = 0; |
| 7887 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7888 | return 0; |
| 7889 | } |
| 7890 | |
| 7891 | void __io_uring_free(struct task_struct *tsk) |
| 7892 | { |
| 7893 | struct io_uring_task *tctx = tsk->io_uring; |
| 7894 | |
| 7895 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 7896 | WARN_ON_ONCE(tctx->io_wq); |
| 7897 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7898 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7899 | kfree(tctx); |
| 7900 | tsk->io_uring = NULL; |
| 7901 | } |
| 7902 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7903 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 7904 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7905 | { |
| 7906 | int ret; |
| 7907 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7908 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 7909 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 7910 | IORING_SETUP_ATTACH_WQ) { |
| 7911 | struct fd f; |
| 7912 | |
| 7913 | f = fdget(p->wq_fd); |
| 7914 | if (!f.file) |
| 7915 | return -ENXIO; |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7916 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 7917 | if (f.file->f_op != &io_uring_fops) |
| 7918 | return -EINVAL; |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7919 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7920 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7921 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7922 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7923 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7924 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7925 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7926 | if (IS_ERR(sqd)) { |
| 7927 | ret = PTR_ERR(sqd); |
| 7928 | goto err; |
| 7929 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7930 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7931 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7932 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7933 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7934 | if (!ctx->sq_thread_idle) |
| 7935 | ctx->sq_thread_idle = HZ; |
| 7936 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 7937 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 7938 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 7939 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 7940 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 7941 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 7942 | io_sq_thread_unpark(sqd); |
| 7943 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 7944 | if (ret < 0) |
| 7945 | goto err; |
| 7946 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7947 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7948 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7949 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7950 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7951 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7952 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 7953 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7954 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7955 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7956 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7957 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7958 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7959 | |
| 7960 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 7961 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7962 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 7963 | if (IS_ERR(tsk)) { |
| 7964 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7965 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7966 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 7967 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7968 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 7969 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 7970 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7971 | if (ret) |
| 7972 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7973 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7974 | /* Can't have SQ_AFF without SQPOLL */ |
| 7975 | ret = -EINVAL; |
| 7976 | goto err; |
| 7977 | } |
| 7978 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7979 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 7980 | err_sqpoll: |
| 7981 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7982 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7983 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7984 | return ret; |
| 7985 | } |
| 7986 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7987 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7988 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7989 | { |
| 7990 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7991 | } |
| 7992 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7993 | static inline int __io_account_mem(struct user_struct *user, |
| 7994 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7995 | { |
| 7996 | unsigned long page_limit, cur_pages, new_pages; |
| 7997 | |
| 7998 | /* Don't allow more pages than we can safely lock */ |
| 7999 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8000 | |
| 8001 | do { |
| 8002 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8003 | new_pages = cur_pages + nr_pages; |
| 8004 | if (new_pages > page_limit) |
| 8005 | return -ENOMEM; |
| 8006 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8007 | new_pages) != cur_pages); |
| 8008 | |
| 8009 | return 0; |
| 8010 | } |
| 8011 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8012 | 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] | 8013 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8014 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8015 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8016 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8017 | if (ctx->mm_account) |
| 8018 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8019 | } |
| 8020 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8021 | 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] | 8022 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8023 | int ret; |
| 8024 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8025 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8026 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8027 | if (ret) |
| 8028 | return ret; |
| 8029 | } |
| 8030 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8031 | if (ctx->mm_account) |
| 8032 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8033 | |
| 8034 | return 0; |
| 8035 | } |
| 8036 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8037 | static void io_mem_free(void *ptr) |
| 8038 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8039 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8040 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8041 | if (!ptr) |
| 8042 | return; |
| 8043 | |
| 8044 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8045 | if (put_page_testzero(page)) |
| 8046 | free_compound_page(page); |
| 8047 | } |
| 8048 | |
| 8049 | static void *io_mem_alloc(size_t size) |
| 8050 | { |
| 8051 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8052 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8053 | |
| 8054 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8055 | } |
| 8056 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8057 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8058 | size_t *sq_offset) |
| 8059 | { |
| 8060 | struct io_rings *rings; |
| 8061 | size_t off, sq_array_size; |
| 8062 | |
| 8063 | off = struct_size(rings, cqes, cq_entries); |
| 8064 | if (off == SIZE_MAX) |
| 8065 | return SIZE_MAX; |
| 8066 | |
| 8067 | #ifdef CONFIG_SMP |
| 8068 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8069 | if (off == 0) |
| 8070 | return SIZE_MAX; |
| 8071 | #endif |
| 8072 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8073 | if (sq_offset) |
| 8074 | *sq_offset = off; |
| 8075 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8076 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8077 | if (sq_array_size == SIZE_MAX) |
| 8078 | return SIZE_MAX; |
| 8079 | |
| 8080 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8081 | return SIZE_MAX; |
| 8082 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8083 | return off; |
| 8084 | } |
| 8085 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8086 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf *imu) |
| 8087 | { |
| 8088 | unsigned int i; |
| 8089 | |
| 8090 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8091 | unpin_user_page(imu->bvec[i].bv_page); |
| 8092 | if (imu->acct_pages) |
| 8093 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8094 | kvfree(imu->bvec); |
| 8095 | imu->nr_bvecs = 0; |
| 8096 | } |
| 8097 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8098 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8099 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8100 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8101 | |
| 8102 | if (!ctx->user_bufs) |
| 8103 | return -ENXIO; |
| 8104 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8105 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8106 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8107 | kfree(ctx->user_bufs); |
| 8108 | ctx->user_bufs = NULL; |
| 8109 | ctx->nr_user_bufs = 0; |
| 8110 | return 0; |
| 8111 | } |
| 8112 | |
| 8113 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8114 | void __user *arg, unsigned index) |
| 8115 | { |
| 8116 | struct iovec __user *src; |
| 8117 | |
| 8118 | #ifdef CONFIG_COMPAT |
| 8119 | if (ctx->compat) { |
| 8120 | struct compat_iovec __user *ciovs; |
| 8121 | struct compat_iovec ciov; |
| 8122 | |
| 8123 | ciovs = (struct compat_iovec __user *) arg; |
| 8124 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8125 | return -EFAULT; |
| 8126 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8127 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8128 | dst->iov_len = ciov.iov_len; |
| 8129 | return 0; |
| 8130 | } |
| 8131 | #endif |
| 8132 | src = (struct iovec __user *) arg; |
| 8133 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8134 | return -EFAULT; |
| 8135 | return 0; |
| 8136 | } |
| 8137 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8138 | /* |
| 8139 | * Not super efficient, but this is just a registration time. And we do cache |
| 8140 | * the last compound head, so generally we'll only do a full search if we don't |
| 8141 | * match that one. |
| 8142 | * |
| 8143 | * We check if the given compound head page has already been accounted, to |
| 8144 | * avoid double accounting it. This allows us to account the full size of the |
| 8145 | * page, not just the constituent pages of a huge page. |
| 8146 | */ |
| 8147 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8148 | int nr_pages, struct page *hpage) |
| 8149 | { |
| 8150 | int i, j; |
| 8151 | |
| 8152 | /* check current page array */ |
| 8153 | for (i = 0; i < nr_pages; i++) { |
| 8154 | if (!PageCompound(pages[i])) |
| 8155 | continue; |
| 8156 | if (compound_head(pages[i]) == hpage) |
| 8157 | return true; |
| 8158 | } |
| 8159 | |
| 8160 | /* check previously registered pages */ |
| 8161 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8162 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8163 | |
| 8164 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8165 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8166 | continue; |
| 8167 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8168 | return true; |
| 8169 | } |
| 8170 | } |
| 8171 | |
| 8172 | return false; |
| 8173 | } |
| 8174 | |
| 8175 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8176 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8177 | struct page **last_hpage) |
| 8178 | { |
| 8179 | int i, ret; |
| 8180 | |
| 8181 | for (i = 0; i < nr_pages; i++) { |
| 8182 | if (!PageCompound(pages[i])) { |
| 8183 | imu->acct_pages++; |
| 8184 | } else { |
| 8185 | struct page *hpage; |
| 8186 | |
| 8187 | hpage = compound_head(pages[i]); |
| 8188 | if (hpage == *last_hpage) |
| 8189 | continue; |
| 8190 | *last_hpage = hpage; |
| 8191 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8192 | continue; |
| 8193 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8194 | } |
| 8195 | } |
| 8196 | |
| 8197 | if (!imu->acct_pages) |
| 8198 | return 0; |
| 8199 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8200 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8201 | if (ret) |
| 8202 | imu->acct_pages = 0; |
| 8203 | return ret; |
| 8204 | } |
| 8205 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8206 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8207 | struct io_mapped_ubuf *imu, |
| 8208 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8209 | { |
| 8210 | struct vm_area_struct **vmas = NULL; |
| 8211 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8212 | unsigned long off, start, end, ubuf; |
| 8213 | size_t size; |
| 8214 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8215 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8216 | ubuf = (unsigned long) iov->iov_base; |
| 8217 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8218 | start = ubuf >> PAGE_SHIFT; |
| 8219 | nr_pages = end - start; |
| 8220 | |
| 8221 | ret = -ENOMEM; |
| 8222 | |
| 8223 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8224 | if (!pages) |
| 8225 | goto done; |
| 8226 | |
| 8227 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8228 | GFP_KERNEL); |
| 8229 | if (!vmas) |
| 8230 | goto done; |
| 8231 | |
| 8232 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8233 | GFP_KERNEL); |
| 8234 | if (!imu->bvec) |
| 8235 | goto done; |
| 8236 | |
| 8237 | ret = 0; |
| 8238 | mmap_read_lock(current->mm); |
| 8239 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8240 | pages, vmas); |
| 8241 | if (pret == nr_pages) { |
| 8242 | /* don't support file backed memory */ |
| 8243 | for (i = 0; i < nr_pages; i++) { |
| 8244 | struct vm_area_struct *vma = vmas[i]; |
| 8245 | |
| 8246 | if (vma->vm_file && |
| 8247 | !is_file_hugepages(vma->vm_file)) { |
| 8248 | ret = -EOPNOTSUPP; |
| 8249 | break; |
| 8250 | } |
| 8251 | } |
| 8252 | } else { |
| 8253 | ret = pret < 0 ? pret : -EFAULT; |
| 8254 | } |
| 8255 | mmap_read_unlock(current->mm); |
| 8256 | if (ret) { |
| 8257 | /* |
| 8258 | * if we did partial map, or found file backed vmas, |
| 8259 | * release any pages we did get |
| 8260 | */ |
| 8261 | if (pret > 0) |
| 8262 | unpin_user_pages(pages, pret); |
| 8263 | kvfree(imu->bvec); |
| 8264 | goto done; |
| 8265 | } |
| 8266 | |
| 8267 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8268 | if (ret) { |
| 8269 | unpin_user_pages(pages, pret); |
| 8270 | kvfree(imu->bvec); |
| 8271 | goto done; |
| 8272 | } |
| 8273 | |
| 8274 | off = ubuf & ~PAGE_MASK; |
| 8275 | size = iov->iov_len; |
| 8276 | for (i = 0; i < nr_pages; i++) { |
| 8277 | size_t vec_len; |
| 8278 | |
| 8279 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8280 | imu->bvec[i].bv_page = pages[i]; |
| 8281 | imu->bvec[i].bv_len = vec_len; |
| 8282 | imu->bvec[i].bv_offset = off; |
| 8283 | off = 0; |
| 8284 | size -= vec_len; |
| 8285 | } |
| 8286 | /* store original address for later verification */ |
| 8287 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 8288 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8289 | imu->nr_bvecs = nr_pages; |
| 8290 | ret = 0; |
| 8291 | done: |
| 8292 | kvfree(pages); |
| 8293 | kvfree(vmas); |
| 8294 | return ret; |
| 8295 | } |
| 8296 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8297 | 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] | 8298 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8299 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 8300 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8301 | } |
| 8302 | |
| 8303 | static int io_buffer_validate(struct iovec *iov) |
| 8304 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 8305 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 8306 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8307 | /* |
| 8308 | * Don't impose further limits on the size and buffer |
| 8309 | * constraints here, we'll -EINVAL later when IO is |
| 8310 | * submitted if they are wrong. |
| 8311 | */ |
| 8312 | if (!iov->iov_base || !iov->iov_len) |
| 8313 | return -EFAULT; |
| 8314 | |
| 8315 | /* arbitrary limit, but we need something */ |
| 8316 | if (iov->iov_len > SZ_1G) |
| 8317 | return -EFAULT; |
| 8318 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 8319 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 8320 | return -EOVERFLOW; |
| 8321 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8322 | return 0; |
| 8323 | } |
| 8324 | |
| 8325 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8326 | unsigned int nr_args) |
| 8327 | { |
| 8328 | int i, ret; |
| 8329 | struct iovec iov; |
| 8330 | struct page *last_hpage = NULL; |
| 8331 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8332 | if (ctx->user_bufs) |
| 8333 | return -EBUSY; |
| 8334 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8335 | return -EINVAL; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8336 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8337 | if (ret) |
| 8338 | return ret; |
| 8339 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 8340 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8341 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8342 | |
| 8343 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8344 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8345 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8346 | ret = io_buffer_validate(&iov); |
| 8347 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8348 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8349 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8350 | if (ret) |
| 8351 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8352 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8353 | |
| 8354 | if (ret) |
| 8355 | io_sqe_buffers_unregister(ctx); |
| 8356 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8357 | return ret; |
| 8358 | } |
| 8359 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8360 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8361 | { |
| 8362 | __s32 __user *fds = arg; |
| 8363 | int fd; |
| 8364 | |
| 8365 | if (ctx->cq_ev_fd) |
| 8366 | return -EBUSY; |
| 8367 | |
| 8368 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8369 | return -EFAULT; |
| 8370 | |
| 8371 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8372 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8373 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8374 | ctx->cq_ev_fd = NULL; |
| 8375 | return ret; |
| 8376 | } |
| 8377 | |
| 8378 | return 0; |
| 8379 | } |
| 8380 | |
| 8381 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8382 | { |
| 8383 | if (ctx->cq_ev_fd) { |
| 8384 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8385 | ctx->cq_ev_fd = NULL; |
| 8386 | return 0; |
| 8387 | } |
| 8388 | |
| 8389 | return -ENXIO; |
| 8390 | } |
| 8391 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8392 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8393 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 8394 | struct io_buffer *buf; |
| 8395 | unsigned long index; |
| 8396 | |
| 8397 | xa_for_each(&ctx->io_buffers, index, buf) |
| 8398 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8399 | } |
| 8400 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8401 | 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] | 8402 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8403 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8404 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8405 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8406 | if (tsk && req->task != tsk) |
| 8407 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8408 | list_del(&req->compl.list); |
| 8409 | kmem_cache_free(req_cachep, req); |
| 8410 | } |
| 8411 | } |
| 8412 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8413 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8414 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8415 | struct io_submit_state *submit_state = &ctx->submit_state; |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8416 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8417 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8418 | mutex_lock(&ctx->uring_lock); |
| 8419 | |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8420 | if (submit_state->free_reqs) { |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8421 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8422 | submit_state->reqs); |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8423 | submit_state->free_reqs = 0; |
| 8424 | } |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8425 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 8426 | io_flush_cached_locked_reqs(ctx, cs); |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8427 | io_req_cache_free(&cs->free_list, NULL); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8428 | mutex_unlock(&ctx->uring_lock); |
| 8429 | } |
| 8430 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8431 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8432 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8433 | io_sq_thread_finish(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8434 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8435 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8436 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8437 | mmdrop(ctx->mm_account); |
| 8438 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8439 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8440 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8441 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8442 | if (ctx->file_data) { |
| 8443 | if (!atomic_dec_and_test(&ctx->file_data->refs)) |
| 8444 | wait_for_completion(&ctx->file_data->done); |
| 8445 | __io_sqe_files_unregister(ctx); |
| 8446 | } |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 8447 | if (ctx->rings) |
| 8448 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8449 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8450 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8451 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 8452 | if (ctx->sq_creds) |
| 8453 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8454 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8455 | /* there are no registered resources left, nobody uses it */ |
| 8456 | if (ctx->rsrc_node) |
| 8457 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8458 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8459 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8460 | flush_delayed_work(&ctx->rsrc_put_work); |
| 8461 | |
| 8462 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 8463 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 8464 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8465 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8466 | if (ctx->ring_sock) { |
| 8467 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8468 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8469 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8470 | #endif |
| 8471 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8472 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8473 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8474 | |
| 8475 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8476 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8477 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8478 | if (ctx->hash_map) |
| 8479 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8480 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8481 | kfree(ctx); |
| 8482 | } |
| 8483 | |
| 8484 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8485 | { |
| 8486 | struct io_ring_ctx *ctx = file->private_data; |
| 8487 | __poll_t mask = 0; |
| 8488 | |
| 8489 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8490 | /* |
| 8491 | * synchronizes with barrier from wq_has_sleeper call in |
| 8492 | * io_commit_cqring |
| 8493 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8494 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8495 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8496 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8497 | |
| 8498 | /* |
| 8499 | * Don't flush cqring overflow list here, just do a simple check. |
| 8500 | * Otherwise there could possible be ABBA deadlock: |
| 8501 | * CPU0 CPU1 |
| 8502 | * ---- ---- |
| 8503 | * lock(&ctx->uring_lock); |
| 8504 | * lock(&ep->mtx); |
| 8505 | * lock(&ctx->uring_lock); |
| 8506 | * lock(&ep->mtx); |
| 8507 | * |
| 8508 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8509 | * pushs them to do the flush. |
| 8510 | */ |
| 8511 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8512 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8513 | |
| 8514 | return mask; |
| 8515 | } |
| 8516 | |
| 8517 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8518 | { |
| 8519 | struct io_ring_ctx *ctx = file->private_data; |
| 8520 | |
| 8521 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8522 | } |
| 8523 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8524 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8525 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8526 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8527 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 8528 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8529 | if (creds) { |
| 8530 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8531 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8532 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8533 | |
| 8534 | return -EINVAL; |
| 8535 | } |
| 8536 | |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 8537 | static inline bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8538 | { |
Pavel Begunkov | 9b46571 | 2021-03-15 14:23:07 +0000 | [diff] [blame] | 8539 | return io_run_task_work_head(&ctx->exit_task_work); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8540 | } |
| 8541 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8542 | struct io_tctx_exit { |
| 8543 | struct callback_head task_work; |
| 8544 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8545 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8546 | }; |
| 8547 | |
| 8548 | static void io_tctx_exit_cb(struct callback_head *cb) |
| 8549 | { |
| 8550 | struct io_uring_task *tctx = current->io_uring; |
| 8551 | struct io_tctx_exit *work; |
| 8552 | |
| 8553 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 8554 | /* |
| 8555 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 8556 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 8557 | */ |
| 8558 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8559 | io_uring_del_task_file((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8560 | complete(&work->completion); |
| 8561 | } |
| 8562 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8563 | static void io_ring_exit_work(struct work_struct *work) |
| 8564 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8565 | 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] | 8566 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8567 | struct io_tctx_exit exit; |
| 8568 | struct io_tctx_node *node; |
| 8569 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8570 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8571 | /* |
| 8572 | * If we're doing polled IO and end up having requests being |
| 8573 | * submitted async (out-of-line), then completions can come in while |
| 8574 | * we're waiting for refs to drop. We need to reap these manually, |
| 8575 | * as nobody else will be looking for them. |
| 8576 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8577 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8578 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 8579 | |
| 8580 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8581 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8582 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 8583 | init_completion(&exit.completion); |
| 8584 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 8585 | exit.ctx = ctx; |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 8586 | /* |
| 8587 | * Some may use context even when all refs and requests have been put, |
| 8588 | * and they are free to do so while still holding uring_lock or |
| 8589 | * completion_lock, see __io_req_task_submit(). Apart from other work, |
| 8590 | * this lock/unlock section also waits them to finish. |
| 8591 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8592 | mutex_lock(&ctx->uring_lock); |
| 8593 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 8594 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 8595 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8596 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 8597 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 8598 | /* don't spin on a single task if cancellation failed */ |
| 8599 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8600 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 8601 | if (WARN_ON_ONCE(ret)) |
| 8602 | continue; |
| 8603 | wake_up_process(node->task); |
| 8604 | |
| 8605 | mutex_unlock(&ctx->uring_lock); |
| 8606 | wait_for_completion(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8607 | mutex_lock(&ctx->uring_lock); |
| 8608 | } |
| 8609 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 8610 | spin_lock_irq(&ctx->completion_lock); |
| 8611 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 8612 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8613 | io_ring_ctx_free(ctx); |
| 8614 | } |
| 8615 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 8616 | /* Returns true if we found and killed one or more timeouts */ |
| 8617 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 8618 | struct files_struct *files) |
| 8619 | { |
| 8620 | struct io_kiocb *req, *tmp; |
| 8621 | int canceled = 0; |
| 8622 | |
| 8623 | spin_lock_irq(&ctx->completion_lock); |
| 8624 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
| 8625 | if (io_match_task(req, tsk, files)) { |
| 8626 | io_kill_timeout(req, -ECANCELED); |
| 8627 | canceled++; |
| 8628 | } |
| 8629 | } |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 8630 | if (canceled != 0) |
| 8631 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 8632 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 8633 | if (canceled != 0) |
| 8634 | io_cqring_ev_posted(ctx); |
| 8635 | return canceled != 0; |
| 8636 | } |
| 8637 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8638 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8639 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 8640 | unsigned long index; |
| 8641 | struct creds *creds; |
| 8642 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8643 | mutex_lock(&ctx->uring_lock); |
| 8644 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8645 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 8646 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 8647 | xa_for_each(&ctx->personalities, index, creds) |
| 8648 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8649 | mutex_unlock(&ctx->uring_lock); |
| 8650 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8651 | io_kill_timeouts(ctx, NULL, NULL); |
| 8652 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8653 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8654 | /* 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] | 8655 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8656 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8657 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8658 | /* |
| 8659 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8660 | * if we're exiting a ton of rings at the same time. It just adds |
| 8661 | * noise and overhead, there's no discernable change in runtime |
| 8662 | * over using system_wq. |
| 8663 | */ |
| 8664 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8665 | } |
| 8666 | |
| 8667 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8668 | { |
| 8669 | struct io_ring_ctx *ctx = file->private_data; |
| 8670 | |
| 8671 | file->private_data = NULL; |
| 8672 | io_ring_ctx_wait_and_kill(ctx); |
| 8673 | return 0; |
| 8674 | } |
| 8675 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8676 | struct io_task_cancel { |
| 8677 | struct task_struct *task; |
| 8678 | struct files_struct *files; |
| 8679 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8680 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8681 | 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] | 8682 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8683 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8684 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8685 | bool ret; |
| 8686 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8687 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8688 | unsigned long flags; |
| 8689 | struct io_ring_ctx *ctx = req->ctx; |
| 8690 | |
| 8691 | /* protect against races with linked timeouts */ |
| 8692 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8693 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8694 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8695 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8696 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8697 | } |
| 8698 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8699 | } |
| 8700 | |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8701 | static bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8702 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8703 | struct files_struct *files) |
| 8704 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8705 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8706 | LIST_HEAD(list); |
| 8707 | |
| 8708 | spin_lock_irq(&ctx->completion_lock); |
| 8709 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8710 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8711 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8712 | break; |
| 8713 | } |
| 8714 | } |
| 8715 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8716 | if (list_empty(&list)) |
| 8717 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8718 | |
| 8719 | while (!list_empty(&list)) { |
| 8720 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8721 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 8722 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8723 | kfree(de); |
| 8724 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8725 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8726 | } |
| 8727 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 8728 | static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
| 8729 | { |
| 8730 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 8731 | |
| 8732 | return req->ctx == data; |
| 8733 | } |
| 8734 | |
| 8735 | static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
| 8736 | { |
| 8737 | struct io_tctx_node *node; |
| 8738 | enum io_wq_cancel cret; |
| 8739 | bool ret = false; |
| 8740 | |
| 8741 | mutex_lock(&ctx->uring_lock); |
| 8742 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 8743 | struct io_uring_task *tctx = node->task->io_uring; |
| 8744 | |
| 8745 | /* |
| 8746 | * io_wq will stay alive while we hold uring_lock, because it's |
| 8747 | * killed after ctx nodes, which requires to take the lock. |
| 8748 | */ |
| 8749 | if (!tctx || !tctx->io_wq) |
| 8750 | continue; |
| 8751 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 8752 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8753 | } |
| 8754 | mutex_unlock(&ctx->uring_lock); |
| 8755 | |
| 8756 | return ret; |
| 8757 | } |
| 8758 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8759 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8760 | struct task_struct *task, |
| 8761 | struct files_struct *files) |
| 8762 | { |
| 8763 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 8764 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8765 | |
| 8766 | while (1) { |
| 8767 | enum io_wq_cancel cret; |
| 8768 | bool ret = false; |
| 8769 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 8770 | if (!task) { |
| 8771 | ret |= io_uring_try_cancel_iowq(ctx); |
| 8772 | } else if (tctx && tctx->io_wq) { |
| 8773 | /* |
| 8774 | * Cancels requests of all rings, not only @ctx, but |
| 8775 | * it's fine as the task is in exit/exec. |
| 8776 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8777 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8778 | &cancel, true); |
| 8779 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8780 | } |
| 8781 | |
| 8782 | /* SQPOLL thread does its own polling */ |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 8783 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && !files) || |
| 8784 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8785 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8786 | io_iopoll_try_reap_events(ctx); |
| 8787 | ret = true; |
| 8788 | } |
| 8789 | } |
| 8790 | |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 8791 | ret |= io_cancel_defer_files(ctx, task, files); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8792 | ret |= io_poll_remove_all(ctx, task, files); |
| 8793 | ret |= io_kill_timeouts(ctx, task, files); |
| 8794 | ret |= io_run_task_work(); |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8795 | ret |= io_run_ctx_fallback(ctx); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8796 | if (!ret) |
| 8797 | break; |
| 8798 | cond_resched(); |
| 8799 | } |
| 8800 | } |
| 8801 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8802 | static int __io_uring_add_task_file(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8803 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8804 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8805 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8806 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8807 | |
| 8808 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8809 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8810 | if (unlikely(ret)) |
| 8811 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8812 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8813 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8814 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 8815 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 8816 | if (!node) |
| 8817 | return -ENOMEM; |
| 8818 | node->ctx = ctx; |
| 8819 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8820 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8821 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 8822 | node, GFP_KERNEL)); |
| 8823 | if (ret) { |
| 8824 | kfree(node); |
| 8825 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8826 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8827 | |
| 8828 | mutex_lock(&ctx->uring_lock); |
| 8829 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 8830 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8831 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8832 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8833 | return 0; |
| 8834 | } |
| 8835 | |
| 8836 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 8837 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8838 | */ |
| 8839 | static inline int io_uring_add_task_file(struct io_ring_ctx *ctx) |
| 8840 | { |
| 8841 | struct io_uring_task *tctx = current->io_uring; |
| 8842 | |
| 8843 | if (likely(tctx && tctx->last == ctx)) |
| 8844 | return 0; |
| 8845 | return __io_uring_add_task_file(ctx); |
| 8846 | } |
| 8847 | |
| 8848 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8849 | * Remove this io_uring_file -> task mapping. |
| 8850 | */ |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8851 | static void io_uring_del_task_file(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8852 | { |
| 8853 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8854 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8855 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 8856 | if (!tctx) |
| 8857 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8858 | node = xa_erase(&tctx->xa, index); |
| 8859 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8860 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8861 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8862 | WARN_ON_ONCE(current != node->task); |
| 8863 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 8864 | |
| 8865 | mutex_lock(&node->ctx->uring_lock); |
| 8866 | list_del(&node->ctx_node); |
| 8867 | mutex_unlock(&node->ctx->uring_lock); |
| 8868 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 8869 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8870 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8871 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8872 | } |
| 8873 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8874 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8875 | { |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8876 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8877 | unsigned long index; |
| 8878 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8879 | xa_for_each(&tctx->xa, index, node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 8880 | io_uring_del_task_file(index); |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8881 | if (tctx->io_wq) { |
| 8882 | io_wq_put_and_exit(tctx->io_wq); |
| 8883 | tctx->io_wq = NULL; |
| 8884 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8885 | } |
| 8886 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8887 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 368b208 | 2021-04-11 01:46:25 +0100 | [diff] [blame] | 8888 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8889 | if (tracked) |
| 8890 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8891 | return percpu_counter_sum(&tctx->inflight); |
| 8892 | } |
| 8893 | |
| 8894 | static void io_sqpoll_cancel_cb(struct callback_head *cb) |
| 8895 | { |
| 8896 | struct io_tctx_exit *work = container_of(cb, struct io_tctx_exit, task_work); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8897 | struct io_sq_data *sqd = work->ctx->sq_data; |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8898 | |
| 8899 | if (sqd->thread) |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8900 | io_uring_cancel_sqpoll(sqd); |
| 8901 | list_del_init(&work->ctx->sqd_list); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8902 | complete(&work->completion); |
| 8903 | } |
| 8904 | |
| 8905 | static void io_sqpoll_cancel_sync(struct io_ring_ctx *ctx) |
| 8906 | { |
| 8907 | struct io_sq_data *sqd = ctx->sq_data; |
| 8908 | struct io_tctx_exit work = { .ctx = ctx, }; |
| 8909 | struct task_struct *task; |
| 8910 | |
| 8911 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8912 | io_sqd_update_thread_idle(sqd); |
| 8913 | task = sqd->thread; |
| 8914 | if (task) { |
| 8915 | init_completion(&work.completion); |
| 8916 | init_task_work(&work.task_work, io_sqpoll_cancel_cb); |
Pavel Begunkov | b7f5a0b | 2021-03-15 14:23:08 +0000 | [diff] [blame] | 8917 | io_task_work_add_head(&sqd->park_task_work, &work.task_work); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8918 | wake_up_process(task); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8919 | } else { |
| 8920 | list_del_init(&ctx->sqd_list); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8921 | } |
| 8922 | io_sq_thread_unpark(sqd); |
| 8923 | |
| 8924 | if (task) |
| 8925 | wait_for_completion(&work.completion); |
| 8926 | } |
| 8927 | |
Pavel Begunkov | 368b208 | 2021-04-11 01:46:25 +0100 | [diff] [blame] | 8928 | static void io_uring_try_cancel(struct files_struct *files) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8929 | { |
| 8930 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 8931 | struct io_tctx_node *node; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 8932 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8933 | |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8934 | xa_for_each(&tctx->xa, index, node) { |
| 8935 | struct io_ring_ctx *ctx = node->ctx; |
| 8936 | |
| 8937 | if (ctx->sq_data) { |
| 8938 | io_sqpoll_cancel_sync(ctx); |
| 8939 | continue; |
| 8940 | } |
Pavel Begunkov | 368b208 | 2021-04-11 01:46:25 +0100 | [diff] [blame] | 8941 | io_uring_try_cancel_requests(ctx, current, files); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8942 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8943 | } |
| 8944 | |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8945 | /* should only be called by SQPOLL task */ |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8946 | static void io_uring_cancel_sqpoll(struct io_sq_data *sqd) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8947 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8948 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8949 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8950 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8951 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8952 | |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8953 | WARN_ON_ONCE(!sqd || sqd->thread != current); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8954 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8955 | atomic_inc(&tctx->in_idle); |
| 8956 | do { |
| 8957 | /* read completions before cancelations */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8958 | inflight = tctx_inflight(tctx, false); |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8959 | if (!inflight) |
| 8960 | break; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 8961 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 8962 | io_uring_try_cancel_requests(ctx, current, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8963 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8964 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8965 | /* |
| 8966 | * If we've seen completions, retry without waiting. This |
| 8967 | * avoids a race where a completion comes in before we did |
| 8968 | * prepare_to_wait(). |
| 8969 | */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8970 | if (inflight == tctx_inflight(tctx, false)) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8971 | schedule(); |
| 8972 | finish_wait(&tctx->wait, &wait); |
| 8973 | } while (1); |
| 8974 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8975 | } |
| 8976 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8977 | /* |
| 8978 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 8979 | * requests. |
| 8980 | */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8981 | void __io_uring_cancel(struct files_struct *files) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8982 | { |
| 8983 | struct io_uring_task *tctx = current->io_uring; |
| 8984 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8985 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8986 | |
| 8987 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8988 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8989 | io_uring_try_cancel(files); |
Pavel Begunkov | 5a978dc | 2021-03-27 09:59:30 +0000 | [diff] [blame] | 8990 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8991 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8992 | /* read completions before cancelations */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8993 | inflight = tctx_inflight(tctx, !!files); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8994 | if (!inflight) |
| 8995 | break; |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 8996 | io_uring_try_cancel(files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8997 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8998 | |
| 8999 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9000 | * If we've seen completions, retry without waiting. This |
| 9001 | * avoids a race where a completion comes in before we did |
| 9002 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9003 | */ |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9004 | if (inflight == tctx_inflight(tctx, !!files)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9005 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9006 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9007 | } while (1); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9008 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9009 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9010 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9011 | if (!files) { |
| 9012 | /* for exec all current's requests should be gone, kill tctx */ |
| 9013 | __io_uring_free(current); |
| 9014 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9015 | } |
| 9016 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9017 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9018 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9019 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9020 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9021 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9022 | struct page *page; |
| 9023 | void *ptr; |
| 9024 | |
| 9025 | switch (offset) { |
| 9026 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9027 | case IORING_OFF_CQ_RING: |
| 9028 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9029 | break; |
| 9030 | case IORING_OFF_SQES: |
| 9031 | ptr = ctx->sq_sqes; |
| 9032 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9033 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9034 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9035 | } |
| 9036 | |
| 9037 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9038 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9039 | return ERR_PTR(-EINVAL); |
| 9040 | |
| 9041 | return ptr; |
| 9042 | } |
| 9043 | |
| 9044 | #ifdef CONFIG_MMU |
| 9045 | |
| 9046 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9047 | { |
| 9048 | size_t sz = vma->vm_end - vma->vm_start; |
| 9049 | unsigned long pfn; |
| 9050 | void *ptr; |
| 9051 | |
| 9052 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9053 | if (IS_ERR(ptr)) |
| 9054 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9055 | |
| 9056 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9057 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9058 | } |
| 9059 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9060 | #else /* !CONFIG_MMU */ |
| 9061 | |
| 9062 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9063 | { |
| 9064 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9065 | } |
| 9066 | |
| 9067 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9068 | { |
| 9069 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9070 | } |
| 9071 | |
| 9072 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9073 | unsigned long addr, unsigned long len, |
| 9074 | unsigned long pgoff, unsigned long flags) |
| 9075 | { |
| 9076 | void *ptr; |
| 9077 | |
| 9078 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9079 | if (IS_ERR(ptr)) |
| 9080 | return PTR_ERR(ptr); |
| 9081 | |
| 9082 | return (unsigned long) ptr; |
| 9083 | } |
| 9084 | |
| 9085 | #endif /* !CONFIG_MMU */ |
| 9086 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9087 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9088 | { |
| 9089 | DEFINE_WAIT(wait); |
| 9090 | |
| 9091 | do { |
| 9092 | if (!io_sqring_full(ctx)) |
| 9093 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9094 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9095 | |
| 9096 | if (!io_sqring_full(ctx)) |
| 9097 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9098 | schedule(); |
| 9099 | } while (!signal_pending(current)); |
| 9100 | |
| 9101 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 9102 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9103 | } |
| 9104 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9105 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9106 | struct __kernel_timespec __user **ts, |
| 9107 | const sigset_t __user **sig) |
| 9108 | { |
| 9109 | struct io_uring_getevents_arg arg; |
| 9110 | |
| 9111 | /* |
| 9112 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9113 | * is just a pointer to the sigset_t. |
| 9114 | */ |
| 9115 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9116 | *sig = (const sigset_t __user *) argp; |
| 9117 | *ts = NULL; |
| 9118 | return 0; |
| 9119 | } |
| 9120 | |
| 9121 | /* |
| 9122 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9123 | * timespec and sigset_t pointers if good. |
| 9124 | */ |
| 9125 | if (*argsz != sizeof(arg)) |
| 9126 | return -EINVAL; |
| 9127 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9128 | return -EFAULT; |
| 9129 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9130 | *argsz = arg.sigmask_sz; |
| 9131 | *ts = u64_to_user_ptr(arg.ts); |
| 9132 | return 0; |
| 9133 | } |
| 9134 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9135 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9136 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9137 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9138 | { |
| 9139 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9140 | int submitted = 0; |
| 9141 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9142 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9143 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9144 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9145 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9146 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 9147 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9148 | return -EINVAL; |
| 9149 | |
| 9150 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9151 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9152 | return -EBADF; |
| 9153 | |
| 9154 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9155 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9156 | goto out_fput; |
| 9157 | |
| 9158 | ret = -ENXIO; |
| 9159 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9160 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9161 | goto out_fput; |
| 9162 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9163 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 9164 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9165 | goto out; |
| 9166 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9167 | /* |
| 9168 | * For SQ polling, the thread will do all submissions and completions. |
| 9169 | * Just return the requested submit count, and wake the thread if |
| 9170 | * we were asked to. |
| 9171 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9172 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9173 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9174 | io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9175 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9176 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 9177 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 9178 | goto out; |
| 9179 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9180 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9181 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9182 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9183 | ret = io_sqpoll_wait_sq(ctx); |
| 9184 | if (ret) |
| 9185 | goto out; |
| 9186 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9187 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9188 | } else if (to_submit) { |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9189 | ret = io_uring_add_task_file(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9190 | if (unlikely(ret)) |
| 9191 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9192 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9193 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9194 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9195 | |
| 9196 | if (submitted != to_submit) |
| 9197 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9198 | } |
| 9199 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9200 | const sigset_t __user *sig; |
| 9201 | struct __kernel_timespec __user *ts; |
| 9202 | |
| 9203 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9204 | if (unlikely(ret)) |
| 9205 | goto out; |
| 9206 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9207 | min_complete = min(min_complete, ctx->cq_entries); |
| 9208 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9209 | /* |
| 9210 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9211 | * space applications don't need to do io completion events |
| 9212 | * polling again, they can rely on io_sq_thread to do polling |
| 9213 | * work, which can reduce cpu usage and uring_lock contention. |
| 9214 | */ |
| 9215 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9216 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9217 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9218 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9219 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9220 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9221 | } |
| 9222 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9223 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9224 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9225 | out_fput: |
| 9226 | fdput(f); |
| 9227 | return submitted ? submitted : ret; |
| 9228 | } |
| 9229 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9230 | #ifdef CONFIG_PROC_FS |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9231 | static int io_uring_show_cred(struct seq_file *m, unsigned int id, |
| 9232 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9233 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9234 | struct user_namespace *uns = seq_user_ns(m); |
| 9235 | struct group_info *gi; |
| 9236 | kernel_cap_t cap; |
| 9237 | unsigned __capi; |
| 9238 | int g; |
| 9239 | |
| 9240 | seq_printf(m, "%5d\n", id); |
| 9241 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9242 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9243 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9244 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9245 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9246 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9247 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9248 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9249 | seq_puts(m, "\n\tGroups:\t"); |
| 9250 | gi = cred->group_info; |
| 9251 | for (g = 0; g < gi->ngroups; g++) { |
| 9252 | seq_put_decimal_ull(m, g ? " " : "", |
| 9253 | from_kgid_munged(uns, gi->gid[g])); |
| 9254 | } |
| 9255 | seq_puts(m, "\n\tCapEff:\t"); |
| 9256 | cap = cred->cap_effective; |
| 9257 | CAP_FOR_EACH_U32(__capi) |
| 9258 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9259 | seq_putc(m, '\n'); |
| 9260 | return 0; |
| 9261 | } |
| 9262 | |
| 9263 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9264 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9265 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9266 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9267 | int i; |
| 9268 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9269 | /* |
| 9270 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9271 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9272 | * cases. If we fail to get the lock, we just don't iterate any |
| 9273 | * structures that could be going away outside the io_uring mutex. |
| 9274 | */ |
| 9275 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9276 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9277 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9278 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9279 | if (!sq->thread) |
| 9280 | sq = NULL; |
| 9281 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9282 | |
| 9283 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9284 | 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] | 9285 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9286 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 9287 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9288 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9289 | if (f) |
| 9290 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9291 | else |
| 9292 | seq_printf(m, "%5u: <none>\n", i); |
| 9293 | } |
| 9294 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9295 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9296 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9297 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9298 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9299 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9300 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9301 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 9302 | unsigned long index; |
| 9303 | const struct cred *cred; |
| 9304 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9305 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9306 | xa_for_each(&ctx->personalities, index, cred) |
| 9307 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9308 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9309 | seq_printf(m, "PollList:\n"); |
| 9310 | spin_lock_irq(&ctx->completion_lock); |
| 9311 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9312 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9313 | struct io_kiocb *req; |
| 9314 | |
| 9315 | hlist_for_each_entry(req, list, hash_node) |
| 9316 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9317 | req->task->task_works != NULL); |
| 9318 | } |
| 9319 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9320 | if (has_lock) |
| 9321 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9322 | } |
| 9323 | |
| 9324 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9325 | { |
| 9326 | struct io_ring_ctx *ctx = f->private_data; |
| 9327 | |
| 9328 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9329 | __io_uring_show_fdinfo(ctx, m); |
| 9330 | percpu_ref_put(&ctx->refs); |
| 9331 | } |
| 9332 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9333 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9334 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9335 | static const struct file_operations io_uring_fops = { |
| 9336 | .release = io_uring_release, |
| 9337 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9338 | #ifndef CONFIG_MMU |
| 9339 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9340 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9341 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9342 | .poll = io_uring_poll, |
| 9343 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9344 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9345 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9346 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9347 | }; |
| 9348 | |
| 9349 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9350 | struct io_uring_params *p) |
| 9351 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9352 | struct io_rings *rings; |
| 9353 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9354 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9355 | /* make sure these are sane, as we already accounted them */ |
| 9356 | ctx->sq_entries = p->sq_entries; |
| 9357 | ctx->cq_entries = p->cq_entries; |
| 9358 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9359 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9360 | if (size == SIZE_MAX) |
| 9361 | return -EOVERFLOW; |
| 9362 | |
| 9363 | rings = io_mem_alloc(size); |
| 9364 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9365 | return -ENOMEM; |
| 9366 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9367 | ctx->rings = rings; |
| 9368 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9369 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9370 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9371 | rings->sq_ring_entries = p->sq_entries; |
| 9372 | rings->cq_ring_entries = p->cq_entries; |
| 9373 | ctx->sq_mask = rings->sq_ring_mask; |
| 9374 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9375 | |
| 9376 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9377 | if (size == SIZE_MAX) { |
| 9378 | io_mem_free(ctx->rings); |
| 9379 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9380 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9381 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9382 | |
| 9383 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9384 | if (!ctx->sq_sqes) { |
| 9385 | io_mem_free(ctx->rings); |
| 9386 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9387 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9388 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9389 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9390 | return 0; |
| 9391 | } |
| 9392 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9393 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9394 | { |
| 9395 | int ret, fd; |
| 9396 | |
| 9397 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9398 | if (fd < 0) |
| 9399 | return fd; |
| 9400 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9401 | ret = io_uring_add_task_file(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9402 | if (ret) { |
| 9403 | put_unused_fd(fd); |
| 9404 | return ret; |
| 9405 | } |
| 9406 | fd_install(fd, file); |
| 9407 | return fd; |
| 9408 | } |
| 9409 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9410 | /* |
| 9411 | * Allocate an anonymous fd, this is what constitutes the application |
| 9412 | * visible backing of an io_uring instance. The application mmaps this |
| 9413 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9414 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9415 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9416 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9417 | { |
| 9418 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9419 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9420 | int ret; |
| 9421 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9422 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9423 | &ctx->ring_sock); |
| 9424 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9425 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9426 | #endif |
| 9427 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9428 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9429 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9430 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9431 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9432 | sock_release(ctx->ring_sock); |
| 9433 | ctx->ring_sock = NULL; |
| 9434 | } else { |
| 9435 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9436 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9437 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9438 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9439 | } |
| 9440 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9441 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9442 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9443 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9444 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9445 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9446 | int ret; |
| 9447 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9448 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9449 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9450 | if (entries > IORING_MAX_ENTRIES) { |
| 9451 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9452 | return -EINVAL; |
| 9453 | entries = IORING_MAX_ENTRIES; |
| 9454 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9455 | |
| 9456 | /* |
| 9457 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9458 | * application to drive a higher depth than the size of the SQ ring, |
| 9459 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9460 | * some flexibility in overcommitting a bit. If the application has |
| 9461 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9462 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9463 | */ |
| 9464 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9465 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9466 | /* |
| 9467 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9468 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9469 | * any cq vs sq ring sizing. |
| 9470 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9471 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9472 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9473 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9474 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9475 | return -EINVAL; |
| 9476 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9477 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9478 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9479 | if (p->cq_entries < p->sq_entries) |
| 9480 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9481 | } else { |
| 9482 | p->cq_entries = 2 * p->sq_entries; |
| 9483 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9484 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9485 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9486 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9487 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9488 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9489 | if (!capable(CAP_IPC_LOCK)) |
| 9490 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9491 | |
| 9492 | /* |
| 9493 | * This is just grabbed for accounting purposes. When a process exits, |
| 9494 | * the mm is exited and dropped before the files, hence we need to hang |
| 9495 | * on to this mm purely for the purposes of being able to unaccount |
| 9496 | * memory (locked/pinned vm). It's not used for anything else. |
| 9497 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9498 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9499 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9500 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9501 | ret = io_allocate_scq_urings(ctx, p); |
| 9502 | if (ret) |
| 9503 | goto err; |
| 9504 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9505 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9506 | if (ret) |
| 9507 | goto err; |
| 9508 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9509 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9510 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9511 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9512 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9513 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9514 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9515 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9516 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9517 | |
| 9518 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9519 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9520 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9521 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9522 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9523 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9524 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9525 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9526 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9527 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9528 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9529 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9530 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Jens Axboe | 1c0aa1f | 2021-02-20 11:55:28 -0700 | [diff] [blame] | 9531 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9532 | |
| 9533 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9534 | ret = -EFAULT; |
| 9535 | goto err; |
| 9536 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9537 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9538 | file = io_uring_get_file(ctx); |
| 9539 | if (IS_ERR(file)) { |
| 9540 | ret = PTR_ERR(file); |
| 9541 | goto err; |
| 9542 | } |
| 9543 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9544 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9545 | * Install ring fd as the very last thing, so we don't risk someone |
| 9546 | * having closed it before we finish setup |
| 9547 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9548 | ret = io_uring_install_fd(ctx, file); |
| 9549 | if (ret < 0) { |
| 9550 | /* fput will clean it up */ |
| 9551 | fput(file); |
| 9552 | return ret; |
| 9553 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9554 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9555 | 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] | 9556 | return ret; |
| 9557 | err: |
| 9558 | io_ring_ctx_wait_and_kill(ctx); |
| 9559 | return ret; |
| 9560 | } |
| 9561 | |
| 9562 | /* |
| 9563 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9564 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9565 | * params structure passed in. |
| 9566 | */ |
| 9567 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9568 | { |
| 9569 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9570 | int i; |
| 9571 | |
| 9572 | if (copy_from_user(&p, params, sizeof(p))) |
| 9573 | return -EFAULT; |
| 9574 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9575 | if (p.resv[i]) |
| 9576 | return -EINVAL; |
| 9577 | } |
| 9578 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9579 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9580 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9581 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9582 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9583 | return -EINVAL; |
| 9584 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9585 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9586 | } |
| 9587 | |
| 9588 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9589 | struct io_uring_params __user *, params) |
| 9590 | { |
| 9591 | return io_uring_setup(entries, params); |
| 9592 | } |
| 9593 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9594 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9595 | { |
| 9596 | struct io_uring_probe *p; |
| 9597 | size_t size; |
| 9598 | int i, ret; |
| 9599 | |
| 9600 | size = struct_size(p, ops, nr_args); |
| 9601 | if (size == SIZE_MAX) |
| 9602 | return -EOVERFLOW; |
| 9603 | p = kzalloc(size, GFP_KERNEL); |
| 9604 | if (!p) |
| 9605 | return -ENOMEM; |
| 9606 | |
| 9607 | ret = -EFAULT; |
| 9608 | if (copy_from_user(p, arg, size)) |
| 9609 | goto out; |
| 9610 | ret = -EINVAL; |
| 9611 | if (memchr_inv(p, 0, size)) |
| 9612 | goto out; |
| 9613 | |
| 9614 | p->last_op = IORING_OP_LAST - 1; |
| 9615 | if (nr_args > IORING_OP_LAST) |
| 9616 | nr_args = IORING_OP_LAST; |
| 9617 | |
| 9618 | for (i = 0; i < nr_args; i++) { |
| 9619 | p->ops[i].op = i; |
| 9620 | if (!io_op_defs[i].not_supported) |
| 9621 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9622 | } |
| 9623 | p->ops_len = i; |
| 9624 | |
| 9625 | ret = 0; |
| 9626 | if (copy_to_user(arg, p, size)) |
| 9627 | ret = -EFAULT; |
| 9628 | out: |
| 9629 | kfree(p); |
| 9630 | return ret; |
| 9631 | } |
| 9632 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9633 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9634 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9635 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9636 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9637 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9638 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9639 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9640 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9641 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 9642 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
| 9643 | if (!ret) |
| 9644 | return id; |
| 9645 | put_cred(creds); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9646 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9647 | } |
| 9648 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9649 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9650 | unsigned int nr_args) |
| 9651 | { |
| 9652 | struct io_uring_restriction *res; |
| 9653 | size_t size; |
| 9654 | int i, ret; |
| 9655 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9656 | /* Restrictions allowed only if rings started disabled */ |
| 9657 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9658 | return -EBADFD; |
| 9659 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9660 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9661 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9662 | return -EBUSY; |
| 9663 | |
| 9664 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9665 | return -EINVAL; |
| 9666 | |
| 9667 | size = array_size(nr_args, sizeof(*res)); |
| 9668 | if (size == SIZE_MAX) |
| 9669 | return -EOVERFLOW; |
| 9670 | |
| 9671 | res = memdup_user(arg, size); |
| 9672 | if (IS_ERR(res)) |
| 9673 | return PTR_ERR(res); |
| 9674 | |
| 9675 | ret = 0; |
| 9676 | |
| 9677 | for (i = 0; i < nr_args; i++) { |
| 9678 | switch (res[i].opcode) { |
| 9679 | case IORING_RESTRICTION_REGISTER_OP: |
| 9680 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9681 | ret = -EINVAL; |
| 9682 | goto out; |
| 9683 | } |
| 9684 | |
| 9685 | __set_bit(res[i].register_op, |
| 9686 | ctx->restrictions.register_op); |
| 9687 | break; |
| 9688 | case IORING_RESTRICTION_SQE_OP: |
| 9689 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9690 | ret = -EINVAL; |
| 9691 | goto out; |
| 9692 | } |
| 9693 | |
| 9694 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9695 | break; |
| 9696 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9697 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9698 | break; |
| 9699 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9700 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9701 | break; |
| 9702 | default: |
| 9703 | ret = -EINVAL; |
| 9704 | goto out; |
| 9705 | } |
| 9706 | } |
| 9707 | |
| 9708 | out: |
| 9709 | /* Reset all restrictions if an error happened */ |
| 9710 | if (ret != 0) |
| 9711 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9712 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9713 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9714 | |
| 9715 | kfree(res); |
| 9716 | return ret; |
| 9717 | } |
| 9718 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9719 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9720 | { |
| 9721 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9722 | return -EBADFD; |
| 9723 | |
| 9724 | if (ctx->restrictions.registered) |
| 9725 | ctx->restricted = 1; |
| 9726 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 9727 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9728 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 9729 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9730 | return 0; |
| 9731 | } |
| 9732 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9733 | static bool io_register_op_must_quiesce(int op) |
| 9734 | { |
| 9735 | switch (op) { |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 9736 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9737 | case IORING_UNREGISTER_FILES: |
| 9738 | case IORING_REGISTER_FILES_UPDATE: |
| 9739 | case IORING_REGISTER_PROBE: |
| 9740 | case IORING_REGISTER_PERSONALITY: |
| 9741 | case IORING_UNREGISTER_PERSONALITY: |
| 9742 | return false; |
| 9743 | default: |
| 9744 | return true; |
| 9745 | } |
| 9746 | } |
| 9747 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9748 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9749 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9750 | __releases(ctx->uring_lock) |
| 9751 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9752 | { |
| 9753 | int ret; |
| 9754 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9755 | /* |
| 9756 | * We're inside the ring mutex, if the ref is already dying, then |
| 9757 | * someone else killed the ctx or is already going through |
| 9758 | * io_uring_register(). |
| 9759 | */ |
| 9760 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9761 | return -ENXIO; |
| 9762 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 9763 | if (ctx->restricted) { |
| 9764 | if (opcode >= IORING_REGISTER_LAST) |
| 9765 | return -EINVAL; |
| 9766 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 9767 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 9768 | return -EACCES; |
| 9769 | } |
| 9770 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9771 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9772 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9773 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9774 | /* |
| 9775 | * Drop uring mutex before waiting for references to exit. If |
| 9776 | * another thread is currently inside io_uring_enter() it might |
| 9777 | * need to grab the uring_lock to make progress. If we hold it |
| 9778 | * here across the drain wait, then we can deadlock. It's safe |
| 9779 | * to drop the mutex here, since no new references will come in |
| 9780 | * after we've killed the percpu ref. |
| 9781 | */ |
| 9782 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9783 | do { |
| 9784 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 9785 | if (!ret) |
| 9786 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 9787 | ret = io_run_task_work_sig(); |
| 9788 | if (ret < 0) |
| 9789 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9790 | } while (1); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9791 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9792 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9793 | if (ret) { |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 9794 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 9795 | return ret; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9796 | } |
| 9797 | } |
| 9798 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9799 | switch (opcode) { |
| 9800 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9801 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9802 | break; |
| 9803 | case IORING_UNREGISTER_BUFFERS: |
| 9804 | ret = -EINVAL; |
| 9805 | if (arg || nr_args) |
| 9806 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9807 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9808 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 9809 | case IORING_REGISTER_FILES: |
| 9810 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 9811 | break; |
| 9812 | case IORING_UNREGISTER_FILES: |
| 9813 | ret = -EINVAL; |
| 9814 | if (arg || nr_args) |
| 9815 | break; |
| 9816 | ret = io_sqe_files_unregister(ctx); |
| 9817 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 9818 | case IORING_REGISTER_FILES_UPDATE: |
| 9819 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 9820 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9821 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9822 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9823 | ret = -EINVAL; |
| 9824 | if (nr_args != 1) |
| 9825 | break; |
| 9826 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9827 | if (ret) |
| 9828 | break; |
| 9829 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 9830 | ctx->eventfd_async = 1; |
| 9831 | else |
| 9832 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9833 | break; |
| 9834 | case IORING_UNREGISTER_EVENTFD: |
| 9835 | ret = -EINVAL; |
| 9836 | if (arg || nr_args) |
| 9837 | break; |
| 9838 | ret = io_eventfd_unregister(ctx); |
| 9839 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9840 | case IORING_REGISTER_PROBE: |
| 9841 | ret = -EINVAL; |
| 9842 | if (!arg || nr_args > 256) |
| 9843 | break; |
| 9844 | ret = io_probe(ctx, arg, nr_args); |
| 9845 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9846 | case IORING_REGISTER_PERSONALITY: |
| 9847 | ret = -EINVAL; |
| 9848 | if (arg || nr_args) |
| 9849 | break; |
| 9850 | ret = io_register_personality(ctx); |
| 9851 | break; |
| 9852 | case IORING_UNREGISTER_PERSONALITY: |
| 9853 | ret = -EINVAL; |
| 9854 | if (arg) |
| 9855 | break; |
| 9856 | ret = io_unregister_personality(ctx, nr_args); |
| 9857 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9858 | case IORING_REGISTER_ENABLE_RINGS: |
| 9859 | ret = -EINVAL; |
| 9860 | if (arg || nr_args) |
| 9861 | break; |
| 9862 | ret = io_register_enable_rings(ctx); |
| 9863 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9864 | case IORING_REGISTER_RESTRICTIONS: |
| 9865 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 9866 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9867 | default: |
| 9868 | ret = -EINVAL; |
| 9869 | break; |
| 9870 | } |
| 9871 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9872 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9873 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9874 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9875 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9876 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9877 | return ret; |
| 9878 | } |
| 9879 | |
| 9880 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 9881 | void __user *, arg, unsigned int, nr_args) |
| 9882 | { |
| 9883 | struct io_ring_ctx *ctx; |
| 9884 | long ret = -EBADF; |
| 9885 | struct fd f; |
| 9886 | |
| 9887 | f = fdget(fd); |
| 9888 | if (!f.file) |
| 9889 | return -EBADF; |
| 9890 | |
| 9891 | ret = -EOPNOTSUPP; |
| 9892 | if (f.file->f_op != &io_uring_fops) |
| 9893 | goto out_fput; |
| 9894 | |
| 9895 | ctx = f.file->private_data; |
| 9896 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 9897 | io_run_task_work(); |
| 9898 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9899 | mutex_lock(&ctx->uring_lock); |
| 9900 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 9901 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9902 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 9903 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9904 | out_fput: |
| 9905 | fdput(f); |
| 9906 | return ret; |
| 9907 | } |
| 9908 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9909 | static int __init io_uring_init(void) |
| 9910 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9911 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 9912 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 9913 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 9914 | } while (0) |
| 9915 | |
| 9916 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 9917 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 9918 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 9919 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 9920 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 9921 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 9922 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 9923 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 9924 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 9925 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9926 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9927 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 9928 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 9929 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 9930 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 9931 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9932 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 9933 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9934 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 9935 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 9936 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 9937 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 9938 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 9939 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 9940 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 9941 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9942 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9943 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 9944 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 9945 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9946 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9947 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 9948 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 9949 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 9950 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 9951 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9952 | return 0; |
| 9953 | }; |
| 9954 | __initcall(io_uring_init); |