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> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 77 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 78 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 79 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 80 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 81 | #include <linux/io_uring.h> |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 82 | #include <linux/blk-cgroup.h> |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 83 | #include <linux/audit.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 85 | #define CREATE_TRACE_POINTS |
| 86 | #include <trace/events/io_uring.h> |
| 87 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 88 | #include <uapi/linux/io_uring.h> |
| 89 | |
| 90 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 91 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 92 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 93 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 98 | */ |
| 99 | #define IORING_FILE_TABLE_SHIFT 9 |
| 100 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 101 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 102 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 103 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 104 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 105 | |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 108 | IOSQE_BUFFER_SELECT) |
| 109 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 110 | struct io_uring { |
| 111 | u32 head ____cacheline_aligned_in_smp; |
| 112 | u32 tail ____cacheline_aligned_in_smp; |
| 113 | }; |
| 114 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 116 | * This data is shared with the application through the mmap at offsets |
| 117 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 118 | * |
| 119 | * The offsets to the member fields are published through struct |
| 120 | * io_sqring_offsets when calling io_uring_setup. |
| 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 123 | /* |
| 124 | * Head and tail offsets into the ring; the offsets need to be |
| 125 | * masked to get valid indices. |
| 126 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 127 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 128 | * and the application controls tail of the sq ring and the head of the |
| 129 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 130 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 131 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 132 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 133 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 134 | * ring_entries - 1) |
| 135 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 136 | u32 sq_ring_mask, cq_ring_mask; |
| 137 | /* Ring sizes (constant, power of 2) */ |
| 138 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Number of invalid entries dropped by the kernel due to |
| 141 | * invalid index stored in array |
| 142 | * |
| 143 | * Written by the kernel, shouldn't be modified by the |
| 144 | * application (i.e. get number of "new events" by comparing to |
| 145 | * cached value). |
| 146 | * |
| 147 | * After a new SQ head value was read by the application this |
| 148 | * counter includes all submissions that were dropped reaching |
| 149 | * the new SQ head (and possibly more). |
| 150 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 151 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 152 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 153 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | * |
| 155 | * Written by the kernel, shouldn't be modified by the |
| 156 | * application. |
| 157 | * |
| 158 | * The application needs a full memory barrier before checking |
| 159 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 160 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 161 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 163 | * Runtime CQ flags |
| 164 | * |
| 165 | * Written by the application, shouldn't be modified by the |
| 166 | * kernel. |
| 167 | */ |
| 168 | u32 cq_flags; |
| 169 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 170 | * Number of completion events lost because the queue was full; |
| 171 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 172 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 173 | * the completion queue. |
| 174 | * |
| 175 | * Written by the kernel, shouldn't be modified by the |
| 176 | * application (i.e. get number of "new events" by comparing to |
| 177 | * cached value). |
| 178 | * |
| 179 | * As completion events come in out of order this counter is not |
| 180 | * ordered with any other data. |
| 181 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 182 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Ring buffer of completion events. |
| 185 | * |
| 186 | * The kernel writes completion events fresh every time they are |
| 187 | * produced, so the application is allowed to modify pending |
| 188 | * entries. |
| 189 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 190 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 193 | enum io_uring_cmd_flags { |
| 194 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 195 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 198 | struct io_mapped_ubuf { |
| 199 | u64 ubuf; |
| 200 | size_t len; |
| 201 | struct bio_vec *bvec; |
| 202 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 203 | unsigned long acct_pages; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 206 | struct io_ring_ctx; |
| 207 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 208 | struct io_rsrc_put { |
| 209 | struct list_head list; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 210 | union { |
| 211 | void *rsrc; |
| 212 | struct file *file; |
| 213 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | struct fixed_rsrc_table { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 217 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 220 | struct fixed_rsrc_ref_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 221 | struct percpu_ref refs; |
| 222 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 223 | struct list_head rsrc_list; |
| 224 | struct fixed_rsrc_data *rsrc_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 225 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 226 | struct io_rsrc_put *prsrc); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 227 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 228 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 229 | }; |
| 230 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 231 | struct fixed_rsrc_data { |
| 232 | struct fixed_rsrc_table *table; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 233 | struct io_ring_ctx *ctx; |
| 234 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 235 | struct fixed_rsrc_ref_node *node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 236 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 237 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 238 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 241 | struct io_buffer { |
| 242 | struct list_head list; |
| 243 | __u64 addr; |
| 244 | __s32 len; |
| 245 | __u16 bid; |
| 246 | }; |
| 247 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 248 | struct io_restriction { |
| 249 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 250 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 251 | u8 sqe_flags_allowed; |
| 252 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 253 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 254 | }; |
| 255 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 256 | enum { |
| 257 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 258 | IO_SQ_THREAD_SHOULD_PARK, |
| 259 | }; |
| 260 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 261 | struct io_sq_data { |
| 262 | refcount_t refs; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 263 | struct mutex lock; |
| 264 | |
| 265 | /* ctx's that are using this sqd */ |
| 266 | struct list_head ctx_list; |
| 267 | struct list_head ctx_new_list; |
| 268 | struct mutex ctx_lock; |
| 269 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 270 | struct task_struct *thread; |
| 271 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 272 | |
| 273 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 274 | int sq_cpu; |
| 275 | pid_t task_pid; |
| 276 | |
| 277 | unsigned long state; |
| 278 | struct completion startup; |
| 279 | struct completion completion; |
| 280 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 281 | }; |
| 282 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 283 | #define IO_IOPOLL_BATCH 8 |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 284 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 285 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 286 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 287 | |
| 288 | struct io_comp_state { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 289 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 290 | unsigned int nr; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 291 | unsigned int locked_free_nr; |
| 292 | /* inline/task_work completion list, under ->uring_lock */ |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 293 | struct list_head free_list; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 294 | /* IRQ completion list, under ->completion_lock */ |
| 295 | struct list_head locked_free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 298 | struct io_submit_link { |
| 299 | struct io_kiocb *head; |
| 300 | struct io_kiocb *last; |
| 301 | }; |
| 302 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 303 | struct io_submit_state { |
| 304 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 305 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * io_kiocb alloc cache |
| 309 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 310 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 311 | unsigned int free_reqs; |
| 312 | |
| 313 | bool plug_started; |
| 314 | |
| 315 | /* |
| 316 | * Batch completion logic |
| 317 | */ |
| 318 | struct io_comp_state comp; |
| 319 | |
| 320 | /* |
| 321 | * File reference cache |
| 322 | */ |
| 323 | struct file *file; |
| 324 | unsigned int fd; |
| 325 | unsigned int file_refs; |
| 326 | unsigned int ios_left; |
| 327 | }; |
| 328 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 329 | struct io_ring_ctx { |
| 330 | struct { |
| 331 | struct percpu_ref refs; |
| 332 | } ____cacheline_aligned_in_smp; |
| 333 | |
| 334 | struct { |
| 335 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 336 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 337 | unsigned int cq_overflow_flushed: 1; |
| 338 | unsigned int drain_next: 1; |
| 339 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 340 | unsigned int restricted: 1; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 341 | unsigned int sqo_dead: 1; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 342 | unsigned int sqo_exec: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 343 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 344 | /* |
| 345 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 346 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 347 | * |
| 348 | * This indirection could e.g. be used to assign fixed |
| 349 | * io_uring_sqe entries to operations and only submit them to |
| 350 | * the queue when needed. |
| 351 | * |
| 352 | * The kernel modifies neither the indices array nor the entries |
| 353 | * array. |
| 354 | */ |
| 355 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 356 | unsigned cached_sq_head; |
| 357 | unsigned sq_entries; |
| 358 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 359 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 360 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 361 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 362 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 363 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 364 | /* hashed buffered write serialization */ |
| 365 | struct io_wq_hash *hash_map; |
| 366 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 367 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 368 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 369 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 370 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 371 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 372 | } ____cacheline_aligned_in_smp; |
| 373 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 374 | struct { |
| 375 | struct mutex uring_lock; |
| 376 | wait_queue_head_t wait; |
| 377 | } ____cacheline_aligned_in_smp; |
| 378 | |
| 379 | struct io_submit_state submit_state; |
| 380 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 381 | struct io_rings *rings; |
| 382 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 383 | /* |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 384 | * For SQPOLL usage |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 385 | */ |
| 386 | struct task_struct *sqo_task; |
| 387 | |
| 388 | /* Only used for accounting purposes */ |
| 389 | struct mm_struct *mm_account; |
| 390 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 391 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 392 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 393 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 394 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 395 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 396 | /* |
| 397 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 398 | * readers must ensure that ->refs is alive as long as the file* is |
| 399 | * used. Only updated through io_uring_register(2). |
| 400 | */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 401 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 402 | unsigned nr_user_files; |
| 403 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 404 | /* if used, fixed mapped user buffers */ |
| 405 | unsigned nr_user_bufs; |
| 406 | struct io_mapped_ubuf *user_bufs; |
| 407 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 408 | struct user_struct *user; |
| 409 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 410 | struct completion ref_comp; |
| 411 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 412 | |
| 413 | #if defined(CONFIG_UNIX) |
| 414 | struct socket *ring_sock; |
| 415 | #endif |
| 416 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 417 | struct idr io_buffer_idr; |
| 418 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 419 | struct idr personality_idr; |
| 420 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 421 | struct { |
| 422 | unsigned cached_cq_tail; |
| 423 | unsigned cq_entries; |
| 424 | unsigned cq_mask; |
| 425 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 426 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 427 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 428 | struct wait_queue_head cq_wait; |
| 429 | struct fasync_struct *cq_fasync; |
| 430 | struct eventfd_ctx *cq_ev_fd; |
| 431 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 432 | |
| 433 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 434 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 435 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 436 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 437 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 438 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 439 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 440 | * manipulate the list, hence no extra locking is needed there. |
| 441 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 442 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 443 | struct hlist_head *cancel_hash; |
| 444 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 445 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 446 | |
| 447 | spinlock_t inflight_lock; |
| 448 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 449 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 450 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 451 | struct delayed_work rsrc_put_work; |
| 452 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 453 | struct list_head rsrc_ref_list; |
| 454 | spinlock_t rsrc_ref_lock; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 455 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 456 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 457 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 458 | /* exit task_work */ |
| 459 | struct callback_head *exit_task_work; |
| 460 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 461 | struct wait_queue_head hash_wait; |
| 462 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 463 | /* Keep this last, we don't need it for the fast path */ |
| 464 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 467 | /* |
| 468 | * First field must be the file pointer in all the |
| 469 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 470 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 471 | struct io_poll_iocb { |
| 472 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 473 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 474 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 475 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 476 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 477 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 478 | }; |
| 479 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 480 | struct io_poll_remove { |
| 481 | struct file *file; |
| 482 | u64 addr; |
| 483 | }; |
| 484 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 485 | struct io_close { |
| 486 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 487 | int fd; |
| 488 | }; |
| 489 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 490 | struct io_timeout_data { |
| 491 | struct io_kiocb *req; |
| 492 | struct hrtimer timer; |
| 493 | struct timespec64 ts; |
| 494 | enum hrtimer_mode mode; |
| 495 | }; |
| 496 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 497 | struct io_accept { |
| 498 | struct file *file; |
| 499 | struct sockaddr __user *addr; |
| 500 | int __user *addr_len; |
| 501 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 502 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | struct io_sync { |
| 506 | struct file *file; |
| 507 | loff_t len; |
| 508 | loff_t off; |
| 509 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 510 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 511 | }; |
| 512 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 513 | struct io_cancel { |
| 514 | struct file *file; |
| 515 | u64 addr; |
| 516 | }; |
| 517 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 518 | struct io_timeout { |
| 519 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 520 | u32 off; |
| 521 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 522 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 523 | /* head of the link, used by linked timeouts only */ |
| 524 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 525 | }; |
| 526 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 527 | struct io_timeout_rem { |
| 528 | struct file *file; |
| 529 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 530 | |
| 531 | /* timeout update */ |
| 532 | struct timespec64 ts; |
| 533 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 534 | }; |
| 535 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 536 | struct io_rw { |
| 537 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 538 | struct kiocb kiocb; |
| 539 | u64 addr; |
| 540 | u64 len; |
| 541 | }; |
| 542 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 543 | struct io_connect { |
| 544 | struct file *file; |
| 545 | struct sockaddr __user *addr; |
| 546 | int addr_len; |
| 547 | }; |
| 548 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 549 | struct io_sr_msg { |
| 550 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 551 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 552 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 553 | void __user *buf; |
| 554 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 555 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 556 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 557 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 558 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 559 | }; |
| 560 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 561 | struct io_open { |
| 562 | struct file *file; |
| 563 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 564 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 565 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 566 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 567 | }; |
| 568 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 569 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 570 | struct file *file; |
| 571 | u64 arg; |
| 572 | u32 nr_args; |
| 573 | u32 offset; |
| 574 | }; |
| 575 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 576 | struct io_fadvise { |
| 577 | struct file *file; |
| 578 | u64 offset; |
| 579 | u32 len; |
| 580 | u32 advice; |
| 581 | }; |
| 582 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 583 | struct io_madvise { |
| 584 | struct file *file; |
| 585 | u64 addr; |
| 586 | u32 len; |
| 587 | u32 advice; |
| 588 | }; |
| 589 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 590 | struct io_epoll { |
| 591 | struct file *file; |
| 592 | int epfd; |
| 593 | int op; |
| 594 | int fd; |
| 595 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 596 | }; |
| 597 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 598 | struct io_splice { |
| 599 | struct file *file_out; |
| 600 | struct file *file_in; |
| 601 | loff_t off_out; |
| 602 | loff_t off_in; |
| 603 | u64 len; |
| 604 | unsigned int flags; |
| 605 | }; |
| 606 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 607 | struct io_provide_buf { |
| 608 | struct file *file; |
| 609 | __u64 addr; |
| 610 | __s32 len; |
| 611 | __u32 bgid; |
| 612 | __u16 nbufs; |
| 613 | __u16 bid; |
| 614 | }; |
| 615 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 616 | struct io_statx { |
| 617 | struct file *file; |
| 618 | int dfd; |
| 619 | unsigned int mask; |
| 620 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 621 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 622 | struct statx __user *buffer; |
| 623 | }; |
| 624 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 625 | struct io_shutdown { |
| 626 | struct file *file; |
| 627 | int how; |
| 628 | }; |
| 629 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 630 | struct io_rename { |
| 631 | struct file *file; |
| 632 | int old_dfd; |
| 633 | int new_dfd; |
| 634 | struct filename *oldpath; |
| 635 | struct filename *newpath; |
| 636 | int flags; |
| 637 | }; |
| 638 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 639 | struct io_unlink { |
| 640 | struct file *file; |
| 641 | int dfd; |
| 642 | int flags; |
| 643 | struct filename *filename; |
| 644 | }; |
| 645 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 646 | struct io_completion { |
| 647 | struct file *file; |
| 648 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 649 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 650 | }; |
| 651 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 652 | struct io_async_connect { |
| 653 | struct sockaddr_storage address; |
| 654 | }; |
| 655 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 656 | struct io_async_msghdr { |
| 657 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 658 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 659 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 660 | struct sockaddr __user *uaddr; |
| 661 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 662 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 663 | }; |
| 664 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 665 | struct io_async_rw { |
| 666 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 667 | const struct iovec *free_iovec; |
| 668 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 669 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 670 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 671 | }; |
| 672 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 673 | enum { |
| 674 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 675 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 676 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 677 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 678 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 679 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 680 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 681 | REQ_F_FAIL_LINK_BIT, |
| 682 | REQ_F_INFLIGHT_BIT, |
| 683 | REQ_F_CUR_POS_BIT, |
| 684 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 685 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 686 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 687 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 688 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 689 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 690 | REQ_F_NO_FILE_TABLE_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 691 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 692 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 693 | |
| 694 | /* not a real bit, just to check we're not overflowing the space */ |
| 695 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | enum { |
| 699 | /* ctx owns file */ |
| 700 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 701 | /* drain existing IO first */ |
| 702 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 703 | /* linked sqes */ |
| 704 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 705 | /* doesn't sever on completion < 0 */ |
| 706 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 707 | /* IOSQE_ASYNC */ |
| 708 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 709 | /* IOSQE_BUFFER_SELECT */ |
| 710 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 711 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 712 | /* fail rest of links */ |
| 713 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 714 | /* on inflight list */ |
| 715 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 716 | /* read/write uses file position */ |
| 717 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 718 | /* must not punt to workers */ |
| 719 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 720 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 721 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 722 | /* regular file */ |
| 723 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 724 | /* needs cleanup */ |
| 725 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 726 | /* already went through poll handler */ |
| 727 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 728 | /* buffer already selected */ |
| 729 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 730 | /* doesn't need file table for this request */ |
| 731 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 732 | /* linked timeout is active, i.e. prepared by link's head */ |
| 733 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 734 | /* completion is deferred through io_comp_state */ |
| 735 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | struct async_poll { |
| 739 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 740 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 741 | }; |
| 742 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 743 | struct io_task_work { |
| 744 | struct io_wq_work_node node; |
| 745 | task_work_func_t func; |
| 746 | }; |
| 747 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 748 | /* |
| 749 | * NOTE! Each of the iocb union members has the file pointer |
| 750 | * as the first entry in their struct definition. So you can |
| 751 | * access the file pointer through any of the sub-structs, |
| 752 | * or directly as just 'ki_filp' in this struct. |
| 753 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 754 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 755 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 756 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 757 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 758 | struct io_poll_iocb poll; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 759 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 760 | struct io_accept accept; |
| 761 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 762 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 763 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 764 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 765 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 766 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 767 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 768 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 769 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 770 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 771 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 772 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 773 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 774 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 775 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 776 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 777 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 778 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 779 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 780 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 781 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 782 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 783 | /* opcode allocated if it needs to store data for async defer */ |
| 784 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 785 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 786 | /* polled IO has completed */ |
| 787 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 788 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 789 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 790 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 791 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 792 | struct io_ring_ctx *ctx; |
| 793 | unsigned int flags; |
| 794 | refcount_t refs; |
| 795 | struct task_struct *task; |
| 796 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 797 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 798 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 799 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 800 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 801 | /* |
| 802 | * 1. used with ctx->iopoll_list with reads/writes |
| 803 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 804 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 805 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 806 | union { |
| 807 | struct io_task_work io_task_work; |
| 808 | struct callback_head task_work; |
| 809 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 810 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 811 | struct hlist_node hash_node; |
| 812 | struct async_poll *apoll; |
| 813 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 814 | }; |
| 815 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 816 | struct io_defer_entry { |
| 817 | struct list_head list; |
| 818 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 819 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 820 | }; |
| 821 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 823 | /* needs req->file assigned */ |
| 824 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 825 | /* hash wq insertion if file is a regular file */ |
| 826 | unsigned hash_reg_file : 1; |
| 827 | /* unbound wq insertion if file is a non-regular file */ |
| 828 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 829 | /* opcode is not supported by this kernel */ |
| 830 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 831 | /* set if opcode supports polled "wait" */ |
| 832 | unsigned pollin : 1; |
| 833 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 834 | /* op supports buffer selection */ |
| 835 | unsigned buffer_select : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 836 | /* must always have async data allocated */ |
| 837 | unsigned needs_async_data : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 838 | /* should block plug */ |
| 839 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 840 | /* size of async data needed, if any */ |
| 841 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 842 | }; |
| 843 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 844 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 845 | [IORING_OP_NOP] = {}, |
| 846 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 847 | .needs_file = 1, |
| 848 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 849 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 850 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 851 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 852 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 853 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 854 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 855 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 856 | .needs_file = 1, |
| 857 | .hash_reg_file = 1, |
| 858 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 859 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 860 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 861 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 862 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 863 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 864 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 865 | .needs_file = 1, |
| 866 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 867 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 868 | .needs_file = 1, |
| 869 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 870 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 871 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 872 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 873 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 874 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 875 | .needs_file = 1, |
| 876 | .hash_reg_file = 1, |
| 877 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 878 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 879 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 880 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 881 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 882 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 883 | .needs_file = 1, |
| 884 | .unbound_nonreg_file = 1, |
| 885 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 886 | [IORING_OP_POLL_REMOVE] = {}, |
| 887 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 888 | .needs_file = 1, |
| 889 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 890 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 891 | .needs_file = 1, |
| 892 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 893 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 894 | .needs_async_data = 1, |
| 895 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 896 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 897 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 898 | .needs_file = 1, |
| 899 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 900 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 901 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 902 | .needs_async_data = 1, |
| 903 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 904 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 905 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 906 | .needs_async_data = 1, |
| 907 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 908 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 909 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 910 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 911 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 912 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 913 | .needs_file = 1, |
| 914 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 915 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 916 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 917 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 918 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 919 | .needs_async_data = 1, |
| 920 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 921 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 922 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 923 | .needs_file = 1, |
| 924 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 925 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 926 | .needs_async_data = 1, |
| 927 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 928 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 929 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 930 | .needs_file = 1, |
| 931 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 932 | [IORING_OP_OPENAT] = {}, |
| 933 | [IORING_OP_CLOSE] = {}, |
| 934 | [IORING_OP_FILES_UPDATE] = {}, |
| 935 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 936 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 937 | .needs_file = 1, |
| 938 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 939 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 940 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 941 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 942 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 943 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 944 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 945 | .needs_file = 1, |
| 946 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 947 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 948 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 949 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 950 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 951 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 952 | .needs_file = 1, |
| 953 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 954 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 955 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 956 | .needs_file = 1, |
| 957 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 958 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 959 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 960 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 961 | .needs_file = 1, |
| 962 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 963 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 964 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 965 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 966 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 967 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 968 | [IORING_OP_EPOLL_CTL] = { |
| 969 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 970 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 971 | [IORING_OP_SPLICE] = { |
| 972 | .needs_file = 1, |
| 973 | .hash_reg_file = 1, |
| 974 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 975 | }, |
| 976 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 977 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 978 | [IORING_OP_TEE] = { |
| 979 | .needs_file = 1, |
| 980 | .hash_reg_file = 1, |
| 981 | .unbound_nonreg_file = 1, |
| 982 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 983 | [IORING_OP_SHUTDOWN] = { |
| 984 | .needs_file = 1, |
| 985 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 986 | [IORING_OP_RENAMEAT] = {}, |
| 987 | [IORING_OP_UNLINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 988 | }; |
| 989 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 990 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 991 | struct task_struct *task, |
| 992 | struct files_struct *files); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 993 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 994 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node); |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 995 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 996 | struct io_ring_ctx *ctx); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 997 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 998 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 999 | static bool io_rw_reissue(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1000 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1001 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1002 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1003 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1004 | static void io_dismantle_req(struct io_kiocb *req); |
| 1005 | static void io_put_task(struct task_struct *task, int nr); |
| 1006 | static void io_queue_next(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1007 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1008 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1009 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1010 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1011 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1012 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1013 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1014 | static struct file *io_file_get(struct io_submit_state *state, |
| 1015 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1016 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1017 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1018 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 1019 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 1020 | struct iov_iter *iter, bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1021 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1022 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1023 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1024 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1025 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1026 | struct io_ring_ctx *ctx); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1027 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1028 | static struct kmem_cache *req_cachep; |
| 1029 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1030 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1031 | |
| 1032 | struct sock *io_uring_get_socket(struct file *file) |
| 1033 | { |
| 1034 | #if defined(CONFIG_UNIX) |
| 1035 | if (file->f_op == &io_uring_fops) { |
| 1036 | struct io_ring_ctx *ctx = file->private_data; |
| 1037 | |
| 1038 | return ctx->ring_sock->sk; |
| 1039 | } |
| 1040 | #endif |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1044 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1045 | #define io_for_each_link(pos, head) \ |
| 1046 | for (pos = (head); pos; pos = pos->link) |
| 1047 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1048 | static inline void io_clean_op(struct io_kiocb *req) |
| 1049 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1050 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1051 | __io_clean_op(req); |
| 1052 | } |
| 1053 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1054 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1055 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1056 | struct io_ring_ctx *ctx = req->ctx; |
| 1057 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1058 | if (!req->fixed_rsrc_refs) { |
| 1059 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; |
| 1060 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1064 | static bool io_match_task(struct io_kiocb *head, |
| 1065 | struct task_struct *task, |
| 1066 | struct files_struct *files) |
| 1067 | { |
| 1068 | struct io_kiocb *req; |
| 1069 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1070 | if (task && head->task != task) { |
| 1071 | /* in terms of cancelation, always match if req task is dead */ |
| 1072 | if (head->task->flags & PF_EXITING) |
| 1073 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1074 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1075 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1076 | if (!files) |
| 1077 | return true; |
| 1078 | |
| 1079 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1080 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1081 | return true; |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 1082 | if (req->task->files == files) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1083 | return true; |
| 1084 | } |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1088 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1089 | { |
| 1090 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1091 | req->flags |= REQ_F_FAIL_LINK; |
| 1092 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1093 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1094 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1095 | { |
| 1096 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1097 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1098 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1101 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1102 | { |
| 1103 | return !req->timeout.off; |
| 1104 | } |
| 1105 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1106 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1107 | { |
| 1108 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1109 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1110 | |
| 1111 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1112 | if (!ctx) |
| 1113 | return NULL; |
| 1114 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1115 | /* |
| 1116 | * Use 5 bits less than the max cq entries, that should give us around |
| 1117 | * 32 entries per hash list if totally full and uniformly spread. |
| 1118 | */ |
| 1119 | hash_bits = ilog2(p->cq_entries); |
| 1120 | hash_bits -= 5; |
| 1121 | if (hash_bits <= 0) |
| 1122 | hash_bits = 1; |
| 1123 | ctx->cancel_hash_bits = hash_bits; |
| 1124 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1125 | GFP_KERNEL); |
| 1126 | if (!ctx->cancel_hash) |
| 1127 | goto err; |
| 1128 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1129 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1130 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1131 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1132 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1133 | |
| 1134 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1135 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1136 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1137 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1138 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1139 | init_completion(&ctx->ref_comp); |
| 1140 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1141 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1142 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1143 | mutex_init(&ctx->uring_lock); |
| 1144 | init_waitqueue_head(&ctx->wait); |
| 1145 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1146 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1147 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1148 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1149 | spin_lock_init(&ctx->inflight_lock); |
| 1150 | INIT_LIST_HEAD(&ctx->inflight_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1151 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1152 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1153 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1154 | init_llist_head(&ctx->rsrc_put_llist); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1155 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1156 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1157 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1158 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1159 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1160 | kfree(ctx); |
| 1161 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1164 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1165 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1166 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1167 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1168 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1169 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1170 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1171 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1172 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1173 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1174 | } |
| 1175 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1176 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1177 | { |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1178 | if (req->flags & REQ_F_INFLIGHT) { |
| 1179 | struct io_ring_ctx *ctx = req->ctx; |
| 1180 | struct io_uring_task *tctx = req->task->io_uring; |
| 1181 | unsigned long flags; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1182 | |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1183 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1184 | list_del(&req->inflight_entry); |
| 1185 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1186 | req->flags &= ~REQ_F_INFLIGHT; |
| 1187 | if (atomic_read(&tctx->in_idle)) |
| 1188 | wake_up(&tctx->wait); |
| 1189 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1190 | } |
| 1191 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1192 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1193 | { |
| 1194 | struct io_ring_ctx *ctx = req->ctx; |
| 1195 | |
| 1196 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1197 | req->flags |= REQ_F_INFLIGHT; |
| 1198 | |
| 1199 | spin_lock_irq(&ctx->inflight_lock); |
| 1200 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1201 | spin_unlock_irq(&ctx->inflight_lock); |
| 1202 | } |
| 1203 | } |
| 1204 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1205 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1206 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1207 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1208 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1209 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1210 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1211 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1212 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1213 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1214 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1215 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1216 | } else { |
| 1217 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1218 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1219 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1220 | } |
| 1221 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1222 | static void io_prep_async_link(struct io_kiocb *req) |
| 1223 | { |
| 1224 | struct io_kiocb *cur; |
| 1225 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1226 | io_for_each_link(cur, req) |
| 1227 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1228 | } |
| 1229 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1230 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1231 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1232 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1233 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1234 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1235 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1236 | BUG_ON(!tctx); |
| 1237 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1238 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1239 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1240 | &req->work, req->flags); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1241 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1242 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1243 | } |
| 1244 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1245 | static void io_queue_async_work(struct io_kiocb *req) |
| 1246 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1247 | struct io_kiocb *link; |
| 1248 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1249 | /* init ->work of the whole link before punting */ |
| 1250 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1251 | link = __io_queue_async_work(req); |
| 1252 | |
| 1253 | if (link) |
| 1254 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1255 | } |
| 1256 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1257 | static void io_kill_timeout(struct io_kiocb *req) |
| 1258 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1259 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1260 | int ret; |
| 1261 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1262 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1263 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1264 | atomic_set(&req->ctx->cq_timeouts, |
| 1265 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1266 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1267 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1268 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1272 | /* |
| 1273 | * Returns true if we found and killed one or more timeouts |
| 1274 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1275 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1276 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1277 | { |
| 1278 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1279 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1280 | |
| 1281 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1282 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1283 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1284 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1285 | canceled++; |
| 1286 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1287 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1288 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1289 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1290 | } |
| 1291 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1292 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1293 | { |
| 1294 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1295 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1296 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1297 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1298 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1299 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1300 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1301 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1302 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1303 | } while (!list_empty(&ctx->defer_list)); |
| 1304 | } |
| 1305 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1306 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1307 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1308 | u32 seq; |
| 1309 | |
| 1310 | if (list_empty(&ctx->timeout_list)) |
| 1311 | return; |
| 1312 | |
| 1313 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1314 | |
| 1315 | do { |
| 1316 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1317 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1318 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1319 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1320 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1321 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1322 | |
| 1323 | /* |
| 1324 | * Since seq can easily wrap around over time, subtract |
| 1325 | * the last seq at which timeouts were flushed before comparing. |
| 1326 | * Assuming not more than 2^31-1 events have happened since, |
| 1327 | * these subtractions won't have wrapped, so we can check if |
| 1328 | * target is in [last_seq, current_seq] by comparing the two. |
| 1329 | */ |
| 1330 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1331 | events_got = seq - ctx->cq_last_tm_flush; |
| 1332 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1333 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1334 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1335 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1336 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1337 | } while (!list_empty(&ctx->timeout_list)); |
| 1338 | |
| 1339 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1340 | } |
| 1341 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1342 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1343 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1344 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1345 | |
| 1346 | /* order cqe stores with ring update */ |
| 1347 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1348 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1349 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1350 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1351 | } |
| 1352 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1353 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1354 | { |
| 1355 | struct io_rings *r = ctx->rings; |
| 1356 | |
| 1357 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1358 | } |
| 1359 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1360 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1361 | { |
| 1362 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1363 | } |
| 1364 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1365 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1366 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1367 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1368 | unsigned tail; |
| 1369 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1370 | /* |
| 1371 | * writes to the cq entry need to come after reading head; the |
| 1372 | * control dependency is enough as we're using WRITE_ONCE to |
| 1373 | * fill the cq entry |
| 1374 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1375 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1376 | return NULL; |
| 1377 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1378 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1379 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1382 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1383 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1384 | if (!ctx->cq_ev_fd) |
| 1385 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1386 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1387 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1388 | if (!ctx->eventfd_async) |
| 1389 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1390 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1393 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1394 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1395 | /* see waitqueue_active() comment */ |
| 1396 | smp_mb(); |
| 1397 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1398 | if (waitqueue_active(&ctx->wait)) |
| 1399 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1400 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1401 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1402 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 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 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1408 | } |
| 1409 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1410 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1411 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1412 | /* see waitqueue_active() comment */ |
| 1413 | smp_mb(); |
| 1414 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1415 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1416 | if (waitqueue_active(&ctx->wait)) |
| 1417 | wake_up(&ctx->wait); |
| 1418 | } |
| 1419 | if (io_should_trigger_evfd(ctx)) |
| 1420 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1421 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1422 | wake_up_interruptible(&ctx->cq_wait); |
| 1423 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1424 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1427 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1428 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1429 | struct task_struct *tsk, |
| 1430 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1431 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1432 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1433 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1434 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1435 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1436 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1437 | LIST_HEAD(list); |
| 1438 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1439 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1440 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1441 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1442 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1443 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1444 | list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1445 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1446 | continue; |
| 1447 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1448 | cqe = io_get_cqring(ctx); |
| 1449 | if (!cqe && !force) |
| 1450 | break; |
| 1451 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1452 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1453 | if (cqe) { |
| 1454 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1455 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1456 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1457 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1458 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1459 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1460 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1461 | } |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1462 | posted = true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1465 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1466 | if (all_flushed) { |
| 1467 | clear_bit(0, &ctx->sq_check_overflow); |
| 1468 | clear_bit(0, &ctx->cq_check_overflow); |
| 1469 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1470 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1471 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1472 | if (posted) |
| 1473 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1474 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1475 | if (posted) |
| 1476 | io_cqring_ev_posted(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1477 | |
| 1478 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1479 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1480 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1481 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1482 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1483 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1484 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1487 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1488 | struct task_struct *tsk, |
| 1489 | struct files_struct *files) |
| 1490 | { |
| 1491 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1492 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1493 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1494 | mutex_lock(&ctx->uring_lock); |
| 1495 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1496 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1497 | mutex_unlock(&ctx->uring_lock); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1501 | static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1502 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1503 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1504 | struct io_uring_cqe *cqe; |
| 1505 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1506 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1507 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * If we can't get a cq entry, userspace overflowed the |
| 1510 | * submission (by quite a lot). Increment the overflow count in |
| 1511 | * the ring. |
| 1512 | */ |
| 1513 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1514 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1515 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1516 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1517 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1518 | } else if (ctx->cq_overflow_flushed || |
| 1519 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1520 | /* |
| 1521 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1522 | * then we cannot store the request for later flushing, we need |
| 1523 | * to drop it on the floor. |
| 1524 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1525 | ctx->cached_cq_overflow++; |
| 1526 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1527 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1528 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1529 | set_bit(0, &ctx->sq_check_overflow); |
| 1530 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1531 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1532 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1533 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1534 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1535 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1536 | refcount_inc(&req->refs); |
| 1537 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1541 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1542 | { |
| 1543 | __io_cqring_fill_event(req, res, 0); |
| 1544 | } |
| 1545 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1546 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
| 1547 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1548 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1549 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1550 | unsigned long flags; |
| 1551 | |
| 1552 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1553 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1554 | io_commit_cqring(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * If we're the last reference to this request, add to our locked |
| 1557 | * free_list cache. |
| 1558 | */ |
| 1559 | if (refcount_dec_and_test(&req->refs)) { |
| 1560 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1561 | |
| 1562 | io_dismantle_req(req); |
| 1563 | io_put_task(req->task, 1); |
| 1564 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1565 | cs->locked_free_nr++; |
| 1566 | } else |
| 1567 | req = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1568 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1569 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1570 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1571 | if (req) { |
| 1572 | io_queue_next(req); |
| 1573 | percpu_ref_put(&ctx->refs); |
| 1574 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1577 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1578 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1579 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1580 | io_clean_op(req); |
| 1581 | req->result = res; |
| 1582 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1583 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1584 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1585 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1586 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1587 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1588 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1589 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1590 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1591 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1592 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1593 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1594 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1595 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1596 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1597 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1600 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1601 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1602 | struct io_submit_state *state = &ctx->submit_state; |
| 1603 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1604 | struct io_kiocb *req = NULL; |
| 1605 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1606 | /* |
| 1607 | * If we have more than a batch's worth of requests in our IRQ side |
| 1608 | * locked cache, grab the lock and move them over to our submission |
| 1609 | * side cache. |
| 1610 | */ |
| 1611 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { |
| 1612 | spin_lock_irq(&ctx->completion_lock); |
| 1613 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1614 | cs->locked_free_nr = 0; |
| 1615 | spin_unlock_irq(&ctx->completion_lock); |
| 1616 | } |
| 1617 | |
| 1618 | while (!list_empty(&cs->free_list)) { |
| 1619 | req = list_first_entry(&cs->free_list, struct io_kiocb, |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1620 | compl.list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1621 | list_del(&req->compl.list); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1622 | state->reqs[state->free_reqs++] = req; |
| 1623 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) |
| 1624 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1625 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1626 | |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1627 | return req != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1630 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1631 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1632 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1633 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1634 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1635 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1636 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1637 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1638 | int ret; |
| 1639 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1640 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1641 | goto got_req; |
| 1642 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1643 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1644 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1645 | |
| 1646 | /* |
| 1647 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1648 | * retry single alloc to be on the safe side. |
| 1649 | */ |
| 1650 | if (unlikely(ret <= 0)) { |
| 1651 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1652 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1653 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1654 | ret = 1; |
| 1655 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1656 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1657 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1658 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1659 | state->free_reqs--; |
| 1660 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1663 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1664 | bool fixed) |
| 1665 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1666 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1667 | fput(file); |
| 1668 | } |
| 1669 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1670 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1671 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1672 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1673 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1674 | if (req->async_data) |
| 1675 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1676 | if (req->file) |
| 1677 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1678 | if (req->fixed_rsrc_refs) |
| 1679 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1680 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1681 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1682 | |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1683 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1684 | { |
| 1685 | struct io_uring_task *tctx = task->io_uring; |
| 1686 | |
| 1687 | percpu_counter_sub(&tctx->inflight, nr); |
| 1688 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1689 | wake_up(&tctx->wait); |
| 1690 | put_task_struct_many(task, nr); |
| 1691 | } |
| 1692 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1693 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1694 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1695 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1696 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1697 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1698 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1699 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1700 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1701 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1702 | } |
| 1703 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1704 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1705 | { |
| 1706 | struct io_kiocb *nxt = req->link; |
| 1707 | |
| 1708 | req->link = nxt->link; |
| 1709 | nxt->link = NULL; |
| 1710 | } |
| 1711 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1712 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1713 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1714 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1715 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1716 | bool cancelled = false; |
| 1717 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1718 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1719 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1720 | link = req->link; |
| 1721 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 1722 | /* |
| 1723 | * Can happen if a linked timeout fired and link had been like |
| 1724 | * req -> link t-out -> link t-out [-> ...] |
| 1725 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1726 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 1727 | struct io_timeout_data *io = link->async_data; |
| 1728 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1729 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1730 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 1731 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1732 | ret = hrtimer_try_to_cancel(&io->timer); |
| 1733 | if (ret != -1) { |
| 1734 | io_cqring_fill_event(link, -ECANCELED); |
| 1735 | io_commit_cqring(ctx); |
| 1736 | cancelled = true; |
| 1737 | } |
| 1738 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1739 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1740 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1741 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1742 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1743 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1744 | io_put_req(link); |
| 1745 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1746 | } |
| 1747 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1748 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1749 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1750 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1751 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1752 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1753 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1754 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1755 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1756 | link = req->link; |
| 1757 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1758 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1759 | while (link) { |
| 1760 | nxt = link->link; |
| 1761 | link->link = NULL; |
| 1762 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1763 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1764 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1765 | |
Jens Axboe | 1575f21 | 2021-02-27 15:20:49 -0700 | [diff] [blame] | 1766 | io_put_req_deferred(link, 2); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1767 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1768 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1769 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1770 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1771 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1772 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1773 | } |
| 1774 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1775 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1776 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1777 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 1778 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1779 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1780 | /* |
| 1781 | * If LINK is set, we have dependent requests in this chain. If we |
| 1782 | * didn't fail this request, queue the first one up, moving any other |
| 1783 | * dependencies to the next request. In case of failure, fail the rest |
| 1784 | * of the chain. |
| 1785 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1786 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 1787 | struct io_kiocb *nxt = req->link; |
| 1788 | |
| 1789 | req->link = NULL; |
| 1790 | return nxt; |
| 1791 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1792 | io_fail_links(req); |
| 1793 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1794 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1795 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1796 | 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] | 1797 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 1798 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1799 | return NULL; |
| 1800 | return __io_req_find_next(req); |
| 1801 | } |
| 1802 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1803 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 1804 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1805 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1806 | struct io_wq_work_list list; |
| 1807 | struct io_wq_work_node *node; |
| 1808 | |
| 1809 | if (wq_list_empty(&tctx->task_list)) |
| 1810 | return false; |
| 1811 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1812 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1813 | list = tctx->task_list; |
| 1814 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1815 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1816 | |
| 1817 | node = list.first; |
| 1818 | while (node) { |
| 1819 | struct io_wq_work_node *next = node->next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1820 | struct io_ring_ctx *this_ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1821 | struct io_kiocb *req; |
| 1822 | |
| 1823 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1824 | this_ctx = req->ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1825 | req->task_work.func(&req->task_work); |
| 1826 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1827 | |
| 1828 | if (!ctx) { |
| 1829 | ctx = this_ctx; |
| 1830 | } else if (ctx != this_ctx) { |
| 1831 | mutex_lock(&ctx->uring_lock); |
| 1832 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1833 | mutex_unlock(&ctx->uring_lock); |
| 1834 | ctx = this_ctx; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | if (ctx && ctx->submit_state.comp.nr) { |
| 1839 | mutex_lock(&ctx->uring_lock); |
| 1840 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1841 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | return list.first != NULL; |
| 1845 | } |
| 1846 | |
| 1847 | static void tctx_task_work(struct callback_head *cb) |
| 1848 | { |
| 1849 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 1850 | |
Jens Axboe | 1d5f360 | 2021-02-26 14:54:16 -0700 | [diff] [blame] | 1851 | clear_bit(0, &tctx->task_state); |
| 1852 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1853 | while (__tctx_task_work(tctx)) |
| 1854 | cond_resched(); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, |
| 1858 | enum task_work_notify_mode notify) |
| 1859 | { |
| 1860 | struct io_uring_task *tctx = tsk->io_uring; |
| 1861 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1862 | unsigned long flags; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1863 | int ret; |
| 1864 | |
| 1865 | WARN_ON_ONCE(!tctx); |
| 1866 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1867 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1868 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1869 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1870 | |
| 1871 | /* task_work already pending, we're done */ |
| 1872 | if (test_bit(0, &tctx->task_state) || |
| 1873 | test_and_set_bit(0, &tctx->task_state)) |
| 1874 | return 0; |
| 1875 | |
| 1876 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 1877 | return 0; |
| 1878 | |
| 1879 | /* |
| 1880 | * Slow path - we failed, find and delete work. if the work is not |
| 1881 | * in the list, it got run and we're fine. |
| 1882 | */ |
| 1883 | ret = 0; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1884 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1885 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 1886 | if (&req->io_task_work.node == node) { |
| 1887 | wq_list_del(&tctx->task_list, node, prev); |
| 1888 | ret = 1; |
| 1889 | break; |
| 1890 | } |
| 1891 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1892 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1893 | clear_bit(0, &tctx->task_state); |
| 1894 | return ret; |
| 1895 | } |
| 1896 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1897 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1898 | { |
| 1899 | struct task_struct *tsk = req->task; |
| 1900 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 1901 | enum task_work_notify_mode notify; |
| 1902 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1903 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 1904 | if (tsk->flags & PF_EXITING) |
| 1905 | return -ESRCH; |
| 1906 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1907 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1908 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1909 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1910 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1911 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1912 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 1913 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1914 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1915 | notify = TWA_SIGNAL; |
| 1916 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1917 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1918 | if (!ret) |
| 1919 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1920 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1921 | return ret; |
| 1922 | } |
| 1923 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1924 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1925 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1926 | { |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 1927 | struct io_ring_ctx *ctx = req->ctx; |
| 1928 | struct callback_head *head; |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1929 | |
| 1930 | init_task_work(&req->task_work, cb); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 1931 | do { |
| 1932 | head = READ_ONCE(ctx->exit_task_work); |
| 1933 | req->task_work.next = head; |
| 1934 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1937 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 1938 | { |
| 1939 | struct io_ring_ctx *ctx = req->ctx; |
| 1940 | |
| 1941 | spin_lock_irq(&ctx->completion_lock); |
| 1942 | io_cqring_fill_event(req, error); |
| 1943 | io_commit_cqring(ctx); |
| 1944 | spin_unlock_irq(&ctx->completion_lock); |
| 1945 | |
| 1946 | io_cqring_ev_posted(ctx); |
| 1947 | req_set_fail_links(req); |
| 1948 | io_double_put_req(req); |
| 1949 | } |
| 1950 | |
| 1951 | static void io_req_task_cancel(struct callback_head *cb) |
| 1952 | { |
| 1953 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 1954 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1955 | |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 1956 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 1957 | __io_req_task_cancel(req, req->result); |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 1958 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 1959 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | static void __io_req_task_submit(struct io_kiocb *req) |
| 1963 | { |
| 1964 | struct io_ring_ctx *ctx = req->ctx; |
| 1965 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 1966 | /* 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] | 1967 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 1968 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && !current->in_execve) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1969 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 1970 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1971 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 1972 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1973 | } |
| 1974 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1975 | static void io_req_task_submit(struct callback_head *cb) |
| 1976 | { |
| 1977 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1978 | |
| 1979 | __io_req_task_submit(req); |
| 1980 | } |
| 1981 | |
| 1982 | static void io_req_task_queue(struct io_kiocb *req) |
| 1983 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1984 | int ret; |
| 1985 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1986 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1987 | ret = io_req_task_work_add(req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1988 | if (unlikely(ret)) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 1989 | req->result = -ECANCELED; |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 1990 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1991 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1992 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1993 | } |
| 1994 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 1995 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 1996 | { |
| 1997 | percpu_ref_get(&req->ctx->refs); |
| 1998 | req->result = ret; |
| 1999 | req->task_work.func = io_req_task_cancel; |
| 2000 | |
| 2001 | if (unlikely(io_req_task_work_add(req))) |
| 2002 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
| 2003 | } |
| 2004 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2005 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2006 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2007 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2008 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2009 | if (nxt) |
| 2010 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2011 | } |
| 2012 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2013 | static void io_free_req(struct io_kiocb *req) |
| 2014 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2015 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2016 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2017 | } |
| 2018 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2019 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2020 | struct task_struct *task; |
| 2021 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2022 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2023 | }; |
| 2024 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2025 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2026 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2027 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2028 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2029 | rb->task = NULL; |
| 2030 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2031 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2032 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2033 | struct req_batch *rb) |
| 2034 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2035 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2036 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2037 | if (rb->ctx_refs) |
| 2038 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2039 | } |
| 2040 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2041 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2042 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2043 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2044 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2045 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2046 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2047 | if (rb->task) |
| 2048 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2049 | rb->task = req->task; |
| 2050 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2051 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2052 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2053 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2054 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2055 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2056 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2057 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2058 | else |
| 2059 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2060 | } |
| 2061 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2062 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2063 | struct io_ring_ctx *ctx) |
| 2064 | { |
| 2065 | int i, nr = cs->nr; |
| 2066 | struct io_kiocb *req; |
| 2067 | struct req_batch rb; |
| 2068 | |
| 2069 | io_init_req_batch(&rb); |
| 2070 | spin_lock_irq(&ctx->completion_lock); |
| 2071 | for (i = 0; i < nr; i++) { |
| 2072 | req = cs->reqs[i]; |
| 2073 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2074 | } |
| 2075 | io_commit_cqring(ctx); |
| 2076 | spin_unlock_irq(&ctx->completion_lock); |
| 2077 | |
| 2078 | io_cqring_ev_posted(ctx); |
| 2079 | for (i = 0; i < nr; i++) { |
| 2080 | req = cs->reqs[i]; |
| 2081 | |
| 2082 | /* submission and completion refs */ |
| 2083 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2084 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | io_req_free_batch_finish(ctx, &rb); |
| 2088 | cs->nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2089 | } |
| 2090 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2091 | /* |
| 2092 | * Drop reference to request, return next in chain (if there is one) if this |
| 2093 | * was the last reference to this request. |
| 2094 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2095 | static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2096 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2097 | struct io_kiocb *nxt = NULL; |
| 2098 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2099 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2100 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2101 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2102 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2103 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2106 | static void io_put_req(struct io_kiocb *req) |
| 2107 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2108 | if (refcount_dec_and_test(&req->refs)) |
| 2109 | io_free_req(req); |
| 2110 | } |
| 2111 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2112 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2113 | { |
| 2114 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2115 | |
| 2116 | io_free_req(req); |
| 2117 | } |
| 2118 | |
| 2119 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2120 | { |
| 2121 | int ret; |
| 2122 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2123 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2124 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2125 | if (unlikely(ret)) |
| 2126 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2130 | { |
| 2131 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2132 | io_free_req_deferred(req); |
| 2133 | } |
| 2134 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2135 | static void io_double_put_req(struct io_kiocb *req) |
| 2136 | { |
| 2137 | /* drop both submit and complete references */ |
| 2138 | if (refcount_sub_and_test(2, &req->refs)) |
| 2139 | io_free_req(req); |
| 2140 | } |
| 2141 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2142 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2143 | { |
| 2144 | /* See comment at the top of this file */ |
| 2145 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2146 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2147 | } |
| 2148 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2149 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2150 | { |
| 2151 | struct io_rings *rings = ctx->rings; |
| 2152 | |
| 2153 | /* make sure SQ entry isn't read before tail */ |
| 2154 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2155 | } |
| 2156 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2157 | 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] | 2158 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2159 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2160 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2161 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2162 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2163 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2164 | kfree(kbuf); |
| 2165 | return cflags; |
| 2166 | } |
| 2167 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2168 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2169 | { |
| 2170 | struct io_buffer *kbuf; |
| 2171 | |
| 2172 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2173 | return io_put_kbuf(req, kbuf); |
| 2174 | } |
| 2175 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2176 | static inline bool io_run_task_work(void) |
| 2177 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2178 | /* |
| 2179 | * Not safe to run on exiting task, and the task_work handling will |
| 2180 | * not add work to such a task. |
| 2181 | */ |
| 2182 | if (unlikely(current->flags & PF_EXITING)) |
| 2183 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2184 | if (current->task_works) { |
| 2185 | __set_current_state(TASK_RUNNING); |
| 2186 | task_work_run(); |
| 2187 | return true; |
| 2188 | } |
| 2189 | |
| 2190 | return false; |
| 2191 | } |
| 2192 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2193 | /* |
| 2194 | * Find and free completed poll iocbs |
| 2195 | */ |
| 2196 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2197 | struct list_head *done) |
| 2198 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2199 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2200 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2201 | |
| 2202 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2203 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2204 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2205 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2206 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2207 | int cflags = 0; |
| 2208 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2209 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2210 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2211 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2212 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2213 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2214 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2215 | continue; |
| 2216 | } |
| 2217 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2218 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2219 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2220 | |
| 2221 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2222 | (*nr_events)++; |
| 2223 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2224 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2225 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2226 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2227 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2228 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2229 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2230 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2233 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2234 | long min) |
| 2235 | { |
| 2236 | struct io_kiocb *req, *tmp; |
| 2237 | LIST_HEAD(done); |
| 2238 | bool spin; |
| 2239 | int ret; |
| 2240 | |
| 2241 | /* |
| 2242 | * Only spin for completions if we don't have multiple devices hanging |
| 2243 | * off our complete list, and we're under the requested amount. |
| 2244 | */ |
| 2245 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2246 | |
| 2247 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2248 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2249 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2250 | |
| 2251 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2252 | * Move completed and retryable entries to our local lists. |
| 2253 | * If we find a request that requires polling, break out |
| 2254 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2255 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2256 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2257 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2258 | continue; |
| 2259 | } |
| 2260 | if (!list_empty(&done)) |
| 2261 | break; |
| 2262 | |
| 2263 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2264 | if (ret < 0) |
| 2265 | break; |
| 2266 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2267 | /* iopoll may have completed current req */ |
| 2268 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2269 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2270 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2271 | if (ret && spin) |
| 2272 | spin = false; |
| 2273 | ret = 0; |
| 2274 | } |
| 2275 | |
| 2276 | if (!list_empty(&done)) |
| 2277 | io_iopoll_complete(ctx, nr_events, &done); |
| 2278 | |
| 2279 | return ret; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2283 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2284 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2285 | * as a non-spinning completion check. |
| 2286 | */ |
| 2287 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2288 | long min) |
| 2289 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2290 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2291 | int ret; |
| 2292 | |
| 2293 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2294 | if (ret < 0) |
| 2295 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2296 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | return 1; |
| 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * We can't just wait for polled events to come to us, we have to actively |
| 2305 | * find and complete them. |
| 2306 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2307 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2308 | { |
| 2309 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2310 | return; |
| 2311 | |
| 2312 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2313 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2314 | unsigned int nr_events = 0; |
| 2315 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2316 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2317 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2318 | /* let it sleep and repeat later if can't complete a request */ |
| 2319 | if (nr_events == 0) |
| 2320 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2321 | /* |
| 2322 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2323 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2324 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2325 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2326 | if (need_resched()) { |
| 2327 | mutex_unlock(&ctx->uring_lock); |
| 2328 | cond_resched(); |
| 2329 | mutex_lock(&ctx->uring_lock); |
| 2330 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2331 | } |
| 2332 | mutex_unlock(&ctx->uring_lock); |
| 2333 | } |
| 2334 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2335 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2336 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2337 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2338 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2339 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2340 | /* |
| 2341 | * We disallow the app entering submit/complete with polling, but we |
| 2342 | * still need to lock the ring to prevent racing with polled issue |
| 2343 | * that got punted to a workqueue. |
| 2344 | */ |
| 2345 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2346 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2347 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2348 | * Don't enter poll loop if we already have events pending. |
| 2349 | * If we do, we can potentially be spinning for commands that |
| 2350 | * already triggered a CQE (eg in error). |
| 2351 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2352 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2353 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2354 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2355 | break; |
| 2356 | |
| 2357 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2358 | * If a submit got punted to a workqueue, we can have the |
| 2359 | * application entering polling for a command before it gets |
| 2360 | * issued. That app will hold the uring_lock for the duration |
| 2361 | * of the poll right here, so we need to take a breather every |
| 2362 | * now and then to ensure that the issue has a chance to add |
| 2363 | * the poll to the issued list. Otherwise we can spin here |
| 2364 | * forever, while the workqueue is stuck trying to acquire the |
| 2365 | * very same mutex. |
| 2366 | */ |
| 2367 | if (!(++iters & 7)) { |
| 2368 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2369 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2370 | mutex_lock(&ctx->uring_lock); |
| 2371 | } |
| 2372 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2373 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2374 | if (ret <= 0) |
| 2375 | break; |
| 2376 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2377 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2378 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2379 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2380 | return ret; |
| 2381 | } |
| 2382 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2383 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2384 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2385 | /* |
| 2386 | * Tell lockdep we inherited freeze protection from submission |
| 2387 | * thread. |
| 2388 | */ |
| 2389 | if (req->flags & REQ_F_ISREG) { |
| 2390 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2391 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2392 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2393 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2394 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2397 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2398 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2399 | { |
| 2400 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2401 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2402 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2403 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2404 | /* already prepared */ |
| 2405 | if (req->async_data) |
| 2406 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2407 | |
| 2408 | switch (req->opcode) { |
| 2409 | case IORING_OP_READV: |
| 2410 | case IORING_OP_READ_FIXED: |
| 2411 | case IORING_OP_READ: |
| 2412 | rw = READ; |
| 2413 | break; |
| 2414 | case IORING_OP_WRITEV: |
| 2415 | case IORING_OP_WRITE_FIXED: |
| 2416 | case IORING_OP_WRITE: |
| 2417 | rw = WRITE; |
| 2418 | break; |
| 2419 | default: |
| 2420 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2421 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2422 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2423 | } |
| 2424 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2425 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2426 | if (ret < 0) |
| 2427 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2428 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2429 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2430 | #endif |
| 2431 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2432 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2433 | { |
| 2434 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2435 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2436 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2437 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2438 | return false; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2439 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2440 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2441 | /* |
| 2442 | * If ref is dying, we might be running poll reap from the exit work. |
| 2443 | * Don't attempt to reissue from that path, just let it fail with |
| 2444 | * -EAGAIN. |
| 2445 | */ |
| 2446 | if (percpu_ref_is_dying(&req->ctx->refs)) |
| 2447 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2448 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2449 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2450 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 2451 | if (io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2452 | refcount_inc(&req->refs); |
| 2453 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2454 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2455 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2456 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2457 | #endif |
| 2458 | return false; |
| 2459 | } |
| 2460 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2461 | 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] | 2462 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2463 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2464 | int cflags = 0; |
| 2465 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2466 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2467 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2468 | if (res != req->result) |
| 2469 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2470 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2471 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2472 | kiocb_end_write(req); |
| 2473 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2474 | cflags = io_put_rw_kbuf(req); |
| 2475 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2479 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2480 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2481 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2482 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2485 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2486 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2487 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2488 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2489 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2490 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2491 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2492 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2493 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2494 | |
| 2495 | WRITE_ONCE(req->result, res); |
| 2496 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2497 | smp_wmb(); |
| 2498 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | /* |
| 2502 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2503 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2504 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2505 | * accessing the kiocb cookie. |
| 2506 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2507 | 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] | 2508 | { |
| 2509 | struct io_ring_ctx *ctx = req->ctx; |
| 2510 | |
| 2511 | /* |
| 2512 | * Track whether we have multiple files in our lists. This will impact |
| 2513 | * how we do polling eventually, not spinning if we're on potentially |
| 2514 | * different devices. |
| 2515 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2516 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2517 | ctx->poll_multi_file = false; |
| 2518 | } else if (!ctx->poll_multi_file) { |
| 2519 | struct io_kiocb *list_req; |
| 2520 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2521 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2522 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2523 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2524 | ctx->poll_multi_file = true; |
| 2525 | } |
| 2526 | |
| 2527 | /* |
| 2528 | * For fast devices, IO may have already completed. If it has, add |
| 2529 | * it to the front so we find it first. |
| 2530 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2531 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2532 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2533 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2534 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2535 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2536 | /* |
| 2537 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2538 | * task context or in io worker task context. If current task context is |
| 2539 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2540 | */ |
| 2541 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2542 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2543 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2546 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2547 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2548 | if (state->file_refs) { |
| 2549 | fput_many(state->file, state->file_refs); |
| 2550 | state->file_refs = 0; |
| 2551 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | /* |
| 2555 | * Get as many references to a file as we have IOs left in this submission, |
| 2556 | * assuming most submissions are for one file, or at least that each file |
| 2557 | * has more than one submission. |
| 2558 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2559 | 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] | 2560 | { |
| 2561 | if (!state) |
| 2562 | return fget(fd); |
| 2563 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2564 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2565 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2566 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2567 | return state->file; |
| 2568 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2569 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2570 | } |
| 2571 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2572 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2573 | return NULL; |
| 2574 | |
| 2575 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2576 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2577 | return state->file; |
| 2578 | } |
| 2579 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2580 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2581 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2582 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2583 | } |
| 2584 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2585 | /* |
| 2586 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2587 | * any file. For now, just ensure that anything potentially problematic is done |
| 2588 | * inline. |
| 2589 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2590 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2591 | { |
| 2592 | umode_t mode = file_inode(file)->i_mode; |
| 2593 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2594 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2595 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2596 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2597 | return true; |
| 2598 | return false; |
| 2599 | } |
| 2600 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2601 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2602 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2603 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2604 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2605 | file->f_op != &io_uring_fops) |
| 2606 | return true; |
| 2607 | return false; |
| 2608 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2609 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2610 | /* any ->read/write should understand O_NONBLOCK */ |
| 2611 | if (file->f_flags & O_NONBLOCK) |
| 2612 | return true; |
| 2613 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2614 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2615 | return false; |
| 2616 | |
| 2617 | if (rw == READ) |
| 2618 | return file->f_op->read_iter != NULL; |
| 2619 | |
| 2620 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2623 | 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] | 2624 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2625 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2626 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2627 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2628 | unsigned ioprio; |
| 2629 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2630 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2631 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2632 | req->flags |= REQ_F_ISREG; |
| 2633 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2634 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2635 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2636 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2637 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2638 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2639 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2640 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2641 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2642 | if (unlikely(ret)) |
| 2643 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2644 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2645 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 2646 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 2647 | req->flags |= REQ_F_NOWAIT; |
| 2648 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2649 | ioprio = READ_ONCE(sqe->ioprio); |
| 2650 | if (ioprio) { |
| 2651 | ret = ioprio_check_cap(ioprio); |
| 2652 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2653 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2654 | |
| 2655 | kiocb->ki_ioprio = ioprio; |
| 2656 | } else |
| 2657 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2658 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2659 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2660 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2661 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2662 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2663 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2664 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2665 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2666 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2667 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2668 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2669 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2670 | kiocb->ki_complete = io_complete_rw; |
| 2671 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2672 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2673 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2674 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2675 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2676 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2680 | { |
| 2681 | switch (ret) { |
| 2682 | case -EIOCBQUEUED: |
| 2683 | break; |
| 2684 | case -ERESTARTSYS: |
| 2685 | case -ERESTARTNOINTR: |
| 2686 | case -ERESTARTNOHAND: |
| 2687 | case -ERESTART_RESTARTBLOCK: |
| 2688 | /* |
| 2689 | * We can't just restart the syscall, since previously |
| 2690 | * submitted sqes may already be in progress. Just fail this |
| 2691 | * IO with EINTR. |
| 2692 | */ |
| 2693 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2694 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2695 | default: |
| 2696 | kiocb->ki_complete(kiocb, ret, 0); |
| 2697 | } |
| 2698 | } |
| 2699 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2700 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2701 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2702 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2703 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2704 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2705 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2706 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2707 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2708 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2709 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2710 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2711 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2712 | } |
| 2713 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2714 | if (req->flags & REQ_F_CUR_POS) |
| 2715 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2716 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2717 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2718 | else |
| 2719 | io_rw_done(kiocb, ret); |
| 2720 | } |
| 2721 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2722 | 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] | 2723 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2724 | struct io_ring_ctx *ctx = req->ctx; |
| 2725 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2726 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 2727 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2728 | size_t offset; |
| 2729 | u64 buf_addr; |
| 2730 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2731 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2732 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2733 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2734 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2735 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2736 | |
| 2737 | /* overflow */ |
| 2738 | if (buf_addr + len < buf_addr) |
| 2739 | return -EFAULT; |
| 2740 | /* not inside the mapped region */ |
| 2741 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2742 | return -EFAULT; |
| 2743 | |
| 2744 | /* |
| 2745 | * May not be a start of buffer, set size appropriately |
| 2746 | * and advance us to the beginning. |
| 2747 | */ |
| 2748 | offset = buf_addr - imu->ubuf; |
| 2749 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2750 | |
| 2751 | if (offset) { |
| 2752 | /* |
| 2753 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2754 | * using the latter parts of a big fixed buffer - it iterates |
| 2755 | * over each segment manually. We can cheat a bit here, because |
| 2756 | * we know that: |
| 2757 | * |
| 2758 | * 1) it's a BVEC iter, we set it up |
| 2759 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2760 | * first and last bvec |
| 2761 | * |
| 2762 | * So just find our index, and adjust the iterator afterwards. |
| 2763 | * If the offset is within the first bvec (or the whole first |
| 2764 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2765 | * since we can just skip the first segment, which may not |
| 2766 | * be PAGE_SIZE aligned. |
| 2767 | */ |
| 2768 | const struct bio_vec *bvec = imu->bvec; |
| 2769 | |
| 2770 | if (offset <= bvec->bv_len) { |
| 2771 | iov_iter_advance(iter, offset); |
| 2772 | } else { |
| 2773 | unsigned long seg_skip; |
| 2774 | |
| 2775 | /* skip first vec */ |
| 2776 | offset -= bvec->bv_len; |
| 2777 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2778 | |
| 2779 | iter->bvec = bvec + seg_skip; |
| 2780 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2781 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2782 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2783 | } |
| 2784 | } |
| 2785 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2786 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2789 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2790 | { |
| 2791 | if (needs_lock) |
| 2792 | mutex_unlock(&ctx->uring_lock); |
| 2793 | } |
| 2794 | |
| 2795 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2796 | { |
| 2797 | /* |
| 2798 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2799 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2800 | * The only exception is when we've detached the request and issue it |
| 2801 | * from an async worker thread, grab the lock for that case. |
| 2802 | */ |
| 2803 | if (needs_lock) |
| 2804 | mutex_lock(&ctx->uring_lock); |
| 2805 | } |
| 2806 | |
| 2807 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2808 | int bgid, struct io_buffer *kbuf, |
| 2809 | bool needs_lock) |
| 2810 | { |
| 2811 | struct io_buffer *head; |
| 2812 | |
| 2813 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2814 | return kbuf; |
| 2815 | |
| 2816 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2817 | |
| 2818 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2819 | |
| 2820 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2821 | if (head) { |
| 2822 | if (!list_empty(&head->list)) { |
| 2823 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2824 | list); |
| 2825 | list_del(&kbuf->list); |
| 2826 | } else { |
| 2827 | kbuf = head; |
| 2828 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2829 | } |
| 2830 | if (*len > kbuf->len) |
| 2831 | *len = kbuf->len; |
| 2832 | } else { |
| 2833 | kbuf = ERR_PTR(-ENOBUFS); |
| 2834 | } |
| 2835 | |
| 2836 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2837 | |
| 2838 | return kbuf; |
| 2839 | } |
| 2840 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2841 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2842 | bool needs_lock) |
| 2843 | { |
| 2844 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2845 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2846 | |
| 2847 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2848 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2849 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2850 | if (IS_ERR(kbuf)) |
| 2851 | return kbuf; |
| 2852 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2853 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2854 | return u64_to_user_ptr(kbuf->addr); |
| 2855 | } |
| 2856 | |
| 2857 | #ifdef CONFIG_COMPAT |
| 2858 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2859 | bool needs_lock) |
| 2860 | { |
| 2861 | struct compat_iovec __user *uiov; |
| 2862 | compat_ssize_t clen; |
| 2863 | void __user *buf; |
| 2864 | ssize_t len; |
| 2865 | |
| 2866 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2867 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2868 | return -EFAULT; |
| 2869 | if (__get_user(clen, &uiov->iov_len)) |
| 2870 | return -EFAULT; |
| 2871 | if (clen < 0) |
| 2872 | return -EINVAL; |
| 2873 | |
| 2874 | len = clen; |
| 2875 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2876 | if (IS_ERR(buf)) |
| 2877 | return PTR_ERR(buf); |
| 2878 | iov[0].iov_base = buf; |
| 2879 | iov[0].iov_len = (compat_size_t) len; |
| 2880 | return 0; |
| 2881 | } |
| 2882 | #endif |
| 2883 | |
| 2884 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2885 | bool needs_lock) |
| 2886 | { |
| 2887 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2888 | void __user *buf; |
| 2889 | ssize_t len; |
| 2890 | |
| 2891 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2892 | return -EFAULT; |
| 2893 | |
| 2894 | len = iov[0].iov_len; |
| 2895 | if (len < 0) |
| 2896 | return -EINVAL; |
| 2897 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2898 | if (IS_ERR(buf)) |
| 2899 | return PTR_ERR(buf); |
| 2900 | iov[0].iov_base = buf; |
| 2901 | iov[0].iov_len = len; |
| 2902 | return 0; |
| 2903 | } |
| 2904 | |
| 2905 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2906 | bool needs_lock) |
| 2907 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2908 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2909 | struct io_buffer *kbuf; |
| 2910 | |
| 2911 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2912 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2913 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2914 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2915 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 2916 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2917 | return -EINVAL; |
| 2918 | |
| 2919 | #ifdef CONFIG_COMPAT |
| 2920 | if (req->ctx->compat) |
| 2921 | return io_compat_import(req, iov, needs_lock); |
| 2922 | #endif |
| 2923 | |
| 2924 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2925 | } |
| 2926 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2927 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 2928 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2929 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2930 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2931 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2932 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2933 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2934 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2935 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2936 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2937 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2938 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2939 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2940 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2941 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2942 | return -EINVAL; |
| 2943 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2944 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2945 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2946 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 2947 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2948 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2949 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2950 | } |
| 2951 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2952 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2953 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 2954 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2957 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2958 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2959 | if (!ret) |
| 2960 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2961 | *iovec = NULL; |
| 2962 | return ret; |
| 2963 | } |
| 2964 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 2965 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 2966 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2969 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 2970 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 2971 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2972 | } |
| 2973 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2974 | /* |
| 2975 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2976 | * by looping over ->read() or ->write() manually. |
| 2977 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 2978 | 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] | 2979 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 2980 | struct kiocb *kiocb = &req->rw.kiocb; |
| 2981 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2982 | ssize_t ret = 0; |
| 2983 | |
| 2984 | /* |
| 2985 | * Don't support polled IO through this interface, and we can't |
| 2986 | * support non-blocking either. For the latter, this just causes |
| 2987 | * the kiocb to be handled from an async context. |
| 2988 | */ |
| 2989 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2990 | return -EOPNOTSUPP; |
| 2991 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2992 | return -EAGAIN; |
| 2993 | |
| 2994 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2995 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2996 | ssize_t nr; |
| 2997 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2998 | if (!iov_iter_is_bvec(iter)) { |
| 2999 | iovec = iov_iter_iovec(iter); |
| 3000 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3001 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3002 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3003 | } |
| 3004 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3005 | if (rw == READ) { |
| 3006 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3007 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3008 | } else { |
| 3009 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3010 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | if (nr < 0) { |
| 3014 | if (!ret) |
| 3015 | ret = nr; |
| 3016 | break; |
| 3017 | } |
| 3018 | ret += nr; |
| 3019 | if (nr != iovec.iov_len) |
| 3020 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3021 | req->rw.len -= nr; |
| 3022 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3023 | iov_iter_advance(iter, nr); |
| 3024 | } |
| 3025 | |
| 3026 | return ret; |
| 3027 | } |
| 3028 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3029 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3030 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3031 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3032 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3033 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3034 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3035 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3036 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3037 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3038 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3039 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3040 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3041 | unsigned iov_off = 0; |
| 3042 | |
| 3043 | rw->iter.iov = rw->fast_iov; |
| 3044 | if (iter->iov != fast_iov) { |
| 3045 | iov_off = iter->iov - fast_iov; |
| 3046 | rw->iter.iov += iov_off; |
| 3047 | } |
| 3048 | if (rw->fast_iov != fast_iov) |
| 3049 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3050 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3051 | } else { |
| 3052 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3056 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3057 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3058 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3059 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3060 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3061 | } |
| 3062 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3063 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3064 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3065 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3066 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3067 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3068 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3069 | } |
| 3070 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3071 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3072 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3073 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3074 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3075 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3076 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3077 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3078 | if (__io_alloc_async_data(req)) { |
| 3079 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3080 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3081 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3082 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3083 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3084 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3085 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3088 | 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] | 3089 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3090 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3091 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3092 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3093 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3094 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3095 | if (unlikely(ret < 0)) |
| 3096 | return ret; |
| 3097 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3098 | iorw->bytes_done = 0; |
| 3099 | iorw->free_iovec = iov; |
| 3100 | if (iov) |
| 3101 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3102 | return 0; |
| 3103 | } |
| 3104 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3105 | 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] | 3106 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3107 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3108 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3109 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3112 | /* |
| 3113 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3114 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3115 | * This gets called when the page is unlocked, and we generally expect that to |
| 3116 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3117 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3118 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3119 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3120 | * slow path. |
| 3121 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3122 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3123 | int sync, void *arg) |
| 3124 | { |
| 3125 | struct wait_page_queue *wpq; |
| 3126 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3127 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3128 | |
| 3129 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3130 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3131 | if (!wake_page_match(wpq, key)) |
| 3132 | return 0; |
| 3133 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3134 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3135 | list_del_init(&wait->entry); |
| 3136 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3137 | /* submit ref gets dropped, acquire a new one */ |
| 3138 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3139 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3140 | return 1; |
| 3141 | } |
| 3142 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3143 | /* |
| 3144 | * This controls whether a given IO request should be armed for async page |
| 3145 | * based retry. If we return false here, the request is handed to the async |
| 3146 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3147 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3148 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3149 | * will register a callback when the page is unlocked at IO completion. Through |
| 3150 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3151 | * That retry will attempt the buffered read again. The retry will generally |
| 3152 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3153 | * async worker threads for a blocking retry. |
| 3154 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3155 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3156 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3157 | struct io_async_rw *rw = req->async_data; |
| 3158 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3159 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3160 | |
| 3161 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3162 | if (req->flags & REQ_F_NOWAIT) |
| 3163 | return false; |
| 3164 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3165 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3166 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3167 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3168 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3169 | /* |
| 3170 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3171 | * support callback based unlocks |
| 3172 | */ |
| 3173 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3174 | return false; |
| 3175 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3176 | wait->wait.func = io_async_buf_func; |
| 3177 | wait->wait.private = req; |
| 3178 | wait->wait.flags = 0; |
| 3179 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3180 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3181 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3182 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3183 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3187 | { |
| 3188 | if (req->file->f_op->read_iter) |
| 3189 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3190 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3191 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3192 | else |
| 3193 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3194 | } |
| 3195 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3196 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3197 | { |
| 3198 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3199 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3200 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3201 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3202 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3203 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3204 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3205 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3206 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3207 | iovec = NULL; |
| 3208 | } else { |
| 3209 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3210 | if (ret < 0) |
| 3211 | return ret; |
| 3212 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3213 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3214 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3215 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3216 | /* Ensure we clear previously set non-block flag */ |
| 3217 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3218 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3219 | else |
| 3220 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3221 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3222 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3223 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3224 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3225 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3226 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3227 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3228 | 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] | 3229 | if (unlikely(ret)) { |
| 3230 | kfree(iovec); |
| 3231 | return ret; |
| 3232 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3233 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3234 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3235 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3236 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3237 | goto out_free; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3238 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3239 | /* IOPOLL retry should happen for io-wq threads */ |
| 3240 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3241 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3242 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3243 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3244 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3245 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3246 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3247 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3248 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3249 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3250 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3251 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3252 | } |
| 3253 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3254 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3255 | if (ret2) |
| 3256 | return ret2; |
| 3257 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3258 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3259 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3260 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3261 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3262 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3263 | do { |
| 3264 | io_size -= ret; |
| 3265 | rw->bytes_done += ret; |
| 3266 | /* if we can retry, do so with the callbacks armed */ |
| 3267 | if (!io_rw_should_retry(req)) { |
| 3268 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3269 | return -EAGAIN; |
| 3270 | } |
| 3271 | |
| 3272 | /* |
| 3273 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3274 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3275 | * desired page gets unlocked. We can also get a partial read |
| 3276 | * here, and if we do, then just retry at the new offset. |
| 3277 | */ |
| 3278 | ret = io_iter_do_read(req, iter); |
| 3279 | if (ret == -EIOCBQUEUED) |
| 3280 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3281 | /* we got some bytes, but not all. retry. */ |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3282 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3283 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3284 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3285 | out_free: |
| 3286 | /* it's faster to check here then delegate to kfree */ |
| 3287 | if (iovec) |
| 3288 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3289 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3290 | } |
| 3291 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3292 | 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] | 3293 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3294 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3295 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3296 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3297 | } |
| 3298 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3299 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3300 | { |
| 3301 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3302 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3303 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3304 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3305 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3306 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3307 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3308 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3309 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3310 | iovec = NULL; |
| 3311 | } else { |
| 3312 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3313 | if (ret < 0) |
| 3314 | return ret; |
| 3315 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3316 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3317 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3318 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3319 | /* Ensure we clear previously set non-block flag */ |
| 3320 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3321 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3322 | else |
| 3323 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3324 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3325 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3326 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3327 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3328 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3329 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3330 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3331 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3332 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3333 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3334 | 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] | 3335 | if (unlikely(ret)) |
| 3336 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3337 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3338 | /* |
| 3339 | * Open-code file_start_write here to grab freeze protection, |
| 3340 | * which will be released by another thread in |
| 3341 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3342 | * released so that it doesn't complain about the held lock when |
| 3343 | * we return to userspace. |
| 3344 | */ |
| 3345 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3346 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3347 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3348 | SB_FREEZE_WRITE); |
| 3349 | } |
| 3350 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3351 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3352 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3353 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3354 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3355 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3356 | else |
| 3357 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3358 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3359 | /* |
| 3360 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3361 | * retry them without IOCB_NOWAIT. |
| 3362 | */ |
| 3363 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3364 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3365 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3366 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3367 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3368 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3369 | /* IOPOLL retry should happen for io-wq threads */ |
| 3370 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3371 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3372 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3373 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3374 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3375 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3376 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3377 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3378 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3379 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3380 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3381 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3382 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3383 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3384 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3385 | return ret; |
| 3386 | } |
| 3387 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3388 | static int io_renameat_prep(struct io_kiocb *req, |
| 3389 | const struct io_uring_sqe *sqe) |
| 3390 | { |
| 3391 | struct io_rename *ren = &req->rename; |
| 3392 | const char __user *oldf, *newf; |
| 3393 | |
| 3394 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3395 | return -EBADF; |
| 3396 | |
| 3397 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3398 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3399 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3400 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3401 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3402 | |
| 3403 | ren->oldpath = getname(oldf); |
| 3404 | if (IS_ERR(ren->oldpath)) |
| 3405 | return PTR_ERR(ren->oldpath); |
| 3406 | |
| 3407 | ren->newpath = getname(newf); |
| 3408 | if (IS_ERR(ren->newpath)) { |
| 3409 | putname(ren->oldpath); |
| 3410 | return PTR_ERR(ren->newpath); |
| 3411 | } |
| 3412 | |
| 3413 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3417 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3418 | { |
| 3419 | struct io_rename *ren = &req->rename; |
| 3420 | int ret; |
| 3421 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3422 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3423 | return -EAGAIN; |
| 3424 | |
| 3425 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3426 | ren->newpath, ren->flags); |
| 3427 | |
| 3428 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3429 | if (ret < 0) |
| 3430 | req_set_fail_links(req); |
| 3431 | io_req_complete(req, ret); |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3435 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3436 | const struct io_uring_sqe *sqe) |
| 3437 | { |
| 3438 | struct io_unlink *un = &req->unlink; |
| 3439 | const char __user *fname; |
| 3440 | |
| 3441 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3442 | return -EBADF; |
| 3443 | |
| 3444 | un->dfd = READ_ONCE(sqe->fd); |
| 3445 | |
| 3446 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3447 | if (un->flags & ~AT_REMOVEDIR) |
| 3448 | return -EINVAL; |
| 3449 | |
| 3450 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3451 | un->filename = getname(fname); |
| 3452 | if (IS_ERR(un->filename)) |
| 3453 | return PTR_ERR(un->filename); |
| 3454 | |
| 3455 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3456 | return 0; |
| 3457 | } |
| 3458 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3459 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3460 | { |
| 3461 | struct io_unlink *un = &req->unlink; |
| 3462 | int ret; |
| 3463 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3464 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3465 | return -EAGAIN; |
| 3466 | |
| 3467 | if (un->flags & AT_REMOVEDIR) |
| 3468 | ret = do_rmdir(un->dfd, un->filename); |
| 3469 | else |
| 3470 | ret = do_unlinkat(un->dfd, un->filename); |
| 3471 | |
| 3472 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3473 | if (ret < 0) |
| 3474 | req_set_fail_links(req); |
| 3475 | io_req_complete(req, ret); |
| 3476 | return 0; |
| 3477 | } |
| 3478 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3479 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3480 | const struct io_uring_sqe *sqe) |
| 3481 | { |
| 3482 | #if defined(CONFIG_NET) |
| 3483 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3484 | return -EINVAL; |
| 3485 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3486 | sqe->buf_index) |
| 3487 | return -EINVAL; |
| 3488 | |
| 3489 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3490 | return 0; |
| 3491 | #else |
| 3492 | return -EOPNOTSUPP; |
| 3493 | #endif |
| 3494 | } |
| 3495 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3496 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3497 | { |
| 3498 | #if defined(CONFIG_NET) |
| 3499 | struct socket *sock; |
| 3500 | int ret; |
| 3501 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3502 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3503 | return -EAGAIN; |
| 3504 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3505 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3506 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3507 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3508 | |
| 3509 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3510 | if (ret < 0) |
| 3511 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3512 | io_req_complete(req, ret); |
| 3513 | return 0; |
| 3514 | #else |
| 3515 | return -EOPNOTSUPP; |
| 3516 | #endif |
| 3517 | } |
| 3518 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3519 | static int __io_splice_prep(struct io_kiocb *req, |
| 3520 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3521 | { |
| 3522 | struct io_splice* sp = &req->splice; |
| 3523 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3524 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3525 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3526 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3527 | |
| 3528 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3529 | sp->len = READ_ONCE(sqe->len); |
| 3530 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3531 | |
| 3532 | if (unlikely(sp->flags & ~valid_flags)) |
| 3533 | return -EINVAL; |
| 3534 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3535 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3536 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3537 | if (!sp->file_in) |
| 3538 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3539 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3540 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3541 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3542 | /* |
| 3543 | * Splice operation will be punted aync, and here need to |
| 3544 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3545 | */ |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3546 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3547 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3548 | |
| 3549 | return 0; |
| 3550 | } |
| 3551 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3552 | static int io_tee_prep(struct io_kiocb *req, |
| 3553 | const struct io_uring_sqe *sqe) |
| 3554 | { |
| 3555 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3556 | return -EINVAL; |
| 3557 | return __io_splice_prep(req, sqe); |
| 3558 | } |
| 3559 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3560 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3561 | { |
| 3562 | struct io_splice *sp = &req->splice; |
| 3563 | struct file *in = sp->file_in; |
| 3564 | struct file *out = sp->file_out; |
| 3565 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3566 | long ret = 0; |
| 3567 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3568 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3569 | return -EAGAIN; |
| 3570 | if (sp->len) |
| 3571 | ret = do_tee(in, out, sp->len, flags); |
| 3572 | |
| 3573 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3574 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3575 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3576 | if (ret != sp->len) |
| 3577 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3578 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3579 | return 0; |
| 3580 | } |
| 3581 | |
| 3582 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3583 | { |
| 3584 | struct io_splice* sp = &req->splice; |
| 3585 | |
| 3586 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3587 | sp->off_out = READ_ONCE(sqe->off); |
| 3588 | return __io_splice_prep(req, sqe); |
| 3589 | } |
| 3590 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3591 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3592 | { |
| 3593 | struct io_splice *sp = &req->splice; |
| 3594 | struct file *in = sp->file_in; |
| 3595 | struct file *out = sp->file_out; |
| 3596 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3597 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3598 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3599 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3600 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3601 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3602 | |
| 3603 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3604 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3605 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3606 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3607 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3608 | |
| 3609 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3610 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3611 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3612 | if (ret != sp->len) |
| 3613 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3614 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3615 | return 0; |
| 3616 | } |
| 3617 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3618 | /* |
| 3619 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3620 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3621 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3622 | { |
| 3623 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3624 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3625 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3626 | return -EINVAL; |
| 3627 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3628 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3629 | return 0; |
| 3630 | } |
| 3631 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 3632 | 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] | 3633 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3634 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3635 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3636 | if (!req->file) |
| 3637 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3638 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3639 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3640 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3641 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3642 | return -EINVAL; |
| 3643 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3644 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3645 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3646 | return -EINVAL; |
| 3647 | |
| 3648 | req->sync.off = READ_ONCE(sqe->off); |
| 3649 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3650 | return 0; |
| 3651 | } |
| 3652 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3653 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3654 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3655 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3656 | int ret; |
| 3657 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3658 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3659 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3660 | return -EAGAIN; |
| 3661 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3662 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3663 | end > 0 ? end : LLONG_MAX, |
| 3664 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3665 | if (ret < 0) |
| 3666 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3667 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3668 | return 0; |
| 3669 | } |
| 3670 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3671 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3672 | const struct io_uring_sqe *sqe) |
| 3673 | { |
| 3674 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3675 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3676 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3677 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3678 | |
| 3679 | req->sync.off = READ_ONCE(sqe->off); |
| 3680 | req->sync.len = READ_ONCE(sqe->addr); |
| 3681 | req->sync.mode = READ_ONCE(sqe->len); |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3685 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3686 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3687 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3688 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3689 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3690 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3691 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3692 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3693 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3694 | if (ret < 0) |
| 3695 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3696 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3697 | return 0; |
| 3698 | } |
| 3699 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3700 | 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] | 3701 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3702 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3703 | int ret; |
| 3704 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3705 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3706 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3707 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3708 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3709 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3710 | /* open.how should be already initialised */ |
| 3711 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3712 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3713 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3714 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3715 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3716 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3717 | if (IS_ERR(req->open.filename)) { |
| 3718 | ret = PTR_ERR(req->open.filename); |
| 3719 | req->open.filename = NULL; |
| 3720 | return ret; |
| 3721 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3722 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3723 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3724 | return 0; |
| 3725 | } |
| 3726 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3727 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3728 | { |
| 3729 | u64 flags, mode; |
| 3730 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3731 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3732 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3733 | mode = READ_ONCE(sqe->len); |
| 3734 | flags = READ_ONCE(sqe->open_flags); |
| 3735 | req->open.how = build_open_how(flags, mode); |
| 3736 | return __io_openat_prep(req, sqe); |
| 3737 | } |
| 3738 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3739 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3740 | { |
| 3741 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3742 | size_t len; |
| 3743 | int ret; |
| 3744 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3745 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3746 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3747 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3748 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3749 | if (len < OPEN_HOW_SIZE_VER0) |
| 3750 | return -EINVAL; |
| 3751 | |
| 3752 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3753 | len); |
| 3754 | if (ret) |
| 3755 | return ret; |
| 3756 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3757 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3758 | } |
| 3759 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3760 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3761 | { |
| 3762 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3763 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3764 | bool nonblock_set; |
| 3765 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3766 | int ret; |
| 3767 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3768 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3769 | if (ret) |
| 3770 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3771 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 3772 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3773 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3774 | /* |
| 3775 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 3776 | * it'll always -EAGAIN |
| 3777 | */ |
| 3778 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 3779 | return -EAGAIN; |
| 3780 | op.lookup_flags |= LOOKUP_CACHED; |
| 3781 | op.open_flag |= O_NONBLOCK; |
| 3782 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3783 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3784 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3785 | if (ret < 0) |
| 3786 | goto err; |
| 3787 | |
| 3788 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3789 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3790 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 3791 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3792 | /* |
| 3793 | * We could hang on to this 'fd', but seems like marginal |
| 3794 | * gain for something that is now known to be a slower path. |
| 3795 | * So just put it, and we'll get a new one when we retry. |
| 3796 | */ |
| 3797 | put_unused_fd(ret); |
| 3798 | return -EAGAIN; |
| 3799 | } |
| 3800 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3801 | if (IS_ERR(file)) { |
| 3802 | put_unused_fd(ret); |
| 3803 | ret = PTR_ERR(file); |
| 3804 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3805 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3806 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3807 | fsnotify_open(file); |
| 3808 | fd_install(ret, file); |
| 3809 | } |
| 3810 | err: |
| 3811 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3812 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3813 | if (ret < 0) |
| 3814 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3815 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3816 | return 0; |
| 3817 | } |
| 3818 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3819 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3820 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3821 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3824 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3825 | const struct io_uring_sqe *sqe) |
| 3826 | { |
| 3827 | struct io_provide_buf *p = &req->pbuf; |
| 3828 | u64 tmp; |
| 3829 | |
| 3830 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3831 | return -EINVAL; |
| 3832 | |
| 3833 | tmp = READ_ONCE(sqe->fd); |
| 3834 | if (!tmp || tmp > USHRT_MAX) |
| 3835 | return -EINVAL; |
| 3836 | |
| 3837 | memset(p, 0, sizeof(*p)); |
| 3838 | p->nbufs = tmp; |
| 3839 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3840 | return 0; |
| 3841 | } |
| 3842 | |
| 3843 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3844 | int bgid, unsigned nbufs) |
| 3845 | { |
| 3846 | unsigned i = 0; |
| 3847 | |
| 3848 | /* shouldn't happen */ |
| 3849 | if (!nbufs) |
| 3850 | return 0; |
| 3851 | |
| 3852 | /* the head kbuf is the list itself */ |
| 3853 | while (!list_empty(&buf->list)) { |
| 3854 | struct io_buffer *nxt; |
| 3855 | |
| 3856 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3857 | list_del(&nxt->list); |
| 3858 | kfree(nxt); |
| 3859 | if (++i == nbufs) |
| 3860 | return i; |
| 3861 | } |
| 3862 | i++; |
| 3863 | kfree(buf); |
| 3864 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3865 | |
| 3866 | return i; |
| 3867 | } |
| 3868 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3869 | 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] | 3870 | { |
| 3871 | struct io_provide_buf *p = &req->pbuf; |
| 3872 | struct io_ring_ctx *ctx = req->ctx; |
| 3873 | struct io_buffer *head; |
| 3874 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3875 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3876 | |
| 3877 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3878 | |
| 3879 | lockdep_assert_held(&ctx->uring_lock); |
| 3880 | |
| 3881 | ret = -ENOENT; |
| 3882 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3883 | if (head) |
| 3884 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3885 | if (ret < 0) |
| 3886 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3887 | |
| 3888 | /* need to hold the lock to complete IOPOLL requests */ |
| 3889 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3890 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3891 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3892 | } else { |
| 3893 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3894 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3895 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3896 | return 0; |
| 3897 | } |
| 3898 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3899 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3900 | const struct io_uring_sqe *sqe) |
| 3901 | { |
| 3902 | struct io_provide_buf *p = &req->pbuf; |
| 3903 | u64 tmp; |
| 3904 | |
| 3905 | if (sqe->ioprio || sqe->rw_flags) |
| 3906 | return -EINVAL; |
| 3907 | |
| 3908 | tmp = READ_ONCE(sqe->fd); |
| 3909 | if (!tmp || tmp > USHRT_MAX) |
| 3910 | return -E2BIG; |
| 3911 | p->nbufs = tmp; |
| 3912 | p->addr = READ_ONCE(sqe->addr); |
| 3913 | p->len = READ_ONCE(sqe->len); |
| 3914 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3915 | if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs))) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3916 | return -EFAULT; |
| 3917 | |
| 3918 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3919 | tmp = READ_ONCE(sqe->off); |
| 3920 | if (tmp > USHRT_MAX) |
| 3921 | return -E2BIG; |
| 3922 | p->bid = tmp; |
| 3923 | return 0; |
| 3924 | } |
| 3925 | |
| 3926 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3927 | { |
| 3928 | struct io_buffer *buf; |
| 3929 | u64 addr = pbuf->addr; |
| 3930 | int i, bid = pbuf->bid; |
| 3931 | |
| 3932 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3933 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3934 | if (!buf) |
| 3935 | break; |
| 3936 | |
| 3937 | buf->addr = addr; |
| 3938 | buf->len = pbuf->len; |
| 3939 | buf->bid = bid; |
| 3940 | addr += pbuf->len; |
| 3941 | bid++; |
| 3942 | if (!*head) { |
| 3943 | INIT_LIST_HEAD(&buf->list); |
| 3944 | *head = buf; |
| 3945 | } else { |
| 3946 | list_add_tail(&buf->list, &(*head)->list); |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | return i ? i : -ENOMEM; |
| 3951 | } |
| 3952 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3953 | 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] | 3954 | { |
| 3955 | struct io_provide_buf *p = &req->pbuf; |
| 3956 | struct io_ring_ctx *ctx = req->ctx; |
| 3957 | struct io_buffer *head, *list; |
| 3958 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3959 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3960 | |
| 3961 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3962 | |
| 3963 | lockdep_assert_held(&ctx->uring_lock); |
| 3964 | |
| 3965 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3966 | |
| 3967 | ret = io_add_buffers(p, &head); |
| 3968 | if (ret < 0) |
| 3969 | goto out; |
| 3970 | |
| 3971 | if (!list) { |
| 3972 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3973 | GFP_KERNEL); |
| 3974 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3975 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3976 | goto out; |
| 3977 | } |
| 3978 | } |
| 3979 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3980 | if (ret < 0) |
| 3981 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3982 | |
| 3983 | /* need to hold the lock to complete IOPOLL requests */ |
| 3984 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3985 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3986 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3987 | } else { |
| 3988 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3989 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3990 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3991 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3992 | } |
| 3993 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3994 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3995 | const struct io_uring_sqe *sqe) |
| 3996 | { |
| 3997 | #if defined(CONFIG_EPOLL) |
| 3998 | if (sqe->ioprio || sqe->buf_index) |
| 3999 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4000 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4001 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4002 | |
| 4003 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4004 | req->epoll.op = READ_ONCE(sqe->len); |
| 4005 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4006 | |
| 4007 | if (ep_op_has_event(req->epoll.op)) { |
| 4008 | struct epoll_event __user *ev; |
| 4009 | |
| 4010 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4011 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4012 | return -EFAULT; |
| 4013 | } |
| 4014 | |
| 4015 | return 0; |
| 4016 | #else |
| 4017 | return -EOPNOTSUPP; |
| 4018 | #endif |
| 4019 | } |
| 4020 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4021 | 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] | 4022 | { |
| 4023 | #if defined(CONFIG_EPOLL) |
| 4024 | struct io_epoll *ie = &req->epoll; |
| 4025 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4026 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4027 | |
| 4028 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4029 | if (force_nonblock && ret == -EAGAIN) |
| 4030 | return -EAGAIN; |
| 4031 | |
| 4032 | if (ret < 0) |
| 4033 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4034 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4035 | return 0; |
| 4036 | #else |
| 4037 | return -EOPNOTSUPP; |
| 4038 | #endif |
| 4039 | } |
| 4040 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4041 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4042 | { |
| 4043 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4044 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4045 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4046 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4047 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4048 | |
| 4049 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4050 | req->madvise.len = READ_ONCE(sqe->len); |
| 4051 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4052 | return 0; |
| 4053 | #else |
| 4054 | return -EOPNOTSUPP; |
| 4055 | #endif |
| 4056 | } |
| 4057 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4058 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4059 | { |
| 4060 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4061 | struct io_madvise *ma = &req->madvise; |
| 4062 | int ret; |
| 4063 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4064 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4065 | return -EAGAIN; |
| 4066 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4067 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4068 | if (ret < 0) |
| 4069 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4070 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4071 | return 0; |
| 4072 | #else |
| 4073 | return -EOPNOTSUPP; |
| 4074 | #endif |
| 4075 | } |
| 4076 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4077 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4078 | { |
| 4079 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4080 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4081 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4082 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4083 | |
| 4084 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4085 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4086 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4087 | return 0; |
| 4088 | } |
| 4089 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4090 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4091 | { |
| 4092 | struct io_fadvise *fa = &req->fadvise; |
| 4093 | int ret; |
| 4094 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4095 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4096 | switch (fa->advice) { |
| 4097 | case POSIX_FADV_NORMAL: |
| 4098 | case POSIX_FADV_RANDOM: |
| 4099 | case POSIX_FADV_SEQUENTIAL: |
| 4100 | break; |
| 4101 | default: |
| 4102 | return -EAGAIN; |
| 4103 | } |
| 4104 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4105 | |
| 4106 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4107 | if (ret < 0) |
| 4108 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4109 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4110 | return 0; |
| 4111 | } |
| 4112 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4113 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4114 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4115 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4116 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4117 | if (sqe->ioprio || sqe->buf_index) |
| 4118 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4119 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4120 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4121 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4122 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4123 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4124 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4125 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4126 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4127 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4128 | return 0; |
| 4129 | } |
| 4130 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4131 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4132 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4133 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4134 | int ret; |
| 4135 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4136 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4137 | /* only need file table for an actual valid fd */ |
| 4138 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4139 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4140 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4141 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4142 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4143 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4144 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4145 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4146 | if (ret < 0) |
| 4147 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4148 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4149 | return 0; |
| 4150 | } |
| 4151 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4152 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4153 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4154 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4155 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4156 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4157 | sqe->rw_flags || sqe->buf_index) |
| 4158 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4159 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4160 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4161 | |
| 4162 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4163 | return 0; |
| 4164 | } |
| 4165 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4166 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4167 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4168 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4169 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4170 | struct fdtable *fdt; |
| 4171 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4172 | int ret; |
| 4173 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4174 | file = NULL; |
| 4175 | ret = -EBADF; |
| 4176 | spin_lock(&files->file_lock); |
| 4177 | fdt = files_fdtable(files); |
| 4178 | if (close->fd >= fdt->max_fds) { |
| 4179 | spin_unlock(&files->file_lock); |
| 4180 | goto err; |
| 4181 | } |
| 4182 | file = fdt->fd[close->fd]; |
| 4183 | if (!file) { |
| 4184 | spin_unlock(&files->file_lock); |
| 4185 | goto err; |
| 4186 | } |
| 4187 | |
| 4188 | if (file->f_op == &io_uring_fops) { |
| 4189 | spin_unlock(&files->file_lock); |
| 4190 | file = NULL; |
| 4191 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4192 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4193 | |
| 4194 | /* 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] | 4195 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4196 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4197 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4198 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4199 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4200 | ret = __close_fd_get_file(close->fd, &file); |
| 4201 | spin_unlock(&files->file_lock); |
| 4202 | if (ret < 0) { |
| 4203 | if (ret == -ENOENT) |
| 4204 | ret = -EBADF; |
| 4205 | goto err; |
| 4206 | } |
| 4207 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4208 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4209 | ret = filp_close(file, current->files); |
| 4210 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4211 | if (ret < 0) |
| 4212 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4213 | if (file) |
| 4214 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4215 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4216 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4219 | 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] | 4220 | { |
| 4221 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4222 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4223 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4224 | return -EINVAL; |
| 4225 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4226 | return -EINVAL; |
| 4227 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4228 | req->sync.off = READ_ONCE(sqe->off); |
| 4229 | req->sync.len = READ_ONCE(sqe->len); |
| 4230 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4231 | return 0; |
| 4232 | } |
| 4233 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4234 | 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] | 4235 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4236 | int ret; |
| 4237 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4238 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4239 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4240 | return -EAGAIN; |
| 4241 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4242 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4243 | req->sync.flags); |
| 4244 | if (ret < 0) |
| 4245 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4246 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4247 | return 0; |
| 4248 | } |
| 4249 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4250 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4251 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4252 | struct io_async_msghdr *kmsg) |
| 4253 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4254 | struct io_async_msghdr *async_msg = req->async_data; |
| 4255 | |
| 4256 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4257 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4258 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4259 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4260 | return -ENOMEM; |
| 4261 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4262 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4263 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4264 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4265 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4266 | /* if were using fast_iov, set it to the new one */ |
| 4267 | if (!async_msg->free_iov) |
| 4268 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4269 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4270 | return -EAGAIN; |
| 4271 | } |
| 4272 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4273 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4274 | struct io_async_msghdr *iomsg) |
| 4275 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4276 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4277 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4278 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4279 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4280 | } |
| 4281 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4282 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4283 | { |
| 4284 | int ret; |
| 4285 | |
| 4286 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4287 | return 0; |
| 4288 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4289 | if (!ret) |
| 4290 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4291 | return ret; |
| 4292 | } |
| 4293 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4294 | 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] | 4295 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4296 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4297 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4298 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4299 | return -EINVAL; |
| 4300 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4301 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4302 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4303 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4304 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4305 | #ifdef CONFIG_COMPAT |
| 4306 | if (req->ctx->compat) |
| 4307 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4308 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4309 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4312 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4313 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4314 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4315 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4316 | unsigned flags; |
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 | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4331 | flags = req->sr_msg.msg_flags; |
| 4332 | if (flags & MSG_DONTWAIT) |
| 4333 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4334 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4335 | flags |= MSG_DONTWAIT; |
| 4336 | |
| 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; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4347 | if (ret < 0) |
| 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; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4360 | int ret; |
| 4361 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4362 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4363 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4364 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4365 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4366 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4367 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4368 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4369 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4370 | msg.msg_name = NULL; |
| 4371 | msg.msg_control = NULL; |
| 4372 | msg.msg_controllen = 0; |
| 4373 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4374 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4375 | flags = req->sr_msg.msg_flags; |
| 4376 | if (flags & MSG_DONTWAIT) |
| 4377 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4378 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4379 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4380 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4381 | msg.msg_flags = flags; |
| 4382 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4383 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4384 | return -EAGAIN; |
| 4385 | if (ret == -ERESTARTSYS) |
| 4386 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4387 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4388 | if (ret < 0) |
| 4389 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4390 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4391 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4394 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4395 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4396 | { |
| 4397 | struct io_sr_msg *sr = &req->sr_msg; |
| 4398 | struct iovec __user *uiov; |
| 4399 | size_t iov_len; |
| 4400 | int ret; |
| 4401 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4402 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4403 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4404 | if (ret) |
| 4405 | return ret; |
| 4406 | |
| 4407 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4408 | if (iov_len > 1) |
| 4409 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4410 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4411 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4412 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4413 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4414 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4415 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4416 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4417 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4418 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4419 | if (ret > 0) |
| 4420 | ret = 0; |
| 4421 | } |
| 4422 | |
| 4423 | return ret; |
| 4424 | } |
| 4425 | |
| 4426 | #ifdef CONFIG_COMPAT |
| 4427 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4428 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4429 | { |
| 4430 | struct compat_msghdr __user *msg_compat; |
| 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 | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4437 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4438 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4439 | &ptr, &len); |
| 4440 | if (ret) |
| 4441 | return ret; |
| 4442 | |
| 4443 | uiov = compat_ptr(ptr); |
| 4444 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4445 | compat_ssize_t clen; |
| 4446 | |
| 4447 | if (len > 1) |
| 4448 | return -EINVAL; |
| 4449 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4450 | return -EFAULT; |
| 4451 | if (__get_user(clen, &uiov->iov_len)) |
| 4452 | return -EFAULT; |
| 4453 | if (clen < 0) |
| 4454 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4455 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4456 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4457 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4458 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4459 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4460 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4461 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4462 | if (ret < 0) |
| 4463 | return ret; |
| 4464 | } |
| 4465 | |
| 4466 | return 0; |
| 4467 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4468 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4469 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4470 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4471 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4472 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4473 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4474 | |
| 4475 | #ifdef CONFIG_COMPAT |
| 4476 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4477 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4478 | #endif |
| 4479 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4480 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4481 | } |
| 4482 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4483 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4484 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4485 | { |
| 4486 | struct io_sr_msg *sr = &req->sr_msg; |
| 4487 | struct io_buffer *kbuf; |
| 4488 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4489 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4490 | if (IS_ERR(kbuf)) |
| 4491 | return kbuf; |
| 4492 | |
| 4493 | sr->kbuf = kbuf; |
| 4494 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4495 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4498 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4499 | { |
| 4500 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4501 | } |
| 4502 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4503 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4504 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4505 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4506 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4507 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4508 | return 0; |
| 4509 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4510 | if (!ret) |
| 4511 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4512 | return ret; |
| 4513 | } |
| 4514 | |
| 4515 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4516 | { |
| 4517 | struct io_sr_msg *sr = &req->sr_msg; |
| 4518 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4519 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4520 | return -EINVAL; |
| 4521 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4522 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4523 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4524 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4525 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4526 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4527 | #ifdef CONFIG_COMPAT |
| 4528 | if (req->ctx->compat) |
| 4529 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4530 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4531 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4532 | } |
| 4533 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4534 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4535 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4536 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4537 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4538 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4539 | unsigned flags; |
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 | |
| 4565 | flags = req->sr_msg.msg_flags; |
| 4566 | if (flags & MSG_DONTWAIT) |
| 4567 | req->flags |= REQ_F_NOWAIT; |
| 4568 | else if (force_nonblock) |
| 4569 | flags |= MSG_DONTWAIT; |
| 4570 | |
| 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; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4584 | if (ret < 0) |
| 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; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4599 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4600 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4601 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4602 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4603 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4604 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4605 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4606 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4607 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4608 | if (IS_ERR(kbuf)) |
| 4609 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4610 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4611 | } |
| 4612 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4613 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4614 | if (unlikely(ret)) |
| 4615 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4616 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4617 | msg.msg_name = NULL; |
| 4618 | msg.msg_control = NULL; |
| 4619 | msg.msg_controllen = 0; |
| 4620 | msg.msg_namelen = 0; |
| 4621 | msg.msg_iocb = NULL; |
| 4622 | msg.msg_flags = 0; |
| 4623 | |
| 4624 | flags = req->sr_msg.msg_flags; |
| 4625 | if (flags & MSG_DONTWAIT) |
| 4626 | req->flags |= REQ_F_NOWAIT; |
| 4627 | else if (force_nonblock) |
| 4628 | flags |= MSG_DONTWAIT; |
| 4629 | |
| 4630 | ret = sock_recvmsg(sock, &msg, flags); |
| 4631 | if (force_nonblock && ret == -EAGAIN) |
| 4632 | return -EAGAIN; |
| 4633 | if (ret == -ERESTARTSYS) |
| 4634 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4635 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4636 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4637 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4638 | if (ret < 0) |
| 4639 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4640 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4641 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4642 | } |
| 4643 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4644 | 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] | 4645 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4646 | struct io_accept *accept = &req->accept; |
| 4647 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4648 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4649 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4650 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4651 | return -EINVAL; |
| 4652 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4653 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4654 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4655 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4656 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4657 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4658 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4659 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4660 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4661 | { |
| 4662 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4663 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4664 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4665 | int ret; |
| 4666 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4667 | if (req->file->f_flags & O_NONBLOCK) |
| 4668 | req->flags |= REQ_F_NOWAIT; |
| 4669 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4670 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4671 | accept->addr_len, accept->flags, |
| 4672 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4673 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4674 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4675 | if (ret < 0) { |
| 4676 | if (ret == -ERESTARTSYS) |
| 4677 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4678 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4679 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4680 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4681 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4682 | } |
| 4683 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4684 | static int io_connect_prep_async(struct io_kiocb *req) |
| 4685 | { |
| 4686 | struct io_async_connect *io = req->async_data; |
| 4687 | struct io_connect *conn = &req->connect; |
| 4688 | |
| 4689 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 4690 | } |
| 4691 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4692 | 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] | 4693 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4694 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4695 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4696 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4697 | return -EINVAL; |
| 4698 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4699 | return -EINVAL; |
| 4700 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4701 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4702 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4703 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4706 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4707 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4708 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4709 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4710 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4711 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4712 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4713 | if (req->async_data) { |
| 4714 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4715 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4716 | ret = move_addr_to_kernel(req->connect.addr, |
| 4717 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4718 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4719 | if (ret) |
| 4720 | goto out; |
| 4721 | io = &__io; |
| 4722 | } |
| 4723 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4724 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4725 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4726 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4727 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4728 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4729 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4730 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4731 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4732 | ret = -ENOMEM; |
| 4733 | goto out; |
| 4734 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4735 | io = req->async_data; |
| 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 | percpu_ref_get(&req->ctx->refs); |
| 4798 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4799 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4800 | * If this fails, then the task is exiting. When a task exits, the |
| 4801 | * work gets canceled, so just cancel this request as well instead |
| 4802 | * of executing it. We can't safely execute it anyway, as we may not |
| 4803 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4804 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 4805 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4806 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4807 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 4808 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4809 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4810 | return 1; |
| 4811 | } |
| 4812 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4813 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4814 | __acquires(&req->ctx->completion_lock) |
| 4815 | { |
| 4816 | struct io_ring_ctx *ctx = req->ctx; |
| 4817 | |
| 4818 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4819 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4820 | |
| 4821 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4822 | } |
| 4823 | |
| 4824 | spin_lock_irq(&ctx->completion_lock); |
| 4825 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4826 | add_wait_queue(poll->head, &poll->wait); |
| 4827 | return true; |
| 4828 | } |
| 4829 | |
| 4830 | return false; |
| 4831 | } |
| 4832 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4833 | 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] | 4834 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4835 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4836 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4837 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4838 | return req->apoll->double_poll; |
| 4839 | } |
| 4840 | |
| 4841 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 4842 | { |
| 4843 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4844 | return &req->poll; |
| 4845 | return &req->apoll->poll; |
| 4846 | } |
| 4847 | |
| 4848 | static void io_poll_remove_double(struct io_kiocb *req) |
| 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) |
| 4860 | refcount_dec(&req->refs); |
| 4861 | poll->head = NULL; |
| 4862 | spin_unlock(&head->lock); |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4867 | { |
| 4868 | struct io_ring_ctx *ctx = req->ctx; |
| 4869 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4870 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4871 | req->poll.done = true; |
| 4872 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4873 | io_commit_cqring(ctx); |
| 4874 | } |
| 4875 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4876 | static void io_poll_task_func(struct callback_head *cb) |
| 4877 | { |
| 4878 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4879 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4880 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4881 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4882 | if (io_poll_rewait(req, &req->poll)) { |
| 4883 | spin_unlock_irq(&ctx->completion_lock); |
| 4884 | } else { |
| 4885 | hash_del(&req->hash_node); |
| 4886 | io_poll_complete(req, req->result, 0); |
| 4887 | spin_unlock_irq(&ctx->completion_lock); |
| 4888 | |
| 4889 | nxt = io_put_req_find_next(req); |
| 4890 | io_cqring_ev_posted(ctx); |
| 4891 | if (nxt) |
| 4892 | __io_req_task_submit(nxt); |
| 4893 | } |
| 4894 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4895 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4899 | int sync, void *key) |
| 4900 | { |
| 4901 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4902 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4903 | __poll_t mask = key_to_poll(key); |
| 4904 | |
| 4905 | /* for instances that support it check for an event match first: */ |
| 4906 | if (mask && !(mask & poll->events)) |
| 4907 | return 0; |
| 4908 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 4909 | list_del_init(&wait->entry); |
| 4910 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4911 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4912 | bool done; |
| 4913 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4914 | spin_lock(&poll->head->lock); |
| 4915 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4916 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4917 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4918 | /* make sure double remove sees this as being gone */ |
| 4919 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4920 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 4921 | if (!done) { |
| 4922 | /* use wait func handler, so it matches the rq type */ |
| 4923 | poll->wait.func(&poll->wait, mode, sync, key); |
| 4924 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4925 | } |
| 4926 | refcount_dec(&req->refs); |
| 4927 | return 1; |
| 4928 | } |
| 4929 | |
| 4930 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4931 | wait_queue_func_t wake_func) |
| 4932 | { |
| 4933 | poll->head = NULL; |
| 4934 | poll->done = false; |
| 4935 | poll->canceled = false; |
| 4936 | poll->events = events; |
| 4937 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4938 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4939 | } |
| 4940 | |
| 4941 | 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] | 4942 | struct wait_queue_head *head, |
| 4943 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4944 | { |
| 4945 | struct io_kiocb *req = pt->req; |
| 4946 | |
| 4947 | /* |
| 4948 | * If poll->head is already set, it's because the file being polled |
| 4949 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4950 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4951 | */ |
| 4952 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 4953 | struct io_poll_iocb *poll_one = poll; |
| 4954 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4955 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4956 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4957 | pt->error = -EINVAL; |
| 4958 | return; |
| 4959 | } |
| 4960 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4961 | if (!poll) { |
| 4962 | pt->error = -ENOMEM; |
| 4963 | return; |
| 4964 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 4965 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4966 | refcount_inc(&req->refs); |
| 4967 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4968 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | pt->error = 0; |
| 4972 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4973 | |
| 4974 | if (poll->events & EPOLLEXCLUSIVE) |
| 4975 | add_wait_queue_exclusive(head, &poll->wait); |
| 4976 | else |
| 4977 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4978 | } |
| 4979 | |
| 4980 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4981 | struct poll_table_struct *p) |
| 4982 | { |
| 4983 | 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] | 4984 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4985 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4986 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4987 | } |
| 4988 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4989 | static void io_async_task_func(struct callback_head *cb) |
| 4990 | { |
| 4991 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4992 | struct async_poll *apoll = req->apoll; |
| 4993 | struct io_ring_ctx *ctx = req->ctx; |
| 4994 | |
| 4995 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 4996 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4997 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4998 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4999 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5000 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5001 | } |
| 5002 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5003 | /* If req is still hashed, it cannot have been canceled. Don't check. */ |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5004 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5005 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5006 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5007 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5008 | spin_unlock_irq(&ctx->completion_lock); |
| 5009 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5010 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5011 | __io_req_task_submit(req); |
| 5012 | else |
| 5013 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5014 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5015 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5016 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5017 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5021 | void *key) |
| 5022 | { |
| 5023 | struct io_kiocb *req = wait->private; |
| 5024 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5025 | |
| 5026 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5027 | key_to_poll(key)); |
| 5028 | |
| 5029 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5030 | } |
| 5031 | |
| 5032 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5033 | { |
| 5034 | struct io_ring_ctx *ctx = req->ctx; |
| 5035 | struct hlist_head *list; |
| 5036 | |
| 5037 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5038 | hlist_add_head(&req->hash_node, list); |
| 5039 | } |
| 5040 | |
| 5041 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5042 | struct io_poll_iocb *poll, |
| 5043 | struct io_poll_table *ipt, __poll_t mask, |
| 5044 | wait_queue_func_t wake_func) |
| 5045 | __acquires(&ctx->completion_lock) |
| 5046 | { |
| 5047 | struct io_ring_ctx *ctx = req->ctx; |
| 5048 | bool cancel = false; |
| 5049 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5050 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5051 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5052 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5053 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5054 | |
| 5055 | ipt->pt._key = mask; |
| 5056 | ipt->req = req; |
| 5057 | ipt->error = -EINVAL; |
| 5058 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5059 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5060 | |
| 5061 | spin_lock_irq(&ctx->completion_lock); |
| 5062 | if (likely(poll->head)) { |
| 5063 | spin_lock(&poll->head->lock); |
| 5064 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5065 | if (ipt->error) |
| 5066 | cancel = true; |
| 5067 | ipt->error = 0; |
| 5068 | mask = 0; |
| 5069 | } |
| 5070 | if (mask || ipt->error) |
| 5071 | list_del_init(&poll->wait.entry); |
| 5072 | else if (cancel) |
| 5073 | WRITE_ONCE(poll->canceled, true); |
| 5074 | else if (!poll->done) /* actually waiting for an event */ |
| 5075 | io_poll_req_insert(req); |
| 5076 | spin_unlock(&poll->head->lock); |
| 5077 | } |
| 5078 | |
| 5079 | return mask; |
| 5080 | } |
| 5081 | |
| 5082 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5083 | { |
| 5084 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5085 | struct io_ring_ctx *ctx = req->ctx; |
| 5086 | struct async_poll *apoll; |
| 5087 | struct io_poll_table ipt; |
| 5088 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5089 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5090 | |
| 5091 | if (!req->file || !file_can_poll(req->file)) |
| 5092 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5093 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5094 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5095 | if (def->pollin) |
| 5096 | rw = READ; |
| 5097 | else if (def->pollout) |
| 5098 | rw = WRITE; |
| 5099 | else |
| 5100 | return false; |
| 5101 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5102 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5103 | return false; |
| 5104 | |
| 5105 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5106 | if (unlikely(!apoll)) |
| 5107 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5108 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5109 | |
| 5110 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5111 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5112 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5113 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5114 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5115 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5116 | if (def->pollout) |
| 5117 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5118 | |
| 5119 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5120 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5121 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5122 | mask &= ~POLLIN; |
| 5123 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5124 | mask |= POLLERR | POLLPRI; |
| 5125 | |
| 5126 | ipt.pt._qproc = io_async_queue_proc; |
| 5127 | |
| 5128 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5129 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5130 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5131 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5132 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5133 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5134 | kfree(apoll); |
| 5135 | return false; |
| 5136 | } |
| 5137 | spin_unlock_irq(&ctx->completion_lock); |
| 5138 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5139 | apoll->poll.events); |
| 5140 | return true; |
| 5141 | } |
| 5142 | |
| 5143 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5144 | struct io_poll_iocb *poll) |
| 5145 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5146 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5147 | |
| 5148 | spin_lock(&poll->head->lock); |
| 5149 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5150 | if (!list_empty(&poll->wait.entry)) { |
| 5151 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5152 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5153 | } |
| 5154 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5155 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5156 | return do_complete; |
| 5157 | } |
| 5158 | |
| 5159 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5160 | { |
| 5161 | bool do_complete; |
| 5162 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5163 | io_poll_remove_double(req); |
| 5164 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5165 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5166 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5167 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5168 | struct async_poll *apoll = req->apoll; |
| 5169 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5170 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5171 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5172 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5173 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5174 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5175 | kfree(apoll); |
| 5176 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5177 | } |
| 5178 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5179 | if (do_complete) { |
| 5180 | io_cqring_fill_event(req, -ECANCELED); |
| 5181 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5182 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5183 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5187 | } |
| 5188 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5189 | /* |
| 5190 | * Returns true if we found and killed one or more poll requests |
| 5191 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5192 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5193 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5194 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5195 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5196 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5197 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5198 | |
| 5199 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5200 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5201 | struct hlist_head *list; |
| 5202 | |
| 5203 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5204 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5205 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5206 | posted += io_poll_remove_one(req); |
| 5207 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5208 | } |
| 5209 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5210 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5211 | if (posted) |
| 5212 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5213 | |
| 5214 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5215 | } |
| 5216 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5217 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5218 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5219 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5220 | struct io_kiocb *req; |
| 5221 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5222 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5223 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5224 | if (sqe_addr != req->user_data) |
| 5225 | continue; |
| 5226 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5227 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5228 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | return -ENOENT; |
| 5232 | } |
| 5233 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5234 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5235 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5236 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5237 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5238 | return -EINVAL; |
| 5239 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5240 | sqe->poll_events) |
| 5241 | return -EINVAL; |
| 5242 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5243 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5244 | return 0; |
| 5245 | } |
| 5246 | |
| 5247 | /* |
| 5248 | * Find a running poll command that matches one specified in sqe->addr, |
| 5249 | * and remove it if found. |
| 5250 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5251 | static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5252 | { |
| 5253 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5254 | int ret; |
| 5255 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5256 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5257 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5258 | spin_unlock_irq(&ctx->completion_lock); |
| 5259 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5260 | if (ret < 0) |
| 5261 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5262 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5263 | return 0; |
| 5264 | } |
| 5265 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5266 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5267 | void *key) |
| 5268 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5269 | struct io_kiocb *req = wait->private; |
| 5270 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5271 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5272 | 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] | 5273 | } |
| 5274 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5275 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5276 | struct poll_table_struct *p) |
| 5277 | { |
| 5278 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5279 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5280 | __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] | 5281 | } |
| 5282 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5283 | 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] | 5284 | { |
| 5285 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5286 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5287 | |
| 5288 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5289 | return -EINVAL; |
| 5290 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5291 | return -EINVAL; |
| 5292 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5293 | events = READ_ONCE(sqe->poll32_events); |
| 5294 | #ifdef __BIG_ENDIAN |
| 5295 | events = swahw32(events); |
| 5296 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5297 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5298 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5299 | return 0; |
| 5300 | } |
| 5301 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5302 | 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] | 5303 | { |
| 5304 | struct io_poll_iocb *poll = &req->poll; |
| 5305 | struct io_ring_ctx *ctx = req->ctx; |
| 5306 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5307 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5308 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5309 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5310 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5311 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5312 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5313 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5314 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5315 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5316 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5317 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5318 | spin_unlock_irq(&ctx->completion_lock); |
| 5319 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5320 | if (mask) { |
| 5321 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5322 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5323 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5324 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5327 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5328 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5329 | struct io_timeout_data *data = container_of(timer, |
| 5330 | struct io_timeout_data, timer); |
| 5331 | struct io_kiocb *req = data->req; |
| 5332 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5333 | unsigned long flags; |
| 5334 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5335 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5336 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5337 | atomic_set(&req->ctx->cq_timeouts, |
| 5338 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5339 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5340 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5341 | io_commit_cqring(ctx); |
| 5342 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5343 | |
| 5344 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5345 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5346 | io_put_req(req); |
| 5347 | return HRTIMER_NORESTART; |
| 5348 | } |
| 5349 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5350 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5351 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5352 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5353 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5354 | struct io_kiocb *req; |
| 5355 | int ret = -ENOENT; |
| 5356 | |
| 5357 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5358 | if (user_data == req->user_data) { |
| 5359 | ret = 0; |
| 5360 | break; |
| 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5365 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5366 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5367 | io = req->async_data; |
| 5368 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5369 | if (ret == -1) |
| 5370 | return ERR_PTR(-EALREADY); |
| 5371 | list_del_init(&req->timeout.list); |
| 5372 | return req; |
| 5373 | } |
| 5374 | |
| 5375 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5376 | { |
| 5377 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5378 | |
| 5379 | if (IS_ERR(req)) |
| 5380 | return PTR_ERR(req); |
| 5381 | |
| 5382 | req_set_fail_links(req); |
| 5383 | io_cqring_fill_event(req, -ECANCELED); |
| 5384 | io_put_req_deferred(req, 1); |
| 5385 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5386 | } |
| 5387 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5388 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5389 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5390 | { |
| 5391 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5392 | struct io_timeout_data *data; |
| 5393 | |
| 5394 | if (IS_ERR(req)) |
| 5395 | return PTR_ERR(req); |
| 5396 | |
| 5397 | req->timeout.off = 0; /* noseq */ |
| 5398 | data = req->async_data; |
| 5399 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5400 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5401 | data->timer.function = io_timeout_fn; |
| 5402 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5403 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5406 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5407 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5408 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5409 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5410 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5411 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5412 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5413 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5414 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5415 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5416 | return -EINVAL; |
| 5417 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5418 | tr->addr = READ_ONCE(sqe->addr); |
| 5419 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5420 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5421 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5422 | return -EINVAL; |
| 5423 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5424 | return -EFAULT; |
| 5425 | } else if (tr->flags) { |
| 5426 | /* timeout removal doesn't support flags */ |
| 5427 | return -EINVAL; |
| 5428 | } |
| 5429 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5430 | return 0; |
| 5431 | } |
| 5432 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5433 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5434 | { |
| 5435 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5436 | : HRTIMER_MODE_REL; |
| 5437 | } |
| 5438 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5439 | /* |
| 5440 | * Remove or update an existing timeout command |
| 5441 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5442 | 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] | 5443 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5444 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5445 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5446 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5447 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5448 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5449 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5450 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5451 | else |
| 5452 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5453 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5454 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5455 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5456 | io_commit_cqring(ctx); |
| 5457 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5458 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5459 | if (ret < 0) |
| 5460 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5461 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5462 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5463 | } |
| 5464 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5465 | 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] | 5466 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5467 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5468 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5469 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5470 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5471 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5472 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5473 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5474 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5475 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5476 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5477 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5478 | flags = READ_ONCE(sqe->timeout_flags); |
| 5479 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5480 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5481 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5482 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5483 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5484 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5485 | return -ENOMEM; |
| 5486 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5487 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5488 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5489 | |
| 5490 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5491 | return -EFAULT; |
| 5492 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5493 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5494 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5498 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5499 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5500 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5501 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5502 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5503 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5504 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5505 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5506 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5507 | /* |
| 5508 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5509 | * timeout event to be satisfied. If it isn't set, then this is |
| 5510 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5511 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5512 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5513 | entry = ctx->timeout_list.prev; |
| 5514 | goto add; |
| 5515 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5516 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5517 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5518 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5519 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5520 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5521 | * This is safe because ->completion_lock is held, and submissions |
| 5522 | * and completions are never mixed in the same ->completion_lock section. |
| 5523 | */ |
| 5524 | ctx->cq_last_tm_flush = tail; |
| 5525 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5526 | /* |
| 5527 | * Insertion sort, ensuring the first entry in the list is always |
| 5528 | * the one we need first. |
| 5529 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5530 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5531 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5532 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5533 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5534 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5535 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5536 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5537 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5538 | break; |
| 5539 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5540 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5541 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5542 | data->timer.function = io_timeout_fn; |
| 5543 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5544 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5545 | return 0; |
| 5546 | } |
| 5547 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5548 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5549 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5550 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5551 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5552 | return req->user_data == (unsigned long) data; |
| 5553 | } |
| 5554 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5555 | static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5556 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5557 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5558 | int ret = 0; |
| 5559 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5560 | if (!tctx->io_wq) |
| 5561 | return -ENOENT; |
| 5562 | |
| 5563 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5564 | switch (cancel_ret) { |
| 5565 | case IO_WQ_CANCEL_OK: |
| 5566 | ret = 0; |
| 5567 | break; |
| 5568 | case IO_WQ_CANCEL_RUNNING: |
| 5569 | ret = -EALREADY; |
| 5570 | break; |
| 5571 | case IO_WQ_CANCEL_NOTFOUND: |
| 5572 | ret = -ENOENT; |
| 5573 | break; |
| 5574 | } |
| 5575 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5576 | return ret; |
| 5577 | } |
| 5578 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5579 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5580 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5581 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5582 | { |
| 5583 | unsigned long flags; |
| 5584 | int ret; |
| 5585 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5586 | ret = io_async_cancel_one(req->task->io_uring, |
| 5587 | (void *) (unsigned long) sqe_addr); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5588 | if (ret != -ENOENT) { |
| 5589 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5590 | goto done; |
| 5591 | } |
| 5592 | |
| 5593 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5594 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5595 | if (ret != -ENOENT) |
| 5596 | goto done; |
| 5597 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5598 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5599 | if (!ret) |
| 5600 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5601 | io_cqring_fill_event(req, ret); |
| 5602 | io_commit_cqring(ctx); |
| 5603 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5604 | io_cqring_ev_posted(ctx); |
| 5605 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5606 | if (ret < 0) |
| 5607 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5608 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5609 | } |
| 5610 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5611 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5612 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5613 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5614 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5615 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5616 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5617 | return -EINVAL; |
| 5618 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5619 | return -EINVAL; |
| 5620 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5621 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5622 | return 0; |
| 5623 | } |
| 5624 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5625 | 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] | 5626 | { |
| 5627 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5628 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5629 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5630 | return 0; |
| 5631 | } |
| 5632 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5633 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5634 | const struct io_uring_sqe *sqe) |
| 5635 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5636 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5637 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5638 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5639 | return -EINVAL; |
| 5640 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5641 | return -EINVAL; |
| 5642 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5643 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 5644 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 5645 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5646 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5647 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5648 | return 0; |
| 5649 | } |
| 5650 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5651 | 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] | 5652 | { |
| 5653 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5654 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5655 | int ret; |
| 5656 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5657 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5658 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5659 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5660 | up.offset = req->rsrc_update.offset; |
| 5661 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5662 | |
| 5663 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5664 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5665 | mutex_unlock(&ctx->uring_lock); |
| 5666 | |
| 5667 | if (ret < 0) |
| 5668 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5669 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5670 | return 0; |
| 5671 | } |
| 5672 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5673 | 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] | 5674 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5675 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5676 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5677 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5678 | case IORING_OP_READV: |
| 5679 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5680 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5681 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5682 | case IORING_OP_WRITEV: |
| 5683 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5684 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5685 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5686 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5687 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5688 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5689 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5690 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5691 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5692 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5693 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5694 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5695 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5696 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5697 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5698 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5699 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5700 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5701 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5702 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5703 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5704 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5705 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5706 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5707 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5708 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5709 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5710 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5711 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5712 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5713 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5714 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5715 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5716 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5717 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5718 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5719 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5720 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5721 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5722 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5723 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5724 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5725 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5726 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5727 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5728 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5729 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5730 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5731 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5732 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5733 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5734 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5735 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5736 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5737 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 5738 | case IORING_OP_SHUTDOWN: |
| 5739 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5740 | case IORING_OP_RENAMEAT: |
| 5741 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5742 | case IORING_OP_UNLINKAT: |
| 5743 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5746 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5747 | req->opcode); |
| 5748 | return-EINVAL; |
| 5749 | } |
| 5750 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5751 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5752 | { |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5753 | switch (req->opcode) { |
| 5754 | case IORING_OP_READV: |
| 5755 | case IORING_OP_READ_FIXED: |
| 5756 | case IORING_OP_READ: |
| 5757 | return io_rw_prep_async(req, READ); |
| 5758 | case IORING_OP_WRITEV: |
| 5759 | case IORING_OP_WRITE_FIXED: |
| 5760 | case IORING_OP_WRITE: |
| 5761 | return io_rw_prep_async(req, WRITE); |
| 5762 | case IORING_OP_SENDMSG: |
| 5763 | case IORING_OP_SEND: |
| 5764 | return io_sendmsg_prep_async(req); |
| 5765 | case IORING_OP_RECVMSG: |
| 5766 | case IORING_OP_RECV: |
| 5767 | return io_recvmsg_prep_async(req); |
| 5768 | case IORING_OP_CONNECT: |
| 5769 | return io_connect_prep_async(req); |
| 5770 | } |
| 5771 | return 0; |
| 5772 | } |
| 5773 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5774 | static int io_req_defer_prep(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5775 | { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5776 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5777 | return 0; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5778 | /* some opcodes init it during the inital prep */ |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5779 | if (req->async_data) |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5780 | return 0; |
| 5781 | if (__io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5782 | return -EAGAIN; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5783 | return io_req_prep_async(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5784 | } |
| 5785 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5786 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5787 | { |
| 5788 | struct io_kiocb *pos; |
| 5789 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5790 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5791 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5792 | io_for_each_link(pos, req) |
| 5793 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5794 | |
| 5795 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5796 | return total_submitted - nr_reqs; |
| 5797 | } |
| 5798 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5799 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5800 | { |
| 5801 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5802 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5803 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5804 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5805 | |
| 5806 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5807 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 5808 | !(req->flags & REQ_F_IO_DRAIN))) |
| 5809 | return 0; |
| 5810 | |
| 5811 | seq = io_get_sequence(req); |
| 5812 | /* Still a chance to pass the sequence check */ |
| 5813 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5814 | return 0; |
| 5815 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5816 | ret = io_req_defer_prep(req); |
| 5817 | if (ret) |
| 5818 | return ret; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 5819 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5820 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 5821 | if (!de) |
| 5822 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5823 | |
| 5824 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5825 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5826 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5827 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 5828 | io_queue_async_work(req); |
| 5829 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5833 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5834 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5835 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5836 | spin_unlock_irq(&ctx->completion_lock); |
| 5837 | return -EIOCBQUEUED; |
| 5838 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5839 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 5840 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5841 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5842 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 5843 | switch (req->opcode) { |
| 5844 | case IORING_OP_READV: |
| 5845 | case IORING_OP_READ_FIXED: |
| 5846 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5847 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5848 | break; |
| 5849 | case IORING_OP_RECVMSG: |
| 5850 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5851 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5852 | break; |
| 5853 | } |
| 5854 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5855 | } |
| 5856 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5857 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 5858 | switch (req->opcode) { |
| 5859 | case IORING_OP_READV: |
| 5860 | case IORING_OP_READ_FIXED: |
| 5861 | case IORING_OP_READ: |
| 5862 | case IORING_OP_WRITEV: |
| 5863 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5864 | case IORING_OP_WRITE: { |
| 5865 | struct io_async_rw *io = req->async_data; |
| 5866 | if (io->free_iovec) |
| 5867 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5868 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5869 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5870 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5871 | case IORING_OP_SENDMSG: { |
| 5872 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5873 | |
| 5874 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5875 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5876 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5877 | case IORING_OP_SPLICE: |
| 5878 | case IORING_OP_TEE: |
| 5879 | io_put_file(req, req->splice.file_in, |
| 5880 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5881 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 5882 | case IORING_OP_OPENAT: |
| 5883 | case IORING_OP_OPENAT2: |
| 5884 | if (req->open.filename) |
| 5885 | putname(req->open.filename); |
| 5886 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5887 | case IORING_OP_RENAMEAT: |
| 5888 | putname(req->rename.oldpath); |
| 5889 | putname(req->rename.newpath); |
| 5890 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5891 | case IORING_OP_UNLINKAT: |
| 5892 | putname(req->unlink.filename); |
| 5893 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5894 | } |
| 5895 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5896 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5897 | } |
| 5898 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5899 | 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] | 5900 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5901 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame^] | 5902 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5903 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5904 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame^] | 5905 | if (req->work.personality) { |
| 5906 | const struct cred *new_creds; |
| 5907 | |
| 5908 | if (!(issue_flags & IO_URING_F_NONBLOCK)) |
| 5909 | mutex_lock(&ctx->uring_lock); |
| 5910 | new_creds = idr_find(&ctx->personality_idr, req->work.personality); |
| 5911 | if (!(issue_flags & IO_URING_F_NONBLOCK)) |
| 5912 | mutex_unlock(&ctx->uring_lock); |
| 5913 | if (!new_creds) |
| 5914 | return -EINVAL; |
| 5915 | creds = override_creds(new_creds); |
| 5916 | } |
| 5917 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5918 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5919 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5920 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5921 | break; |
| 5922 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5923 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5924 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5925 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5926 | break; |
| 5927 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5928 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5929 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5930 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5931 | break; |
| 5932 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5933 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5934 | break; |
| 5935 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5936 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5937 | break; |
| 5938 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5939 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5940 | break; |
| 5941 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5942 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5943 | break; |
| 5944 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5945 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 5946 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5947 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5948 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5949 | break; |
| 5950 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5951 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 5952 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5953 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5954 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5955 | break; |
| 5956 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5957 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5958 | break; |
| 5959 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5960 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5961 | break; |
| 5962 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5963 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5964 | break; |
| 5965 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5966 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5967 | break; |
| 5968 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5969 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5970 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5971 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5972 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5973 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5974 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5975 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5976 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5977 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5978 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5979 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5980 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5981 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5982 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5983 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5984 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5985 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5986 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5987 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5988 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5989 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5990 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5991 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5992 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5993 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5994 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5995 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5996 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5997 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5998 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5999 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6000 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6001 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6002 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6003 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6004 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6005 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6006 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6007 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6008 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6009 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6010 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6011 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6012 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6013 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6014 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6015 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6016 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6017 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6018 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6019 | default: |
| 6020 | ret = -EINVAL; |
| 6021 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6022 | } |
| 6023 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame^] | 6024 | if (creds) |
| 6025 | revert_creds(creds); |
| 6026 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6027 | if (ret) |
| 6028 | return ret; |
| 6029 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6030 | /* If the op doesn't have a file, we're not polling for it */ |
| 6031 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6032 | const bool in_async = io_wq_current_is_worker(); |
| 6033 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6034 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6035 | if (in_async) |
| 6036 | mutex_lock(&ctx->uring_lock); |
| 6037 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6038 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6039 | |
| 6040 | if (in_async) |
| 6041 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6042 | } |
| 6043 | |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6047 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6048 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6049 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6050 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6051 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6052 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6053 | timeout = io_prep_linked_timeout(req); |
| 6054 | if (timeout) |
| 6055 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6056 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6057 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6058 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6059 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6060 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6061 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6062 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6063 | /* |
| 6064 | * We can get EAGAIN for polled IO even though we're |
| 6065 | * forcing a sync submission from here, since we can't |
| 6066 | * wait for request slots on the block side. |
| 6067 | */ |
| 6068 | if (ret != -EAGAIN) |
| 6069 | break; |
| 6070 | cond_resched(); |
| 6071 | } while (1); |
| 6072 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6073 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6074 | /* avoid locking problems by failing it from a clean context */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6075 | if (ret) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6076 | /* io-wq is going to take one down */ |
| 6077 | refcount_inc(&req->refs); |
| 6078 | io_req_task_queue_fail(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6079 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6080 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6081 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6082 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6083 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6084 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6085 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6086 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6087 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6088 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6089 | } |
| 6090 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6091 | static struct file *io_file_get(struct io_submit_state *state, |
| 6092 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6093 | { |
| 6094 | struct io_ring_ctx *ctx = req->ctx; |
| 6095 | struct file *file; |
| 6096 | |
| 6097 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6098 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6099 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6100 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6101 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6102 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6103 | } else { |
| 6104 | trace_io_uring_file_get(ctx, fd); |
| 6105 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6106 | } |
| 6107 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6108 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6109 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6110 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6111 | } |
| 6112 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6113 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6114 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6115 | struct io_timeout_data *data = container_of(timer, |
| 6116 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6117 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6118 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6119 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6120 | |
| 6121 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6122 | prev = req->timeout.head; |
| 6123 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6124 | |
| 6125 | /* |
| 6126 | * We don't expect the list to be empty, that will only happen if we |
| 6127 | * race with the completion of the linked work. |
| 6128 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6129 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6130 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6131 | else |
| 6132 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6133 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6134 | |
| 6135 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6136 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6137 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6138 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6139 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6140 | io_req_complete_post(req, -ETIME, 0); |
| 6141 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6142 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6143 | return HRTIMER_NORESTART; |
| 6144 | } |
| 6145 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6146 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6147 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6148 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6149 | * If the back reference is NULL, then our linked request finished |
| 6150 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6151 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6152 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6153 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6154 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6155 | data->timer.function = io_link_timeout_fn; |
| 6156 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6157 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6158 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6159 | } |
| 6160 | |
| 6161 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6162 | { |
| 6163 | struct io_ring_ctx *ctx = req->ctx; |
| 6164 | |
| 6165 | spin_lock_irq(&ctx->completion_lock); |
| 6166 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6167 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6168 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6169 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6170 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6171 | } |
| 6172 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6173 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6174 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6175 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6176 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6177 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6178 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6179 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6180 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6181 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6182 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6183 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6184 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6185 | } |
| 6186 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6187 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6188 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6189 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6190 | int ret; |
| 6191 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6192 | 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] | 6193 | |
| 6194 | /* |
| 6195 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6196 | * doesn't support non-blocking read/write attempts |
| 6197 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6198 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6199 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6200 | /* |
| 6201 | * Queued up for async execution, worker will release |
| 6202 | * submit reference when the iocb is actually submitted. |
| 6203 | */ |
| 6204 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6205 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6206 | } else if (likely(!ret)) { |
| 6207 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6208 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6209 | struct io_ring_ctx *ctx = req->ctx; |
| 6210 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6211 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6212 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6213 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6214 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6215 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6216 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6217 | } |
| 6218 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6219 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6220 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6221 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6222 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6223 | if (linked_timeout) |
| 6224 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6225 | } |
| 6226 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6227 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6228 | { |
| 6229 | int ret; |
| 6230 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6231 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6232 | if (ret) { |
| 6233 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6234 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6235 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6236 | io_put_req(req); |
| 6237 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6238 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6239 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6240 | ret = io_req_defer_prep(req); |
| 6241 | if (unlikely(ret)) |
| 6242 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6243 | io_queue_async_work(req); |
| 6244 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6245 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6246 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6247 | } |
| 6248 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6249 | /* |
| 6250 | * Check SQE restrictions (opcode and flags). |
| 6251 | * |
| 6252 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6253 | */ |
| 6254 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6255 | struct io_kiocb *req, |
| 6256 | unsigned int sqe_flags) |
| 6257 | { |
| 6258 | if (!ctx->restricted) |
| 6259 | return true; |
| 6260 | |
| 6261 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6262 | return false; |
| 6263 | |
| 6264 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6265 | ctx->restrictions.sqe_flags_required) |
| 6266 | return false; |
| 6267 | |
| 6268 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6269 | ctx->restrictions.sqe_flags_required)) |
| 6270 | return false; |
| 6271 | |
| 6272 | return true; |
| 6273 | } |
| 6274 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6275 | 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] | 6276 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6277 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6278 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6279 | unsigned int sqe_flags; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame^] | 6280 | int ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6281 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6282 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6283 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6284 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6285 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6286 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6287 | req->file = NULL; |
| 6288 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6289 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6290 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6291 | /* one is dropped after submission, the other at completion */ |
| 6292 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6293 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6294 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6295 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6296 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6297 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
| 6298 | req->flags = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6299 | return -EINVAL; |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6300 | } |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6301 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6302 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6303 | return -EINVAL; |
| 6304 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6305 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6306 | return -EACCES; |
| 6307 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6308 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6309 | !io_op_defs[req->opcode].buffer_select) |
| 6310 | return -EOPNOTSUPP; |
| 6311 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame^] | 6312 | req->work.list.next = NULL; |
| 6313 | req->work.flags = 0; |
| 6314 | req->work.personality = READ_ONCE(sqe->personality); |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6315 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6316 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6317 | /* |
| 6318 | * Plug now if we have more than 1 IO left after this, and the target |
| 6319 | * is potentially a read/write to block based storage. |
| 6320 | */ |
| 6321 | if (!state->plug_started && state->ios_left > 1 && |
| 6322 | io_op_defs[req->opcode].plug) { |
| 6323 | blk_start_plug(&state->plug); |
| 6324 | state->plug_started = true; |
| 6325 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6326 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6327 | if (io_op_defs[req->opcode].needs_file) { |
| 6328 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6329 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6330 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6331 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6332 | ret = -EBADF; |
| 6333 | } |
| 6334 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6335 | state->ios_left--; |
| 6336 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6337 | } |
| 6338 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6339 | 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] | 6340 | const struct io_uring_sqe *sqe) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6341 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6342 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6343 | int ret; |
| 6344 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6345 | ret = io_init_req(ctx, req, sqe); |
| 6346 | if (unlikely(ret)) { |
| 6347 | fail_req: |
| 6348 | io_put_req(req); |
| 6349 | io_req_complete(req, ret); |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6350 | if (link->head) { |
| 6351 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6352 | link->head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6353 | io_put_req(link->head); |
| 6354 | io_req_complete(link->head, -ECANCELED); |
| 6355 | link->head = NULL; |
| 6356 | } |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6357 | return ret; |
| 6358 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6359 | ret = io_req_prep(req, sqe); |
| 6360 | if (unlikely(ret)) |
| 6361 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6362 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6363 | /* don't need @sqe from now on */ |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6364 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 6365 | true, ctx->flags & IORING_SETUP_SQPOLL); |
| 6366 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6367 | /* |
| 6368 | * If we already have a head request, queue this one for async |
| 6369 | * submittal once the head completes. If we don't have a head but |
| 6370 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6371 | * submitted sync once the chain is complete. If none of those |
| 6372 | * conditions are true (normal request), then just queue it. |
| 6373 | */ |
| 6374 | if (link->head) { |
| 6375 | struct io_kiocb *head = link->head; |
| 6376 | |
| 6377 | /* |
| 6378 | * Taking sequential execution of a link, draining both sides |
| 6379 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6380 | * requests in the link. So, it drains the head and the |
| 6381 | * next after the link request. The last one is done via |
| 6382 | * drain_next flag to persist the effect across calls. |
| 6383 | */ |
| 6384 | if (req->flags & REQ_F_IO_DRAIN) { |
| 6385 | head->flags |= REQ_F_IO_DRAIN; |
| 6386 | ctx->drain_next = 1; |
| 6387 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6388 | ret = io_req_defer_prep(req); |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6389 | if (unlikely(ret)) |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6390 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6391 | trace_io_uring_link(ctx, req, head); |
| 6392 | link->last->link = req; |
| 6393 | link->last = req; |
| 6394 | |
| 6395 | /* last request of a link, enqueue the link */ |
| 6396 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6397 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6398 | link->head = NULL; |
| 6399 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6400 | } else { |
| 6401 | if (unlikely(ctx->drain_next)) { |
| 6402 | req->flags |= REQ_F_IO_DRAIN; |
| 6403 | ctx->drain_next = 0; |
| 6404 | } |
| 6405 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6406 | link->head = req; |
| 6407 | link->last = req; |
| 6408 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6409 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | return 0; |
| 6414 | } |
| 6415 | |
| 6416 | /* |
| 6417 | * Batched submission is done, ensure local IO is flushed out. |
| 6418 | */ |
| 6419 | static void io_submit_state_end(struct io_submit_state *state, |
| 6420 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6421 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6422 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6423 | io_queue_sqe(state->link.head); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6424 | if (state->comp.nr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6425 | io_submit_flush_completions(&state->comp, ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6426 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6427 | blk_finish_plug(&state->plug); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6428 | io_state_file_put(state); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6429 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6430 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6431 | /* |
| 6432 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6433 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6434 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6435 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6436 | { |
| 6437 | state->plug_started = false; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6438 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6439 | /* set only head, no need to init link_last in advance */ |
| 6440 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6441 | } |
| 6442 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6443 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6444 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6445 | struct io_rings *rings = ctx->rings; |
| 6446 | |
| 6447 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6448 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6449 | * since once we write the new head, the application could |
| 6450 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 6451 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6452 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6453 | } |
| 6454 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6455 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6456 | * 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] | 6457 | * that is mapped by userspace. This means that care needs to be taken to |
| 6458 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 6459 | * being a good citizen. If members of the sqe are validated and then later |
| 6460 | * 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] | 6461 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6462 | */ |
| 6463 | 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] | 6464 | { |
| 6465 | u32 *sq_array = ctx->sq_array; |
| 6466 | unsigned head; |
| 6467 | |
| 6468 | /* |
| 6469 | * The cached sq head (or cq tail) serves two purposes: |
| 6470 | * |
| 6471 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6472 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6473 | * 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] | 6474 | * though the application is the one updating it. |
| 6475 | */ |
| 6476 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
| 6477 | if (likely(head < ctx->sq_entries)) |
| 6478 | return &ctx->sq_sqes[head]; |
| 6479 | |
| 6480 | /* drop invalid entries */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6481 | ctx->cached_sq_dropped++; |
| 6482 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
| 6483 | return NULL; |
| 6484 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 6485 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6486 | 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] | 6487 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6488 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6489 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6490 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6491 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6492 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6493 | return -EBUSY; |
| 6494 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6495 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6496 | /* make sure SQ entry isn't read before tail */ |
| 6497 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6498 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6499 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6500 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6501 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6502 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6503 | refcount_add(nr, ¤t->usage); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6504 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6505 | |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6506 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6507 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6508 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6509 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6510 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6511 | if (unlikely(!req)) { |
| 6512 | if (!submitted) |
| 6513 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6514 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6515 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6516 | sqe = io_get_sqe(ctx); |
| 6517 | if (unlikely(!sqe)) { |
| 6518 | kmem_cache_free(req_cachep, req); |
| 6519 | break; |
| 6520 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6521 | /* will complete beyond this point, count as submitted */ |
| 6522 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6523 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6524 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6525 | } |
| 6526 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6527 | if (unlikely(submitted != nr)) { |
| 6528 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6529 | struct io_uring_task *tctx = current->io_uring; |
| 6530 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6531 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6532 | percpu_ref_put_many(&ctx->refs, unused); |
| 6533 | percpu_counter_sub(&tctx->inflight, unused); |
| 6534 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6535 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6536 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6537 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6538 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6539 | io_commit_sqring(ctx); |
| 6540 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6541 | return submitted; |
| 6542 | } |
| 6543 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6544 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6545 | { |
| 6546 | /* Tell userspace we may need a wakeup call */ |
| 6547 | spin_lock_irq(&ctx->completion_lock); |
| 6548 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6549 | spin_unlock_irq(&ctx->completion_lock); |
| 6550 | } |
| 6551 | |
| 6552 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6553 | { |
| 6554 | spin_lock_irq(&ctx->completion_lock); |
| 6555 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6556 | spin_unlock_irq(&ctx->completion_lock); |
| 6557 | } |
| 6558 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6559 | 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] | 6560 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6561 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6562 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6563 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6564 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6565 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6566 | if (cap_entries && to_submit > 8) |
| 6567 | to_submit = 8; |
| 6568 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6569 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6570 | unsigned nr_events = 0; |
| 6571 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6572 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6573 | if (!list_empty(&ctx->iopoll_list)) |
| 6574 | io_do_iopoll(ctx, &nr_events, 0); |
| 6575 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6576 | if (to_submit && !ctx->sqo_dead && |
| 6577 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6578 | ret = io_submit_sqes(ctx, to_submit); |
| 6579 | mutex_unlock(&ctx->uring_lock); |
| 6580 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6581 | |
| 6582 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6583 | wake_up(&ctx->sqo_sq_wait); |
| 6584 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6585 | return ret; |
| 6586 | } |
| 6587 | |
| 6588 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 6589 | { |
| 6590 | struct io_ring_ctx *ctx; |
| 6591 | unsigned sq_thread_idle = 0; |
| 6592 | |
| 6593 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6594 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 6595 | sq_thread_idle = ctx->sq_thread_idle; |
| 6596 | } |
| 6597 | |
| 6598 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6599 | } |
| 6600 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6601 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 6602 | { |
| 6603 | struct io_ring_ctx *ctx; |
| 6604 | |
| 6605 | while (!list_empty(&sqd->ctx_new_list)) { |
| 6606 | ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6607 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 6608 | complete(&ctx->sq_thread_comp); |
| 6609 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6610 | |
| 6611 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6612 | } |
| 6613 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6614 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
| 6615 | { |
| 6616 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 6617 | } |
| 6618 | |
| 6619 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) |
| 6620 | { |
| 6621 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 6622 | } |
| 6623 | |
| 6624 | static void io_sq_thread_parkme(struct io_sq_data *sqd) |
| 6625 | { |
| 6626 | for (;;) { |
| 6627 | /* |
| 6628 | * TASK_PARKED is a special state; we must serialize against |
| 6629 | * possible pending wakeups to avoid store-store collisions on |
| 6630 | * task->state. |
| 6631 | * |
| 6632 | * Such a collision might possibly result in the task state |
| 6633 | * changin from TASK_PARKED and us failing the |
| 6634 | * wait_task_inactive() in kthread_park(). |
| 6635 | */ |
| 6636 | set_special_state(TASK_PARKED); |
| 6637 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) |
| 6638 | break; |
| 6639 | |
| 6640 | /* |
| 6641 | * Thread is going to call schedule(), do not preempt it, |
| 6642 | * or the caller of kthread_park() may spend more time in |
| 6643 | * wait_task_inactive(). |
| 6644 | */ |
| 6645 | preempt_disable(); |
| 6646 | complete(&sqd->completion); |
| 6647 | schedule_preempt_disabled(); |
| 6648 | preempt_enable(); |
| 6649 | } |
| 6650 | __set_current_state(TASK_RUNNING); |
| 6651 | } |
| 6652 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6653 | static int io_sq_thread(void *data) |
| 6654 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6655 | struct io_sq_data *sqd = data; |
| 6656 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 6657 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6658 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6659 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6660 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6661 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
| 6662 | set_task_comm(current, buf); |
| 6663 | sqd->thread = current; |
| 6664 | current->pf_io_worker = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6665 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6666 | if (sqd->sq_cpu != -1) |
| 6667 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 6668 | else |
| 6669 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 6670 | current->flags |= PF_NO_SETAFFINITY; |
| 6671 | |
| 6672 | complete(&sqd->completion); |
| 6673 | |
| 6674 | wait_for_completion(&sqd->startup); |
| 6675 | |
| 6676 | while (!io_sq_thread_should_stop(sqd)) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6677 | int ret; |
| 6678 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6679 | |
| 6680 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6681 | * Any changes to the sqd lists are synchronized through the |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6682 | * thread parking. This synchronizes the thread vs users, |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6683 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6684 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6685 | if (io_sq_thread_should_park(sqd)) { |
| 6686 | io_sq_thread_parkme(sqd); |
| 6687 | continue; |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 6688 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6689 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6690 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6691 | timeout = jiffies + sqd->sq_thread_idle; |
| 6692 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6693 | if (fatal_signal_pending(current)) |
| 6694 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6695 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6696 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6697 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6698 | ret = __io_sq_thread(ctx, cap_entries); |
| 6699 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 6700 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6701 | } |
| 6702 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6703 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6704 | io_run_task_work(); |
| 6705 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6706 | if (sqt_spin) |
| 6707 | timeout = jiffies + sqd->sq_thread_idle; |
| 6708 | continue; |
| 6709 | } |
| 6710 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6711 | needs_sched = true; |
| 6712 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 6713 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6714 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6715 | !list_empty_careful(&ctx->iopoll_list)) { |
| 6716 | needs_sched = false; |
| 6717 | break; |
| 6718 | } |
| 6719 | if (io_sqring_entries(ctx)) { |
| 6720 | needs_sched = false; |
| 6721 | break; |
| 6722 | } |
| 6723 | } |
| 6724 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6725 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6726 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6727 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6728 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6729 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6730 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6731 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6732 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6733 | |
| 6734 | finish_wait(&sqd->wait, &wait); |
| 6735 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6736 | } |
| 6737 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6738 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6739 | io_uring_cancel_sqpoll(ctx); |
| 6740 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6741 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6742 | |
Jens Axboe | 8629397 | 2021-02-26 13:46:49 -0700 | [diff] [blame] | 6743 | if (io_sq_thread_should_park(sqd)) |
| 6744 | io_sq_thread_parkme(sqd); |
| 6745 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6746 | /* |
| 6747 | * Clear thread under lock so that concurrent parks work correctly |
| 6748 | */ |
Jens Axboe | 8629397 | 2021-02-26 13:46:49 -0700 | [diff] [blame] | 6749 | complete(&sqd->completion); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6750 | mutex_lock(&sqd->lock); |
| 6751 | sqd->thread = NULL; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 6752 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6753 | ctx->sqo_exec = 1; |
| 6754 | io_ring_set_wakeup_flag(ctx); |
| 6755 | } |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6756 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6757 | complete(&sqd->exited); |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 6758 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6759 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6760 | } |
| 6761 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6762 | struct io_wait_queue { |
| 6763 | struct wait_queue_entry wq; |
| 6764 | struct io_ring_ctx *ctx; |
| 6765 | unsigned to_wait; |
| 6766 | unsigned nr_timeouts; |
| 6767 | }; |
| 6768 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6769 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6770 | { |
| 6771 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6772 | |
| 6773 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6774 | * 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] | 6775 | * started waiting. For timeouts, we always want to return to userspace, |
| 6776 | * regardless of event count. |
| 6777 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6778 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6779 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6780 | } |
| 6781 | |
| 6782 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6783 | int wake_flags, void *key) |
| 6784 | { |
| 6785 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6786 | wq); |
| 6787 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6788 | /* |
| 6789 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 6790 | * the task, and the next invocation will do it. |
| 6791 | */ |
| 6792 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 6793 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6794 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6795 | } |
| 6796 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6797 | static int io_run_task_work_sig(void) |
| 6798 | { |
| 6799 | if (io_run_task_work()) |
| 6800 | return 1; |
| 6801 | if (!signal_pending(current)) |
| 6802 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 6803 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 6804 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6805 | return -EINTR; |
| 6806 | } |
| 6807 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6808 | /* when returns >0, the caller should retry */ |
| 6809 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 6810 | struct io_wait_queue *iowq, |
| 6811 | signed long *timeout) |
| 6812 | { |
| 6813 | int ret; |
| 6814 | |
| 6815 | /* make sure we run task_work before checking for signals */ |
| 6816 | ret = io_run_task_work_sig(); |
| 6817 | if (ret || io_should_wake(iowq)) |
| 6818 | return ret; |
| 6819 | /* let the caller flush overflows, retry */ |
| 6820 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 6821 | return 1; |
| 6822 | |
| 6823 | *timeout = schedule_timeout(*timeout); |
| 6824 | return !*timeout ? -ETIME : 1; |
| 6825 | } |
| 6826 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6827 | /* |
| 6828 | * Wait until events become available, if we don't already have some. The |
| 6829 | * application must reap them itself, as they reside on the shared cq ring. |
| 6830 | */ |
| 6831 | 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] | 6832 | const sigset_t __user *sig, size_t sigsz, |
| 6833 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6834 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6835 | struct io_wait_queue iowq = { |
| 6836 | .wq = { |
| 6837 | .private = current, |
| 6838 | .func = io_wake_function, |
| 6839 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6840 | }, |
| 6841 | .ctx = ctx, |
| 6842 | .to_wait = min_events, |
| 6843 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6844 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6845 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 6846 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6847 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6848 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6849 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 6850 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6851 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6852 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6853 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6854 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6855 | |
| 6856 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6857 | #ifdef CONFIG_COMPAT |
| 6858 | if (in_compat_syscall()) |
| 6859 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6860 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6861 | else |
| 6862 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6863 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6864 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6865 | if (ret) |
| 6866 | return ret; |
| 6867 | } |
| 6868 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6869 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6870 | struct timespec64 ts; |
| 6871 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6872 | if (get_timespec64(&ts, uts)) |
| 6873 | return -EFAULT; |
| 6874 | timeout = timespec64_to_jiffies(&ts); |
| 6875 | } |
| 6876 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6877 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6878 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6879 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6880 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6881 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6882 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6883 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 6884 | finish_wait(&ctx->wait, &iowq.wq); |
| 6885 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6886 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6887 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6888 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6889 | 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] | 6890 | } |
| 6891 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6892 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6893 | { |
| 6894 | #if defined(CONFIG_UNIX) |
| 6895 | if (ctx->ring_sock) { |
| 6896 | struct sock *sock = ctx->ring_sock->sk; |
| 6897 | struct sk_buff *skb; |
| 6898 | |
| 6899 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6900 | kfree_skb(skb); |
| 6901 | } |
| 6902 | #else |
| 6903 | int i; |
| 6904 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6905 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6906 | struct file *file; |
| 6907 | |
| 6908 | file = io_file_from_index(ctx, i); |
| 6909 | if (file) |
| 6910 | fput(file); |
| 6911 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6912 | #endif |
| 6913 | } |
| 6914 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 6915 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6916 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6917 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6918 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6919 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6920 | complete(&data->done); |
| 6921 | } |
| 6922 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6923 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 6924 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6925 | spin_lock_bh(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 6926 | } |
| 6927 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6928 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6929 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6930 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 6931 | } |
| 6932 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 6933 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 6934 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6935 | struct fixed_rsrc_ref_node *ref_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6936 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6937 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6938 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 6939 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6940 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6941 | percpu_ref_get(&rsrc_data->refs); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6942 | } |
| 6943 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6944 | static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6945 | { |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6946 | struct fixed_rsrc_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6947 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6948 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 6949 | ref_node = data->node; |
Pavel Begunkov | e6cb007 | 2021-02-20 18:03:47 +0000 | [diff] [blame] | 6950 | data->node = NULL; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6951 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6952 | if (ref_node) |
| 6953 | percpu_ref_kill(&ref_node->refs); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6954 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6955 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6956 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 6957 | struct io_ring_ctx *ctx, |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6958 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 6959 | struct io_rsrc_put *prsrc)) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6960 | { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6961 | struct fixed_rsrc_ref_node *backup_node; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6962 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6963 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6964 | if (data->quiesce) |
| 6965 | return -ENXIO; |
| 6966 | |
| 6967 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 6968 | do { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6969 | ret = -ENOMEM; |
| 6970 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 6971 | if (!backup_node) |
| 6972 | break; |
| 6973 | backup_node->rsrc_data = data; |
| 6974 | backup_node->rsrc_put = rsrc_put; |
| 6975 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6976 | io_sqe_rsrc_kill_node(ctx, data); |
| 6977 | percpu_ref_kill(&data->refs); |
| 6978 | flush_delayed_work(&ctx->rsrc_put_work); |
| 6979 | |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 6980 | ret = wait_for_completion_interruptible(&data->done); |
| 6981 | if (!ret) |
| 6982 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6983 | |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 6984 | percpu_ref_resurrect(&data->refs); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6985 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
| 6986 | backup_node = NULL; |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 6987 | reinit_completion(&data->done); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6988 | mutex_unlock(&ctx->uring_lock); |
| 6989 | ret = io_run_task_work_sig(); |
| 6990 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6991 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6992 | data->quiesce = false; |
| 6993 | |
| 6994 | if (backup_node) |
| 6995 | destroy_fixed_rsrc_ref_node(backup_node); |
| 6996 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 6999 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
| 7000 | { |
| 7001 | struct fixed_rsrc_data *data; |
| 7002 | |
| 7003 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7004 | if (!data) |
| 7005 | return NULL; |
| 7006 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7007 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7008 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7009 | kfree(data); |
| 7010 | return NULL; |
| 7011 | } |
| 7012 | data->ctx = ctx; |
| 7013 | init_completion(&data->done); |
| 7014 | return data; |
| 7015 | } |
| 7016 | |
| 7017 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) |
| 7018 | { |
| 7019 | percpu_ref_exit(&data->refs); |
| 7020 | kfree(data->table); |
| 7021 | kfree(data); |
| 7022 | } |
| 7023 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7024 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7025 | { |
| 7026 | struct fixed_rsrc_data *data = ctx->file_data; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7027 | unsigned nr_tables, i; |
| 7028 | int ret; |
| 7029 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7030 | /* |
| 7031 | * percpu_ref_is_dying() is to stop parallel files unregister |
| 7032 | * Since we possibly drop uring lock later in this function to |
| 7033 | * run task work. |
| 7034 | */ |
| 7035 | if (!data || percpu_ref_is_dying(&data->refs)) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7036 | return -ENXIO; |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7037 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7038 | if (ret) |
| 7039 | return ret; |
| 7040 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7041 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7042 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7043 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7044 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7045 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7046 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7047 | ctx->nr_user_files = 0; |
| 7048 | return 0; |
| 7049 | } |
| 7050 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7051 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7052 | __releases(&sqd->lock) |
| 7053 | { |
| 7054 | if (!sqd->thread) |
| 7055 | return; |
| 7056 | if (sqd->thread == current) |
| 7057 | return; |
| 7058 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 7059 | wake_up_state(sqd->thread, TASK_PARKED); |
| 7060 | mutex_unlock(&sqd->lock); |
| 7061 | } |
| 7062 | |
| 7063 | static bool io_sq_thread_park(struct io_sq_data *sqd) |
| 7064 | __acquires(&sqd->lock) |
| 7065 | { |
| 7066 | if (sqd->thread == current) |
| 7067 | return true; |
| 7068 | mutex_lock(&sqd->lock); |
| 7069 | if (!sqd->thread) { |
| 7070 | mutex_unlock(&sqd->lock); |
| 7071 | return false; |
| 7072 | } |
| 7073 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 7074 | wake_up_process(sqd->thread); |
| 7075 | wait_for_completion(&sqd->completion); |
| 7076 | return true; |
| 7077 | } |
| 7078 | |
| 7079 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7080 | { |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 7081 | if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7082 | return; |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 7083 | mutex_lock(&sqd->lock); |
| 7084 | if (sqd->thread) { |
| 7085 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7086 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); |
| 7087 | wake_up_process(sqd->thread); |
| 7088 | mutex_unlock(&sqd->lock); |
| 7089 | wait_for_completion(&sqd->exited); |
| 7090 | WARN_ON_ONCE(sqd->thread); |
| 7091 | } else { |
| 7092 | mutex_unlock(&sqd->lock); |
| 7093 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7094 | } |
| 7095 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7096 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7097 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7098 | if (refcount_dec_and_test(&sqd->refs)) { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7099 | io_sq_thread_stop(sqd); |
| 7100 | kfree(sqd); |
| 7101 | } |
| 7102 | } |
| 7103 | |
| 7104 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7105 | { |
| 7106 | struct io_sq_data *sqd = ctx->sq_data; |
| 7107 | |
| 7108 | if (sqd) { |
Jens Axboe | eb85890 | 2021-02-25 10:13:29 -0700 | [diff] [blame] | 7109 | complete(&sqd->startup); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7110 | if (sqd->thread) { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7111 | wait_for_completion(&ctx->sq_thread_comp); |
| 7112 | io_sq_thread_park(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7113 | } |
| 7114 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7115 | mutex_lock(&sqd->ctx_lock); |
| 7116 | list_del(&ctx->sqd_list); |
| 7117 | io_sqd_update_thread_idle(sqd); |
| 7118 | mutex_unlock(&sqd->ctx_lock); |
| 7119 | |
| 7120 | if (sqd->thread) |
| 7121 | io_sq_thread_unpark(sqd); |
| 7122 | |
| 7123 | io_put_sq_data(sqd); |
| 7124 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7125 | } |
| 7126 | } |
| 7127 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7128 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7129 | { |
| 7130 | struct io_ring_ctx *ctx_attach; |
| 7131 | struct io_sq_data *sqd; |
| 7132 | struct fd f; |
| 7133 | |
| 7134 | f = fdget(p->wq_fd); |
| 7135 | if (!f.file) |
| 7136 | return ERR_PTR(-ENXIO); |
| 7137 | if (f.file->f_op != &io_uring_fops) { |
| 7138 | fdput(f); |
| 7139 | return ERR_PTR(-EINVAL); |
| 7140 | } |
| 7141 | |
| 7142 | ctx_attach = f.file->private_data; |
| 7143 | sqd = ctx_attach->sq_data; |
| 7144 | if (!sqd) { |
| 7145 | fdput(f); |
| 7146 | return ERR_PTR(-EINVAL); |
| 7147 | } |
| 7148 | |
| 7149 | refcount_inc(&sqd->refs); |
| 7150 | fdput(f); |
| 7151 | return sqd; |
| 7152 | } |
| 7153 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7154 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7155 | { |
| 7156 | struct io_sq_data *sqd; |
| 7157 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7158 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7159 | return io_attach_sq_data(p); |
| 7160 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7161 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7162 | if (!sqd) |
| 7163 | return ERR_PTR(-ENOMEM); |
| 7164 | |
| 7165 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7166 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7167 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7168 | mutex_init(&sqd->ctx_lock); |
| 7169 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7170 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7171 | init_completion(&sqd->startup); |
| 7172 | init_completion(&sqd->completion); |
| 7173 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7174 | return sqd; |
| 7175 | } |
| 7176 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7177 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7178 | /* |
| 7179 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7180 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7181 | * loops in the file referencing. |
| 7182 | */ |
| 7183 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7184 | { |
| 7185 | struct sock *sk = ctx->ring_sock->sk; |
| 7186 | struct scm_fp_list *fpl; |
| 7187 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7188 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7189 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7190 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7191 | if (!fpl) |
| 7192 | return -ENOMEM; |
| 7193 | |
| 7194 | skb = alloc_skb(0, GFP_KERNEL); |
| 7195 | if (!skb) { |
| 7196 | kfree(fpl); |
| 7197 | return -ENOMEM; |
| 7198 | } |
| 7199 | |
| 7200 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7201 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7202 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7203 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7204 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7205 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7206 | |
| 7207 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7208 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7209 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7210 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7211 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7212 | } |
| 7213 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7214 | if (nr_files) { |
| 7215 | fpl->max = SCM_MAX_FD; |
| 7216 | fpl->count = nr_files; |
| 7217 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7218 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7219 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7220 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7221 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7222 | for (i = 0; i < nr_files; i++) |
| 7223 | fput(fpl->fp[i]); |
| 7224 | } else { |
| 7225 | kfree_skb(skb); |
| 7226 | kfree(fpl); |
| 7227 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7228 | |
| 7229 | return 0; |
| 7230 | } |
| 7231 | |
| 7232 | /* |
| 7233 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7234 | * causes regular reference counting to break down. We rely on the UNIX |
| 7235 | * garbage collection to take care of this problem for us. |
| 7236 | */ |
| 7237 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7238 | { |
| 7239 | unsigned left, total; |
| 7240 | int ret = 0; |
| 7241 | |
| 7242 | total = 0; |
| 7243 | left = ctx->nr_user_files; |
| 7244 | while (left) { |
| 7245 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7246 | |
| 7247 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7248 | if (ret) |
| 7249 | break; |
| 7250 | left -= this_files; |
| 7251 | total += this_files; |
| 7252 | } |
| 7253 | |
| 7254 | if (!ret) |
| 7255 | return 0; |
| 7256 | |
| 7257 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7258 | struct file *file = io_file_from_index(ctx, total); |
| 7259 | |
| 7260 | if (file) |
| 7261 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7262 | total++; |
| 7263 | } |
| 7264 | |
| 7265 | return ret; |
| 7266 | } |
| 7267 | #else |
| 7268 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7269 | { |
| 7270 | return 0; |
| 7271 | } |
| 7272 | #endif |
| 7273 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7274 | static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data, |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7275 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7276 | { |
| 7277 | int i; |
| 7278 | |
| 7279 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7280 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7281 | unsigned this_files; |
| 7282 | |
| 7283 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7284 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7285 | GFP_KERNEL); |
| 7286 | if (!table->files) |
| 7287 | break; |
| 7288 | nr_files -= this_files; |
| 7289 | } |
| 7290 | |
| 7291 | if (i == nr_tables) |
| 7292 | return 0; |
| 7293 | |
| 7294 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7295 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7296 | kfree(table->files); |
| 7297 | } |
| 7298 | return 1; |
| 7299 | } |
| 7300 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7301 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7302 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7303 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7304 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7305 | struct sock *sock = ctx->ring_sock->sk; |
| 7306 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7307 | struct sk_buff *skb; |
| 7308 | int i; |
| 7309 | |
| 7310 | __skb_queue_head_init(&list); |
| 7311 | |
| 7312 | /* |
| 7313 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7314 | * remove this entry and rearrange the file array. |
| 7315 | */ |
| 7316 | skb = skb_dequeue(head); |
| 7317 | while (skb) { |
| 7318 | struct scm_fp_list *fp; |
| 7319 | |
| 7320 | fp = UNIXCB(skb).fp; |
| 7321 | for (i = 0; i < fp->count; i++) { |
| 7322 | int left; |
| 7323 | |
| 7324 | if (fp->fp[i] != file) |
| 7325 | continue; |
| 7326 | |
| 7327 | unix_notinflight(fp->user, fp->fp[i]); |
| 7328 | left = fp->count - 1 - i; |
| 7329 | if (left) { |
| 7330 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7331 | left * sizeof(struct file *)); |
| 7332 | } |
| 7333 | fp->count--; |
| 7334 | if (!fp->count) { |
| 7335 | kfree_skb(skb); |
| 7336 | skb = NULL; |
| 7337 | } else { |
| 7338 | __skb_queue_tail(&list, skb); |
| 7339 | } |
| 7340 | fput(file); |
| 7341 | file = NULL; |
| 7342 | break; |
| 7343 | } |
| 7344 | |
| 7345 | if (!file) |
| 7346 | break; |
| 7347 | |
| 7348 | __skb_queue_tail(&list, skb); |
| 7349 | |
| 7350 | skb = skb_dequeue(head); |
| 7351 | } |
| 7352 | |
| 7353 | if (skb_peek(&list)) { |
| 7354 | spin_lock_irq(&head->lock); |
| 7355 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7356 | __skb_queue_tail(head, skb); |
| 7357 | spin_unlock_irq(&head->lock); |
| 7358 | } |
| 7359 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7360 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7361 | #endif |
| 7362 | } |
| 7363 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7364 | static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7365 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7366 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7367 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7368 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7369 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7370 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7371 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7372 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7373 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7374 | } |
| 7375 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7376 | percpu_ref_exit(&ref_node->refs); |
| 7377 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7378 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7379 | } |
| 7380 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7381 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7382 | { |
| 7383 | struct io_ring_ctx *ctx; |
| 7384 | struct llist_node *node; |
| 7385 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7386 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7387 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7388 | |
| 7389 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7390 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7391 | struct llist_node *next = node->next; |
| 7392 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7393 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7394 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7395 | node = next; |
| 7396 | } |
| 7397 | } |
| 7398 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7399 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7400 | unsigned i) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7401 | { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7402 | struct fixed_rsrc_table *table; |
| 7403 | |
| 7404 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7405 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7406 | } |
| 7407 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7408 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7409 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7410 | struct fixed_rsrc_ref_node *ref_node; |
| 7411 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7412 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7413 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7414 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7415 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7416 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7417 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7418 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7419 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7420 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7421 | ref_node->done = true; |
| 7422 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7423 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7424 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7425 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7426 | /* recycle ref nodes in order */ |
| 7427 | if (!ref_node->done) |
| 7428 | break; |
| 7429 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7430 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7431 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7432 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7433 | |
| 7434 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7435 | delay = 0; |
| 7436 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7437 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7438 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7439 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7440 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7441 | } |
| 7442 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7443 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7444 | struct io_ring_ctx *ctx) |
| 7445 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7446 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7447 | |
| 7448 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7449 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7450 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7451 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7452 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7453 | 0, GFP_KERNEL)) { |
| 7454 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7455 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7456 | } |
| 7457 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7458 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7459 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7460 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7461 | } |
| 7462 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7463 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7464 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7465 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7466 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7467 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7468 | } |
| 7469 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7470 | static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node) |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7471 | { |
| 7472 | percpu_ref_exit(&ref_node->refs); |
| 7473 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7474 | } |
| 7475 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7476 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7477 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7478 | unsigned nr_args) |
| 7479 | { |
| 7480 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7481 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7482 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7483 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7484 | struct fixed_rsrc_ref_node *ref_node; |
| 7485 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7486 | |
| 7487 | if (ctx->file_data) |
| 7488 | return -EBUSY; |
| 7489 | if (!nr_args) |
| 7490 | return -EINVAL; |
| 7491 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7492 | return -EMFILE; |
| 7493 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7494 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7495 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7496 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7497 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7498 | |
| 7499 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7500 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7501 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7502 | if (!file_data->table) |
| 7503 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7504 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7505 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7506 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7507 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7508 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7509 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7510 | ret = -EFAULT; |
| 7511 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7512 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7513 | /* allow sparse sets */ |
| 7514 | if (fd == -1) |
| 7515 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7516 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7517 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7518 | ret = -EBADF; |
| 7519 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7520 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7521 | |
| 7522 | /* |
| 7523 | * Don't allow io_uring instances to be registered. If UNIX |
| 7524 | * isn't enabled, then this causes a reference cycle and this |
| 7525 | * instance can never get freed. If UNIX is enabled we'll |
| 7526 | * handle it just fine, but there's still no point in allowing |
| 7527 | * a ring fd as it doesn't support regular read/write anyway. |
| 7528 | */ |
| 7529 | if (file->f_op == &io_uring_fops) { |
| 7530 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7531 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7532 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7533 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7534 | } |
| 7535 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7536 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7537 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7538 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7539 | return ret; |
| 7540 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7541 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7542 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7543 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7544 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7545 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7546 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7547 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7548 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7549 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7550 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7551 | out_fput: |
| 7552 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7553 | file = io_file_from_index(ctx, i); |
| 7554 | if (file) |
| 7555 | fput(file); |
| 7556 | } |
| 7557 | for (i = 0; i < nr_tables; i++) |
| 7558 | kfree(file_data->table[i].files); |
| 7559 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7560 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7561 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7562 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7563 | return ret; |
| 7564 | } |
| 7565 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7566 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7567 | int index) |
| 7568 | { |
| 7569 | #if defined(CONFIG_UNIX) |
| 7570 | struct sock *sock = ctx->ring_sock->sk; |
| 7571 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7572 | struct sk_buff *skb; |
| 7573 | |
| 7574 | /* |
| 7575 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7576 | * file set. If there's no room, fall back to allocating a new skb |
| 7577 | * and filling it in. |
| 7578 | */ |
| 7579 | spin_lock_irq(&head->lock); |
| 7580 | skb = skb_peek(head); |
| 7581 | if (skb) { |
| 7582 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7583 | |
| 7584 | if (fpl->count < SCM_MAX_FD) { |
| 7585 | __skb_unlink(skb, head); |
| 7586 | spin_unlock_irq(&head->lock); |
| 7587 | fpl->fp[fpl->count] = get_file(file); |
| 7588 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7589 | fpl->count++; |
| 7590 | spin_lock_irq(&head->lock); |
| 7591 | __skb_queue_head(head, skb); |
| 7592 | } else { |
| 7593 | skb = NULL; |
| 7594 | } |
| 7595 | } |
| 7596 | spin_unlock_irq(&head->lock); |
| 7597 | |
| 7598 | if (skb) { |
| 7599 | fput(file); |
| 7600 | return 0; |
| 7601 | } |
| 7602 | |
| 7603 | return __io_sqe_files_scm(ctx, 1, index); |
| 7604 | #else |
| 7605 | return 0; |
| 7606 | #endif |
| 7607 | } |
| 7608 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7609 | static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7610 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7611 | struct io_rsrc_put *prsrc; |
| 7612 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7613 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7614 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7615 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7616 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7617 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7618 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7619 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7620 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7621 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7622 | } |
| 7623 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7624 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7625 | struct file *file) |
| 7626 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7627 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7630 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7631 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7632 | unsigned nr_args) |
| 7633 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7634 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7635 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7636 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7637 | __s32 __user *fds; |
| 7638 | int fd, i, err; |
| 7639 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7640 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7641 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7642 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7643 | return -EOVERFLOW; |
| 7644 | if (done > ctx->nr_user_files) |
| 7645 | return -EINVAL; |
| 7646 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7647 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7648 | if (!ref_node) |
| 7649 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7650 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7651 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7652 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7653 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7654 | err = 0; |
| 7655 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7656 | err = -EFAULT; |
| 7657 | break; |
| 7658 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 7659 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 7660 | continue; |
| 7661 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7662 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7663 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 7664 | |
| 7665 | if (*file_slot) { |
| 7666 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7667 | if (err) |
| 7668 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7669 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7670 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7671 | } |
| 7672 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7673 | file = fget(fd); |
| 7674 | if (!file) { |
| 7675 | err = -EBADF; |
| 7676 | break; |
| 7677 | } |
| 7678 | /* |
| 7679 | * Don't allow io_uring instances to be registered. If |
| 7680 | * UNIX isn't enabled, then this causes a reference |
| 7681 | * cycle and this instance can never get freed. If UNIX |
| 7682 | * is enabled we'll handle it just fine, but there's |
| 7683 | * still no point in allowing a ring fd as it doesn't |
| 7684 | * support regular read/write anyway. |
| 7685 | */ |
| 7686 | if (file->f_op == &io_uring_fops) { |
| 7687 | fput(file); |
| 7688 | err = -EBADF; |
| 7689 | break; |
| 7690 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 7691 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7692 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7693 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 7694 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7695 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7696 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7697 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7698 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7699 | } |
| 7700 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7701 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 7702 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7703 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7704 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7705 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7706 | |
| 7707 | return done ? done : err; |
| 7708 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7709 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7710 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7711 | unsigned nr_args) |
| 7712 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7713 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7714 | |
| 7715 | if (!ctx->file_data) |
| 7716 | return -ENXIO; |
| 7717 | if (!nr_args) |
| 7718 | return -EINVAL; |
| 7719 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7720 | return -EFAULT; |
| 7721 | if (up.resv) |
| 7722 | return -EINVAL; |
| 7723 | |
| 7724 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7725 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7726 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7727 | 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] | 7728 | { |
| 7729 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7730 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7731 | req = io_put_req_find_next(req); |
| 7732 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7733 | } |
| 7734 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7735 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7736 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7737 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7738 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7739 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7740 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7741 | hash = ctx->hash_map; |
| 7742 | if (!hash) { |
| 7743 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 7744 | if (!hash) |
| 7745 | return ERR_PTR(-ENOMEM); |
| 7746 | refcount_set(&hash->refs, 1); |
| 7747 | init_waitqueue_head(&hash->wait); |
| 7748 | ctx->hash_map = hash; |
| 7749 | } |
| 7750 | |
| 7751 | data.hash = hash; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7752 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7753 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7754 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7755 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7756 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7757 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7758 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7759 | } |
| 7760 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7761 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 7762 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7763 | { |
| 7764 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7765 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7766 | |
| 7767 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 7768 | if (unlikely(!tctx)) |
| 7769 | return -ENOMEM; |
| 7770 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7771 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 7772 | if (unlikely(ret)) { |
| 7773 | kfree(tctx); |
| 7774 | return ret; |
| 7775 | } |
| 7776 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7777 | tctx->io_wq = io_init_wq_offload(ctx); |
| 7778 | if (IS_ERR(tctx->io_wq)) { |
| 7779 | ret = PTR_ERR(tctx->io_wq); |
| 7780 | percpu_counter_destroy(&tctx->inflight); |
| 7781 | kfree(tctx); |
| 7782 | return ret; |
| 7783 | } |
| 7784 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7785 | xa_init(&tctx->xa); |
| 7786 | init_waitqueue_head(&tctx->wait); |
| 7787 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 7788 | atomic_set(&tctx->in_idle, 0); |
| 7789 | tctx->sqpoll = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7790 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 7791 | spin_lock_init(&tctx->task_lock); |
| 7792 | INIT_WQ_LIST(&tctx->task_list); |
| 7793 | tctx->task_state = 0; |
| 7794 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7795 | return 0; |
| 7796 | } |
| 7797 | |
| 7798 | void __io_uring_free(struct task_struct *tsk) |
| 7799 | { |
| 7800 | struct io_uring_task *tctx = tsk->io_uring; |
| 7801 | |
| 7802 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 7803 | WARN_ON_ONCE(tctx->io_wq); |
| 7804 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7805 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7806 | kfree(tctx); |
| 7807 | tsk->io_uring = NULL; |
| 7808 | } |
| 7809 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7810 | static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx) |
| 7811 | { |
| 7812 | int ret; |
| 7813 | |
| 7814 | clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7815 | reinit_completion(&sqd->completion); |
| 7816 | ctx->sqo_dead = ctx->sqo_exec = 0; |
| 7817 | sqd->task_pid = current->pid; |
| 7818 | current->flags |= PF_IO_WORKER; |
| 7819 | ret = io_wq_fork_thread(io_sq_thread, sqd); |
| 7820 | current->flags &= ~PF_IO_WORKER; |
| 7821 | if (ret < 0) { |
| 7822 | sqd->thread = NULL; |
| 7823 | return ret; |
| 7824 | } |
| 7825 | wait_for_completion(&sqd->completion); |
| 7826 | return io_uring_alloc_task_context(sqd->thread, ctx); |
| 7827 | } |
| 7828 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7829 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 7830 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7831 | { |
| 7832 | int ret; |
| 7833 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7834 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 7835 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 7836 | IORING_SETUP_ATTACH_WQ) { |
| 7837 | struct fd f; |
| 7838 | |
| 7839 | f = fdget(p->wq_fd); |
| 7840 | if (!f.file) |
| 7841 | return -ENXIO; |
| 7842 | if (f.file->f_op != &io_uring_fops) { |
| 7843 | fdput(f); |
| 7844 | return -EINVAL; |
| 7845 | } |
| 7846 | fdput(f); |
| 7847 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7848 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7849 | struct io_sq_data *sqd; |
| 7850 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7851 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 7852 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7853 | goto err; |
| 7854 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7855 | sqd = io_get_sq_data(p); |
| 7856 | if (IS_ERR(sqd)) { |
| 7857 | ret = PTR_ERR(sqd); |
| 7858 | goto err; |
| 7859 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7860 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7861 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7862 | io_sq_thread_park(sqd); |
| 7863 | mutex_lock(&sqd->ctx_lock); |
| 7864 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 7865 | mutex_unlock(&sqd->ctx_lock); |
| 7866 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7867 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7868 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7869 | if (!ctx->sq_thread_idle) |
| 7870 | ctx->sq_thread_idle = HZ; |
| 7871 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7872 | if (sqd->thread) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7873 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7874 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7875 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7876 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7877 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7878 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7879 | if (cpu >= nr_cpu_ids) |
| 7880 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7881 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7882 | goto err; |
| 7883 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7884 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7885 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7886 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7887 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7888 | |
| 7889 | sqd->task_pid = current->pid; |
| 7890 | current->flags |= PF_IO_WORKER; |
| 7891 | ret = io_wq_fork_thread(io_sq_thread, sqd); |
| 7892 | current->flags &= ~PF_IO_WORKER; |
| 7893 | if (ret < 0) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7894 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7895 | goto err; |
| 7896 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7897 | wait_for_completion(&sqd->completion); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7898 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7899 | if (ret) |
| 7900 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7901 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7902 | /* Can't have SQ_AFF without SQPOLL */ |
| 7903 | ret = -EINVAL; |
| 7904 | goto err; |
| 7905 | } |
| 7906 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7907 | return 0; |
| 7908 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7909 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7910 | return ret; |
| 7911 | } |
| 7912 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7913 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 7914 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7915 | struct io_sq_data *sqd = ctx->sq_data; |
| 7916 | |
Jens Axboe | eb85890 | 2021-02-25 10:13:29 -0700 | [diff] [blame] | 7917 | if (ctx->flags & IORING_SETUP_SQPOLL) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7918 | complete(&sqd->startup); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7919 | } |
| 7920 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7921 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7922 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7923 | { |
| 7924 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7925 | } |
| 7926 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7927 | static inline int __io_account_mem(struct user_struct *user, |
| 7928 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7929 | { |
| 7930 | unsigned long page_limit, cur_pages, new_pages; |
| 7931 | |
| 7932 | /* Don't allow more pages than we can safely lock */ |
| 7933 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7934 | |
| 7935 | do { |
| 7936 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7937 | new_pages = cur_pages + nr_pages; |
| 7938 | if (new_pages > page_limit) |
| 7939 | return -ENOMEM; |
| 7940 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7941 | new_pages) != cur_pages); |
| 7942 | |
| 7943 | return 0; |
| 7944 | } |
| 7945 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7946 | 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] | 7947 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7948 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7949 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7950 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7951 | if (ctx->mm_account) |
| 7952 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7953 | } |
| 7954 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7955 | 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] | 7956 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7957 | int ret; |
| 7958 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7959 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7960 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7961 | if (ret) |
| 7962 | return ret; |
| 7963 | } |
| 7964 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7965 | if (ctx->mm_account) |
| 7966 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7967 | |
| 7968 | return 0; |
| 7969 | } |
| 7970 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7971 | static void io_mem_free(void *ptr) |
| 7972 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7973 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7974 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7975 | if (!ptr) |
| 7976 | return; |
| 7977 | |
| 7978 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7979 | if (put_page_testzero(page)) |
| 7980 | free_compound_page(page); |
| 7981 | } |
| 7982 | |
| 7983 | static void *io_mem_alloc(size_t size) |
| 7984 | { |
| 7985 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7986 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7987 | |
| 7988 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7989 | } |
| 7990 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7991 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7992 | size_t *sq_offset) |
| 7993 | { |
| 7994 | struct io_rings *rings; |
| 7995 | size_t off, sq_array_size; |
| 7996 | |
| 7997 | off = struct_size(rings, cqes, cq_entries); |
| 7998 | if (off == SIZE_MAX) |
| 7999 | return SIZE_MAX; |
| 8000 | |
| 8001 | #ifdef CONFIG_SMP |
| 8002 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8003 | if (off == 0) |
| 8004 | return SIZE_MAX; |
| 8005 | #endif |
| 8006 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8007 | if (sq_offset) |
| 8008 | *sq_offset = off; |
| 8009 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8010 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8011 | if (sq_array_size == SIZE_MAX) |
| 8012 | return SIZE_MAX; |
| 8013 | |
| 8014 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8015 | return SIZE_MAX; |
| 8016 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8017 | return off; |
| 8018 | } |
| 8019 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8020 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8021 | { |
| 8022 | int i, j; |
| 8023 | |
| 8024 | if (!ctx->user_bufs) |
| 8025 | return -ENXIO; |
| 8026 | |
| 8027 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8028 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8029 | |
| 8030 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8031 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8032 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8033 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8034 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8035 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8036 | imu->nr_bvecs = 0; |
| 8037 | } |
| 8038 | |
| 8039 | kfree(ctx->user_bufs); |
| 8040 | ctx->user_bufs = NULL; |
| 8041 | ctx->nr_user_bufs = 0; |
| 8042 | return 0; |
| 8043 | } |
| 8044 | |
| 8045 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8046 | void __user *arg, unsigned index) |
| 8047 | { |
| 8048 | struct iovec __user *src; |
| 8049 | |
| 8050 | #ifdef CONFIG_COMPAT |
| 8051 | if (ctx->compat) { |
| 8052 | struct compat_iovec __user *ciovs; |
| 8053 | struct compat_iovec ciov; |
| 8054 | |
| 8055 | ciovs = (struct compat_iovec __user *) arg; |
| 8056 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8057 | return -EFAULT; |
| 8058 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8059 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8060 | dst->iov_len = ciov.iov_len; |
| 8061 | return 0; |
| 8062 | } |
| 8063 | #endif |
| 8064 | src = (struct iovec __user *) arg; |
| 8065 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8066 | return -EFAULT; |
| 8067 | return 0; |
| 8068 | } |
| 8069 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8070 | /* |
| 8071 | * Not super efficient, but this is just a registration time. And we do cache |
| 8072 | * the last compound head, so generally we'll only do a full search if we don't |
| 8073 | * match that one. |
| 8074 | * |
| 8075 | * We check if the given compound head page has already been accounted, to |
| 8076 | * avoid double accounting it. This allows us to account the full size of the |
| 8077 | * page, not just the constituent pages of a huge page. |
| 8078 | */ |
| 8079 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8080 | int nr_pages, struct page *hpage) |
| 8081 | { |
| 8082 | int i, j; |
| 8083 | |
| 8084 | /* check current page array */ |
| 8085 | for (i = 0; i < nr_pages; i++) { |
| 8086 | if (!PageCompound(pages[i])) |
| 8087 | continue; |
| 8088 | if (compound_head(pages[i]) == hpage) |
| 8089 | return true; |
| 8090 | } |
| 8091 | |
| 8092 | /* check previously registered pages */ |
| 8093 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8094 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8095 | |
| 8096 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8097 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8098 | continue; |
| 8099 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8100 | return true; |
| 8101 | } |
| 8102 | } |
| 8103 | |
| 8104 | return false; |
| 8105 | } |
| 8106 | |
| 8107 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8108 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8109 | struct page **last_hpage) |
| 8110 | { |
| 8111 | int i, ret; |
| 8112 | |
| 8113 | for (i = 0; i < nr_pages; i++) { |
| 8114 | if (!PageCompound(pages[i])) { |
| 8115 | imu->acct_pages++; |
| 8116 | } else { |
| 8117 | struct page *hpage; |
| 8118 | |
| 8119 | hpage = compound_head(pages[i]); |
| 8120 | if (hpage == *last_hpage) |
| 8121 | continue; |
| 8122 | *last_hpage = hpage; |
| 8123 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8124 | continue; |
| 8125 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8126 | } |
| 8127 | } |
| 8128 | |
| 8129 | if (!imu->acct_pages) |
| 8130 | return 0; |
| 8131 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8132 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8133 | if (ret) |
| 8134 | imu->acct_pages = 0; |
| 8135 | return ret; |
| 8136 | } |
| 8137 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8138 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8139 | struct io_mapped_ubuf *imu, |
| 8140 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8141 | { |
| 8142 | struct vm_area_struct **vmas = NULL; |
| 8143 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8144 | unsigned long off, start, end, ubuf; |
| 8145 | size_t size; |
| 8146 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8147 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8148 | ubuf = (unsigned long) iov->iov_base; |
| 8149 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8150 | start = ubuf >> PAGE_SHIFT; |
| 8151 | nr_pages = end - start; |
| 8152 | |
| 8153 | ret = -ENOMEM; |
| 8154 | |
| 8155 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8156 | if (!pages) |
| 8157 | goto done; |
| 8158 | |
| 8159 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8160 | GFP_KERNEL); |
| 8161 | if (!vmas) |
| 8162 | goto done; |
| 8163 | |
| 8164 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8165 | GFP_KERNEL); |
| 8166 | if (!imu->bvec) |
| 8167 | goto done; |
| 8168 | |
| 8169 | ret = 0; |
| 8170 | mmap_read_lock(current->mm); |
| 8171 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8172 | pages, vmas); |
| 8173 | if (pret == nr_pages) { |
| 8174 | /* don't support file backed memory */ |
| 8175 | for (i = 0; i < nr_pages; i++) { |
| 8176 | struct vm_area_struct *vma = vmas[i]; |
| 8177 | |
| 8178 | if (vma->vm_file && |
| 8179 | !is_file_hugepages(vma->vm_file)) { |
| 8180 | ret = -EOPNOTSUPP; |
| 8181 | break; |
| 8182 | } |
| 8183 | } |
| 8184 | } else { |
| 8185 | ret = pret < 0 ? pret : -EFAULT; |
| 8186 | } |
| 8187 | mmap_read_unlock(current->mm); |
| 8188 | if (ret) { |
| 8189 | /* |
| 8190 | * if we did partial map, or found file backed vmas, |
| 8191 | * release any pages we did get |
| 8192 | */ |
| 8193 | if (pret > 0) |
| 8194 | unpin_user_pages(pages, pret); |
| 8195 | kvfree(imu->bvec); |
| 8196 | goto done; |
| 8197 | } |
| 8198 | |
| 8199 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8200 | if (ret) { |
| 8201 | unpin_user_pages(pages, pret); |
| 8202 | kvfree(imu->bvec); |
| 8203 | goto done; |
| 8204 | } |
| 8205 | |
| 8206 | off = ubuf & ~PAGE_MASK; |
| 8207 | size = iov->iov_len; |
| 8208 | for (i = 0; i < nr_pages; i++) { |
| 8209 | size_t vec_len; |
| 8210 | |
| 8211 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8212 | imu->bvec[i].bv_page = pages[i]; |
| 8213 | imu->bvec[i].bv_len = vec_len; |
| 8214 | imu->bvec[i].bv_offset = off; |
| 8215 | off = 0; |
| 8216 | size -= vec_len; |
| 8217 | } |
| 8218 | /* store original address for later verification */ |
| 8219 | imu->ubuf = ubuf; |
| 8220 | imu->len = iov->iov_len; |
| 8221 | imu->nr_bvecs = nr_pages; |
| 8222 | ret = 0; |
| 8223 | done: |
| 8224 | kvfree(pages); |
| 8225 | kvfree(vmas); |
| 8226 | return ret; |
| 8227 | } |
| 8228 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8229 | 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] | 8230 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8231 | if (ctx->user_bufs) |
| 8232 | return -EBUSY; |
| 8233 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8234 | return -EINVAL; |
| 8235 | |
| 8236 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8237 | GFP_KERNEL); |
| 8238 | if (!ctx->user_bufs) |
| 8239 | return -ENOMEM; |
| 8240 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8241 | return 0; |
| 8242 | } |
| 8243 | |
| 8244 | static int io_buffer_validate(struct iovec *iov) |
| 8245 | { |
| 8246 | /* |
| 8247 | * Don't impose further limits on the size and buffer |
| 8248 | * constraints here, we'll -EINVAL later when IO is |
| 8249 | * submitted if they are wrong. |
| 8250 | */ |
| 8251 | if (!iov->iov_base || !iov->iov_len) |
| 8252 | return -EFAULT; |
| 8253 | |
| 8254 | /* arbitrary limit, but we need something */ |
| 8255 | if (iov->iov_len > SZ_1G) |
| 8256 | return -EFAULT; |
| 8257 | |
| 8258 | return 0; |
| 8259 | } |
| 8260 | |
| 8261 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8262 | unsigned int nr_args) |
| 8263 | { |
| 8264 | int i, ret; |
| 8265 | struct iovec iov; |
| 8266 | struct page *last_hpage = NULL; |
| 8267 | |
| 8268 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8269 | if (ret) |
| 8270 | return ret; |
| 8271 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8272 | for (i = 0; i < nr_args; i++) { |
| 8273 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8274 | |
| 8275 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8276 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8277 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8278 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8279 | ret = io_buffer_validate(&iov); |
| 8280 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8281 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8282 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8283 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8284 | if (ret) |
| 8285 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8286 | |
| 8287 | ctx->nr_user_bufs++; |
| 8288 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8289 | |
| 8290 | if (ret) |
| 8291 | io_sqe_buffers_unregister(ctx); |
| 8292 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8293 | return ret; |
| 8294 | } |
| 8295 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8296 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8297 | { |
| 8298 | __s32 __user *fds = arg; |
| 8299 | int fd; |
| 8300 | |
| 8301 | if (ctx->cq_ev_fd) |
| 8302 | return -EBUSY; |
| 8303 | |
| 8304 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8305 | return -EFAULT; |
| 8306 | |
| 8307 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8308 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8309 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8310 | ctx->cq_ev_fd = NULL; |
| 8311 | return ret; |
| 8312 | } |
| 8313 | |
| 8314 | return 0; |
| 8315 | } |
| 8316 | |
| 8317 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8318 | { |
| 8319 | if (ctx->cq_ev_fd) { |
| 8320 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8321 | ctx->cq_ev_fd = NULL; |
| 8322 | return 0; |
| 8323 | } |
| 8324 | |
| 8325 | return -ENXIO; |
| 8326 | } |
| 8327 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8328 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8329 | { |
| 8330 | struct io_ring_ctx *ctx = data; |
| 8331 | struct io_buffer *buf = p; |
| 8332 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8333 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8334 | return 0; |
| 8335 | } |
| 8336 | |
| 8337 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8338 | { |
| 8339 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8340 | idr_destroy(&ctx->io_buffer_idr); |
| 8341 | } |
| 8342 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8343 | 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] | 8344 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8345 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8346 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8347 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8348 | if (tsk && req->task != tsk) |
| 8349 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8350 | list_del(&req->compl.list); |
| 8351 | kmem_cache_free(req_cachep, req); |
| 8352 | } |
| 8353 | } |
| 8354 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8355 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8356 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8357 | struct io_submit_state *submit_state = &ctx->submit_state; |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8358 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8359 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8360 | mutex_lock(&ctx->uring_lock); |
| 8361 | |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8362 | if (submit_state->free_reqs) { |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8363 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8364 | submit_state->reqs); |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8365 | submit_state->free_reqs = 0; |
| 8366 | } |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8367 | |
| 8368 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8369 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 8370 | cs->locked_free_nr = 0; |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8371 | spin_unlock_irq(&ctx->completion_lock); |
| 8372 | |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8373 | io_req_cache_free(&cs->free_list, NULL); |
| 8374 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8375 | mutex_unlock(&ctx->uring_lock); |
| 8376 | } |
| 8377 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8378 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8379 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8380 | /* |
| 8381 | * Some may use context even when all refs and requests have been put, |
| 8382 | * and they are free to do so while still holding uring_lock, see |
| 8383 | * __io_req_task_submit(). Wait for them to finish. |
| 8384 | */ |
| 8385 | mutex_lock(&ctx->uring_lock); |
| 8386 | mutex_unlock(&ctx->uring_lock); |
| 8387 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8388 | io_sq_thread_finish(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8389 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8390 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8391 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8392 | mmdrop(ctx->mm_account); |
| 8393 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8394 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8395 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8396 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8397 | io_sqe_files_unregister(ctx); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8398 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8399 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8400 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8401 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8402 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8403 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8404 | if (ctx->ring_sock) { |
| 8405 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8406 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8407 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8408 | #endif |
| 8409 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8410 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8411 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8412 | |
| 8413 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8414 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8415 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8416 | if (ctx->hash_map) |
| 8417 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8418 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8419 | kfree(ctx); |
| 8420 | } |
| 8421 | |
| 8422 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8423 | { |
| 8424 | struct io_ring_ctx *ctx = file->private_data; |
| 8425 | __poll_t mask = 0; |
| 8426 | |
| 8427 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8428 | /* |
| 8429 | * synchronizes with barrier from wq_has_sleeper call in |
| 8430 | * io_commit_cqring |
| 8431 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8432 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8433 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8434 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8435 | |
| 8436 | /* |
| 8437 | * Don't flush cqring overflow list here, just do a simple check. |
| 8438 | * Otherwise there could possible be ABBA deadlock: |
| 8439 | * CPU0 CPU1 |
| 8440 | * ---- ---- |
| 8441 | * lock(&ctx->uring_lock); |
| 8442 | * lock(&ep->mtx); |
| 8443 | * lock(&ctx->uring_lock); |
| 8444 | * lock(&ep->mtx); |
| 8445 | * |
| 8446 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8447 | * pushs them to do the flush. |
| 8448 | */ |
| 8449 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8450 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8451 | |
| 8452 | return mask; |
| 8453 | } |
| 8454 | |
| 8455 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8456 | { |
| 8457 | struct io_ring_ctx *ctx = file->private_data; |
| 8458 | |
| 8459 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8460 | } |
| 8461 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8462 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8463 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8464 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8465 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8466 | creds = idr_remove(&ctx->personality_idr, id); |
| 8467 | if (creds) { |
| 8468 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8469 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8470 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8471 | |
| 8472 | return -EINVAL; |
| 8473 | } |
| 8474 | |
| 8475 | static int io_remove_personalities(int id, void *p, void *data) |
| 8476 | { |
| 8477 | struct io_ring_ctx *ctx = data; |
| 8478 | |
| 8479 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8480 | return 0; |
| 8481 | } |
| 8482 | |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8483 | static bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8484 | { |
| 8485 | struct callback_head *work, *head, *next; |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8486 | bool executed = false; |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8487 | |
| 8488 | do { |
| 8489 | do { |
| 8490 | head = NULL; |
| 8491 | work = READ_ONCE(ctx->exit_task_work); |
| 8492 | } while (cmpxchg(&ctx->exit_task_work, work, head) != work); |
| 8493 | |
| 8494 | if (!work) |
| 8495 | break; |
| 8496 | |
| 8497 | do { |
| 8498 | next = work->next; |
| 8499 | work->func(work); |
| 8500 | work = next; |
| 8501 | cond_resched(); |
| 8502 | } while (work); |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8503 | executed = true; |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8504 | } while (1); |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8505 | |
| 8506 | return executed; |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8507 | } |
| 8508 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8509 | static void io_ring_exit_work(struct work_struct *work) |
| 8510 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8511 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8512 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8513 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8514 | /* |
| 8515 | * If we're doing polled IO and end up having requests being |
| 8516 | * submitted async (out-of-line), then completions can come in while |
| 8517 | * we're waiting for refs to drop. We need to reap these manually, |
| 8518 | * as nobody else will be looking for them. |
| 8519 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8520 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8521 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8522 | io_run_ctx_fallback(ctx); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8523 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8524 | io_ring_ctx_free(ctx); |
| 8525 | } |
| 8526 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8527 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8528 | { |
| 8529 | mutex_lock(&ctx->uring_lock); |
| 8530 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8531 | |
| 8532 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8533 | ctx->sqo_dead = 1; |
| 8534 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8535 | /* if force is set, the ring is going away. always drop after that */ |
| 8536 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8537 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8538 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8539 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8540 | mutex_unlock(&ctx->uring_lock); |
| 8541 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8542 | io_kill_timeouts(ctx, NULL, NULL); |
| 8543 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8544 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8545 | /* 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] | 8546 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8547 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8548 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8549 | /* |
| 8550 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8551 | * if we're exiting a ton of rings at the same time. It just adds |
| 8552 | * noise and overhead, there's no discernable change in runtime |
| 8553 | * over using system_wq. |
| 8554 | */ |
| 8555 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8556 | } |
| 8557 | |
| 8558 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8559 | { |
| 8560 | struct io_ring_ctx *ctx = file->private_data; |
| 8561 | |
| 8562 | file->private_data = NULL; |
| 8563 | io_ring_ctx_wait_and_kill(ctx); |
| 8564 | return 0; |
| 8565 | } |
| 8566 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8567 | struct io_task_cancel { |
| 8568 | struct task_struct *task; |
| 8569 | struct files_struct *files; |
| 8570 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8571 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8572 | 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] | 8573 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8574 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8575 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8576 | bool ret; |
| 8577 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8578 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8579 | unsigned long flags; |
| 8580 | struct io_ring_ctx *ctx = req->ctx; |
| 8581 | |
| 8582 | /* protect against races with linked timeouts */ |
| 8583 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8584 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8585 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8586 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8587 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8588 | } |
| 8589 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8590 | } |
| 8591 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8592 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8593 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8594 | struct files_struct *files) |
| 8595 | { |
| 8596 | struct io_defer_entry *de = NULL; |
| 8597 | LIST_HEAD(list); |
| 8598 | |
| 8599 | spin_lock_irq(&ctx->completion_lock); |
| 8600 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8601 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8602 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8603 | break; |
| 8604 | } |
| 8605 | } |
| 8606 | spin_unlock_irq(&ctx->completion_lock); |
| 8607 | |
| 8608 | while (!list_empty(&list)) { |
| 8609 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8610 | list_del_init(&de->list); |
| 8611 | req_set_fail_links(de->req); |
| 8612 | io_put_req(de->req); |
| 8613 | io_req_complete(de->req, -ECANCELED); |
| 8614 | kfree(de); |
| 8615 | } |
| 8616 | } |
| 8617 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8618 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8619 | struct task_struct *task, |
| 8620 | struct files_struct *files) |
| 8621 | { |
| 8622 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8623 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8624 | |
| 8625 | while (1) { |
| 8626 | enum io_wq_cancel cret; |
| 8627 | bool ret = false; |
| 8628 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8629 | if (tctx && tctx->io_wq) { |
| 8630 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8631 | &cancel, true); |
| 8632 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8633 | } |
| 8634 | |
| 8635 | /* SQPOLL thread does its own polling */ |
| 8636 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8637 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8638 | io_iopoll_try_reap_events(ctx); |
| 8639 | ret = true; |
| 8640 | } |
| 8641 | } |
| 8642 | |
| 8643 | ret |= io_poll_remove_all(ctx, task, files); |
| 8644 | ret |= io_kill_timeouts(ctx, task, files); |
| 8645 | ret |= io_run_task_work(); |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8646 | ret |= io_run_ctx_fallback(ctx); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8647 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8648 | if (!ret) |
| 8649 | break; |
| 8650 | cond_resched(); |
| 8651 | } |
| 8652 | } |
| 8653 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8654 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8655 | struct task_struct *task, |
| 8656 | struct files_struct *files) |
| 8657 | { |
| 8658 | struct io_kiocb *req; |
| 8659 | int cnt = 0; |
| 8660 | |
| 8661 | spin_lock_irq(&ctx->inflight_lock); |
| 8662 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8663 | cnt += io_match_task(req, task, files); |
| 8664 | spin_unlock_irq(&ctx->inflight_lock); |
| 8665 | return cnt; |
| 8666 | } |
| 8667 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8668 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8669 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8670 | struct files_struct *files) |
| 8671 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8672 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8673 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8674 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8675 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8676 | inflight = io_uring_count_inflight(ctx, task, files); |
| 8677 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8678 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8679 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8680 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8681 | |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 8682 | if (ctx->sq_data) |
| 8683 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8684 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 8685 | TASK_UNINTERRUPTIBLE); |
| 8686 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 8687 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8688 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 8689 | if (ctx->sq_data) |
| 8690 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8691 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8692 | } |
| 8693 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8694 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 8695 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8696 | mutex_lock(&ctx->uring_lock); |
| 8697 | ctx->sqo_dead = 1; |
| 8698 | mutex_unlock(&ctx->uring_lock); |
| 8699 | |
| 8700 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 8701 | if (ctx->rings) |
| 8702 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8703 | } |
| 8704 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8705 | /* |
| 8706 | * We need to iteratively cancel requests, in case a request has dependent |
| 8707 | * hard links. These persist even for failure of cancelations, hence keep |
| 8708 | * looping until none are found. |
| 8709 | */ |
| 8710 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8711 | struct files_struct *files) |
| 8712 | { |
| 8713 | struct task_struct *task = current; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8714 | bool did_park = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8715 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8716 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8717 | io_disable_sqo_submit(ctx); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8718 | did_park = io_sq_thread_park(ctx->sq_data); |
| 8719 | if (did_park) { |
| 8720 | task = ctx->sq_data->thread; |
| 8721 | atomic_inc(&task->io_uring->in_idle); |
| 8722 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8723 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8724 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8725 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8726 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 8727 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8728 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8729 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8730 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8731 | if (did_park) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8732 | atomic_dec(&task->io_uring->in_idle); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8733 | io_sq_thread_unpark(ctx->sq_data); |
| 8734 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8735 | } |
| 8736 | |
| 8737 | /* |
| 8738 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8739 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8740 | static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8741 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8742 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8743 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8744 | |
| 8745 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8746 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8747 | if (unlikely(ret)) |
| 8748 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8749 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8750 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8751 | if (tctx->last != file) { |
| 8752 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8753 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8754 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8755 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8756 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 8757 | file, GFP_KERNEL)); |
| 8758 | if (ret) { |
| 8759 | fput(file); |
| 8760 | return ret; |
| 8761 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 8762 | |
| 8763 | /* one and only SQPOLL file note, held by sqo_task */ |
| 8764 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 8765 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8766 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8767 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8768 | } |
| 8769 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8770 | /* |
| 8771 | * This is race safe in that the task itself is doing this, hence it |
| 8772 | * cannot be going through the exit/cancel paths at the same time. |
| 8773 | * This cannot be modified while exit/cancel is running. |
| 8774 | */ |
| 8775 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 8776 | tctx->sqpoll = true; |
| 8777 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8778 | return 0; |
| 8779 | } |
| 8780 | |
| 8781 | /* |
| 8782 | * Remove this io_uring_file -> task mapping. |
| 8783 | */ |
| 8784 | static void io_uring_del_task_file(struct file *file) |
| 8785 | { |
| 8786 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8787 | |
| 8788 | if (tctx->last == file) |
| 8789 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 8790 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8791 | if (file) |
| 8792 | fput(file); |
| 8793 | } |
| 8794 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8795 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8796 | { |
| 8797 | struct file *file; |
| 8798 | unsigned long index; |
| 8799 | |
| 8800 | xa_for_each(&tctx->xa, index, file) |
| 8801 | io_uring_del_task_file(file); |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8802 | if (tctx->io_wq) { |
| 8803 | io_wq_put_and_exit(tctx->io_wq); |
| 8804 | tctx->io_wq = NULL; |
| 8805 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8806 | } |
| 8807 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8808 | void __io_uring_files_cancel(struct files_struct *files) |
| 8809 | { |
| 8810 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 8811 | struct file *file; |
| 8812 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8813 | |
| 8814 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8815 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8816 | xa_for_each(&tctx->xa, index, file) |
| 8817 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8818 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8819 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8820 | if (files) |
| 8821 | io_uring_clean_tctx(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8822 | } |
| 8823 | |
| 8824 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 8825 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8826 | return percpu_counter_sum(&tctx->inflight); |
| 8827 | } |
| 8828 | |
| 8829 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 8830 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8831 | struct io_sq_data *sqd = ctx->sq_data; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8832 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8833 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8834 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8835 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8836 | if (!sqd) |
| 8837 | return; |
| 8838 | io_disable_sqo_submit(ctx); |
| 8839 | if (!io_sq_thread_park(sqd)) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8840 | return; |
| 8841 | tctx = ctx->sq_data->thread->io_uring; |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 8842 | /* can happen on fork/alloc failure, just ignore that state */ |
| 8843 | if (!tctx) { |
| 8844 | io_sq_thread_unpark(sqd); |
| 8845 | return; |
| 8846 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8847 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8848 | atomic_inc(&tctx->in_idle); |
| 8849 | do { |
| 8850 | /* read completions before cancelations */ |
| 8851 | inflight = tctx_inflight(tctx); |
| 8852 | if (!inflight) |
| 8853 | break; |
| 8854 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8855 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8856 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8857 | /* |
| 8858 | * If we've seen completions, retry without waiting. This |
| 8859 | * avoids a race where a completion comes in before we did |
| 8860 | * prepare_to_wait(). |
| 8861 | */ |
| 8862 | if (inflight == tctx_inflight(tctx)) |
| 8863 | schedule(); |
| 8864 | finish_wait(&tctx->wait, &wait); |
| 8865 | } while (1); |
| 8866 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8867 | io_sq_thread_unpark(sqd); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8868 | } |
| 8869 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8870 | /* |
| 8871 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 8872 | * requests. |
| 8873 | */ |
| 8874 | void __io_uring_task_cancel(void) |
| 8875 | { |
| 8876 | struct io_uring_task *tctx = current->io_uring; |
| 8877 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8878 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8879 | |
| 8880 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8881 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8882 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 8883 | /* trigger io_disable_sqo_submit() */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8884 | if (tctx->sqpoll) { |
| 8885 | struct file *file; |
| 8886 | unsigned long index; |
| 8887 | |
| 8888 | xa_for_each(&tctx->xa, index, file) |
| 8889 | io_uring_cancel_sqpoll(file->private_data); |
| 8890 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 8891 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8892 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8893 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8894 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8895 | if (!inflight) |
| 8896 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8897 | __io_uring_files_cancel(NULL); |
| 8898 | |
| 8899 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8900 | |
| 8901 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 8902 | * If we've seen completions, retry without waiting. This |
| 8903 | * avoids a race where a completion comes in before we did |
| 8904 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8905 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 8906 | if (inflight == tctx_inflight(tctx)) |
| 8907 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 8908 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8909 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8910 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8911 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8912 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8913 | io_uring_clean_tctx(tctx); |
| 8914 | /* all current's requests should be gone, we can kill tctx */ |
| 8915 | __io_uring_free(current); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8916 | } |
| 8917 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8918 | static int io_uring_flush(struct file *file, void *data) |
| 8919 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8920 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8921 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8922 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 8923 | /* Ignore helper thread files exit */ |
| 8924 | if (current->flags & PF_IO_WORKER) |
| 8925 | return 0; |
| 8926 | |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 8927 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 8928 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8929 | io_req_caches_free(ctx); |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 8930 | } |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 8931 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8932 | io_run_ctx_fallback(ctx); |
| 8933 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8934 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 8935 | return 0; |
| 8936 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8937 | /* we should have cancelled and erased it before PF_EXITING */ |
| 8938 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 8939 | xa_load(&tctx->xa, (unsigned long)file)); |
| 8940 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 8941 | /* |
| 8942 | * fput() is pending, will be 2 if the only other ref is our potential |
| 8943 | * task file note. If the task is exiting, drop regardless of count. |
| 8944 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8945 | if (atomic_long_read(&file->f_count) != 2) |
| 8946 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 8947 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8948 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 8949 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 8950 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 8951 | xa_load(&tctx->xa, (unsigned long)file)); |
| 8952 | /* sqo_dead check is for when this happens after cancellation */ |
| 8953 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8954 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 8955 | |
| 8956 | io_disable_sqo_submit(ctx); |
| 8957 | } |
| 8958 | |
| 8959 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 8960 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8961 | return 0; |
| 8962 | } |
| 8963 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8964 | static void *io_uring_validate_mmap_request(struct file *file, |
| 8965 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8966 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8967 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8968 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8969 | struct page *page; |
| 8970 | void *ptr; |
| 8971 | |
| 8972 | switch (offset) { |
| 8973 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8974 | case IORING_OFF_CQ_RING: |
| 8975 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8976 | break; |
| 8977 | case IORING_OFF_SQES: |
| 8978 | ptr = ctx->sq_sqes; |
| 8979 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8980 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8981 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8982 | } |
| 8983 | |
| 8984 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 8985 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8986 | return ERR_PTR(-EINVAL); |
| 8987 | |
| 8988 | return ptr; |
| 8989 | } |
| 8990 | |
| 8991 | #ifdef CONFIG_MMU |
| 8992 | |
| 8993 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8994 | { |
| 8995 | size_t sz = vma->vm_end - vma->vm_start; |
| 8996 | unsigned long pfn; |
| 8997 | void *ptr; |
| 8998 | |
| 8999 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9000 | if (IS_ERR(ptr)) |
| 9001 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9002 | |
| 9003 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9004 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9005 | } |
| 9006 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9007 | #else /* !CONFIG_MMU */ |
| 9008 | |
| 9009 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9010 | { |
| 9011 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9012 | } |
| 9013 | |
| 9014 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9015 | { |
| 9016 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9017 | } |
| 9018 | |
| 9019 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9020 | unsigned long addr, unsigned long len, |
| 9021 | unsigned long pgoff, unsigned long flags) |
| 9022 | { |
| 9023 | void *ptr; |
| 9024 | |
| 9025 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9026 | if (IS_ERR(ptr)) |
| 9027 | return PTR_ERR(ptr); |
| 9028 | |
| 9029 | return (unsigned long) ptr; |
| 9030 | } |
| 9031 | |
| 9032 | #endif /* !CONFIG_MMU */ |
| 9033 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9034 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9035 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9036 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9037 | DEFINE_WAIT(wait); |
| 9038 | |
| 9039 | do { |
| 9040 | if (!io_sqring_full(ctx)) |
| 9041 | break; |
| 9042 | |
| 9043 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9044 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9045 | if (unlikely(ctx->sqo_dead)) { |
| 9046 | ret = -EOWNERDEAD; |
| 9047 | goto out; |
| 9048 | } |
| 9049 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9050 | if (!io_sqring_full(ctx)) |
| 9051 | break; |
| 9052 | |
| 9053 | schedule(); |
| 9054 | } while (!signal_pending(current)); |
| 9055 | |
| 9056 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9057 | out: |
| 9058 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9059 | } |
| 9060 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9061 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9062 | struct __kernel_timespec __user **ts, |
| 9063 | const sigset_t __user **sig) |
| 9064 | { |
| 9065 | struct io_uring_getevents_arg arg; |
| 9066 | |
| 9067 | /* |
| 9068 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9069 | * is just a pointer to the sigset_t. |
| 9070 | */ |
| 9071 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9072 | *sig = (const sigset_t __user *) argp; |
| 9073 | *ts = NULL; |
| 9074 | return 0; |
| 9075 | } |
| 9076 | |
| 9077 | /* |
| 9078 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9079 | * timespec and sigset_t pointers if good. |
| 9080 | */ |
| 9081 | if (*argsz != sizeof(arg)) |
| 9082 | return -EINVAL; |
| 9083 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9084 | return -EFAULT; |
| 9085 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9086 | *argsz = arg.sigmask_sz; |
| 9087 | *ts = u64_to_user_ptr(arg.ts); |
| 9088 | return 0; |
| 9089 | } |
| 9090 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9091 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9092 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9093 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9094 | { |
| 9095 | struct io_ring_ctx *ctx; |
| 9096 | long ret = -EBADF; |
| 9097 | int submitted = 0; |
| 9098 | struct fd f; |
| 9099 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9100 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9101 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9102 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9103 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9104 | return -EINVAL; |
| 9105 | |
| 9106 | f = fdget(fd); |
| 9107 | if (!f.file) |
| 9108 | return -EBADF; |
| 9109 | |
| 9110 | ret = -EOPNOTSUPP; |
| 9111 | if (f.file->f_op != &io_uring_fops) |
| 9112 | goto out_fput; |
| 9113 | |
| 9114 | ret = -ENXIO; |
| 9115 | ctx = f.file->private_data; |
| 9116 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9117 | goto out_fput; |
| 9118 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9119 | ret = -EBADFD; |
| 9120 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9121 | goto out; |
| 9122 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9123 | /* |
| 9124 | * For SQ polling, the thread will do all submissions and completions. |
| 9125 | * Just return the requested submit count, and wake the thread if |
| 9126 | * we were asked to. |
| 9127 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9128 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9129 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9130 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9131 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9132 | if (unlikely(ctx->sqo_exec)) { |
| 9133 | ret = io_sq_thread_fork(ctx->sq_data, ctx); |
| 9134 | if (ret) |
| 9135 | goto out; |
| 9136 | ctx->sqo_exec = 0; |
| 9137 | } |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9138 | ret = -EOWNERDEAD; |
| 9139 | if (unlikely(ctx->sqo_dead)) |
| 9140 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9141 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9142 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9143 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9144 | ret = io_sqpoll_wait_sq(ctx); |
| 9145 | if (ret) |
| 9146 | goto out; |
| 9147 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9148 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9149 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9150 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9151 | if (unlikely(ret)) |
| 9152 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9153 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9154 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9155 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9156 | |
| 9157 | if (submitted != to_submit) |
| 9158 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9159 | } |
| 9160 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9161 | const sigset_t __user *sig; |
| 9162 | struct __kernel_timespec __user *ts; |
| 9163 | |
| 9164 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9165 | if (unlikely(ret)) |
| 9166 | goto out; |
| 9167 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9168 | min_complete = min(min_complete, ctx->cq_entries); |
| 9169 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9170 | /* |
| 9171 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9172 | * space applications don't need to do io completion events |
| 9173 | * polling again, they can rely on io_sq_thread to do polling |
| 9174 | * work, which can reduce cpu usage and uring_lock contention. |
| 9175 | */ |
| 9176 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9177 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9178 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9179 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9180 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9181 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9182 | } |
| 9183 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9184 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9185 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9186 | out_fput: |
| 9187 | fdput(f); |
| 9188 | return submitted ? submitted : ret; |
| 9189 | } |
| 9190 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9191 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9192 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9193 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9194 | const struct cred *cred = p; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9195 | struct seq_file *m = data; |
| 9196 | struct user_namespace *uns = seq_user_ns(m); |
| 9197 | struct group_info *gi; |
| 9198 | kernel_cap_t cap; |
| 9199 | unsigned __capi; |
| 9200 | int g; |
| 9201 | |
| 9202 | seq_printf(m, "%5d\n", id); |
| 9203 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9204 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9205 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9206 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9207 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9208 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9209 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9210 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9211 | seq_puts(m, "\n\tGroups:\t"); |
| 9212 | gi = cred->group_info; |
| 9213 | for (g = 0; g < gi->ngroups; g++) { |
| 9214 | seq_put_decimal_ull(m, g ? " " : "", |
| 9215 | from_kgid_munged(uns, gi->gid[g])); |
| 9216 | } |
| 9217 | seq_puts(m, "\n\tCapEff:\t"); |
| 9218 | cap = cred->cap_effective; |
| 9219 | CAP_FOR_EACH_U32(__capi) |
| 9220 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9221 | seq_putc(m, '\n'); |
| 9222 | return 0; |
| 9223 | } |
| 9224 | |
| 9225 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9226 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9227 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9228 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9229 | int i; |
| 9230 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9231 | /* |
| 9232 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9233 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9234 | * cases. If we fail to get the lock, we just don't iterate any |
| 9235 | * structures that could be going away outside the io_uring mutex. |
| 9236 | */ |
| 9237 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9238 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9239 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9240 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9241 | if (!sq->thread) |
| 9242 | sq = NULL; |
| 9243 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9244 | |
| 9245 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9246 | 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] | 9247 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9248 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9249 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9250 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9251 | if (f) |
| 9252 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9253 | else |
| 9254 | seq_printf(m, "%5u: <none>\n", i); |
| 9255 | } |
| 9256 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9257 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9258 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9259 | |
| 9260 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9261 | (unsigned int) buf->len); |
| 9262 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9263 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9264 | seq_printf(m, "Personalities:\n"); |
| 9265 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9266 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9267 | seq_printf(m, "PollList:\n"); |
| 9268 | spin_lock_irq(&ctx->completion_lock); |
| 9269 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9270 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9271 | struct io_kiocb *req; |
| 9272 | |
| 9273 | hlist_for_each_entry(req, list, hash_node) |
| 9274 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9275 | req->task->task_works != NULL); |
| 9276 | } |
| 9277 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9278 | if (has_lock) |
| 9279 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9280 | } |
| 9281 | |
| 9282 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9283 | { |
| 9284 | struct io_ring_ctx *ctx = f->private_data; |
| 9285 | |
| 9286 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9287 | __io_uring_show_fdinfo(ctx, m); |
| 9288 | percpu_ref_put(&ctx->refs); |
| 9289 | } |
| 9290 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9291 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9292 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9293 | static const struct file_operations io_uring_fops = { |
| 9294 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9295 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9296 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9297 | #ifndef CONFIG_MMU |
| 9298 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9299 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9300 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9301 | .poll = io_uring_poll, |
| 9302 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9303 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9304 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9305 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9306 | }; |
| 9307 | |
| 9308 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9309 | struct io_uring_params *p) |
| 9310 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9311 | struct io_rings *rings; |
| 9312 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9313 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9314 | /* make sure these are sane, as we already accounted them */ |
| 9315 | ctx->sq_entries = p->sq_entries; |
| 9316 | ctx->cq_entries = p->cq_entries; |
| 9317 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9318 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9319 | if (size == SIZE_MAX) |
| 9320 | return -EOVERFLOW; |
| 9321 | |
| 9322 | rings = io_mem_alloc(size); |
| 9323 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9324 | return -ENOMEM; |
| 9325 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9326 | ctx->rings = rings; |
| 9327 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9328 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9329 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9330 | rings->sq_ring_entries = p->sq_entries; |
| 9331 | rings->cq_ring_entries = p->cq_entries; |
| 9332 | ctx->sq_mask = rings->sq_ring_mask; |
| 9333 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9334 | |
| 9335 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9336 | if (size == SIZE_MAX) { |
| 9337 | io_mem_free(ctx->rings); |
| 9338 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9339 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9340 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9341 | |
| 9342 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9343 | if (!ctx->sq_sqes) { |
| 9344 | io_mem_free(ctx->rings); |
| 9345 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9346 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9347 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9348 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9349 | return 0; |
| 9350 | } |
| 9351 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9352 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9353 | { |
| 9354 | int ret, fd; |
| 9355 | |
| 9356 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9357 | if (fd < 0) |
| 9358 | return fd; |
| 9359 | |
| 9360 | ret = io_uring_add_task_file(ctx, file); |
| 9361 | if (ret) { |
| 9362 | put_unused_fd(fd); |
| 9363 | return ret; |
| 9364 | } |
| 9365 | fd_install(fd, file); |
| 9366 | return fd; |
| 9367 | } |
| 9368 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9369 | /* |
| 9370 | * Allocate an anonymous fd, this is what constitutes the application |
| 9371 | * visible backing of an io_uring instance. The application mmaps this |
| 9372 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9373 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9374 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9375 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9376 | { |
| 9377 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9378 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9379 | int ret; |
| 9380 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9381 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9382 | &ctx->ring_sock); |
| 9383 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9384 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9385 | #endif |
| 9386 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9387 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9388 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9389 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9390 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9391 | sock_release(ctx->ring_sock); |
| 9392 | ctx->ring_sock = NULL; |
| 9393 | } else { |
| 9394 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9395 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9396 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9397 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9398 | } |
| 9399 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9400 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9401 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9402 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9403 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9404 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9405 | int ret; |
| 9406 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9407 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9408 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9409 | if (entries > IORING_MAX_ENTRIES) { |
| 9410 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9411 | return -EINVAL; |
| 9412 | entries = IORING_MAX_ENTRIES; |
| 9413 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9414 | |
| 9415 | /* |
| 9416 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9417 | * application to drive a higher depth than the size of the SQ ring, |
| 9418 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9419 | * some flexibility in overcommitting a bit. If the application has |
| 9420 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9421 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9422 | */ |
| 9423 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9424 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9425 | /* |
| 9426 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9427 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9428 | * any cq vs sq ring sizing. |
| 9429 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9430 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9431 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9432 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9433 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9434 | return -EINVAL; |
| 9435 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9436 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9437 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9438 | if (p->cq_entries < p->sq_entries) |
| 9439 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9440 | } else { |
| 9441 | p->cq_entries = 2 * p->sq_entries; |
| 9442 | } |
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 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9445 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9446 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9447 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9448 | if (!capable(CAP_IPC_LOCK)) |
| 9449 | ctx->user = get_uid(current_user()); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9450 | ctx->sqo_task = current; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9451 | |
| 9452 | /* |
| 9453 | * This is just grabbed for accounting purposes. When a process exits, |
| 9454 | * the mm is exited and dropped before the files, hence we need to hang |
| 9455 | * on to this mm purely for the purposes of being able to unaccount |
| 9456 | * memory (locked/pinned vm). It's not used for anything else. |
| 9457 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9458 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9459 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9460 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9461 | ret = io_allocate_scq_urings(ctx, p); |
| 9462 | if (ret) |
| 9463 | goto err; |
| 9464 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9465 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9466 | if (ret) |
| 9467 | goto err; |
| 9468 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9469 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9470 | io_sq_offload_start(ctx); |
| 9471 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9472 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9473 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9474 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9475 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9476 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9477 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9478 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9479 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9480 | |
| 9481 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9482 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9483 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9484 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9485 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9486 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9487 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9488 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9489 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9490 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9491 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9492 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9493 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Jens Axboe | 1c0aa1f | 2021-02-20 11:55:28 -0700 | [diff] [blame] | 9494 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9495 | |
| 9496 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9497 | ret = -EFAULT; |
| 9498 | goto err; |
| 9499 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9500 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9501 | file = io_uring_get_file(ctx); |
| 9502 | if (IS_ERR(file)) { |
| 9503 | ret = PTR_ERR(file); |
| 9504 | goto err; |
| 9505 | } |
| 9506 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9507 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9508 | * Install ring fd as the very last thing, so we don't risk someone |
| 9509 | * having closed it before we finish setup |
| 9510 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9511 | ret = io_uring_install_fd(ctx, file); |
| 9512 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9513 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9514 | /* fput will clean it up */ |
| 9515 | fput(file); |
| 9516 | return ret; |
| 9517 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9518 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9519 | 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] | 9520 | return ret; |
| 9521 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9522 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9523 | io_ring_ctx_wait_and_kill(ctx); |
| 9524 | return ret; |
| 9525 | } |
| 9526 | |
| 9527 | /* |
| 9528 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9529 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9530 | * params structure passed in. |
| 9531 | */ |
| 9532 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9533 | { |
| 9534 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9535 | int i; |
| 9536 | |
| 9537 | if (copy_from_user(&p, params, sizeof(p))) |
| 9538 | return -EFAULT; |
| 9539 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9540 | if (p.resv[i]) |
| 9541 | return -EINVAL; |
| 9542 | } |
| 9543 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9544 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9545 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9546 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9547 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9548 | return -EINVAL; |
| 9549 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9550 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9551 | } |
| 9552 | |
| 9553 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9554 | struct io_uring_params __user *, params) |
| 9555 | { |
| 9556 | return io_uring_setup(entries, params); |
| 9557 | } |
| 9558 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9559 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9560 | { |
| 9561 | struct io_uring_probe *p; |
| 9562 | size_t size; |
| 9563 | int i, ret; |
| 9564 | |
| 9565 | size = struct_size(p, ops, nr_args); |
| 9566 | if (size == SIZE_MAX) |
| 9567 | return -EOVERFLOW; |
| 9568 | p = kzalloc(size, GFP_KERNEL); |
| 9569 | if (!p) |
| 9570 | return -ENOMEM; |
| 9571 | |
| 9572 | ret = -EFAULT; |
| 9573 | if (copy_from_user(p, arg, size)) |
| 9574 | goto out; |
| 9575 | ret = -EINVAL; |
| 9576 | if (memchr_inv(p, 0, size)) |
| 9577 | goto out; |
| 9578 | |
| 9579 | p->last_op = IORING_OP_LAST - 1; |
| 9580 | if (nr_args > IORING_OP_LAST) |
| 9581 | nr_args = IORING_OP_LAST; |
| 9582 | |
| 9583 | for (i = 0; i < nr_args; i++) { |
| 9584 | p->ops[i].op = i; |
| 9585 | if (!io_op_defs[i].not_supported) |
| 9586 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9587 | } |
| 9588 | p->ops_len = i; |
| 9589 | |
| 9590 | ret = 0; |
| 9591 | if (copy_to_user(arg, p, size)) |
| 9592 | ret = -EFAULT; |
| 9593 | out: |
| 9594 | kfree(p); |
| 9595 | return ret; |
| 9596 | } |
| 9597 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9598 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9599 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9600 | const struct cred *creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9601 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9602 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9603 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9604 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9605 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 9606 | USHRT_MAX, GFP_KERNEL); |
| 9607 | if (ret < 0) |
| 9608 | put_cred(creds); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9609 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9610 | } |
| 9611 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9612 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9613 | unsigned int nr_args) |
| 9614 | { |
| 9615 | struct io_uring_restriction *res; |
| 9616 | size_t size; |
| 9617 | int i, ret; |
| 9618 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9619 | /* Restrictions allowed only if rings started disabled */ |
| 9620 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9621 | return -EBADFD; |
| 9622 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9623 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9624 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9625 | return -EBUSY; |
| 9626 | |
| 9627 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9628 | return -EINVAL; |
| 9629 | |
| 9630 | size = array_size(nr_args, sizeof(*res)); |
| 9631 | if (size == SIZE_MAX) |
| 9632 | return -EOVERFLOW; |
| 9633 | |
| 9634 | res = memdup_user(arg, size); |
| 9635 | if (IS_ERR(res)) |
| 9636 | return PTR_ERR(res); |
| 9637 | |
| 9638 | ret = 0; |
| 9639 | |
| 9640 | for (i = 0; i < nr_args; i++) { |
| 9641 | switch (res[i].opcode) { |
| 9642 | case IORING_RESTRICTION_REGISTER_OP: |
| 9643 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9644 | ret = -EINVAL; |
| 9645 | goto out; |
| 9646 | } |
| 9647 | |
| 9648 | __set_bit(res[i].register_op, |
| 9649 | ctx->restrictions.register_op); |
| 9650 | break; |
| 9651 | case IORING_RESTRICTION_SQE_OP: |
| 9652 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9653 | ret = -EINVAL; |
| 9654 | goto out; |
| 9655 | } |
| 9656 | |
| 9657 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9658 | break; |
| 9659 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9660 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9661 | break; |
| 9662 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9663 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9664 | break; |
| 9665 | default: |
| 9666 | ret = -EINVAL; |
| 9667 | goto out; |
| 9668 | } |
| 9669 | } |
| 9670 | |
| 9671 | out: |
| 9672 | /* Reset all restrictions if an error happened */ |
| 9673 | if (ret != 0) |
| 9674 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9675 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9676 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9677 | |
| 9678 | kfree(res); |
| 9679 | return ret; |
| 9680 | } |
| 9681 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9682 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9683 | { |
| 9684 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9685 | return -EBADFD; |
| 9686 | |
| 9687 | if (ctx->restrictions.registered) |
| 9688 | ctx->restricted = 1; |
| 9689 | |
| 9690 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9691 | |
| 9692 | io_sq_offload_start(ctx); |
| 9693 | |
| 9694 | return 0; |
| 9695 | } |
| 9696 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9697 | static bool io_register_op_must_quiesce(int op) |
| 9698 | { |
| 9699 | switch (op) { |
| 9700 | case IORING_UNREGISTER_FILES: |
| 9701 | case IORING_REGISTER_FILES_UPDATE: |
| 9702 | case IORING_REGISTER_PROBE: |
| 9703 | case IORING_REGISTER_PERSONALITY: |
| 9704 | case IORING_UNREGISTER_PERSONALITY: |
| 9705 | return false; |
| 9706 | default: |
| 9707 | return true; |
| 9708 | } |
| 9709 | } |
| 9710 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9711 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9712 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9713 | __releases(ctx->uring_lock) |
| 9714 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9715 | { |
| 9716 | int ret; |
| 9717 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9718 | /* |
| 9719 | * We're inside the ring mutex, if the ref is already dying, then |
| 9720 | * someone else killed the ctx or is already going through |
| 9721 | * io_uring_register(). |
| 9722 | */ |
| 9723 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9724 | return -ENXIO; |
| 9725 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9726 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9727 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9728 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9729 | /* |
| 9730 | * Drop uring mutex before waiting for references to exit. If |
| 9731 | * another thread is currently inside io_uring_enter() it might |
| 9732 | * need to grab the uring_lock to make progress. If we hold it |
| 9733 | * here across the drain wait, then we can deadlock. It's safe |
| 9734 | * to drop the mutex here, since no new references will come in |
| 9735 | * after we've killed the percpu ref. |
| 9736 | */ |
| 9737 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9738 | do { |
| 9739 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 9740 | if (!ret) |
| 9741 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 9742 | ret = io_run_task_work_sig(); |
| 9743 | if (ret < 0) |
| 9744 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9745 | } while (1); |
| 9746 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9747 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9748 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9749 | if (ret) { |
| 9750 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9751 | goto out_quiesce; |
| 9752 | } |
| 9753 | } |
| 9754 | |
| 9755 | if (ctx->restricted) { |
| 9756 | if (opcode >= IORING_REGISTER_LAST) { |
| 9757 | ret = -EINVAL; |
| 9758 | goto out; |
| 9759 | } |
| 9760 | |
| 9761 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 9762 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9763 | goto out; |
| 9764 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9765 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9766 | |
| 9767 | switch (opcode) { |
| 9768 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9769 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9770 | break; |
| 9771 | case IORING_UNREGISTER_BUFFERS: |
| 9772 | ret = -EINVAL; |
| 9773 | if (arg || nr_args) |
| 9774 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9775 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9776 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 9777 | case IORING_REGISTER_FILES: |
| 9778 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 9779 | break; |
| 9780 | case IORING_UNREGISTER_FILES: |
| 9781 | ret = -EINVAL; |
| 9782 | if (arg || nr_args) |
| 9783 | break; |
| 9784 | ret = io_sqe_files_unregister(ctx); |
| 9785 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 9786 | case IORING_REGISTER_FILES_UPDATE: |
| 9787 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 9788 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9789 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9790 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9791 | ret = -EINVAL; |
| 9792 | if (nr_args != 1) |
| 9793 | break; |
| 9794 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9795 | if (ret) |
| 9796 | break; |
| 9797 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 9798 | ctx->eventfd_async = 1; |
| 9799 | else |
| 9800 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9801 | break; |
| 9802 | case IORING_UNREGISTER_EVENTFD: |
| 9803 | ret = -EINVAL; |
| 9804 | if (arg || nr_args) |
| 9805 | break; |
| 9806 | ret = io_eventfd_unregister(ctx); |
| 9807 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9808 | case IORING_REGISTER_PROBE: |
| 9809 | ret = -EINVAL; |
| 9810 | if (!arg || nr_args > 256) |
| 9811 | break; |
| 9812 | ret = io_probe(ctx, arg, nr_args); |
| 9813 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9814 | case IORING_REGISTER_PERSONALITY: |
| 9815 | ret = -EINVAL; |
| 9816 | if (arg || nr_args) |
| 9817 | break; |
| 9818 | ret = io_register_personality(ctx); |
| 9819 | break; |
| 9820 | case IORING_UNREGISTER_PERSONALITY: |
| 9821 | ret = -EINVAL; |
| 9822 | if (arg) |
| 9823 | break; |
| 9824 | ret = io_unregister_personality(ctx, nr_args); |
| 9825 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9826 | case IORING_REGISTER_ENABLE_RINGS: |
| 9827 | ret = -EINVAL; |
| 9828 | if (arg || nr_args) |
| 9829 | break; |
| 9830 | ret = io_register_enable_rings(ctx); |
| 9831 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9832 | case IORING_REGISTER_RESTRICTIONS: |
| 9833 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 9834 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9835 | default: |
| 9836 | ret = -EINVAL; |
| 9837 | break; |
| 9838 | } |
| 9839 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9840 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9841 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9842 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9843 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9844 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9845 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9846 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9847 | return ret; |
| 9848 | } |
| 9849 | |
| 9850 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 9851 | void __user *, arg, unsigned int, nr_args) |
| 9852 | { |
| 9853 | struct io_ring_ctx *ctx; |
| 9854 | long ret = -EBADF; |
| 9855 | struct fd f; |
| 9856 | |
| 9857 | f = fdget(fd); |
| 9858 | if (!f.file) |
| 9859 | return -EBADF; |
| 9860 | |
| 9861 | ret = -EOPNOTSUPP; |
| 9862 | if (f.file->f_op != &io_uring_fops) |
| 9863 | goto out_fput; |
| 9864 | |
| 9865 | ctx = f.file->private_data; |
| 9866 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 9867 | io_run_task_work(); |
| 9868 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9869 | mutex_lock(&ctx->uring_lock); |
| 9870 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 9871 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9872 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 9873 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9874 | out_fput: |
| 9875 | fdput(f); |
| 9876 | return ret; |
| 9877 | } |
| 9878 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9879 | static int __init io_uring_init(void) |
| 9880 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9881 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 9882 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 9883 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 9884 | } while (0) |
| 9885 | |
| 9886 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 9887 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 9888 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 9889 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 9890 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 9891 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 9892 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 9893 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 9894 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 9895 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9896 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9897 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 9898 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 9899 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 9900 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 9901 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9902 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 9903 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9904 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 9905 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 9906 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 9907 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 9908 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 9909 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 9910 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 9911 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9912 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9913 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 9914 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 9915 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9916 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9917 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 9918 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 9919 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 9920 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 9921 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9922 | return 0; |
| 9923 | }; |
| 9924 | __initcall(io_uring_init); |