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; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 341 | unsigned int sqo_exec: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 342 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 343 | /* |
| 344 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 345 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 346 | * |
| 347 | * This indirection could e.g. be used to assign fixed |
| 348 | * io_uring_sqe entries to operations and only submit them to |
| 349 | * the queue when needed. |
| 350 | * |
| 351 | * The kernel modifies neither the indices array nor the entries |
| 352 | * array. |
| 353 | */ |
| 354 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 355 | unsigned cached_sq_head; |
| 356 | unsigned sq_entries; |
| 357 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 358 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 359 | unsigned cached_sq_dropped; |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 360 | unsigned cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 361 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 362 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 363 | /* hashed buffered write serialization */ |
| 364 | struct io_wq_hash *hash_map; |
| 365 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 366 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 367 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 368 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 369 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 370 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 371 | } ____cacheline_aligned_in_smp; |
| 372 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 373 | struct { |
| 374 | struct mutex uring_lock; |
| 375 | wait_queue_head_t wait; |
| 376 | } ____cacheline_aligned_in_smp; |
| 377 | |
| 378 | struct io_submit_state submit_state; |
| 379 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 380 | struct io_rings *rings; |
| 381 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 382 | /* |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 383 | * For SQPOLL usage |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 384 | */ |
| 385 | struct task_struct *sqo_task; |
| 386 | |
| 387 | /* Only used for accounting purposes */ |
| 388 | struct mm_struct *mm_account; |
| 389 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 390 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 391 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 392 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 393 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 394 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 395 | /* |
| 396 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 397 | * readers must ensure that ->refs is alive as long as the file* is |
| 398 | * used. Only updated through io_uring_register(2). |
| 399 | */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 400 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 401 | unsigned nr_user_files; |
| 402 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 403 | /* if used, fixed mapped user buffers */ |
| 404 | unsigned nr_user_bufs; |
| 405 | struct io_mapped_ubuf *user_bufs; |
| 406 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 407 | struct user_struct *user; |
| 408 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 409 | struct completion ref_comp; |
| 410 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 411 | |
| 412 | #if defined(CONFIG_UNIX) |
| 413 | struct socket *ring_sock; |
| 414 | #endif |
| 415 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 416 | struct idr io_buffer_idr; |
| 417 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 418 | struct idr personality_idr; |
| 419 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 420 | struct { |
| 421 | unsigned cached_cq_tail; |
| 422 | unsigned cq_entries; |
| 423 | unsigned cq_mask; |
| 424 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 425 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 426 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 427 | struct wait_queue_head cq_wait; |
| 428 | struct fasync_struct *cq_fasync; |
| 429 | struct eventfd_ctx *cq_ev_fd; |
| 430 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 431 | |
| 432 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 433 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 434 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 435 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 436 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 437 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 438 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 439 | * manipulate the list, hence no extra locking is needed there. |
| 440 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 441 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 442 | struct hlist_head *cancel_hash; |
| 443 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 444 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 445 | |
| 446 | spinlock_t inflight_lock; |
| 447 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 448 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 449 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 450 | struct delayed_work rsrc_put_work; |
| 451 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 452 | struct list_head rsrc_ref_list; |
| 453 | spinlock_t rsrc_ref_lock; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 454 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 455 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 456 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 457 | /* exit task_work */ |
| 458 | struct callback_head *exit_task_work; |
| 459 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 460 | struct wait_queue_head hash_wait; |
| 461 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 462 | /* Keep this last, we don't need it for the fast path */ |
| 463 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 464 | }; |
| 465 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 466 | /* |
| 467 | * First field must be the file pointer in all the |
| 468 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 469 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 470 | struct io_poll_iocb { |
| 471 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 472 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 473 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 474 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 475 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 476 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 477 | }; |
| 478 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 479 | struct io_poll_remove { |
| 480 | struct file *file; |
| 481 | u64 addr; |
| 482 | }; |
| 483 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 484 | struct io_close { |
| 485 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 486 | int fd; |
| 487 | }; |
| 488 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 489 | struct io_timeout_data { |
| 490 | struct io_kiocb *req; |
| 491 | struct hrtimer timer; |
| 492 | struct timespec64 ts; |
| 493 | enum hrtimer_mode mode; |
| 494 | }; |
| 495 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 496 | struct io_accept { |
| 497 | struct file *file; |
| 498 | struct sockaddr __user *addr; |
| 499 | int __user *addr_len; |
| 500 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 501 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | struct io_sync { |
| 505 | struct file *file; |
| 506 | loff_t len; |
| 507 | loff_t off; |
| 508 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 509 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 512 | struct io_cancel { |
| 513 | struct file *file; |
| 514 | u64 addr; |
| 515 | }; |
| 516 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 517 | struct io_timeout { |
| 518 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 519 | u32 off; |
| 520 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 521 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 522 | /* head of the link, used by linked timeouts only */ |
| 523 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 524 | }; |
| 525 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 526 | struct io_timeout_rem { |
| 527 | struct file *file; |
| 528 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 529 | |
| 530 | /* timeout update */ |
| 531 | struct timespec64 ts; |
| 532 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 533 | }; |
| 534 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 535 | struct io_rw { |
| 536 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 537 | struct kiocb kiocb; |
| 538 | u64 addr; |
| 539 | u64 len; |
| 540 | }; |
| 541 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 542 | struct io_connect { |
| 543 | struct file *file; |
| 544 | struct sockaddr __user *addr; |
| 545 | int addr_len; |
| 546 | }; |
| 547 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 548 | struct io_sr_msg { |
| 549 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 550 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 551 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 552 | void __user *buf; |
| 553 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 554 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 555 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 556 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 557 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 558 | }; |
| 559 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 560 | struct io_open { |
| 561 | struct file *file; |
| 562 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 563 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 564 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 565 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 566 | }; |
| 567 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 568 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 569 | struct file *file; |
| 570 | u64 arg; |
| 571 | u32 nr_args; |
| 572 | u32 offset; |
| 573 | }; |
| 574 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 575 | struct io_fadvise { |
| 576 | struct file *file; |
| 577 | u64 offset; |
| 578 | u32 len; |
| 579 | u32 advice; |
| 580 | }; |
| 581 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 582 | struct io_madvise { |
| 583 | struct file *file; |
| 584 | u64 addr; |
| 585 | u32 len; |
| 586 | u32 advice; |
| 587 | }; |
| 588 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 589 | struct io_epoll { |
| 590 | struct file *file; |
| 591 | int epfd; |
| 592 | int op; |
| 593 | int fd; |
| 594 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 595 | }; |
| 596 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 597 | struct io_splice { |
| 598 | struct file *file_out; |
| 599 | struct file *file_in; |
| 600 | loff_t off_out; |
| 601 | loff_t off_in; |
| 602 | u64 len; |
| 603 | unsigned int flags; |
| 604 | }; |
| 605 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 606 | struct io_provide_buf { |
| 607 | struct file *file; |
| 608 | __u64 addr; |
| 609 | __s32 len; |
| 610 | __u32 bgid; |
| 611 | __u16 nbufs; |
| 612 | __u16 bid; |
| 613 | }; |
| 614 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 615 | struct io_statx { |
| 616 | struct file *file; |
| 617 | int dfd; |
| 618 | unsigned int mask; |
| 619 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 620 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 621 | struct statx __user *buffer; |
| 622 | }; |
| 623 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 624 | struct io_shutdown { |
| 625 | struct file *file; |
| 626 | int how; |
| 627 | }; |
| 628 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 629 | struct io_rename { |
| 630 | struct file *file; |
| 631 | int old_dfd; |
| 632 | int new_dfd; |
| 633 | struct filename *oldpath; |
| 634 | struct filename *newpath; |
| 635 | int flags; |
| 636 | }; |
| 637 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 638 | struct io_unlink { |
| 639 | struct file *file; |
| 640 | int dfd; |
| 641 | int flags; |
| 642 | struct filename *filename; |
| 643 | }; |
| 644 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 645 | struct io_completion { |
| 646 | struct file *file; |
| 647 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 648 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 649 | }; |
| 650 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 651 | struct io_async_connect { |
| 652 | struct sockaddr_storage address; |
| 653 | }; |
| 654 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 655 | struct io_async_msghdr { |
| 656 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 657 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 658 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 659 | struct sockaddr __user *uaddr; |
| 660 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 661 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 664 | struct io_async_rw { |
| 665 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 666 | const struct iovec *free_iovec; |
| 667 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 668 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 669 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 670 | }; |
| 671 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 672 | enum { |
| 673 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 674 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 675 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 676 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 677 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 678 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 679 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 680 | REQ_F_FAIL_LINK_BIT, |
| 681 | REQ_F_INFLIGHT_BIT, |
| 682 | REQ_F_CUR_POS_BIT, |
| 683 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 684 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 685 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 686 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 687 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 688 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 689 | REQ_F_NO_FILE_TABLE_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 690 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 691 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 692 | |
| 693 | /* not a real bit, just to check we're not overflowing the space */ |
| 694 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 695 | }; |
| 696 | |
| 697 | enum { |
| 698 | /* ctx owns file */ |
| 699 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 700 | /* drain existing IO first */ |
| 701 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 702 | /* linked sqes */ |
| 703 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 704 | /* doesn't sever on completion < 0 */ |
| 705 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 706 | /* IOSQE_ASYNC */ |
| 707 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 708 | /* IOSQE_BUFFER_SELECT */ |
| 709 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 710 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 711 | /* fail rest of links */ |
| 712 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 713 | /* on inflight list */ |
| 714 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 715 | /* read/write uses file position */ |
| 716 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 717 | /* must not punt to workers */ |
| 718 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 719 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 720 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 721 | /* regular file */ |
| 722 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 723 | /* needs cleanup */ |
| 724 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 725 | /* already went through poll handler */ |
| 726 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 727 | /* buffer already selected */ |
| 728 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 729 | /* doesn't need file table for this request */ |
| 730 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 731 | /* linked timeout is active, i.e. prepared by link's head */ |
| 732 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 733 | /* completion is deferred through io_comp_state */ |
| 734 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 735 | }; |
| 736 | |
| 737 | struct async_poll { |
| 738 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 739 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 740 | }; |
| 741 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 742 | struct io_task_work { |
| 743 | struct io_wq_work_node node; |
| 744 | task_work_func_t func; |
| 745 | }; |
| 746 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 747 | /* |
| 748 | * NOTE! Each of the iocb union members has the file pointer |
| 749 | * as the first entry in their struct definition. So you can |
| 750 | * access the file pointer through any of the sub-structs, |
| 751 | * or directly as just 'ki_filp' in this struct. |
| 752 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 753 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 754 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 755 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 756 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 757 | struct io_poll_iocb poll; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 758 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 759 | struct io_accept accept; |
| 760 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 761 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 762 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 763 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 764 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 765 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 766 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 767 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 768 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 769 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 770 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 771 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 772 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 773 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 774 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 775 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 776 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 777 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 778 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 779 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 780 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 781 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 782 | /* opcode allocated if it needs to store data for async defer */ |
| 783 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 784 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 785 | /* polled IO has completed */ |
| 786 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 787 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 788 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 789 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 790 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 791 | struct io_ring_ctx *ctx; |
| 792 | unsigned int flags; |
| 793 | refcount_t refs; |
| 794 | struct task_struct *task; |
| 795 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 796 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 797 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 798 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 799 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 800 | /* |
| 801 | * 1. used with ctx->iopoll_list with reads/writes |
| 802 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 803 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 804 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 805 | union { |
| 806 | struct io_task_work io_task_work; |
| 807 | struct callback_head task_work; |
| 808 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 809 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 810 | struct hlist_node hash_node; |
| 811 | struct async_poll *apoll; |
| 812 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 813 | }; |
| 814 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 815 | struct io_defer_entry { |
| 816 | struct list_head list; |
| 817 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 818 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 819 | }; |
| 820 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 821 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | /* needs req->file assigned */ |
| 823 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 824 | /* hash wq insertion if file is a regular file */ |
| 825 | unsigned hash_reg_file : 1; |
| 826 | /* unbound wq insertion if file is a non-regular file */ |
| 827 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 828 | /* opcode is not supported by this kernel */ |
| 829 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 830 | /* set if opcode supports polled "wait" */ |
| 831 | unsigned pollin : 1; |
| 832 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 833 | /* op supports buffer selection */ |
| 834 | unsigned buffer_select : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 835 | /* must always have async data allocated */ |
| 836 | unsigned needs_async_data : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 837 | /* should block plug */ |
| 838 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 839 | /* size of async data needed, if any */ |
| 840 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 841 | }; |
| 842 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 843 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 844 | [IORING_OP_NOP] = {}, |
| 845 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 846 | .needs_file = 1, |
| 847 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 848 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 849 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 850 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 851 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 852 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 853 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 854 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 855 | .needs_file = 1, |
| 856 | .hash_reg_file = 1, |
| 857 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 858 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 859 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 860 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 861 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 862 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 863 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 864 | .needs_file = 1, |
| 865 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 866 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 867 | .needs_file = 1, |
| 868 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 869 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 870 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 871 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 872 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 873 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 874 | .needs_file = 1, |
| 875 | .hash_reg_file = 1, |
| 876 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 877 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 878 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 879 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 880 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 881 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 882 | .needs_file = 1, |
| 883 | .unbound_nonreg_file = 1, |
| 884 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 885 | [IORING_OP_POLL_REMOVE] = {}, |
| 886 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 887 | .needs_file = 1, |
| 888 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 889 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 890 | .needs_file = 1, |
| 891 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 892 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 893 | .needs_async_data = 1, |
| 894 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 895 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 896 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 897 | .needs_file = 1, |
| 898 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 899 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 900 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 901 | .needs_async_data = 1, |
| 902 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 903 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 904 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 905 | .needs_async_data = 1, |
| 906 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 907 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 908 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 909 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 910 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 911 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 912 | .needs_file = 1, |
| 913 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 914 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 915 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 916 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 917 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 918 | .needs_async_data = 1, |
| 919 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 920 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 921 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 922 | .needs_file = 1, |
| 923 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 924 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 925 | .needs_async_data = 1, |
| 926 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 927 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 928 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 929 | .needs_file = 1, |
| 930 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 931 | [IORING_OP_OPENAT] = {}, |
| 932 | [IORING_OP_CLOSE] = {}, |
| 933 | [IORING_OP_FILES_UPDATE] = {}, |
| 934 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 935 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 936 | .needs_file = 1, |
| 937 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 938 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 939 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 940 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 941 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 942 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 943 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 944 | .needs_file = 1, |
| 945 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 946 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 947 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 948 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 949 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 950 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 951 | .needs_file = 1, |
| 952 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 953 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 954 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 955 | .needs_file = 1, |
| 956 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 957 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 958 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 959 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 960 | .needs_file = 1, |
| 961 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 962 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 963 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 964 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 965 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 966 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 967 | [IORING_OP_EPOLL_CTL] = { |
| 968 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 969 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 970 | [IORING_OP_SPLICE] = { |
| 971 | .needs_file = 1, |
| 972 | .hash_reg_file = 1, |
| 973 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 974 | }, |
| 975 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 976 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 977 | [IORING_OP_TEE] = { |
| 978 | .needs_file = 1, |
| 979 | .hash_reg_file = 1, |
| 980 | .unbound_nonreg_file = 1, |
| 981 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 982 | [IORING_OP_SHUTDOWN] = { |
| 983 | .needs_file = 1, |
| 984 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 985 | [IORING_OP_RENAMEAT] = {}, |
| 986 | [IORING_OP_UNLINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 987 | }; |
| 988 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 989 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 990 | struct task_struct *task, |
| 991 | struct files_struct *files); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 992 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 993 | 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] | 994 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 995 | struct io_ring_ctx *ctx); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 996 | 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] | 997 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 998 | static bool io_rw_reissue(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 999 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1000 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1001 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1002 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1003 | static void io_dismantle_req(struct io_kiocb *req); |
| 1004 | static void io_put_task(struct task_struct *task, int nr); |
| 1005 | static void io_queue_next(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1006 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1007 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1008 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1009 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1010 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1011 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1012 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1013 | static struct file *io_file_get(struct io_submit_state *state, |
| 1014 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1015 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1016 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1017 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 1018 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 1019 | struct iov_iter *iter, bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1020 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1021 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1022 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1023 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1024 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1025 | struct io_ring_ctx *ctx); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1026 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1027 | static struct kmem_cache *req_cachep; |
| 1028 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1029 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1030 | |
| 1031 | struct sock *io_uring_get_socket(struct file *file) |
| 1032 | { |
| 1033 | #if defined(CONFIG_UNIX) |
| 1034 | if (file->f_op == &io_uring_fops) { |
| 1035 | struct io_ring_ctx *ctx = file->private_data; |
| 1036 | |
| 1037 | return ctx->ring_sock->sk; |
| 1038 | } |
| 1039 | #endif |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1043 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1044 | #define io_for_each_link(pos, head) \ |
| 1045 | for (pos = (head); pos; pos = pos->link) |
| 1046 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1047 | static inline void io_clean_op(struct io_kiocb *req) |
| 1048 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1049 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1050 | __io_clean_op(req); |
| 1051 | } |
| 1052 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1053 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1054 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1055 | struct io_ring_ctx *ctx = req->ctx; |
| 1056 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1057 | if (!req->fixed_rsrc_refs) { |
| 1058 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; |
| 1059 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1063 | static bool io_match_task(struct io_kiocb *head, |
| 1064 | struct task_struct *task, |
| 1065 | struct files_struct *files) |
| 1066 | { |
| 1067 | struct io_kiocb *req; |
| 1068 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1069 | if (task && head->task != task) { |
| 1070 | /* in terms of cancelation, always match if req task is dead */ |
| 1071 | if (head->task->flags & PF_EXITING) |
| 1072 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1073 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1074 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1075 | if (!files) |
| 1076 | return true; |
| 1077 | |
| 1078 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1079 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1080 | return true; |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 1081 | if (req->task->files == files) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1082 | return true; |
| 1083 | } |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1087 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1088 | { |
| 1089 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1090 | req->flags |= REQ_F_FAIL_LINK; |
| 1091 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1092 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1093 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1094 | { |
| 1095 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1096 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1097 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1100 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1101 | { |
| 1102 | return !req->timeout.off; |
| 1103 | } |
| 1104 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1105 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1106 | { |
| 1107 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1108 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1109 | |
| 1110 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1111 | if (!ctx) |
| 1112 | return NULL; |
| 1113 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1114 | /* |
| 1115 | * Use 5 bits less than the max cq entries, that should give us around |
| 1116 | * 32 entries per hash list if totally full and uniformly spread. |
| 1117 | */ |
| 1118 | hash_bits = ilog2(p->cq_entries); |
| 1119 | hash_bits -= 5; |
| 1120 | if (hash_bits <= 0) |
| 1121 | hash_bits = 1; |
| 1122 | ctx->cancel_hash_bits = hash_bits; |
| 1123 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1124 | GFP_KERNEL); |
| 1125 | if (!ctx->cancel_hash) |
| 1126 | goto err; |
| 1127 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1128 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1129 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1130 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1131 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1132 | |
| 1133 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1134 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1135 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1136 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1137 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1138 | init_completion(&ctx->ref_comp); |
| 1139 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1140 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1141 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1142 | mutex_init(&ctx->uring_lock); |
| 1143 | init_waitqueue_head(&ctx->wait); |
| 1144 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1145 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1146 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1147 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1148 | spin_lock_init(&ctx->inflight_lock); |
| 1149 | INIT_LIST_HEAD(&ctx->inflight_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1150 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1151 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1152 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1153 | init_llist_head(&ctx->rsrc_put_llist); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1154 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1155 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1156 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1157 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1158 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1159 | kfree(ctx); |
| 1160 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1163 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1164 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1165 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1166 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1167 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1168 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1169 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1170 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1171 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1172 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1173 | } |
| 1174 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1175 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1176 | { |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1177 | if (req->flags & REQ_F_INFLIGHT) { |
| 1178 | struct io_ring_ctx *ctx = req->ctx; |
| 1179 | struct io_uring_task *tctx = req->task->io_uring; |
| 1180 | unsigned long flags; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1181 | |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1182 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1183 | list_del(&req->inflight_entry); |
| 1184 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1185 | req->flags &= ~REQ_F_INFLIGHT; |
| 1186 | if (atomic_read(&tctx->in_idle)) |
| 1187 | wake_up(&tctx->wait); |
| 1188 | } |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1189 | } |
| 1190 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1191 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1192 | { |
| 1193 | struct io_ring_ctx *ctx = req->ctx; |
| 1194 | |
| 1195 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1196 | req->flags |= REQ_F_INFLIGHT; |
| 1197 | |
| 1198 | spin_lock_irq(&ctx->inflight_lock); |
| 1199 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1200 | spin_unlock_irq(&ctx->inflight_lock); |
| 1201 | } |
| 1202 | } |
| 1203 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1204 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1205 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1206 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1207 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1208 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1209 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1210 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1211 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1212 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1213 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1214 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1215 | } else { |
| 1216 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1217 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1218 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1219 | } |
| 1220 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1221 | static void io_prep_async_link(struct io_kiocb *req) |
| 1222 | { |
| 1223 | struct io_kiocb *cur; |
| 1224 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1225 | io_for_each_link(cur, req) |
| 1226 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1227 | } |
| 1228 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1229 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1230 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1231 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1232 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1233 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1234 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1235 | BUG_ON(!tctx); |
| 1236 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1237 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1238 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1239 | &req->work, req->flags); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1240 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1241 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1242 | } |
| 1243 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1244 | static void io_queue_async_work(struct io_kiocb *req) |
| 1245 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1246 | struct io_kiocb *link; |
| 1247 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1248 | /* init ->work of the whole link before punting */ |
| 1249 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1250 | link = __io_queue_async_work(req); |
| 1251 | |
| 1252 | if (link) |
| 1253 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1254 | } |
| 1255 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1256 | static void io_kill_timeout(struct io_kiocb *req) |
| 1257 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1258 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1259 | int ret; |
| 1260 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1261 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1262 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1263 | atomic_set(&req->ctx->cq_timeouts, |
| 1264 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1265 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1266 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1267 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1271 | /* |
| 1272 | * Returns true if we found and killed one or more timeouts |
| 1273 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1274 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1275 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1276 | { |
| 1277 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1278 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1279 | |
| 1280 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1281 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1282 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1283 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1284 | canceled++; |
| 1285 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1286 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1287 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1288 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1289 | } |
| 1290 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1291 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1292 | { |
| 1293 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1294 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1295 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1296 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1297 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1298 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1299 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1300 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1301 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1302 | } while (!list_empty(&ctx->defer_list)); |
| 1303 | } |
| 1304 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1305 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1306 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1307 | u32 seq; |
| 1308 | |
| 1309 | if (list_empty(&ctx->timeout_list)) |
| 1310 | return; |
| 1311 | |
| 1312 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1313 | |
| 1314 | do { |
| 1315 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1316 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1317 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1318 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1319 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1320 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * Since seq can easily wrap around over time, subtract |
| 1324 | * the last seq at which timeouts were flushed before comparing. |
| 1325 | * Assuming not more than 2^31-1 events have happened since, |
| 1326 | * these subtractions won't have wrapped, so we can check if |
| 1327 | * target is in [last_seq, current_seq] by comparing the two. |
| 1328 | */ |
| 1329 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1330 | events_got = seq - ctx->cq_last_tm_flush; |
| 1331 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1332 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1333 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1334 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1335 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1336 | } while (!list_empty(&ctx->timeout_list)); |
| 1337 | |
| 1338 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1339 | } |
| 1340 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1341 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1342 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1343 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* order cqe stores with ring update */ |
| 1346 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1347 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1348 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1349 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1350 | } |
| 1351 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1352 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1353 | { |
| 1354 | struct io_rings *r = ctx->rings; |
| 1355 | |
| 1356 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1357 | } |
| 1358 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1359 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1360 | { |
| 1361 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1362 | } |
| 1363 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1364 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1365 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1366 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1367 | unsigned tail; |
| 1368 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1369 | /* |
| 1370 | * writes to the cq entry need to come after reading head; the |
| 1371 | * control dependency is enough as we're using WRITE_ONCE to |
| 1372 | * fill the cq entry |
| 1373 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1374 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1375 | return NULL; |
| 1376 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1377 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1378 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1381 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1382 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1383 | if (!ctx->cq_ev_fd) |
| 1384 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1385 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1386 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1387 | if (!ctx->eventfd_async) |
| 1388 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1389 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1392 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1393 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1394 | /* see waitqueue_active() comment */ |
| 1395 | smp_mb(); |
| 1396 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1397 | if (waitqueue_active(&ctx->wait)) |
| 1398 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1399 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1400 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1401 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1402 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1403 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1404 | wake_up_interruptible(&ctx->cq_wait); |
| 1405 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1406 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1407 | } |
| 1408 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1409 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1410 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1411 | /* see waitqueue_active() comment */ |
| 1412 | smp_mb(); |
| 1413 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1414 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1415 | if (waitqueue_active(&ctx->wait)) |
| 1416 | wake_up(&ctx->wait); |
| 1417 | } |
| 1418 | if (io_should_trigger_evfd(ctx)) |
| 1419 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1420 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1421 | wake_up_interruptible(&ctx->cq_wait); |
| 1422 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1423 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1426 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1427 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1428 | struct task_struct *tsk, |
| 1429 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1430 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1431 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1432 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1433 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1434 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1435 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1436 | LIST_HEAD(list); |
| 1437 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1438 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1439 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1440 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1441 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1442 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1443 | 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] | 1444 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1445 | continue; |
| 1446 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1447 | cqe = io_get_cqring(ctx); |
| 1448 | if (!cqe && !force) |
| 1449 | break; |
| 1450 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1451 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1452 | if (cqe) { |
| 1453 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1454 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1455 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1456 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1457 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1458 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1459 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1460 | } |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1461 | posted = true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1464 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1465 | if (all_flushed) { |
| 1466 | clear_bit(0, &ctx->sq_check_overflow); |
| 1467 | clear_bit(0, &ctx->cq_check_overflow); |
| 1468 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1469 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1470 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1471 | if (posted) |
| 1472 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1473 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1474 | if (posted) |
| 1475 | io_cqring_ev_posted(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1476 | |
| 1477 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1478 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1479 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1480 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1481 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1482 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1483 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1486 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1487 | struct task_struct *tsk, |
| 1488 | struct files_struct *files) |
| 1489 | { |
| 1490 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1491 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1492 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1493 | mutex_lock(&ctx->uring_lock); |
| 1494 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1495 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1496 | mutex_unlock(&ctx->uring_lock); |
| 1497 | } |
| 1498 | } |
| 1499 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1500 | 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] | 1501 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1502 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1503 | struct io_uring_cqe *cqe; |
| 1504 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1505 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1506 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1507 | /* |
| 1508 | * If we can't get a cq entry, userspace overflowed the |
| 1509 | * submission (by quite a lot). Increment the overflow count in |
| 1510 | * the ring. |
| 1511 | */ |
| 1512 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1513 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1514 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1515 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1516 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1517 | } else if (ctx->cq_overflow_flushed || |
| 1518 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1519 | /* |
| 1520 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1521 | * then we cannot store the request for later flushing, we need |
| 1522 | * to drop it on the floor. |
| 1523 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1524 | ctx->cached_cq_overflow++; |
| 1525 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1526 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1527 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1528 | set_bit(0, &ctx->sq_check_overflow); |
| 1529 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1530 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1531 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1532 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1533 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1534 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1535 | refcount_inc(&req->refs); |
| 1536 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1540 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1541 | { |
| 1542 | __io_cqring_fill_event(req, res, 0); |
| 1543 | } |
| 1544 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1545 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
| 1546 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1547 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1548 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1549 | unsigned long flags; |
| 1550 | |
| 1551 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1552 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1553 | io_commit_cqring(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1554 | /* |
| 1555 | * If we're the last reference to this request, add to our locked |
| 1556 | * free_list cache. |
| 1557 | */ |
| 1558 | if (refcount_dec_and_test(&req->refs)) { |
| 1559 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1560 | |
| 1561 | io_dismantle_req(req); |
| 1562 | io_put_task(req->task, 1); |
| 1563 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1564 | cs->locked_free_nr++; |
| 1565 | } else |
| 1566 | req = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1567 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1568 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1569 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1570 | if (req) { |
| 1571 | io_queue_next(req); |
| 1572 | percpu_ref_put(&ctx->refs); |
| 1573 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1576 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1577 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1578 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1579 | io_clean_op(req); |
| 1580 | req->result = res; |
| 1581 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1582 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1583 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1584 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1585 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1586 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1587 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1588 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1589 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1590 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1591 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1592 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1593 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1594 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1595 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1596 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1599 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1600 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1601 | struct io_submit_state *state = &ctx->submit_state; |
| 1602 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1603 | struct io_kiocb *req = NULL; |
| 1604 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1605 | /* |
| 1606 | * If we have more than a batch's worth of requests in our IRQ side |
| 1607 | * locked cache, grab the lock and move them over to our submission |
| 1608 | * side cache. |
| 1609 | */ |
| 1610 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { |
| 1611 | spin_lock_irq(&ctx->completion_lock); |
| 1612 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1613 | cs->locked_free_nr = 0; |
| 1614 | spin_unlock_irq(&ctx->completion_lock); |
| 1615 | } |
| 1616 | |
| 1617 | while (!list_empty(&cs->free_list)) { |
| 1618 | req = list_first_entry(&cs->free_list, struct io_kiocb, |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1619 | compl.list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1620 | list_del(&req->compl.list); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1621 | state->reqs[state->free_reqs++] = req; |
| 1622 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) |
| 1623 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1624 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1625 | |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1626 | return req != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1629 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1630 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1631 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1632 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1633 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1634 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1635 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1636 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1637 | int ret; |
| 1638 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1639 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1640 | goto got_req; |
| 1641 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1642 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1643 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1644 | |
| 1645 | /* |
| 1646 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1647 | * retry single alloc to be on the safe side. |
| 1648 | */ |
| 1649 | if (unlikely(ret <= 0)) { |
| 1650 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1651 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1652 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1653 | ret = 1; |
| 1654 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1655 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1656 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1657 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1658 | state->free_reqs--; |
| 1659 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1662 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1663 | bool fixed) |
| 1664 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1665 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1666 | fput(file); |
| 1667 | } |
| 1668 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1669 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1670 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1671 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1672 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1673 | if (req->async_data) |
| 1674 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1675 | if (req->file) |
| 1676 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1677 | if (req->fixed_rsrc_refs) |
| 1678 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1679 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1680 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1681 | |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1682 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1683 | { |
| 1684 | struct io_uring_task *tctx = task->io_uring; |
| 1685 | |
| 1686 | percpu_counter_sub(&tctx->inflight, nr); |
| 1687 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1688 | wake_up(&tctx->wait); |
| 1689 | put_task_struct_many(task, nr); |
| 1690 | } |
| 1691 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1692 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1693 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1694 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1695 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1696 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1697 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1698 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1699 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1700 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1701 | } |
| 1702 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1703 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1704 | { |
| 1705 | struct io_kiocb *nxt = req->link; |
| 1706 | |
| 1707 | req->link = nxt->link; |
| 1708 | nxt->link = NULL; |
| 1709 | } |
| 1710 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1711 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1712 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1713 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1714 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1715 | bool cancelled = false; |
| 1716 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1717 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1718 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1719 | link = req->link; |
| 1720 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 1721 | /* |
| 1722 | * Can happen if a linked timeout fired and link had been like |
| 1723 | * req -> link t-out -> link t-out [-> ...] |
| 1724 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1725 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 1726 | struct io_timeout_data *io = link->async_data; |
| 1727 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1728 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1729 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 1730 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1731 | ret = hrtimer_try_to_cancel(&io->timer); |
| 1732 | if (ret != -1) { |
| 1733 | io_cqring_fill_event(link, -ECANCELED); |
| 1734 | io_commit_cqring(ctx); |
| 1735 | cancelled = true; |
| 1736 | } |
| 1737 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1738 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1739 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1740 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1741 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1742 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1743 | io_put_req(link); |
| 1744 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1745 | } |
| 1746 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1747 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1748 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1749 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1750 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1751 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1752 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1753 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1754 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1755 | link = req->link; |
| 1756 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1757 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1758 | while (link) { |
| 1759 | nxt = link->link; |
| 1760 | link->link = NULL; |
| 1761 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1762 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1763 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1764 | |
Jens Axboe | 1575f21 | 2021-02-27 15:20:49 -0700 | [diff] [blame] | 1765 | io_put_req_deferred(link, 2); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1766 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1767 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1768 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1769 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1770 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1771 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1772 | } |
| 1773 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1774 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1775 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1776 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 1777 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1778 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1779 | /* |
| 1780 | * If LINK is set, we have dependent requests in this chain. If we |
| 1781 | * didn't fail this request, queue the first one up, moving any other |
| 1782 | * dependencies to the next request. In case of failure, fail the rest |
| 1783 | * of the chain. |
| 1784 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1785 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 1786 | struct io_kiocb *nxt = req->link; |
| 1787 | |
| 1788 | req->link = NULL; |
| 1789 | return nxt; |
| 1790 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1791 | io_fail_links(req); |
| 1792 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1793 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1794 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1795 | 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] | 1796 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 1797 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1798 | return NULL; |
| 1799 | return __io_req_find_next(req); |
| 1800 | } |
| 1801 | |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1802 | static void ctx_flush_and_put(struct io_ring_ctx *ctx) |
| 1803 | { |
| 1804 | if (!ctx) |
| 1805 | return; |
| 1806 | if (ctx->submit_state.comp.nr) { |
| 1807 | mutex_lock(&ctx->uring_lock); |
| 1808 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1809 | mutex_unlock(&ctx->uring_lock); |
| 1810 | } |
| 1811 | percpu_ref_put(&ctx->refs); |
| 1812 | } |
| 1813 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1814 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 1815 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1816 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1817 | struct io_wq_work_list list; |
| 1818 | struct io_wq_work_node *node; |
| 1819 | |
| 1820 | if (wq_list_empty(&tctx->task_list)) |
| 1821 | return false; |
| 1822 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1823 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1824 | list = tctx->task_list; |
| 1825 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1826 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1827 | |
| 1828 | node = list.first; |
| 1829 | while (node) { |
| 1830 | struct io_wq_work_node *next = node->next; |
| 1831 | struct io_kiocb *req; |
| 1832 | |
| 1833 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1834 | if (req->ctx != ctx) { |
| 1835 | ctx_flush_and_put(ctx); |
| 1836 | ctx = req->ctx; |
| 1837 | percpu_ref_get(&ctx->refs); |
| 1838 | } |
| 1839 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1840 | req->task_work.func(&req->task_work); |
| 1841 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 1844 | ctx_flush_and_put(ctx); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1845 | return list.first != NULL; |
| 1846 | } |
| 1847 | |
| 1848 | static void tctx_task_work(struct callback_head *cb) |
| 1849 | { |
| 1850 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 1851 | |
Jens Axboe | 1d5f360 | 2021-02-26 14:54:16 -0700 | [diff] [blame] | 1852 | clear_bit(0, &tctx->task_state); |
| 1853 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1854 | while (__tctx_task_work(tctx)) |
| 1855 | cond_resched(); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, |
| 1859 | enum task_work_notify_mode notify) |
| 1860 | { |
| 1861 | struct io_uring_task *tctx = tsk->io_uring; |
| 1862 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1863 | unsigned long flags; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1864 | int ret; |
| 1865 | |
| 1866 | WARN_ON_ONCE(!tctx); |
| 1867 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1868 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1869 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1870 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1871 | |
| 1872 | /* task_work already pending, we're done */ |
| 1873 | if (test_bit(0, &tctx->task_state) || |
| 1874 | test_and_set_bit(0, &tctx->task_state)) |
| 1875 | return 0; |
| 1876 | |
| 1877 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 1878 | return 0; |
| 1879 | |
| 1880 | /* |
| 1881 | * Slow path - we failed, find and delete work. if the work is not |
| 1882 | * in the list, it got run and we're fine. |
| 1883 | */ |
| 1884 | ret = 0; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1885 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1886 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 1887 | if (&req->io_task_work.node == node) { |
| 1888 | wq_list_del(&tctx->task_list, node, prev); |
| 1889 | ret = 1; |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1893 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1894 | clear_bit(0, &tctx->task_state); |
| 1895 | return ret; |
| 1896 | } |
| 1897 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1898 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1899 | { |
| 1900 | struct task_struct *tsk = req->task; |
| 1901 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 1902 | enum task_work_notify_mode notify; |
| 1903 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1904 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 1905 | if (tsk->flags & PF_EXITING) |
| 1906 | return -ESRCH; |
| 1907 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1908 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1909 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1910 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1911 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1912 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1913 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 1914 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1915 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1916 | notify = TWA_SIGNAL; |
| 1917 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1918 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1919 | if (!ret) |
| 1920 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1921 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1922 | return ret; |
| 1923 | } |
| 1924 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1925 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1926 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1927 | { |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 1928 | struct io_ring_ctx *ctx = req->ctx; |
| 1929 | struct callback_head *head; |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1930 | |
| 1931 | init_task_work(&req->task_work, cb); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 1932 | do { |
| 1933 | head = READ_ONCE(ctx->exit_task_work); |
| 1934 | req->task_work.next = head; |
| 1935 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1938 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 1939 | { |
| 1940 | struct io_ring_ctx *ctx = req->ctx; |
| 1941 | |
| 1942 | spin_lock_irq(&ctx->completion_lock); |
| 1943 | io_cqring_fill_event(req, error); |
| 1944 | io_commit_cqring(ctx); |
| 1945 | spin_unlock_irq(&ctx->completion_lock); |
| 1946 | |
| 1947 | io_cqring_ev_posted(ctx); |
| 1948 | req_set_fail_links(req); |
| 1949 | io_double_put_req(req); |
| 1950 | } |
| 1951 | |
| 1952 | static void io_req_task_cancel(struct callback_head *cb) |
| 1953 | { |
| 1954 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 1955 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1956 | |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 1957 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 1958 | __io_req_task_cancel(req, req->result); |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 1959 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 1960 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | static void __io_req_task_submit(struct io_kiocb *req) |
| 1964 | { |
| 1965 | struct io_ring_ctx *ctx = req->ctx; |
| 1966 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 1967 | /* 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] | 1968 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 70aacfe | 2021-03-01 13:02:15 +0000 | [diff] [blame^] | 1969 | if (!(current->flags & PF_EXITING) && !current->in_execve) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1970 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 1971 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1972 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 1973 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1974 | } |
| 1975 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1976 | static void io_req_task_submit(struct callback_head *cb) |
| 1977 | { |
| 1978 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 1979 | |
| 1980 | __io_req_task_submit(req); |
| 1981 | } |
| 1982 | |
| 1983 | static void io_req_task_queue(struct io_kiocb *req) |
| 1984 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1985 | int ret; |
| 1986 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1987 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1988 | ret = io_req_task_work_add(req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1989 | if (unlikely(ret)) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 1990 | req->result = -ECANCELED; |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 1991 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1992 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1993 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1994 | } |
| 1995 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 1996 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 1997 | { |
| 1998 | percpu_ref_get(&req->ctx->refs); |
| 1999 | req->result = ret; |
| 2000 | req->task_work.func = io_req_task_cancel; |
| 2001 | |
| 2002 | if (unlikely(io_req_task_work_add(req))) |
| 2003 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
| 2004 | } |
| 2005 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2006 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2007 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2008 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2009 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2010 | if (nxt) |
| 2011 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2012 | } |
| 2013 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2014 | static void io_free_req(struct io_kiocb *req) |
| 2015 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2016 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2017 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2018 | } |
| 2019 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2020 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2021 | struct task_struct *task; |
| 2022 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2023 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2024 | }; |
| 2025 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2026 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2027 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2028 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2029 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2030 | rb->task = NULL; |
| 2031 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2032 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2033 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2034 | struct req_batch *rb) |
| 2035 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2036 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2037 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2038 | if (rb->ctx_refs) |
| 2039 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2040 | } |
| 2041 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2042 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2043 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2044 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2045 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2046 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2047 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2048 | if (rb->task) |
| 2049 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2050 | rb->task = req->task; |
| 2051 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2052 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2053 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2054 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2055 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2056 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2057 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2058 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2059 | else |
| 2060 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2061 | } |
| 2062 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2063 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2064 | struct io_ring_ctx *ctx) |
| 2065 | { |
| 2066 | int i, nr = cs->nr; |
| 2067 | struct io_kiocb *req; |
| 2068 | struct req_batch rb; |
| 2069 | |
| 2070 | io_init_req_batch(&rb); |
| 2071 | spin_lock_irq(&ctx->completion_lock); |
| 2072 | for (i = 0; i < nr; i++) { |
| 2073 | req = cs->reqs[i]; |
| 2074 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2075 | } |
| 2076 | io_commit_cqring(ctx); |
| 2077 | spin_unlock_irq(&ctx->completion_lock); |
| 2078 | |
| 2079 | io_cqring_ev_posted(ctx); |
| 2080 | for (i = 0; i < nr; i++) { |
| 2081 | req = cs->reqs[i]; |
| 2082 | |
| 2083 | /* submission and completion refs */ |
| 2084 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2085 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | io_req_free_batch_finish(ctx, &rb); |
| 2089 | cs->nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2090 | } |
| 2091 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2092 | /* |
| 2093 | * Drop reference to request, return next in chain (if there is one) if this |
| 2094 | * was the last reference to this request. |
| 2095 | */ |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2096 | 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] | 2097 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2098 | struct io_kiocb *nxt = NULL; |
| 2099 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2100 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2101 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2102 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2103 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2104 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2107 | static void io_put_req(struct io_kiocb *req) |
| 2108 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2109 | if (refcount_dec_and_test(&req->refs)) |
| 2110 | io_free_req(req); |
| 2111 | } |
| 2112 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2113 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2114 | { |
| 2115 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2116 | |
| 2117 | io_free_req(req); |
| 2118 | } |
| 2119 | |
| 2120 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2121 | { |
| 2122 | int ret; |
| 2123 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2124 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2125 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2126 | if (unlikely(ret)) |
| 2127 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2131 | { |
| 2132 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2133 | io_free_req_deferred(req); |
| 2134 | } |
| 2135 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2136 | static void io_double_put_req(struct io_kiocb *req) |
| 2137 | { |
| 2138 | /* drop both submit and complete references */ |
| 2139 | if (refcount_sub_and_test(2, &req->refs)) |
| 2140 | io_free_req(req); |
| 2141 | } |
| 2142 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2143 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2144 | { |
| 2145 | /* See comment at the top of this file */ |
| 2146 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2147 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2148 | } |
| 2149 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2150 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2151 | { |
| 2152 | struct io_rings *rings = ctx->rings; |
| 2153 | |
| 2154 | /* make sure SQ entry isn't read before tail */ |
| 2155 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2156 | } |
| 2157 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2158 | 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] | 2159 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2160 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2161 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2162 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2163 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2164 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2165 | kfree(kbuf); |
| 2166 | return cflags; |
| 2167 | } |
| 2168 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2169 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2170 | { |
| 2171 | struct io_buffer *kbuf; |
| 2172 | |
| 2173 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2174 | return io_put_kbuf(req, kbuf); |
| 2175 | } |
| 2176 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2177 | static inline bool io_run_task_work(void) |
| 2178 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2179 | /* |
| 2180 | * Not safe to run on exiting task, and the task_work handling will |
| 2181 | * not add work to such a task. |
| 2182 | */ |
| 2183 | if (unlikely(current->flags & PF_EXITING)) |
| 2184 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2185 | if (current->task_works) { |
| 2186 | __set_current_state(TASK_RUNNING); |
| 2187 | task_work_run(); |
| 2188 | return true; |
| 2189 | } |
| 2190 | |
| 2191 | return false; |
| 2192 | } |
| 2193 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2194 | /* |
| 2195 | * Find and free completed poll iocbs |
| 2196 | */ |
| 2197 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2198 | struct list_head *done) |
| 2199 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2200 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2201 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2202 | |
| 2203 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2204 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2205 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2206 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2207 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2208 | int cflags = 0; |
| 2209 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2210 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2211 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2212 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2213 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2214 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2215 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2216 | continue; |
| 2217 | } |
| 2218 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2219 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2220 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2221 | |
| 2222 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2223 | (*nr_events)++; |
| 2224 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2225 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2226 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2227 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2228 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2229 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2230 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2231 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2234 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2235 | long min) |
| 2236 | { |
| 2237 | struct io_kiocb *req, *tmp; |
| 2238 | LIST_HEAD(done); |
| 2239 | bool spin; |
| 2240 | int ret; |
| 2241 | |
| 2242 | /* |
| 2243 | * Only spin for completions if we don't have multiple devices hanging |
| 2244 | * off our complete list, and we're under the requested amount. |
| 2245 | */ |
| 2246 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2247 | |
| 2248 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2249 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2250 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2251 | |
| 2252 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2253 | * Move completed and retryable entries to our local lists. |
| 2254 | * If we find a request that requires polling, break out |
| 2255 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2256 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2257 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2258 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2259 | continue; |
| 2260 | } |
| 2261 | if (!list_empty(&done)) |
| 2262 | break; |
| 2263 | |
| 2264 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2265 | if (ret < 0) |
| 2266 | break; |
| 2267 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2268 | /* iopoll may have completed current req */ |
| 2269 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2270 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2271 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2272 | if (ret && spin) |
| 2273 | spin = false; |
| 2274 | ret = 0; |
| 2275 | } |
| 2276 | |
| 2277 | if (!list_empty(&done)) |
| 2278 | io_iopoll_complete(ctx, nr_events, &done); |
| 2279 | |
| 2280 | return ret; |
| 2281 | } |
| 2282 | |
| 2283 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2284 | * 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] | 2285 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2286 | * as a non-spinning completion check. |
| 2287 | */ |
| 2288 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2289 | long min) |
| 2290 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2291 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2292 | int ret; |
| 2293 | |
| 2294 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2295 | if (ret < 0) |
| 2296 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2297 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2298 | return 0; |
| 2299 | } |
| 2300 | |
| 2301 | return 1; |
| 2302 | } |
| 2303 | |
| 2304 | /* |
| 2305 | * We can't just wait for polled events to come to us, we have to actively |
| 2306 | * find and complete them. |
| 2307 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2308 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2309 | { |
| 2310 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2311 | return; |
| 2312 | |
| 2313 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2314 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2315 | unsigned int nr_events = 0; |
| 2316 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2317 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2318 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2319 | /* let it sleep and repeat later if can't complete a request */ |
| 2320 | if (nr_events == 0) |
| 2321 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2322 | /* |
| 2323 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2324 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2325 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2326 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2327 | if (need_resched()) { |
| 2328 | mutex_unlock(&ctx->uring_lock); |
| 2329 | cond_resched(); |
| 2330 | mutex_lock(&ctx->uring_lock); |
| 2331 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2332 | } |
| 2333 | mutex_unlock(&ctx->uring_lock); |
| 2334 | } |
| 2335 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2336 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2337 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2338 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2339 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2340 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2341 | /* |
| 2342 | * We disallow the app entering submit/complete with polling, but we |
| 2343 | * still need to lock the ring to prevent racing with polled issue |
| 2344 | * that got punted to a workqueue. |
| 2345 | */ |
| 2346 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2347 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2348 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2349 | * Don't enter poll loop if we already have events pending. |
| 2350 | * If we do, we can potentially be spinning for commands that |
| 2351 | * already triggered a CQE (eg in error). |
| 2352 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2353 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2354 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2355 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2356 | break; |
| 2357 | |
| 2358 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2359 | * If a submit got punted to a workqueue, we can have the |
| 2360 | * application entering polling for a command before it gets |
| 2361 | * issued. That app will hold the uring_lock for the duration |
| 2362 | * of the poll right here, so we need to take a breather every |
| 2363 | * now and then to ensure that the issue has a chance to add |
| 2364 | * the poll to the issued list. Otherwise we can spin here |
| 2365 | * forever, while the workqueue is stuck trying to acquire the |
| 2366 | * very same mutex. |
| 2367 | */ |
| 2368 | if (!(++iters & 7)) { |
| 2369 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2370 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2371 | mutex_lock(&ctx->uring_lock); |
| 2372 | } |
| 2373 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2374 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2375 | if (ret <= 0) |
| 2376 | break; |
| 2377 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2378 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2379 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2380 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2381 | return ret; |
| 2382 | } |
| 2383 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2384 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2385 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2386 | /* |
| 2387 | * Tell lockdep we inherited freeze protection from submission |
| 2388 | * thread. |
| 2389 | */ |
| 2390 | if (req->flags & REQ_F_ISREG) { |
| 2391 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2392 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2393 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2394 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2395 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2398 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2399 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2400 | { |
| 2401 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2402 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2403 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2404 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2405 | /* already prepared */ |
| 2406 | if (req->async_data) |
| 2407 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2408 | |
| 2409 | switch (req->opcode) { |
| 2410 | case IORING_OP_READV: |
| 2411 | case IORING_OP_READ_FIXED: |
| 2412 | case IORING_OP_READ: |
| 2413 | rw = READ; |
| 2414 | break; |
| 2415 | case IORING_OP_WRITEV: |
| 2416 | case IORING_OP_WRITE_FIXED: |
| 2417 | case IORING_OP_WRITE: |
| 2418 | rw = WRITE; |
| 2419 | break; |
| 2420 | default: |
| 2421 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2422 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2423 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2424 | } |
| 2425 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2426 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2427 | if (ret < 0) |
| 2428 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2429 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2430 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2431 | #endif |
| 2432 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2433 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2434 | { |
| 2435 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2436 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2437 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2438 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2439 | return false; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2440 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2441 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2442 | /* |
| 2443 | * If ref is dying, we might be running poll reap from the exit work. |
| 2444 | * Don't attempt to reissue from that path, just let it fail with |
| 2445 | * -EAGAIN. |
| 2446 | */ |
| 2447 | if (percpu_ref_is_dying(&req->ctx->refs)) |
| 2448 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2449 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2450 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2451 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 2452 | if (io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2453 | refcount_inc(&req->refs); |
| 2454 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2455 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2456 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2457 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2458 | #endif |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2462 | 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] | 2463 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2464 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2465 | int cflags = 0; |
| 2466 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2467 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2468 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2469 | if (res != req->result) |
| 2470 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2471 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2472 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2473 | kiocb_end_write(req); |
| 2474 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2475 | cflags = io_put_rw_kbuf(req); |
| 2476 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2480 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2481 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2482 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2483 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2484 | } |
| 2485 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2486 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2487 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2488 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2489 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2490 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2491 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2492 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2493 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2494 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2495 | |
| 2496 | WRITE_ONCE(req->result, res); |
| 2497 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2498 | smp_wmb(); |
| 2499 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /* |
| 2503 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2504 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2505 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2506 | * accessing the kiocb cookie. |
| 2507 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2508 | 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] | 2509 | { |
| 2510 | struct io_ring_ctx *ctx = req->ctx; |
| 2511 | |
| 2512 | /* |
| 2513 | * Track whether we have multiple files in our lists. This will impact |
| 2514 | * how we do polling eventually, not spinning if we're on potentially |
| 2515 | * different devices. |
| 2516 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2517 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2518 | ctx->poll_multi_file = false; |
| 2519 | } else if (!ctx->poll_multi_file) { |
| 2520 | struct io_kiocb *list_req; |
| 2521 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2522 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2523 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2524 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2525 | ctx->poll_multi_file = true; |
| 2526 | } |
| 2527 | |
| 2528 | /* |
| 2529 | * For fast devices, IO may have already completed. If it has, add |
| 2530 | * it to the front so we find it first. |
| 2531 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2532 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2533 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2534 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2535 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2536 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2537 | /* |
| 2538 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2539 | * task context or in io worker task context. If current task context is |
| 2540 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2541 | */ |
| 2542 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2543 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2544 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2547 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2548 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2549 | if (state->file_refs) { |
| 2550 | fput_many(state->file, state->file_refs); |
| 2551 | state->file_refs = 0; |
| 2552 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * Get as many references to a file as we have IOs left in this submission, |
| 2557 | * assuming most submissions are for one file, or at least that each file |
| 2558 | * has more than one submission. |
| 2559 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2560 | 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] | 2561 | { |
| 2562 | if (!state) |
| 2563 | return fget(fd); |
| 2564 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2565 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2566 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2567 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2568 | return state->file; |
| 2569 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2570 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2571 | } |
| 2572 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2573 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2574 | return NULL; |
| 2575 | |
| 2576 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2577 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2578 | return state->file; |
| 2579 | } |
| 2580 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2581 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2582 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2583 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2584 | } |
| 2585 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2586 | /* |
| 2587 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2588 | * any file. For now, just ensure that anything potentially problematic is done |
| 2589 | * inline. |
| 2590 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2591 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2592 | { |
| 2593 | umode_t mode = file_inode(file)->i_mode; |
| 2594 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2595 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2596 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2597 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2598 | return true; |
| 2599 | return false; |
| 2600 | } |
| 2601 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2602 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2603 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2604 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2605 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2606 | file->f_op != &io_uring_fops) |
| 2607 | return true; |
| 2608 | return false; |
| 2609 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2610 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2611 | /* any ->read/write should understand O_NONBLOCK */ |
| 2612 | if (file->f_flags & O_NONBLOCK) |
| 2613 | return true; |
| 2614 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2615 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2616 | return false; |
| 2617 | |
| 2618 | if (rw == READ) |
| 2619 | return file->f_op->read_iter != NULL; |
| 2620 | |
| 2621 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2624 | 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] | 2625 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2626 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2627 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2628 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2629 | unsigned ioprio; |
| 2630 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2631 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2632 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2633 | req->flags |= REQ_F_ISREG; |
| 2634 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2635 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2636 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2637 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2638 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2639 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2640 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2641 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2642 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2643 | if (unlikely(ret)) |
| 2644 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2645 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2646 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 2647 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 2648 | req->flags |= REQ_F_NOWAIT; |
| 2649 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2650 | ioprio = READ_ONCE(sqe->ioprio); |
| 2651 | if (ioprio) { |
| 2652 | ret = ioprio_check_cap(ioprio); |
| 2653 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2654 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2655 | |
| 2656 | kiocb->ki_ioprio = ioprio; |
| 2657 | } else |
| 2658 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2659 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2660 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2661 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2662 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2663 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2664 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2665 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2666 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2667 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2668 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2669 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2670 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2671 | kiocb->ki_complete = io_complete_rw; |
| 2672 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2673 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2674 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2675 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2676 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2677 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2681 | { |
| 2682 | switch (ret) { |
| 2683 | case -EIOCBQUEUED: |
| 2684 | break; |
| 2685 | case -ERESTARTSYS: |
| 2686 | case -ERESTARTNOINTR: |
| 2687 | case -ERESTARTNOHAND: |
| 2688 | case -ERESTART_RESTARTBLOCK: |
| 2689 | /* |
| 2690 | * We can't just restart the syscall, since previously |
| 2691 | * submitted sqes may already be in progress. Just fail this |
| 2692 | * IO with EINTR. |
| 2693 | */ |
| 2694 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2695 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2696 | default: |
| 2697 | kiocb->ki_complete(kiocb, ret, 0); |
| 2698 | } |
| 2699 | } |
| 2700 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2701 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2702 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2703 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2704 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2705 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2706 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2707 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2708 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2709 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2710 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2711 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2712 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2713 | } |
| 2714 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2715 | if (req->flags & REQ_F_CUR_POS) |
| 2716 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2717 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2718 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2719 | else |
| 2720 | io_rw_done(kiocb, ret); |
| 2721 | } |
| 2722 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2723 | 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] | 2724 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2725 | struct io_ring_ctx *ctx = req->ctx; |
| 2726 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2727 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 2728 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2729 | size_t offset; |
| 2730 | u64 buf_addr; |
| 2731 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2732 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2733 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2734 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2735 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2736 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2737 | |
| 2738 | /* overflow */ |
| 2739 | if (buf_addr + len < buf_addr) |
| 2740 | return -EFAULT; |
| 2741 | /* not inside the mapped region */ |
| 2742 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2743 | return -EFAULT; |
| 2744 | |
| 2745 | /* |
| 2746 | * May not be a start of buffer, set size appropriately |
| 2747 | * and advance us to the beginning. |
| 2748 | */ |
| 2749 | offset = buf_addr - imu->ubuf; |
| 2750 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2751 | |
| 2752 | if (offset) { |
| 2753 | /* |
| 2754 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2755 | * using the latter parts of a big fixed buffer - it iterates |
| 2756 | * over each segment manually. We can cheat a bit here, because |
| 2757 | * we know that: |
| 2758 | * |
| 2759 | * 1) it's a BVEC iter, we set it up |
| 2760 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2761 | * first and last bvec |
| 2762 | * |
| 2763 | * So just find our index, and adjust the iterator afterwards. |
| 2764 | * If the offset is within the first bvec (or the whole first |
| 2765 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2766 | * since we can just skip the first segment, which may not |
| 2767 | * be PAGE_SIZE aligned. |
| 2768 | */ |
| 2769 | const struct bio_vec *bvec = imu->bvec; |
| 2770 | |
| 2771 | if (offset <= bvec->bv_len) { |
| 2772 | iov_iter_advance(iter, offset); |
| 2773 | } else { |
| 2774 | unsigned long seg_skip; |
| 2775 | |
| 2776 | /* skip first vec */ |
| 2777 | offset -= bvec->bv_len; |
| 2778 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2779 | |
| 2780 | iter->bvec = bvec + seg_skip; |
| 2781 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2782 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2783 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2784 | } |
| 2785 | } |
| 2786 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2787 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2788 | } |
| 2789 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2790 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2791 | { |
| 2792 | if (needs_lock) |
| 2793 | mutex_unlock(&ctx->uring_lock); |
| 2794 | } |
| 2795 | |
| 2796 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2797 | { |
| 2798 | /* |
| 2799 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2800 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2801 | * The only exception is when we've detached the request and issue it |
| 2802 | * from an async worker thread, grab the lock for that case. |
| 2803 | */ |
| 2804 | if (needs_lock) |
| 2805 | mutex_lock(&ctx->uring_lock); |
| 2806 | } |
| 2807 | |
| 2808 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2809 | int bgid, struct io_buffer *kbuf, |
| 2810 | bool needs_lock) |
| 2811 | { |
| 2812 | struct io_buffer *head; |
| 2813 | |
| 2814 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2815 | return kbuf; |
| 2816 | |
| 2817 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2818 | |
| 2819 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2820 | |
| 2821 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2822 | if (head) { |
| 2823 | if (!list_empty(&head->list)) { |
| 2824 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2825 | list); |
| 2826 | list_del(&kbuf->list); |
| 2827 | } else { |
| 2828 | kbuf = head; |
| 2829 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2830 | } |
| 2831 | if (*len > kbuf->len) |
| 2832 | *len = kbuf->len; |
| 2833 | } else { |
| 2834 | kbuf = ERR_PTR(-ENOBUFS); |
| 2835 | } |
| 2836 | |
| 2837 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2838 | |
| 2839 | return kbuf; |
| 2840 | } |
| 2841 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2842 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2843 | bool needs_lock) |
| 2844 | { |
| 2845 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2846 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2847 | |
| 2848 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2849 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2850 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2851 | if (IS_ERR(kbuf)) |
| 2852 | return kbuf; |
| 2853 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2854 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2855 | return u64_to_user_ptr(kbuf->addr); |
| 2856 | } |
| 2857 | |
| 2858 | #ifdef CONFIG_COMPAT |
| 2859 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2860 | bool needs_lock) |
| 2861 | { |
| 2862 | struct compat_iovec __user *uiov; |
| 2863 | compat_ssize_t clen; |
| 2864 | void __user *buf; |
| 2865 | ssize_t len; |
| 2866 | |
| 2867 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2868 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2869 | return -EFAULT; |
| 2870 | if (__get_user(clen, &uiov->iov_len)) |
| 2871 | return -EFAULT; |
| 2872 | if (clen < 0) |
| 2873 | return -EINVAL; |
| 2874 | |
| 2875 | len = clen; |
| 2876 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2877 | if (IS_ERR(buf)) |
| 2878 | return PTR_ERR(buf); |
| 2879 | iov[0].iov_base = buf; |
| 2880 | iov[0].iov_len = (compat_size_t) len; |
| 2881 | return 0; |
| 2882 | } |
| 2883 | #endif |
| 2884 | |
| 2885 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2886 | bool needs_lock) |
| 2887 | { |
| 2888 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2889 | void __user *buf; |
| 2890 | ssize_t len; |
| 2891 | |
| 2892 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2893 | return -EFAULT; |
| 2894 | |
| 2895 | len = iov[0].iov_len; |
| 2896 | if (len < 0) |
| 2897 | return -EINVAL; |
| 2898 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2899 | if (IS_ERR(buf)) |
| 2900 | return PTR_ERR(buf); |
| 2901 | iov[0].iov_base = buf; |
| 2902 | iov[0].iov_len = len; |
| 2903 | return 0; |
| 2904 | } |
| 2905 | |
| 2906 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2907 | bool needs_lock) |
| 2908 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2909 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2910 | struct io_buffer *kbuf; |
| 2911 | |
| 2912 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2913 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2914 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2915 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2916 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 2917 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2918 | return -EINVAL; |
| 2919 | |
| 2920 | #ifdef CONFIG_COMPAT |
| 2921 | if (req->ctx->compat) |
| 2922 | return io_compat_import(req, iov, needs_lock); |
| 2923 | #endif |
| 2924 | |
| 2925 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2926 | } |
| 2927 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2928 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 2929 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2930 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2931 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2932 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2933 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2934 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2935 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2936 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2937 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2938 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2939 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2940 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2941 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2942 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2943 | return -EINVAL; |
| 2944 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2945 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2946 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2947 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 2948 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2949 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2950 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2953 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2954 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 2955 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2958 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 2959 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2960 | if (!ret) |
| 2961 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2962 | *iovec = NULL; |
| 2963 | return ret; |
| 2964 | } |
| 2965 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 2966 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 2967 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2968 | } |
| 2969 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2970 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 2971 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 2972 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 2973 | } |
| 2974 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2975 | /* |
| 2976 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2977 | * by looping over ->read() or ->write() manually. |
| 2978 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 2979 | 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] | 2980 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 2981 | struct kiocb *kiocb = &req->rw.kiocb; |
| 2982 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2983 | ssize_t ret = 0; |
| 2984 | |
| 2985 | /* |
| 2986 | * Don't support polled IO through this interface, and we can't |
| 2987 | * support non-blocking either. For the latter, this just causes |
| 2988 | * the kiocb to be handled from an async context. |
| 2989 | */ |
| 2990 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2991 | return -EOPNOTSUPP; |
| 2992 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2993 | return -EAGAIN; |
| 2994 | |
| 2995 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2996 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2997 | ssize_t nr; |
| 2998 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2999 | if (!iov_iter_is_bvec(iter)) { |
| 3000 | iovec = iov_iter_iovec(iter); |
| 3001 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3002 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3003 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3004 | } |
| 3005 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3006 | if (rw == READ) { |
| 3007 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3008 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3009 | } else { |
| 3010 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3011 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | if (nr < 0) { |
| 3015 | if (!ret) |
| 3016 | ret = nr; |
| 3017 | break; |
| 3018 | } |
| 3019 | ret += nr; |
| 3020 | if (nr != iovec.iov_len) |
| 3021 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3022 | req->rw.len -= nr; |
| 3023 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3024 | iov_iter_advance(iter, nr); |
| 3025 | } |
| 3026 | |
| 3027 | return ret; |
| 3028 | } |
| 3029 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3030 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3031 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3032 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3033 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3034 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3035 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3036 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3037 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3038 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3039 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3040 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3041 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3042 | unsigned iov_off = 0; |
| 3043 | |
| 3044 | rw->iter.iov = rw->fast_iov; |
| 3045 | if (iter->iov != fast_iov) { |
| 3046 | iov_off = iter->iov - fast_iov; |
| 3047 | rw->iter.iov += iov_off; |
| 3048 | } |
| 3049 | if (rw->fast_iov != fast_iov) |
| 3050 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3051 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3052 | } else { |
| 3053 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3057 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3058 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3059 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3060 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3061 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3062 | } |
| 3063 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3064 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3065 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3066 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3067 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3068 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3069 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3072 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3073 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3074 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3075 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3076 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3077 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3078 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3079 | if (__io_alloc_async_data(req)) { |
| 3080 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3081 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3082 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3083 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3084 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3085 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3086 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3089 | 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] | 3090 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3091 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3092 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3093 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3094 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3095 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3096 | if (unlikely(ret < 0)) |
| 3097 | return ret; |
| 3098 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3099 | iorw->bytes_done = 0; |
| 3100 | iorw->free_iovec = iov; |
| 3101 | if (iov) |
| 3102 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3103 | return 0; |
| 3104 | } |
| 3105 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3106 | 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] | 3107 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3108 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3109 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3110 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3111 | } |
| 3112 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3113 | /* |
| 3114 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3115 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3116 | * This gets called when the page is unlocked, and we generally expect that to |
| 3117 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3118 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3119 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3120 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3121 | * slow path. |
| 3122 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3123 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3124 | int sync, void *arg) |
| 3125 | { |
| 3126 | struct wait_page_queue *wpq; |
| 3127 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3128 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3129 | |
| 3130 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3131 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3132 | if (!wake_page_match(wpq, key)) |
| 3133 | return 0; |
| 3134 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3135 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3136 | list_del_init(&wait->entry); |
| 3137 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3138 | /* submit ref gets dropped, acquire a new one */ |
| 3139 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3140 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3141 | return 1; |
| 3142 | } |
| 3143 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3144 | /* |
| 3145 | * This controls whether a given IO request should be armed for async page |
| 3146 | * based retry. If we return false here, the request is handed to the async |
| 3147 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3148 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3149 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3150 | * will register a callback when the page is unlocked at IO completion. Through |
| 3151 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3152 | * That retry will attempt the buffered read again. The retry will generally |
| 3153 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3154 | * async worker threads for a blocking retry. |
| 3155 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3156 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3157 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3158 | struct io_async_rw *rw = req->async_data; |
| 3159 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3160 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3161 | |
| 3162 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3163 | if (req->flags & REQ_F_NOWAIT) |
| 3164 | return false; |
| 3165 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3166 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3167 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3168 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3169 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3170 | /* |
| 3171 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3172 | * support callback based unlocks |
| 3173 | */ |
| 3174 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3175 | return false; |
| 3176 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3177 | wait->wait.func = io_async_buf_func; |
| 3178 | wait->wait.private = req; |
| 3179 | wait->wait.flags = 0; |
| 3180 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3181 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3182 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3183 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3184 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3188 | { |
| 3189 | if (req->file->f_op->read_iter) |
| 3190 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3191 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3192 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3193 | else |
| 3194 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3195 | } |
| 3196 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3197 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3198 | { |
| 3199 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3200 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3201 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3202 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3203 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3204 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3205 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3206 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3207 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3208 | iovec = NULL; |
| 3209 | } else { |
| 3210 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3211 | if (ret < 0) |
| 3212 | return ret; |
| 3213 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3214 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3215 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3216 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3217 | /* Ensure we clear previously set non-block flag */ |
| 3218 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3219 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3220 | else |
| 3221 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3222 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3223 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3224 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3225 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3226 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3227 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3228 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3229 | 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] | 3230 | if (unlikely(ret)) { |
| 3231 | kfree(iovec); |
| 3232 | return ret; |
| 3233 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3234 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3235 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3236 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3237 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3238 | goto out_free; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3239 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3240 | /* IOPOLL retry should happen for io-wq threads */ |
| 3241 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3242 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3243 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3244 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3245 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3246 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3247 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3248 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3249 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3250 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3251 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3252 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3253 | } |
| 3254 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3255 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3256 | if (ret2) |
| 3257 | return ret2; |
| 3258 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3259 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3260 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3261 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3262 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3263 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3264 | do { |
| 3265 | io_size -= ret; |
| 3266 | rw->bytes_done += ret; |
| 3267 | /* if we can retry, do so with the callbacks armed */ |
| 3268 | if (!io_rw_should_retry(req)) { |
| 3269 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3270 | return -EAGAIN; |
| 3271 | } |
| 3272 | |
| 3273 | /* |
| 3274 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3275 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3276 | * desired page gets unlocked. We can also get a partial read |
| 3277 | * here, and if we do, then just retry at the new offset. |
| 3278 | */ |
| 3279 | ret = io_iter_do_read(req, iter); |
| 3280 | if (ret == -EIOCBQUEUED) |
| 3281 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3282 | /* we got some bytes, but not all. retry. */ |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3283 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3284 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3285 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3286 | out_free: |
| 3287 | /* it's faster to check here then delegate to kfree */ |
| 3288 | if (iovec) |
| 3289 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3290 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3291 | } |
| 3292 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3293 | 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] | 3294 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3295 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3296 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3297 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3298 | } |
| 3299 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3300 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3301 | { |
| 3302 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3303 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3304 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3305 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3306 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3307 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3308 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3309 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3310 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3311 | iovec = NULL; |
| 3312 | } else { |
| 3313 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3314 | if (ret < 0) |
| 3315 | return ret; |
| 3316 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3317 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3318 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3319 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3320 | /* Ensure we clear previously set non-block flag */ |
| 3321 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3322 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3323 | else |
| 3324 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3325 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3326 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3327 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3328 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3329 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3330 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3331 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3332 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3333 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3334 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3335 | 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] | 3336 | if (unlikely(ret)) |
| 3337 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3338 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3339 | /* |
| 3340 | * Open-code file_start_write here to grab freeze protection, |
| 3341 | * which will be released by another thread in |
| 3342 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3343 | * released so that it doesn't complain about the held lock when |
| 3344 | * we return to userspace. |
| 3345 | */ |
| 3346 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3347 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3348 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3349 | SB_FREEZE_WRITE); |
| 3350 | } |
| 3351 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3352 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3353 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3354 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3355 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3356 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3357 | else |
| 3358 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3359 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3360 | /* |
| 3361 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3362 | * retry them without IOCB_NOWAIT. |
| 3363 | */ |
| 3364 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3365 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3366 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3367 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3368 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3369 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3370 | /* IOPOLL retry should happen for io-wq threads */ |
| 3371 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3372 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3373 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3374 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3375 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3376 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3377 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3378 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3379 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3380 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3381 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3382 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3383 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3384 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3385 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3386 | return ret; |
| 3387 | } |
| 3388 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3389 | static int io_renameat_prep(struct io_kiocb *req, |
| 3390 | const struct io_uring_sqe *sqe) |
| 3391 | { |
| 3392 | struct io_rename *ren = &req->rename; |
| 3393 | const char __user *oldf, *newf; |
| 3394 | |
| 3395 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3396 | return -EBADF; |
| 3397 | |
| 3398 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3399 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3400 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3401 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3402 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3403 | |
| 3404 | ren->oldpath = getname(oldf); |
| 3405 | if (IS_ERR(ren->oldpath)) |
| 3406 | return PTR_ERR(ren->oldpath); |
| 3407 | |
| 3408 | ren->newpath = getname(newf); |
| 3409 | if (IS_ERR(ren->newpath)) { |
| 3410 | putname(ren->oldpath); |
| 3411 | return PTR_ERR(ren->newpath); |
| 3412 | } |
| 3413 | |
| 3414 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3415 | return 0; |
| 3416 | } |
| 3417 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3418 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3419 | { |
| 3420 | struct io_rename *ren = &req->rename; |
| 3421 | int ret; |
| 3422 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3423 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3424 | return -EAGAIN; |
| 3425 | |
| 3426 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3427 | ren->newpath, ren->flags); |
| 3428 | |
| 3429 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3430 | if (ret < 0) |
| 3431 | req_set_fail_links(req); |
| 3432 | io_req_complete(req, ret); |
| 3433 | return 0; |
| 3434 | } |
| 3435 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3436 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3437 | const struct io_uring_sqe *sqe) |
| 3438 | { |
| 3439 | struct io_unlink *un = &req->unlink; |
| 3440 | const char __user *fname; |
| 3441 | |
| 3442 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3443 | return -EBADF; |
| 3444 | |
| 3445 | un->dfd = READ_ONCE(sqe->fd); |
| 3446 | |
| 3447 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3448 | if (un->flags & ~AT_REMOVEDIR) |
| 3449 | return -EINVAL; |
| 3450 | |
| 3451 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3452 | un->filename = getname(fname); |
| 3453 | if (IS_ERR(un->filename)) |
| 3454 | return PTR_ERR(un->filename); |
| 3455 | |
| 3456 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3460 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3461 | { |
| 3462 | struct io_unlink *un = &req->unlink; |
| 3463 | int ret; |
| 3464 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3465 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3466 | return -EAGAIN; |
| 3467 | |
| 3468 | if (un->flags & AT_REMOVEDIR) |
| 3469 | ret = do_rmdir(un->dfd, un->filename); |
| 3470 | else |
| 3471 | ret = do_unlinkat(un->dfd, un->filename); |
| 3472 | |
| 3473 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3474 | if (ret < 0) |
| 3475 | req_set_fail_links(req); |
| 3476 | io_req_complete(req, ret); |
| 3477 | return 0; |
| 3478 | } |
| 3479 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3480 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3481 | const struct io_uring_sqe *sqe) |
| 3482 | { |
| 3483 | #if defined(CONFIG_NET) |
| 3484 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3485 | return -EINVAL; |
| 3486 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3487 | sqe->buf_index) |
| 3488 | return -EINVAL; |
| 3489 | |
| 3490 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3491 | return 0; |
| 3492 | #else |
| 3493 | return -EOPNOTSUPP; |
| 3494 | #endif |
| 3495 | } |
| 3496 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3497 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3498 | { |
| 3499 | #if defined(CONFIG_NET) |
| 3500 | struct socket *sock; |
| 3501 | int ret; |
| 3502 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3503 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3504 | return -EAGAIN; |
| 3505 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3506 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3507 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3508 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3509 | |
| 3510 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3511 | if (ret < 0) |
| 3512 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3513 | io_req_complete(req, ret); |
| 3514 | return 0; |
| 3515 | #else |
| 3516 | return -EOPNOTSUPP; |
| 3517 | #endif |
| 3518 | } |
| 3519 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3520 | static int __io_splice_prep(struct io_kiocb *req, |
| 3521 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3522 | { |
| 3523 | struct io_splice* sp = &req->splice; |
| 3524 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3525 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3526 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3527 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3528 | |
| 3529 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3530 | sp->len = READ_ONCE(sqe->len); |
| 3531 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3532 | |
| 3533 | if (unlikely(sp->flags & ~valid_flags)) |
| 3534 | return -EINVAL; |
| 3535 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3536 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3537 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3538 | if (!sp->file_in) |
| 3539 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3540 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3541 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3542 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3543 | /* |
| 3544 | * Splice operation will be punted aync, and here need to |
| 3545 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3546 | */ |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3547 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3548 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3549 | |
| 3550 | return 0; |
| 3551 | } |
| 3552 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3553 | static int io_tee_prep(struct io_kiocb *req, |
| 3554 | const struct io_uring_sqe *sqe) |
| 3555 | { |
| 3556 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3557 | return -EINVAL; |
| 3558 | return __io_splice_prep(req, sqe); |
| 3559 | } |
| 3560 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3561 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3562 | { |
| 3563 | struct io_splice *sp = &req->splice; |
| 3564 | struct file *in = sp->file_in; |
| 3565 | struct file *out = sp->file_out; |
| 3566 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3567 | long ret = 0; |
| 3568 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3569 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3570 | return -EAGAIN; |
| 3571 | if (sp->len) |
| 3572 | ret = do_tee(in, out, sp->len, flags); |
| 3573 | |
| 3574 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3575 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3576 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3577 | if (ret != sp->len) |
| 3578 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3579 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3580 | return 0; |
| 3581 | } |
| 3582 | |
| 3583 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3584 | { |
| 3585 | struct io_splice* sp = &req->splice; |
| 3586 | |
| 3587 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3588 | sp->off_out = READ_ONCE(sqe->off); |
| 3589 | return __io_splice_prep(req, sqe); |
| 3590 | } |
| 3591 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3592 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3593 | { |
| 3594 | struct io_splice *sp = &req->splice; |
| 3595 | struct file *in = sp->file_in; |
| 3596 | struct file *out = sp->file_out; |
| 3597 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3598 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3599 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3600 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3601 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3602 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3603 | |
| 3604 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3605 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3606 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3607 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3608 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3609 | |
| 3610 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3611 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3612 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3613 | if (ret != sp->len) |
| 3614 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3615 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3616 | return 0; |
| 3617 | } |
| 3618 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3619 | /* |
| 3620 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3621 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3622 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3623 | { |
| 3624 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3625 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3626 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3627 | return -EINVAL; |
| 3628 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3629 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3630 | return 0; |
| 3631 | } |
| 3632 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 3633 | 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] | 3634 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3635 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3636 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3637 | if (!req->file) |
| 3638 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3639 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3640 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3641 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3642 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3643 | return -EINVAL; |
| 3644 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3645 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3646 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3647 | return -EINVAL; |
| 3648 | |
| 3649 | req->sync.off = READ_ONCE(sqe->off); |
| 3650 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3651 | return 0; |
| 3652 | } |
| 3653 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3654 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3655 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3656 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3657 | int ret; |
| 3658 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3659 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3660 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3661 | return -EAGAIN; |
| 3662 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3663 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3664 | end > 0 ? end : LLONG_MAX, |
| 3665 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3666 | if (ret < 0) |
| 3667 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3668 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3669 | return 0; |
| 3670 | } |
| 3671 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3672 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3673 | const struct io_uring_sqe *sqe) |
| 3674 | { |
| 3675 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3676 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3677 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3678 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3679 | |
| 3680 | req->sync.off = READ_ONCE(sqe->off); |
| 3681 | req->sync.len = READ_ONCE(sqe->addr); |
| 3682 | req->sync.mode = READ_ONCE(sqe->len); |
| 3683 | return 0; |
| 3684 | } |
| 3685 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3686 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3687 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3688 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3689 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3690 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3691 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3692 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3693 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3694 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3695 | if (ret < 0) |
| 3696 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3697 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3698 | return 0; |
| 3699 | } |
| 3700 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3701 | 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] | 3702 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3703 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3704 | int ret; |
| 3705 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3706 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3707 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3708 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3709 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3710 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3711 | /* open.how should be already initialised */ |
| 3712 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3713 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3714 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3715 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3716 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3717 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3718 | if (IS_ERR(req->open.filename)) { |
| 3719 | ret = PTR_ERR(req->open.filename); |
| 3720 | req->open.filename = NULL; |
| 3721 | return ret; |
| 3722 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3723 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3724 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3725 | return 0; |
| 3726 | } |
| 3727 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3728 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3729 | { |
| 3730 | u64 flags, mode; |
| 3731 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3732 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3733 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3734 | mode = READ_ONCE(sqe->len); |
| 3735 | flags = READ_ONCE(sqe->open_flags); |
| 3736 | req->open.how = build_open_how(flags, mode); |
| 3737 | return __io_openat_prep(req, sqe); |
| 3738 | } |
| 3739 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3740 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3741 | { |
| 3742 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3743 | size_t len; |
| 3744 | int ret; |
| 3745 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3746 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3747 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3748 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3749 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3750 | if (len < OPEN_HOW_SIZE_VER0) |
| 3751 | return -EINVAL; |
| 3752 | |
| 3753 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3754 | len); |
| 3755 | if (ret) |
| 3756 | return ret; |
| 3757 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3758 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3761 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3762 | { |
| 3763 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3764 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3765 | bool nonblock_set; |
| 3766 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3767 | int ret; |
| 3768 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3769 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3770 | if (ret) |
| 3771 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3772 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 3773 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3774 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3775 | /* |
| 3776 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 3777 | * it'll always -EAGAIN |
| 3778 | */ |
| 3779 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 3780 | return -EAGAIN; |
| 3781 | op.lookup_flags |= LOOKUP_CACHED; |
| 3782 | op.open_flag |= O_NONBLOCK; |
| 3783 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3784 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3785 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3786 | if (ret < 0) |
| 3787 | goto err; |
| 3788 | |
| 3789 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3790 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3791 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 3792 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3793 | /* |
| 3794 | * We could hang on to this 'fd', but seems like marginal |
| 3795 | * gain for something that is now known to be a slower path. |
| 3796 | * So just put it, and we'll get a new one when we retry. |
| 3797 | */ |
| 3798 | put_unused_fd(ret); |
| 3799 | return -EAGAIN; |
| 3800 | } |
| 3801 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3802 | if (IS_ERR(file)) { |
| 3803 | put_unused_fd(ret); |
| 3804 | ret = PTR_ERR(file); |
| 3805 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3806 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3807 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3808 | fsnotify_open(file); |
| 3809 | fd_install(ret, file); |
| 3810 | } |
| 3811 | err: |
| 3812 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3813 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3814 | if (ret < 0) |
| 3815 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3816 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3817 | return 0; |
| 3818 | } |
| 3819 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3820 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3821 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3822 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3823 | } |
| 3824 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3825 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3826 | const struct io_uring_sqe *sqe) |
| 3827 | { |
| 3828 | struct io_provide_buf *p = &req->pbuf; |
| 3829 | u64 tmp; |
| 3830 | |
| 3831 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3832 | return -EINVAL; |
| 3833 | |
| 3834 | tmp = READ_ONCE(sqe->fd); |
| 3835 | if (!tmp || tmp > USHRT_MAX) |
| 3836 | return -EINVAL; |
| 3837 | |
| 3838 | memset(p, 0, sizeof(*p)); |
| 3839 | p->nbufs = tmp; |
| 3840 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3841 | return 0; |
| 3842 | } |
| 3843 | |
| 3844 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3845 | int bgid, unsigned nbufs) |
| 3846 | { |
| 3847 | unsigned i = 0; |
| 3848 | |
| 3849 | /* shouldn't happen */ |
| 3850 | if (!nbufs) |
| 3851 | return 0; |
| 3852 | |
| 3853 | /* the head kbuf is the list itself */ |
| 3854 | while (!list_empty(&buf->list)) { |
| 3855 | struct io_buffer *nxt; |
| 3856 | |
| 3857 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3858 | list_del(&nxt->list); |
| 3859 | kfree(nxt); |
| 3860 | if (++i == nbufs) |
| 3861 | return i; |
| 3862 | } |
| 3863 | i++; |
| 3864 | kfree(buf); |
| 3865 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3866 | |
| 3867 | return i; |
| 3868 | } |
| 3869 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3870 | 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] | 3871 | { |
| 3872 | struct io_provide_buf *p = &req->pbuf; |
| 3873 | struct io_ring_ctx *ctx = req->ctx; |
| 3874 | struct io_buffer *head; |
| 3875 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3876 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3877 | |
| 3878 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3879 | |
| 3880 | lockdep_assert_held(&ctx->uring_lock); |
| 3881 | |
| 3882 | ret = -ENOENT; |
| 3883 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3884 | if (head) |
| 3885 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3886 | if (ret < 0) |
| 3887 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3888 | |
| 3889 | /* need to hold the lock to complete IOPOLL requests */ |
| 3890 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3891 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3892 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3893 | } else { |
| 3894 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3895 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3896 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3897 | return 0; |
| 3898 | } |
| 3899 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3900 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3901 | const struct io_uring_sqe *sqe) |
| 3902 | { |
| 3903 | struct io_provide_buf *p = &req->pbuf; |
| 3904 | u64 tmp; |
| 3905 | |
| 3906 | if (sqe->ioprio || sqe->rw_flags) |
| 3907 | return -EINVAL; |
| 3908 | |
| 3909 | tmp = READ_ONCE(sqe->fd); |
| 3910 | if (!tmp || tmp > USHRT_MAX) |
| 3911 | return -E2BIG; |
| 3912 | p->nbufs = tmp; |
| 3913 | p->addr = READ_ONCE(sqe->addr); |
| 3914 | p->len = READ_ONCE(sqe->len); |
| 3915 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3916 | 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] | 3917 | return -EFAULT; |
| 3918 | |
| 3919 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3920 | tmp = READ_ONCE(sqe->off); |
| 3921 | if (tmp > USHRT_MAX) |
| 3922 | return -E2BIG; |
| 3923 | p->bid = tmp; |
| 3924 | return 0; |
| 3925 | } |
| 3926 | |
| 3927 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3928 | { |
| 3929 | struct io_buffer *buf; |
| 3930 | u64 addr = pbuf->addr; |
| 3931 | int i, bid = pbuf->bid; |
| 3932 | |
| 3933 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3934 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3935 | if (!buf) |
| 3936 | break; |
| 3937 | |
| 3938 | buf->addr = addr; |
| 3939 | buf->len = pbuf->len; |
| 3940 | buf->bid = bid; |
| 3941 | addr += pbuf->len; |
| 3942 | bid++; |
| 3943 | if (!*head) { |
| 3944 | INIT_LIST_HEAD(&buf->list); |
| 3945 | *head = buf; |
| 3946 | } else { |
| 3947 | list_add_tail(&buf->list, &(*head)->list); |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | return i ? i : -ENOMEM; |
| 3952 | } |
| 3953 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3954 | 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] | 3955 | { |
| 3956 | struct io_provide_buf *p = &req->pbuf; |
| 3957 | struct io_ring_ctx *ctx = req->ctx; |
| 3958 | struct io_buffer *head, *list; |
| 3959 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3960 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3961 | |
| 3962 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3963 | |
| 3964 | lockdep_assert_held(&ctx->uring_lock); |
| 3965 | |
| 3966 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3967 | |
| 3968 | ret = io_add_buffers(p, &head); |
| 3969 | if (ret < 0) |
| 3970 | goto out; |
| 3971 | |
| 3972 | if (!list) { |
| 3973 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 3974 | GFP_KERNEL); |
| 3975 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3976 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3977 | goto out; |
| 3978 | } |
| 3979 | } |
| 3980 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3981 | if (ret < 0) |
| 3982 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3983 | |
| 3984 | /* need to hold the lock to complete IOPOLL requests */ |
| 3985 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3986 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3987 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3988 | } else { |
| 3989 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3990 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3991 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3992 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3993 | } |
| 3994 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 3995 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 3996 | const struct io_uring_sqe *sqe) |
| 3997 | { |
| 3998 | #if defined(CONFIG_EPOLL) |
| 3999 | if (sqe->ioprio || sqe->buf_index) |
| 4000 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4001 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4002 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4003 | |
| 4004 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4005 | req->epoll.op = READ_ONCE(sqe->len); |
| 4006 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4007 | |
| 4008 | if (ep_op_has_event(req->epoll.op)) { |
| 4009 | struct epoll_event __user *ev; |
| 4010 | |
| 4011 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4012 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4013 | return -EFAULT; |
| 4014 | } |
| 4015 | |
| 4016 | return 0; |
| 4017 | #else |
| 4018 | return -EOPNOTSUPP; |
| 4019 | #endif |
| 4020 | } |
| 4021 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4022 | 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] | 4023 | { |
| 4024 | #if defined(CONFIG_EPOLL) |
| 4025 | struct io_epoll *ie = &req->epoll; |
| 4026 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4027 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4028 | |
| 4029 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4030 | if (force_nonblock && ret == -EAGAIN) |
| 4031 | return -EAGAIN; |
| 4032 | |
| 4033 | if (ret < 0) |
| 4034 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4035 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4036 | return 0; |
| 4037 | #else |
| 4038 | return -EOPNOTSUPP; |
| 4039 | #endif |
| 4040 | } |
| 4041 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4042 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4043 | { |
| 4044 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4045 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4046 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4047 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4048 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4049 | |
| 4050 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4051 | req->madvise.len = READ_ONCE(sqe->len); |
| 4052 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4053 | return 0; |
| 4054 | #else |
| 4055 | return -EOPNOTSUPP; |
| 4056 | #endif |
| 4057 | } |
| 4058 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4059 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4060 | { |
| 4061 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4062 | struct io_madvise *ma = &req->madvise; |
| 4063 | int ret; |
| 4064 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4065 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4066 | return -EAGAIN; |
| 4067 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4068 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4069 | if (ret < 0) |
| 4070 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4071 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4072 | return 0; |
| 4073 | #else |
| 4074 | return -EOPNOTSUPP; |
| 4075 | #endif |
| 4076 | } |
| 4077 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4078 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4079 | { |
| 4080 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4081 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4082 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4083 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4084 | |
| 4085 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4086 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4087 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4088 | return 0; |
| 4089 | } |
| 4090 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4091 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4092 | { |
| 4093 | struct io_fadvise *fa = &req->fadvise; |
| 4094 | int ret; |
| 4095 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4096 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4097 | switch (fa->advice) { |
| 4098 | case POSIX_FADV_NORMAL: |
| 4099 | case POSIX_FADV_RANDOM: |
| 4100 | case POSIX_FADV_SEQUENTIAL: |
| 4101 | break; |
| 4102 | default: |
| 4103 | return -EAGAIN; |
| 4104 | } |
| 4105 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4106 | |
| 4107 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4108 | if (ret < 0) |
| 4109 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4110 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4111 | return 0; |
| 4112 | } |
| 4113 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4114 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4115 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4116 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4117 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4118 | if (sqe->ioprio || sqe->buf_index) |
| 4119 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4120 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4121 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4122 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4123 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4124 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4125 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4126 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4127 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4128 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4129 | return 0; |
| 4130 | } |
| 4131 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4132 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4133 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4134 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4135 | int ret; |
| 4136 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4137 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4138 | /* only need file table for an actual valid fd */ |
| 4139 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4140 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4141 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4142 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4143 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4144 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4145 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4146 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4147 | if (ret < 0) |
| 4148 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4149 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4150 | return 0; |
| 4151 | } |
| 4152 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4153 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4154 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4155 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4156 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4157 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4158 | sqe->rw_flags || sqe->buf_index) |
| 4159 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4160 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4161 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4162 | |
| 4163 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4164 | return 0; |
| 4165 | } |
| 4166 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4167 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4168 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4169 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4170 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4171 | struct fdtable *fdt; |
| 4172 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4173 | int ret; |
| 4174 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4175 | file = NULL; |
| 4176 | ret = -EBADF; |
| 4177 | spin_lock(&files->file_lock); |
| 4178 | fdt = files_fdtable(files); |
| 4179 | if (close->fd >= fdt->max_fds) { |
| 4180 | spin_unlock(&files->file_lock); |
| 4181 | goto err; |
| 4182 | } |
| 4183 | file = fdt->fd[close->fd]; |
| 4184 | if (!file) { |
| 4185 | spin_unlock(&files->file_lock); |
| 4186 | goto err; |
| 4187 | } |
| 4188 | |
| 4189 | if (file->f_op == &io_uring_fops) { |
| 4190 | spin_unlock(&files->file_lock); |
| 4191 | file = NULL; |
| 4192 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4193 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4194 | |
| 4195 | /* 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] | 4196 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4197 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4198 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4199 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4200 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4201 | ret = __close_fd_get_file(close->fd, &file); |
| 4202 | spin_unlock(&files->file_lock); |
| 4203 | if (ret < 0) { |
| 4204 | if (ret == -ENOENT) |
| 4205 | ret = -EBADF; |
| 4206 | goto err; |
| 4207 | } |
| 4208 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4209 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4210 | ret = filp_close(file, current->files); |
| 4211 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4212 | if (ret < 0) |
| 4213 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4214 | if (file) |
| 4215 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4216 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4217 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4218 | } |
| 4219 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4220 | 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] | 4221 | { |
| 4222 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4223 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4224 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4225 | return -EINVAL; |
| 4226 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4227 | return -EINVAL; |
| 4228 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4229 | req->sync.off = READ_ONCE(sqe->off); |
| 4230 | req->sync.len = READ_ONCE(sqe->len); |
| 4231 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4232 | return 0; |
| 4233 | } |
| 4234 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4235 | 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] | 4236 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4237 | int ret; |
| 4238 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4239 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4240 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4241 | return -EAGAIN; |
| 4242 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4243 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4244 | req->sync.flags); |
| 4245 | if (ret < 0) |
| 4246 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4247 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4248 | return 0; |
| 4249 | } |
| 4250 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4251 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4252 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4253 | struct io_async_msghdr *kmsg) |
| 4254 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4255 | struct io_async_msghdr *async_msg = req->async_data; |
| 4256 | |
| 4257 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4258 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4259 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4260 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4261 | return -ENOMEM; |
| 4262 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4263 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4264 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4265 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4266 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4267 | /* if were using fast_iov, set it to the new one */ |
| 4268 | if (!async_msg->free_iov) |
| 4269 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4270 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4271 | return -EAGAIN; |
| 4272 | } |
| 4273 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4274 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4275 | struct io_async_msghdr *iomsg) |
| 4276 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4277 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4278 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4279 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4280 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4281 | } |
| 4282 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4283 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4284 | { |
| 4285 | int ret; |
| 4286 | |
| 4287 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4288 | return 0; |
| 4289 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4290 | if (!ret) |
| 4291 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4292 | return ret; |
| 4293 | } |
| 4294 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4295 | 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] | 4296 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4297 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4298 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4299 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4300 | return -EINVAL; |
| 4301 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4302 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4303 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4304 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4305 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4306 | #ifdef CONFIG_COMPAT |
| 4307 | if (req->ctx->compat) |
| 4308 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4309 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4310 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4311 | } |
| 4312 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4313 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4314 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4315 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4316 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4317 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4318 | int ret; |
| 4319 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4320 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4321 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4322 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4323 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4324 | kmsg = req->async_data; |
| 4325 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4326 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4327 | if (ret) |
| 4328 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4329 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4330 | } |
| 4331 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4332 | flags = req->sr_msg.msg_flags; |
| 4333 | if (flags & MSG_DONTWAIT) |
| 4334 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4335 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4336 | flags |= MSG_DONTWAIT; |
| 4337 | |
| 4338 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4339 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4340 | return io_setup_async_msg(req, kmsg); |
| 4341 | if (ret == -ERESTARTSYS) |
| 4342 | ret = -EINTR; |
| 4343 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4344 | /* fast path, check for non-NULL to avoid function call */ |
| 4345 | if (kmsg->free_iov) |
| 4346 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4347 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4348 | if (ret < 0) |
| 4349 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4350 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4351 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4352 | } |
| 4353 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4354 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4355 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4356 | struct io_sr_msg *sr = &req->sr_msg; |
| 4357 | struct msghdr msg; |
| 4358 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4359 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4360 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4361 | int ret; |
| 4362 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4363 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4364 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4365 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4366 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4367 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4368 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4369 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4370 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4371 | msg.msg_name = NULL; |
| 4372 | msg.msg_control = NULL; |
| 4373 | msg.msg_controllen = 0; |
| 4374 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4375 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4376 | flags = req->sr_msg.msg_flags; |
| 4377 | if (flags & MSG_DONTWAIT) |
| 4378 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4379 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4380 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4381 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4382 | msg.msg_flags = flags; |
| 4383 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4384 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4385 | return -EAGAIN; |
| 4386 | if (ret == -ERESTARTSYS) |
| 4387 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4388 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4389 | if (ret < 0) |
| 4390 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4391 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4392 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4393 | } |
| 4394 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4395 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4396 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4397 | { |
| 4398 | struct io_sr_msg *sr = &req->sr_msg; |
| 4399 | struct iovec __user *uiov; |
| 4400 | size_t iov_len; |
| 4401 | int ret; |
| 4402 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4403 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4404 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4405 | if (ret) |
| 4406 | return ret; |
| 4407 | |
| 4408 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4409 | if (iov_len > 1) |
| 4410 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4411 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4412 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4413 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4414 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4415 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4416 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4417 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4418 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4419 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4420 | if (ret > 0) |
| 4421 | ret = 0; |
| 4422 | } |
| 4423 | |
| 4424 | return ret; |
| 4425 | } |
| 4426 | |
| 4427 | #ifdef CONFIG_COMPAT |
| 4428 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4429 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4430 | { |
| 4431 | struct compat_msghdr __user *msg_compat; |
| 4432 | struct io_sr_msg *sr = &req->sr_msg; |
| 4433 | struct compat_iovec __user *uiov; |
| 4434 | compat_uptr_t ptr; |
| 4435 | compat_size_t len; |
| 4436 | int ret; |
| 4437 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4438 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4439 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4440 | &ptr, &len); |
| 4441 | if (ret) |
| 4442 | return ret; |
| 4443 | |
| 4444 | uiov = compat_ptr(ptr); |
| 4445 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4446 | compat_ssize_t clen; |
| 4447 | |
| 4448 | if (len > 1) |
| 4449 | return -EINVAL; |
| 4450 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4451 | return -EFAULT; |
| 4452 | if (__get_user(clen, &uiov->iov_len)) |
| 4453 | return -EFAULT; |
| 4454 | if (clen < 0) |
| 4455 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4456 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4457 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4458 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4459 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4460 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4461 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4462 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4463 | if (ret < 0) |
| 4464 | return ret; |
| 4465 | } |
| 4466 | |
| 4467 | return 0; |
| 4468 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4469 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4470 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4471 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4472 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4473 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4474 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4475 | |
| 4476 | #ifdef CONFIG_COMPAT |
| 4477 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4478 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4479 | #endif |
| 4480 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4481 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4484 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4485 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4486 | { |
| 4487 | struct io_sr_msg *sr = &req->sr_msg; |
| 4488 | struct io_buffer *kbuf; |
| 4489 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4490 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4491 | if (IS_ERR(kbuf)) |
| 4492 | return kbuf; |
| 4493 | |
| 4494 | sr->kbuf = kbuf; |
| 4495 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4496 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4499 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4500 | { |
| 4501 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4502 | } |
| 4503 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4504 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4505 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4506 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4507 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4508 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4509 | return 0; |
| 4510 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4511 | if (!ret) |
| 4512 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4513 | return ret; |
| 4514 | } |
| 4515 | |
| 4516 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4517 | { |
| 4518 | struct io_sr_msg *sr = &req->sr_msg; |
| 4519 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4520 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4521 | return -EINVAL; |
| 4522 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4523 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4524 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4525 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4526 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4527 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4528 | #ifdef CONFIG_COMPAT |
| 4529 | if (req->ctx->compat) |
| 4530 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4531 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4532 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4533 | } |
| 4534 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4535 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4536 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4537 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4538 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4539 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4540 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4541 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4542 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4543 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4544 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4545 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4546 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4547 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4548 | kmsg = req->async_data; |
| 4549 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4550 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4551 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4552 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4553 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4554 | } |
| 4555 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4556 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4557 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4558 | if (IS_ERR(kbuf)) |
| 4559 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4560 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4561 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4562 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4563 | 1, req->sr_msg.len); |
| 4564 | } |
| 4565 | |
| 4566 | flags = req->sr_msg.msg_flags; |
| 4567 | if (flags & MSG_DONTWAIT) |
| 4568 | req->flags |= REQ_F_NOWAIT; |
| 4569 | else if (force_nonblock) |
| 4570 | flags |= MSG_DONTWAIT; |
| 4571 | |
| 4572 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4573 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4574 | if (force_nonblock && ret == -EAGAIN) |
| 4575 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4576 | if (ret == -ERESTARTSYS) |
| 4577 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4578 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4579 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4580 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4581 | /* fast path, check for non-NULL to avoid function call */ |
| 4582 | if (kmsg->free_iov) |
| 4583 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4584 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4585 | if (ret < 0) |
| 4586 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4587 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4588 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4589 | } |
| 4590 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4591 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4592 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4593 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4594 | struct io_sr_msg *sr = &req->sr_msg; |
| 4595 | struct msghdr msg; |
| 4596 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4597 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4598 | struct iovec iov; |
| 4599 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4600 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4601 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4602 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4603 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4604 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4605 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4606 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4607 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4608 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4609 | if (IS_ERR(kbuf)) |
| 4610 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4611 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4612 | } |
| 4613 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4614 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4615 | if (unlikely(ret)) |
| 4616 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4617 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4618 | msg.msg_name = NULL; |
| 4619 | msg.msg_control = NULL; |
| 4620 | msg.msg_controllen = 0; |
| 4621 | msg.msg_namelen = 0; |
| 4622 | msg.msg_iocb = NULL; |
| 4623 | msg.msg_flags = 0; |
| 4624 | |
| 4625 | flags = req->sr_msg.msg_flags; |
| 4626 | if (flags & MSG_DONTWAIT) |
| 4627 | req->flags |= REQ_F_NOWAIT; |
| 4628 | else if (force_nonblock) |
| 4629 | flags |= MSG_DONTWAIT; |
| 4630 | |
| 4631 | ret = sock_recvmsg(sock, &msg, flags); |
| 4632 | if (force_nonblock && ret == -EAGAIN) |
| 4633 | return -EAGAIN; |
| 4634 | if (ret == -ERESTARTSYS) |
| 4635 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4636 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4637 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4638 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4639 | if (ret < 0) |
| 4640 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4641 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4642 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4645 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4646 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4647 | struct io_accept *accept = &req->accept; |
| 4648 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4649 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4650 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4651 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4652 | return -EINVAL; |
| 4653 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4654 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4655 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4656 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4657 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4658 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4659 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4660 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4661 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4662 | { |
| 4663 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4664 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4665 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4666 | int ret; |
| 4667 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4668 | if (req->file->f_flags & O_NONBLOCK) |
| 4669 | req->flags |= REQ_F_NOWAIT; |
| 4670 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4671 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4672 | accept->addr_len, accept->flags, |
| 4673 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4674 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4675 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4676 | if (ret < 0) { |
| 4677 | if (ret == -ERESTARTSYS) |
| 4678 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4679 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4680 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4681 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4682 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4685 | static int io_connect_prep_async(struct io_kiocb *req) |
| 4686 | { |
| 4687 | struct io_async_connect *io = req->async_data; |
| 4688 | struct io_connect *conn = &req->connect; |
| 4689 | |
| 4690 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 4691 | } |
| 4692 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4693 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4694 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4695 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4696 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4697 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4698 | return -EINVAL; |
| 4699 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4700 | return -EINVAL; |
| 4701 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4702 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4703 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4704 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4707 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4708 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4709 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4710 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4711 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4712 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4713 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4714 | if (req->async_data) { |
| 4715 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4716 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4717 | ret = move_addr_to_kernel(req->connect.addr, |
| 4718 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4719 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4720 | if (ret) |
| 4721 | goto out; |
| 4722 | io = &__io; |
| 4723 | } |
| 4724 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4725 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4726 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4727 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4728 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4729 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4730 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4731 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4732 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4733 | ret = -ENOMEM; |
| 4734 | goto out; |
| 4735 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4736 | io = req->async_data; |
| 4737 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4738 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4739 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4740 | if (ret == -ERESTARTSYS) |
| 4741 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4742 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4743 | if (ret < 0) |
| 4744 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4745 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4746 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4747 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4748 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4749 | #define IO_NETOP_FN(op) \ |
| 4750 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 4751 | { \ |
| 4752 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4755 | #define IO_NETOP_PREP(op) \ |
| 4756 | IO_NETOP_FN(op) \ |
| 4757 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 4758 | { \ |
| 4759 | return -EOPNOTSUPP; \ |
| 4760 | } \ |
| 4761 | |
| 4762 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 4763 | IO_NETOP_PREP(op) \ |
| 4764 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 4765 | { \ |
| 4766 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4767 | } |
| 4768 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4769 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 4770 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 4771 | IO_NETOP_PREP_ASYNC(connect); |
| 4772 | IO_NETOP_PREP(accept); |
| 4773 | IO_NETOP_FN(send); |
| 4774 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4775 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4776 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4777 | struct io_poll_table { |
| 4778 | struct poll_table_struct pt; |
| 4779 | struct io_kiocb *req; |
| 4780 | int error; |
| 4781 | }; |
| 4782 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4783 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4784 | __poll_t mask, task_work_func_t func) |
| 4785 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4786 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4787 | |
| 4788 | /* for instances that support it check for an event match first: */ |
| 4789 | if (mask && !(mask & poll->events)) |
| 4790 | return 0; |
| 4791 | |
| 4792 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4793 | |
| 4794 | list_del_init(&poll->wait.entry); |
| 4795 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4796 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 4797 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4798 | percpu_ref_get(&req->ctx->refs); |
| 4799 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4800 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4801 | * If this fails, then the task is exiting. When a task exits, the |
| 4802 | * work gets canceled, so just cancel this request as well instead |
| 4803 | * of executing it. We can't safely execute it anyway, as we may not |
| 4804 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4805 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 4806 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4807 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4808 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 4809 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4810 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4811 | return 1; |
| 4812 | } |
| 4813 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4814 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4815 | __acquires(&req->ctx->completion_lock) |
| 4816 | { |
| 4817 | struct io_ring_ctx *ctx = req->ctx; |
| 4818 | |
| 4819 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4820 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4821 | |
| 4822 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4823 | } |
| 4824 | |
| 4825 | spin_lock_irq(&ctx->completion_lock); |
| 4826 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4827 | add_wait_queue(poll->head, &poll->wait); |
| 4828 | return true; |
| 4829 | } |
| 4830 | |
| 4831 | return false; |
| 4832 | } |
| 4833 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4834 | 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] | 4835 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4836 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4837 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4838 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4839 | return req->apoll->double_poll; |
| 4840 | } |
| 4841 | |
| 4842 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 4843 | { |
| 4844 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4845 | return &req->poll; |
| 4846 | return &req->apoll->poll; |
| 4847 | } |
| 4848 | |
| 4849 | static void io_poll_remove_double(struct io_kiocb *req) |
| 4850 | { |
| 4851 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4852 | |
| 4853 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4854 | |
| 4855 | if (poll && poll->head) { |
| 4856 | struct wait_queue_head *head = poll->head; |
| 4857 | |
| 4858 | spin_lock(&head->lock); |
| 4859 | list_del_init(&poll->wait.entry); |
| 4860 | if (poll->wait.private) |
| 4861 | refcount_dec(&req->refs); |
| 4862 | poll->head = NULL; |
| 4863 | spin_unlock(&head->lock); |
| 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4868 | { |
| 4869 | struct io_ring_ctx *ctx = req->ctx; |
| 4870 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4871 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4872 | req->poll.done = true; |
| 4873 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4874 | io_commit_cqring(ctx); |
| 4875 | } |
| 4876 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4877 | static void io_poll_task_func(struct callback_head *cb) |
| 4878 | { |
| 4879 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4880 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4881 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4882 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4883 | if (io_poll_rewait(req, &req->poll)) { |
| 4884 | spin_unlock_irq(&ctx->completion_lock); |
| 4885 | } else { |
| 4886 | hash_del(&req->hash_node); |
| 4887 | io_poll_complete(req, req->result, 0); |
| 4888 | spin_unlock_irq(&ctx->completion_lock); |
| 4889 | |
| 4890 | nxt = io_put_req_find_next(req); |
| 4891 | io_cqring_ev_posted(ctx); |
| 4892 | if (nxt) |
| 4893 | __io_req_task_submit(nxt); |
| 4894 | } |
| 4895 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4896 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4900 | int sync, void *key) |
| 4901 | { |
| 4902 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4903 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4904 | __poll_t mask = key_to_poll(key); |
| 4905 | |
| 4906 | /* for instances that support it check for an event match first: */ |
| 4907 | if (mask && !(mask & poll->events)) |
| 4908 | return 0; |
| 4909 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 4910 | list_del_init(&wait->entry); |
| 4911 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4912 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4913 | bool done; |
| 4914 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4915 | spin_lock(&poll->head->lock); |
| 4916 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4917 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4918 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4919 | /* make sure double remove sees this as being gone */ |
| 4920 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4921 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 4922 | if (!done) { |
| 4923 | /* use wait func handler, so it matches the rq type */ |
| 4924 | poll->wait.func(&poll->wait, mode, sync, key); |
| 4925 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4926 | } |
| 4927 | refcount_dec(&req->refs); |
| 4928 | return 1; |
| 4929 | } |
| 4930 | |
| 4931 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4932 | wait_queue_func_t wake_func) |
| 4933 | { |
| 4934 | poll->head = NULL; |
| 4935 | poll->done = false; |
| 4936 | poll->canceled = false; |
| 4937 | poll->events = events; |
| 4938 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4939 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4940 | } |
| 4941 | |
| 4942 | 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] | 4943 | struct wait_queue_head *head, |
| 4944 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4945 | { |
| 4946 | struct io_kiocb *req = pt->req; |
| 4947 | |
| 4948 | /* |
| 4949 | * If poll->head is already set, it's because the file being polled |
| 4950 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4951 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4952 | */ |
| 4953 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 4954 | struct io_poll_iocb *poll_one = poll; |
| 4955 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4956 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4957 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4958 | pt->error = -EINVAL; |
| 4959 | return; |
| 4960 | } |
Jens Axboe | 1c3b3e6 | 2021-02-28 16:07:30 -0700 | [diff] [blame] | 4961 | /* double add on the same waitqueue head, ignore */ |
| 4962 | if (poll->head == head) |
| 4963 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4964 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 4965 | if (!poll) { |
| 4966 | pt->error = -ENOMEM; |
| 4967 | return; |
| 4968 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 4969 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4970 | refcount_inc(&req->refs); |
| 4971 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4972 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | pt->error = 0; |
| 4976 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 4977 | |
| 4978 | if (poll->events & EPOLLEXCLUSIVE) |
| 4979 | add_wait_queue_exclusive(head, &poll->wait); |
| 4980 | else |
| 4981 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 4985 | struct poll_table_struct *p) |
| 4986 | { |
| 4987 | 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] | 4988 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4989 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4990 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4991 | } |
| 4992 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4993 | static void io_async_task_func(struct callback_head *cb) |
| 4994 | { |
| 4995 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 4996 | struct async_poll *apoll = req->apoll; |
| 4997 | struct io_ring_ctx *ctx = req->ctx; |
| 4998 | |
| 4999 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5000 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5001 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5002 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5003 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5004 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5007 | /* 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] | 5008 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5009 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5010 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5011 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5012 | spin_unlock_irq(&ctx->completion_lock); |
| 5013 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5014 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5015 | __io_req_task_submit(req); |
| 5016 | else |
| 5017 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5018 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5019 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5020 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5021 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5025 | void *key) |
| 5026 | { |
| 5027 | struct io_kiocb *req = wait->private; |
| 5028 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5029 | |
| 5030 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5031 | key_to_poll(key)); |
| 5032 | |
| 5033 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5034 | } |
| 5035 | |
| 5036 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5037 | { |
| 5038 | struct io_ring_ctx *ctx = req->ctx; |
| 5039 | struct hlist_head *list; |
| 5040 | |
| 5041 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5042 | hlist_add_head(&req->hash_node, list); |
| 5043 | } |
| 5044 | |
| 5045 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5046 | struct io_poll_iocb *poll, |
| 5047 | struct io_poll_table *ipt, __poll_t mask, |
| 5048 | wait_queue_func_t wake_func) |
| 5049 | __acquires(&ctx->completion_lock) |
| 5050 | { |
| 5051 | struct io_ring_ctx *ctx = req->ctx; |
| 5052 | bool cancel = false; |
| 5053 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5054 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5055 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5056 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5057 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5058 | |
| 5059 | ipt->pt._key = mask; |
| 5060 | ipt->req = req; |
| 5061 | ipt->error = -EINVAL; |
| 5062 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5063 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5064 | |
| 5065 | spin_lock_irq(&ctx->completion_lock); |
| 5066 | if (likely(poll->head)) { |
| 5067 | spin_lock(&poll->head->lock); |
| 5068 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5069 | if (ipt->error) |
| 5070 | cancel = true; |
| 5071 | ipt->error = 0; |
| 5072 | mask = 0; |
| 5073 | } |
| 5074 | if (mask || ipt->error) |
| 5075 | list_del_init(&poll->wait.entry); |
| 5076 | else if (cancel) |
| 5077 | WRITE_ONCE(poll->canceled, true); |
| 5078 | else if (!poll->done) /* actually waiting for an event */ |
| 5079 | io_poll_req_insert(req); |
| 5080 | spin_unlock(&poll->head->lock); |
| 5081 | } |
| 5082 | |
| 5083 | return mask; |
| 5084 | } |
| 5085 | |
| 5086 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5087 | { |
| 5088 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5089 | struct io_ring_ctx *ctx = req->ctx; |
| 5090 | struct async_poll *apoll; |
| 5091 | struct io_poll_table ipt; |
| 5092 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5093 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5094 | |
| 5095 | if (!req->file || !file_can_poll(req->file)) |
| 5096 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5097 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5098 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5099 | if (def->pollin) |
| 5100 | rw = READ; |
| 5101 | else if (def->pollout) |
| 5102 | rw = WRITE; |
| 5103 | else |
| 5104 | return false; |
| 5105 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5106 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5107 | return false; |
| 5108 | |
| 5109 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5110 | if (unlikely(!apoll)) |
| 5111 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5112 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5113 | |
| 5114 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5115 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5116 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5117 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5118 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5119 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5120 | if (def->pollout) |
| 5121 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5122 | |
| 5123 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5124 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5125 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5126 | mask &= ~POLLIN; |
| 5127 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5128 | mask |= POLLERR | POLLPRI; |
| 5129 | |
| 5130 | ipt.pt._qproc = io_async_queue_proc; |
| 5131 | |
| 5132 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5133 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5134 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5135 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5136 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5137 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5138 | kfree(apoll); |
| 5139 | return false; |
| 5140 | } |
| 5141 | spin_unlock_irq(&ctx->completion_lock); |
| 5142 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5143 | apoll->poll.events); |
| 5144 | return true; |
| 5145 | } |
| 5146 | |
| 5147 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5148 | struct io_poll_iocb *poll) |
| 5149 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5150 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5151 | |
| 5152 | spin_lock(&poll->head->lock); |
| 5153 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5154 | if (!list_empty(&poll->wait.entry)) { |
| 5155 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5156 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5157 | } |
| 5158 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5159 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5160 | return do_complete; |
| 5161 | } |
| 5162 | |
| 5163 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5164 | { |
| 5165 | bool do_complete; |
| 5166 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5167 | io_poll_remove_double(req); |
| 5168 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5169 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5170 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5171 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5172 | struct async_poll *apoll = req->apoll; |
| 5173 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5174 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5175 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5176 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5177 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5178 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5179 | kfree(apoll); |
| 5180 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5181 | } |
| 5182 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5183 | if (do_complete) { |
| 5184 | io_cqring_fill_event(req, -ECANCELED); |
| 5185 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5186 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5187 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5193 | /* |
| 5194 | * Returns true if we found and killed one or more poll requests |
| 5195 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5196 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5197 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5198 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5199 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5200 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5201 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5202 | |
| 5203 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5204 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5205 | struct hlist_head *list; |
| 5206 | |
| 5207 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5208 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5209 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5210 | posted += io_poll_remove_one(req); |
| 5211 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5212 | } |
| 5213 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5214 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5215 | if (posted) |
| 5216 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5217 | |
| 5218 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5219 | } |
| 5220 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5221 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5222 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5223 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5224 | struct io_kiocb *req; |
| 5225 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5226 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5227 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5228 | if (sqe_addr != req->user_data) |
| 5229 | continue; |
| 5230 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5231 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5232 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | return -ENOENT; |
| 5236 | } |
| 5237 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5238 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5239 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5240 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5241 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5242 | return -EINVAL; |
| 5243 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5244 | sqe->poll_events) |
| 5245 | return -EINVAL; |
| 5246 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5247 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5248 | return 0; |
| 5249 | } |
| 5250 | |
| 5251 | /* |
| 5252 | * Find a running poll command that matches one specified in sqe->addr, |
| 5253 | * and remove it if found. |
| 5254 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5255 | 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] | 5256 | { |
| 5257 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5258 | int ret; |
| 5259 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5260 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5261 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5262 | spin_unlock_irq(&ctx->completion_lock); |
| 5263 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5264 | if (ret < 0) |
| 5265 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5266 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5267 | return 0; |
| 5268 | } |
| 5269 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5270 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5271 | void *key) |
| 5272 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5273 | struct io_kiocb *req = wait->private; |
| 5274 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5275 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5276 | 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] | 5277 | } |
| 5278 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5279 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5280 | struct poll_table_struct *p) |
| 5281 | { |
| 5282 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5283 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5284 | __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] | 5285 | } |
| 5286 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5287 | 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] | 5288 | { |
| 5289 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5290 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5291 | |
| 5292 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5293 | return -EINVAL; |
| 5294 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5295 | return -EINVAL; |
| 5296 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5297 | events = READ_ONCE(sqe->poll32_events); |
| 5298 | #ifdef __BIG_ENDIAN |
| 5299 | events = swahw32(events); |
| 5300 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5301 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5302 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5303 | return 0; |
| 5304 | } |
| 5305 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5306 | 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] | 5307 | { |
| 5308 | struct io_poll_iocb *poll = &req->poll; |
| 5309 | struct io_ring_ctx *ctx = req->ctx; |
| 5310 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5311 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5312 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5313 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5314 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5315 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5316 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5317 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5318 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5319 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5320 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5321 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5322 | spin_unlock_irq(&ctx->completion_lock); |
| 5323 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5324 | if (mask) { |
| 5325 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5326 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5327 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5328 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5329 | } |
| 5330 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5331 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5332 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5333 | struct io_timeout_data *data = container_of(timer, |
| 5334 | struct io_timeout_data, timer); |
| 5335 | struct io_kiocb *req = data->req; |
| 5336 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5337 | unsigned long flags; |
| 5338 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5339 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5340 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5341 | atomic_set(&req->ctx->cq_timeouts, |
| 5342 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5343 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5344 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5345 | io_commit_cqring(ctx); |
| 5346 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5347 | |
| 5348 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5349 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5350 | io_put_req(req); |
| 5351 | return HRTIMER_NORESTART; |
| 5352 | } |
| 5353 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5354 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5355 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5356 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5357 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5358 | struct io_kiocb *req; |
| 5359 | int ret = -ENOENT; |
| 5360 | |
| 5361 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5362 | if (user_data == req->user_data) { |
| 5363 | ret = 0; |
| 5364 | break; |
| 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5369 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5370 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5371 | io = req->async_data; |
| 5372 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5373 | if (ret == -1) |
| 5374 | return ERR_PTR(-EALREADY); |
| 5375 | list_del_init(&req->timeout.list); |
| 5376 | return req; |
| 5377 | } |
| 5378 | |
| 5379 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5380 | { |
| 5381 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5382 | |
| 5383 | if (IS_ERR(req)) |
| 5384 | return PTR_ERR(req); |
| 5385 | |
| 5386 | req_set_fail_links(req); |
| 5387 | io_cqring_fill_event(req, -ECANCELED); |
| 5388 | io_put_req_deferred(req, 1); |
| 5389 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5390 | } |
| 5391 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5392 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5393 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5394 | { |
| 5395 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5396 | struct io_timeout_data *data; |
| 5397 | |
| 5398 | if (IS_ERR(req)) |
| 5399 | return PTR_ERR(req); |
| 5400 | |
| 5401 | req->timeout.off = 0; /* noseq */ |
| 5402 | data = req->async_data; |
| 5403 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5404 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5405 | data->timer.function = io_timeout_fn; |
| 5406 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5407 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5410 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5411 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5412 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5413 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5414 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5415 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5416 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5417 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5418 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5419 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5420 | return -EINVAL; |
| 5421 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5422 | tr->addr = READ_ONCE(sqe->addr); |
| 5423 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5424 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5425 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5426 | return -EINVAL; |
| 5427 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5428 | return -EFAULT; |
| 5429 | } else if (tr->flags) { |
| 5430 | /* timeout removal doesn't support flags */ |
| 5431 | return -EINVAL; |
| 5432 | } |
| 5433 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5434 | return 0; |
| 5435 | } |
| 5436 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5437 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5438 | { |
| 5439 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5440 | : HRTIMER_MODE_REL; |
| 5441 | } |
| 5442 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5443 | /* |
| 5444 | * Remove or update an existing timeout command |
| 5445 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5446 | 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] | 5447 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5448 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5449 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5450 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5451 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5452 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5453 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5454 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5455 | else |
| 5456 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5457 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5458 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5459 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5460 | io_commit_cqring(ctx); |
| 5461 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5462 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5463 | if (ret < 0) |
| 5464 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5465 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5466 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5467 | } |
| 5468 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5469 | 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] | 5470 | bool is_timeout_link) |
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 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5473 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5474 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5475 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5476 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5477 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5478 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5479 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5480 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5481 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5482 | flags = READ_ONCE(sqe->timeout_flags); |
| 5483 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5484 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5485 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5486 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5487 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5488 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5489 | return -ENOMEM; |
| 5490 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5491 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5492 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5493 | |
| 5494 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5495 | return -EFAULT; |
| 5496 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5497 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5498 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5499 | return 0; |
| 5500 | } |
| 5501 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5502 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5503 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5504 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5505 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5506 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5507 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5508 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5509 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5510 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5511 | /* |
| 5512 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5513 | * timeout event to be satisfied. If it isn't set, then this is |
| 5514 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5515 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5516 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5517 | entry = ctx->timeout_list.prev; |
| 5518 | goto add; |
| 5519 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5520 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5521 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5522 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5523 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5524 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5525 | * This is safe because ->completion_lock is held, and submissions |
| 5526 | * and completions are never mixed in the same ->completion_lock section. |
| 5527 | */ |
| 5528 | ctx->cq_last_tm_flush = tail; |
| 5529 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5530 | /* |
| 5531 | * Insertion sort, ensuring the first entry in the list is always |
| 5532 | * the one we need first. |
| 5533 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5534 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5535 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5536 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5537 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5538 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5539 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5540 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5541 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5542 | break; |
| 5543 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5544 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5545 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5546 | data->timer.function = io_timeout_fn; |
| 5547 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5548 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5549 | return 0; |
| 5550 | } |
| 5551 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5552 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5553 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5554 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5555 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5556 | return req->user_data == (unsigned long) data; |
| 5557 | } |
| 5558 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5559 | 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] | 5560 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5561 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5562 | int ret = 0; |
| 5563 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5564 | if (!tctx->io_wq) |
| 5565 | return -ENOENT; |
| 5566 | |
| 5567 | 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] | 5568 | switch (cancel_ret) { |
| 5569 | case IO_WQ_CANCEL_OK: |
| 5570 | ret = 0; |
| 5571 | break; |
| 5572 | case IO_WQ_CANCEL_RUNNING: |
| 5573 | ret = -EALREADY; |
| 5574 | break; |
| 5575 | case IO_WQ_CANCEL_NOTFOUND: |
| 5576 | ret = -ENOENT; |
| 5577 | break; |
| 5578 | } |
| 5579 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5580 | return ret; |
| 5581 | } |
| 5582 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5583 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5584 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5585 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5586 | { |
| 5587 | unsigned long flags; |
| 5588 | int ret; |
| 5589 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5590 | ret = io_async_cancel_one(req->task->io_uring, |
| 5591 | (void *) (unsigned long) sqe_addr); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5592 | if (ret != -ENOENT) { |
| 5593 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5594 | goto done; |
| 5595 | } |
| 5596 | |
| 5597 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5598 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5599 | if (ret != -ENOENT) |
| 5600 | goto done; |
| 5601 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5602 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5603 | if (!ret) |
| 5604 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5605 | io_cqring_fill_event(req, ret); |
| 5606 | io_commit_cqring(ctx); |
| 5607 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5608 | io_cqring_ev_posted(ctx); |
| 5609 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5610 | if (ret < 0) |
| 5611 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5612 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5613 | } |
| 5614 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5615 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5616 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5617 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5618 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5619 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5620 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5621 | return -EINVAL; |
| 5622 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5623 | return -EINVAL; |
| 5624 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5625 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5626 | return 0; |
| 5627 | } |
| 5628 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5629 | 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] | 5630 | { |
| 5631 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5632 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5633 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5634 | return 0; |
| 5635 | } |
| 5636 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5637 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5638 | const struct io_uring_sqe *sqe) |
| 5639 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5640 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5641 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5642 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5643 | return -EINVAL; |
| 5644 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5645 | return -EINVAL; |
| 5646 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5647 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 5648 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 5649 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5650 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5651 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5652 | return 0; |
| 5653 | } |
| 5654 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5655 | 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] | 5656 | { |
| 5657 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5658 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5659 | int ret; |
| 5660 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5661 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5662 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5663 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5664 | up.offset = req->rsrc_update.offset; |
| 5665 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5666 | |
| 5667 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5668 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5669 | mutex_unlock(&ctx->uring_lock); |
| 5670 | |
| 5671 | if (ret < 0) |
| 5672 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5673 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5674 | return 0; |
| 5675 | } |
| 5676 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5677 | 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] | 5678 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5679 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5680 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5681 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5682 | case IORING_OP_READV: |
| 5683 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5684 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5685 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5686 | case IORING_OP_WRITEV: |
| 5687 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5688 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5689 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5690 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5691 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5692 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5693 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5694 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5695 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5696 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5697 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5698 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5699 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5700 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5701 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5702 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5703 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5704 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5705 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5706 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5707 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5708 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5709 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5710 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5711 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5712 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5713 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5714 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5715 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5716 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5717 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5718 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5719 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5720 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5721 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5722 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5723 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5724 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5725 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5726 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5727 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5728 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5729 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5730 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5731 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5732 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5733 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5734 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5735 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5736 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5737 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5738 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5739 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5740 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5741 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 5742 | case IORING_OP_SHUTDOWN: |
| 5743 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5744 | case IORING_OP_RENAMEAT: |
| 5745 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5746 | case IORING_OP_UNLINKAT: |
| 5747 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5750 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5751 | req->opcode); |
| 5752 | return-EINVAL; |
| 5753 | } |
| 5754 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5755 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5756 | { |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5757 | switch (req->opcode) { |
| 5758 | case IORING_OP_READV: |
| 5759 | case IORING_OP_READ_FIXED: |
| 5760 | case IORING_OP_READ: |
| 5761 | return io_rw_prep_async(req, READ); |
| 5762 | case IORING_OP_WRITEV: |
| 5763 | case IORING_OP_WRITE_FIXED: |
| 5764 | case IORING_OP_WRITE: |
| 5765 | return io_rw_prep_async(req, WRITE); |
| 5766 | case IORING_OP_SENDMSG: |
| 5767 | case IORING_OP_SEND: |
| 5768 | return io_sendmsg_prep_async(req); |
| 5769 | case IORING_OP_RECVMSG: |
| 5770 | case IORING_OP_RECV: |
| 5771 | return io_recvmsg_prep_async(req); |
| 5772 | case IORING_OP_CONNECT: |
| 5773 | return io_connect_prep_async(req); |
| 5774 | } |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5778 | static int io_req_defer_prep(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5779 | { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5780 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5781 | return 0; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5782 | /* some opcodes init it during the inital prep */ |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5783 | if (req->async_data) |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5784 | return 0; |
| 5785 | if (__io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5786 | return -EAGAIN; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5787 | return io_req_prep_async(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5790 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5791 | { |
| 5792 | struct io_kiocb *pos; |
| 5793 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5794 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5795 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5796 | io_for_each_link(pos, req) |
| 5797 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5798 | |
| 5799 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5800 | return total_submitted - nr_reqs; |
| 5801 | } |
| 5802 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5803 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5804 | { |
| 5805 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5806 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5807 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5808 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5809 | |
| 5810 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5811 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 5812 | !(req->flags & REQ_F_IO_DRAIN))) |
| 5813 | return 0; |
| 5814 | |
| 5815 | seq = io_get_sequence(req); |
| 5816 | /* Still a chance to pass the sequence check */ |
| 5817 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5818 | return 0; |
| 5819 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5820 | ret = io_req_defer_prep(req); |
| 5821 | if (ret) |
| 5822 | return ret; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 5823 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5824 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 5825 | if (!de) |
| 5826 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5827 | |
| 5828 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5829 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5830 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5831 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 5832 | io_queue_async_work(req); |
| 5833 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5837 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5838 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5839 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5840 | spin_unlock_irq(&ctx->completion_lock); |
| 5841 | return -EIOCBQUEUED; |
| 5842 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5843 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 5844 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5845 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5846 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 5847 | switch (req->opcode) { |
| 5848 | case IORING_OP_READV: |
| 5849 | case IORING_OP_READ_FIXED: |
| 5850 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5851 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5852 | break; |
| 5853 | case IORING_OP_RECVMSG: |
| 5854 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5855 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5856 | break; |
| 5857 | } |
| 5858 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5859 | } |
| 5860 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5861 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 5862 | switch (req->opcode) { |
| 5863 | case IORING_OP_READV: |
| 5864 | case IORING_OP_READ_FIXED: |
| 5865 | case IORING_OP_READ: |
| 5866 | case IORING_OP_WRITEV: |
| 5867 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5868 | case IORING_OP_WRITE: { |
| 5869 | struct io_async_rw *io = req->async_data; |
| 5870 | if (io->free_iovec) |
| 5871 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5872 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5873 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5874 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5875 | case IORING_OP_SENDMSG: { |
| 5876 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5877 | |
| 5878 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5879 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5880 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5881 | case IORING_OP_SPLICE: |
| 5882 | case IORING_OP_TEE: |
| 5883 | io_put_file(req, req->splice.file_in, |
| 5884 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5885 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 5886 | case IORING_OP_OPENAT: |
| 5887 | case IORING_OP_OPENAT2: |
| 5888 | if (req->open.filename) |
| 5889 | putname(req->open.filename); |
| 5890 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5891 | case IORING_OP_RENAMEAT: |
| 5892 | putname(req->rename.oldpath); |
| 5893 | putname(req->rename.newpath); |
| 5894 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5895 | case IORING_OP_UNLINKAT: |
| 5896 | putname(req->unlink.filename); |
| 5897 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5898 | } |
| 5899 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5900 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5901 | } |
| 5902 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5903 | 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] | 5904 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5905 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 5906 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5907 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5908 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 5909 | if (req->work.personality) { |
| 5910 | const struct cred *new_creds; |
| 5911 | |
| 5912 | if (!(issue_flags & IO_URING_F_NONBLOCK)) |
| 5913 | mutex_lock(&ctx->uring_lock); |
| 5914 | new_creds = idr_find(&ctx->personality_idr, req->work.personality); |
| 5915 | if (!(issue_flags & IO_URING_F_NONBLOCK)) |
| 5916 | mutex_unlock(&ctx->uring_lock); |
| 5917 | if (!new_creds) |
| 5918 | return -EINVAL; |
| 5919 | creds = override_creds(new_creds); |
| 5920 | } |
| 5921 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5922 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5923 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5924 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5925 | break; |
| 5926 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5927 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5928 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5929 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5930 | break; |
| 5931 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5932 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5933 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5934 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5935 | break; |
| 5936 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5937 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5938 | break; |
| 5939 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5940 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5941 | break; |
| 5942 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5943 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5944 | break; |
| 5945 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5946 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5947 | break; |
| 5948 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5949 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 5950 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5951 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5952 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5953 | break; |
| 5954 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5955 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 5956 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5957 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5958 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5959 | break; |
| 5960 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5961 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5962 | break; |
| 5963 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5964 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5965 | break; |
| 5966 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5967 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5968 | break; |
| 5969 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5970 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5971 | break; |
| 5972 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5973 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5974 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5975 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5976 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5977 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5978 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5979 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5980 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5981 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5982 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5983 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5984 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5985 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5986 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5987 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5988 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5989 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5990 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5991 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5992 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5993 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5994 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5995 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5996 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5997 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5998 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5999 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6000 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6001 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6002 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6003 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6004 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6005 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6006 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6007 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6008 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6009 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6010 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6011 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6012 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6013 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6014 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6015 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6016 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6017 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6018 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6019 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6020 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6021 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6022 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6023 | default: |
| 6024 | ret = -EINVAL; |
| 6025 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6026 | } |
| 6027 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6028 | if (creds) |
| 6029 | revert_creds(creds); |
| 6030 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6031 | if (ret) |
| 6032 | return ret; |
| 6033 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6034 | /* If the op doesn't have a file, we're not polling for it */ |
| 6035 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6036 | const bool in_async = io_wq_current_is_worker(); |
| 6037 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6038 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6039 | if (in_async) |
| 6040 | mutex_lock(&ctx->uring_lock); |
| 6041 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6042 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6043 | |
| 6044 | if (in_async) |
| 6045 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | return 0; |
| 6049 | } |
| 6050 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6051 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6052 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6053 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6054 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6055 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6056 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6057 | timeout = io_prep_linked_timeout(req); |
| 6058 | if (timeout) |
| 6059 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6060 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6061 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6062 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6063 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6064 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6065 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6066 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6067 | /* |
| 6068 | * We can get EAGAIN for polled IO even though we're |
| 6069 | * forcing a sync submission from here, since we can't |
| 6070 | * wait for request slots on the block side. |
| 6071 | */ |
| 6072 | if (ret != -EAGAIN) |
| 6073 | break; |
| 6074 | cond_resched(); |
| 6075 | } while (1); |
| 6076 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6077 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6078 | /* avoid locking problems by failing it from a clean context */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6079 | if (ret) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6080 | /* io-wq is going to take one down */ |
| 6081 | refcount_inc(&req->refs); |
| 6082 | io_req_task_queue_fail(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6083 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6084 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6085 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6086 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6087 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6088 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6089 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6090 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6091 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6092 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6093 | } |
| 6094 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6095 | static struct file *io_file_get(struct io_submit_state *state, |
| 6096 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6097 | { |
| 6098 | struct io_ring_ctx *ctx = req->ctx; |
| 6099 | struct file *file; |
| 6100 | |
| 6101 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6102 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6103 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6104 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6105 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6106 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6107 | } else { |
| 6108 | trace_io_uring_file_get(ctx, fd); |
| 6109 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6110 | } |
| 6111 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6112 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6113 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6114 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6115 | } |
| 6116 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6117 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6118 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6119 | struct io_timeout_data *data = container_of(timer, |
| 6120 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6121 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6122 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6123 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6124 | |
| 6125 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6126 | prev = req->timeout.head; |
| 6127 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6128 | |
| 6129 | /* |
| 6130 | * We don't expect the list to be empty, that will only happen if we |
| 6131 | * race with the completion of the linked work. |
| 6132 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6133 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6134 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6135 | else |
| 6136 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6137 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6138 | |
| 6139 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6140 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6141 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6142 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6143 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6144 | io_req_complete_post(req, -ETIME, 0); |
| 6145 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6146 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6147 | return HRTIMER_NORESTART; |
| 6148 | } |
| 6149 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6150 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6151 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6152 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6153 | * If the back reference is NULL, then our linked request finished |
| 6154 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6155 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6156 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6157 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6158 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6159 | data->timer.function = io_link_timeout_fn; |
| 6160 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6161 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6162 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6166 | { |
| 6167 | struct io_ring_ctx *ctx = req->ctx; |
| 6168 | |
| 6169 | spin_lock_irq(&ctx->completion_lock); |
| 6170 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6171 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6172 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6173 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6174 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6177 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6178 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6179 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6180 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6181 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6182 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6183 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6184 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6185 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6186 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6187 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6188 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6189 | } |
| 6190 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6191 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6192 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6193 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6194 | int ret; |
| 6195 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6196 | 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] | 6197 | |
| 6198 | /* |
| 6199 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6200 | * doesn't support non-blocking read/write attempts |
| 6201 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6202 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6203 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6204 | /* |
| 6205 | * Queued up for async execution, worker will release |
| 6206 | * submit reference when the iocb is actually submitted. |
| 6207 | */ |
| 6208 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6209 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6210 | } else if (likely(!ret)) { |
| 6211 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6212 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6213 | struct io_ring_ctx *ctx = req->ctx; |
| 6214 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6215 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6216 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6217 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6218 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6219 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6220 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6221 | } |
| 6222 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6223 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6224 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6225 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6226 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6227 | if (linked_timeout) |
| 6228 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6229 | } |
| 6230 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6231 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6232 | { |
| 6233 | int ret; |
| 6234 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6235 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6236 | if (ret) { |
| 6237 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6238 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6239 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6240 | io_put_req(req); |
| 6241 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6242 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6243 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6244 | ret = io_req_defer_prep(req); |
| 6245 | if (unlikely(ret)) |
| 6246 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6247 | io_queue_async_work(req); |
| 6248 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6249 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6250 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6251 | } |
| 6252 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6253 | /* |
| 6254 | * Check SQE restrictions (opcode and flags). |
| 6255 | * |
| 6256 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6257 | */ |
| 6258 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6259 | struct io_kiocb *req, |
| 6260 | unsigned int sqe_flags) |
| 6261 | { |
| 6262 | if (!ctx->restricted) |
| 6263 | return true; |
| 6264 | |
| 6265 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6266 | return false; |
| 6267 | |
| 6268 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6269 | ctx->restrictions.sqe_flags_required) |
| 6270 | return false; |
| 6271 | |
| 6272 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6273 | ctx->restrictions.sqe_flags_required)) |
| 6274 | return false; |
| 6275 | |
| 6276 | return true; |
| 6277 | } |
| 6278 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6279 | 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] | 6280 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6281 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6282 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6283 | unsigned int sqe_flags; |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6284 | int ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6285 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6286 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6287 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6288 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6289 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6290 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6291 | req->file = NULL; |
| 6292 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6293 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6294 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6295 | /* one is dropped after submission, the other at completion */ |
| 6296 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6297 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6298 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6299 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6300 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6301 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
| 6302 | req->flags = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6303 | return -EINVAL; |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6304 | } |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6305 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6306 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6307 | return -EINVAL; |
| 6308 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6309 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6310 | return -EACCES; |
| 6311 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6312 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6313 | !io_op_defs[req->opcode].buffer_select) |
| 6314 | return -EOPNOTSUPP; |
| 6315 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6316 | req->work.list.next = NULL; |
| 6317 | req->work.flags = 0; |
| 6318 | req->work.personality = READ_ONCE(sqe->personality); |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6319 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6320 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6321 | /* |
| 6322 | * Plug now if we have more than 1 IO left after this, and the target |
| 6323 | * is potentially a read/write to block based storage. |
| 6324 | */ |
| 6325 | if (!state->plug_started && state->ios_left > 1 && |
| 6326 | io_op_defs[req->opcode].plug) { |
| 6327 | blk_start_plug(&state->plug); |
| 6328 | state->plug_started = true; |
| 6329 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6330 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6331 | if (io_op_defs[req->opcode].needs_file) { |
| 6332 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6333 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6334 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6335 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6336 | ret = -EBADF; |
| 6337 | } |
| 6338 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6339 | state->ios_left--; |
| 6340 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6341 | } |
| 6342 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6343 | 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] | 6344 | const struct io_uring_sqe *sqe) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6345 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6346 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6347 | int ret; |
| 6348 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6349 | ret = io_init_req(ctx, req, sqe); |
| 6350 | if (unlikely(ret)) { |
| 6351 | fail_req: |
| 6352 | io_put_req(req); |
| 6353 | io_req_complete(req, ret); |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6354 | if (link->head) { |
| 6355 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6356 | link->head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6357 | io_put_req(link->head); |
| 6358 | io_req_complete(link->head, -ECANCELED); |
| 6359 | link->head = NULL; |
| 6360 | } |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6361 | return ret; |
| 6362 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6363 | ret = io_req_prep(req, sqe); |
| 6364 | if (unlikely(ret)) |
| 6365 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6366 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6367 | /* don't need @sqe from now on */ |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6368 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 6369 | true, ctx->flags & IORING_SETUP_SQPOLL); |
| 6370 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6371 | /* |
| 6372 | * If we already have a head request, queue this one for async |
| 6373 | * submittal once the head completes. If we don't have a head but |
| 6374 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6375 | * submitted sync once the chain is complete. If none of those |
| 6376 | * conditions are true (normal request), then just queue it. |
| 6377 | */ |
| 6378 | if (link->head) { |
| 6379 | struct io_kiocb *head = link->head; |
| 6380 | |
| 6381 | /* |
| 6382 | * Taking sequential execution of a link, draining both sides |
| 6383 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6384 | * requests in the link. So, it drains the head and the |
| 6385 | * next after the link request. The last one is done via |
| 6386 | * drain_next flag to persist the effect across calls. |
| 6387 | */ |
| 6388 | if (req->flags & REQ_F_IO_DRAIN) { |
| 6389 | head->flags |= REQ_F_IO_DRAIN; |
| 6390 | ctx->drain_next = 1; |
| 6391 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6392 | ret = io_req_defer_prep(req); |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6393 | if (unlikely(ret)) |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6394 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6395 | trace_io_uring_link(ctx, req, head); |
| 6396 | link->last->link = req; |
| 6397 | link->last = req; |
| 6398 | |
| 6399 | /* last request of a link, enqueue the link */ |
| 6400 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6401 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6402 | link->head = NULL; |
| 6403 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6404 | } else { |
| 6405 | if (unlikely(ctx->drain_next)) { |
| 6406 | req->flags |= REQ_F_IO_DRAIN; |
| 6407 | ctx->drain_next = 0; |
| 6408 | } |
| 6409 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6410 | link->head = req; |
| 6411 | link->last = req; |
| 6412 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6413 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6414 | } |
| 6415 | } |
| 6416 | |
| 6417 | return 0; |
| 6418 | } |
| 6419 | |
| 6420 | /* |
| 6421 | * Batched submission is done, ensure local IO is flushed out. |
| 6422 | */ |
| 6423 | static void io_submit_state_end(struct io_submit_state *state, |
| 6424 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6425 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6426 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6427 | io_queue_sqe(state->link.head); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6428 | if (state->comp.nr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6429 | io_submit_flush_completions(&state->comp, ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6430 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6431 | blk_finish_plug(&state->plug); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6432 | io_state_file_put(state); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6433 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6434 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6435 | /* |
| 6436 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6437 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6438 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6439 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6440 | { |
| 6441 | state->plug_started = false; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6442 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6443 | /* set only head, no need to init link_last in advance */ |
| 6444 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6445 | } |
| 6446 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6447 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6448 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6449 | struct io_rings *rings = ctx->rings; |
| 6450 | |
| 6451 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6452 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6453 | * since once we write the new head, the application could |
| 6454 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 6455 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6456 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6457 | } |
| 6458 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6459 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6460 | * 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] | 6461 | * that is mapped by userspace. This means that care needs to be taken to |
| 6462 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 6463 | * being a good citizen. If members of the sqe are validated and then later |
| 6464 | * 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] | 6465 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6466 | */ |
| 6467 | 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] | 6468 | { |
| 6469 | u32 *sq_array = ctx->sq_array; |
| 6470 | unsigned head; |
| 6471 | |
| 6472 | /* |
| 6473 | * The cached sq head (or cq tail) serves two purposes: |
| 6474 | * |
| 6475 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6476 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6477 | * 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] | 6478 | * though the application is the one updating it. |
| 6479 | */ |
| 6480 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
| 6481 | if (likely(head < ctx->sq_entries)) |
| 6482 | return &ctx->sq_sqes[head]; |
| 6483 | |
| 6484 | /* drop invalid entries */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6485 | ctx->cached_sq_dropped++; |
| 6486 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
| 6487 | return NULL; |
| 6488 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 6489 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6490 | 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] | 6491 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6492 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6493 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6494 | /* 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] | 6495 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6496 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6497 | return -EBUSY; |
| 6498 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6499 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6500 | /* make sure SQ entry isn't read before tail */ |
| 6501 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6502 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6503 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6504 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6505 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6506 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6507 | refcount_add(nr, ¤t->usage); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6508 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6509 | |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6510 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6511 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6512 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6513 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6514 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6515 | if (unlikely(!req)) { |
| 6516 | if (!submitted) |
| 6517 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6518 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6519 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6520 | sqe = io_get_sqe(ctx); |
| 6521 | if (unlikely(!sqe)) { |
| 6522 | kmem_cache_free(req_cachep, req); |
| 6523 | break; |
| 6524 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6525 | /* will complete beyond this point, count as submitted */ |
| 6526 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6527 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6528 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6529 | } |
| 6530 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6531 | if (unlikely(submitted != nr)) { |
| 6532 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6533 | struct io_uring_task *tctx = current->io_uring; |
| 6534 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6535 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6536 | percpu_ref_put_many(&ctx->refs, unused); |
| 6537 | percpu_counter_sub(&tctx->inflight, unused); |
| 6538 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6539 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6540 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6541 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6542 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6543 | io_commit_sqring(ctx); |
| 6544 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6545 | return submitted; |
| 6546 | } |
| 6547 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6548 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6549 | { |
| 6550 | /* Tell userspace we may need a wakeup call */ |
| 6551 | spin_lock_irq(&ctx->completion_lock); |
| 6552 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6553 | spin_unlock_irq(&ctx->completion_lock); |
| 6554 | } |
| 6555 | |
| 6556 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6557 | { |
| 6558 | spin_lock_irq(&ctx->completion_lock); |
| 6559 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6560 | spin_unlock_irq(&ctx->completion_lock); |
| 6561 | } |
| 6562 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6563 | 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] | 6564 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6565 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6566 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6567 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6568 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6569 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6570 | if (cap_entries && to_submit > 8) |
| 6571 | to_submit = 8; |
| 6572 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6573 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6574 | unsigned nr_events = 0; |
| 6575 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6576 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6577 | if (!list_empty(&ctx->iopoll_list)) |
| 6578 | io_do_iopoll(ctx, &nr_events, 0); |
| 6579 | |
Pavel Begunkov | 70aacfe | 2021-03-01 13:02:15 +0000 | [diff] [blame^] | 6580 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6581 | ret = io_submit_sqes(ctx, to_submit); |
| 6582 | mutex_unlock(&ctx->uring_lock); |
| 6583 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6584 | |
| 6585 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6586 | wake_up(&ctx->sqo_sq_wait); |
| 6587 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6588 | return ret; |
| 6589 | } |
| 6590 | |
| 6591 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 6592 | { |
| 6593 | struct io_ring_ctx *ctx; |
| 6594 | unsigned sq_thread_idle = 0; |
| 6595 | |
| 6596 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6597 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 6598 | sq_thread_idle = ctx->sq_thread_idle; |
| 6599 | } |
| 6600 | |
| 6601 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6602 | } |
| 6603 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6604 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 6605 | { |
| 6606 | struct io_ring_ctx *ctx; |
| 6607 | |
| 6608 | while (!list_empty(&sqd->ctx_new_list)) { |
| 6609 | 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] | 6610 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 6611 | complete(&ctx->sq_thread_comp); |
| 6612 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6613 | |
| 6614 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6615 | } |
| 6616 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6617 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
| 6618 | { |
| 6619 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 6620 | } |
| 6621 | |
| 6622 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) |
| 6623 | { |
| 6624 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 6625 | } |
| 6626 | |
| 6627 | static void io_sq_thread_parkme(struct io_sq_data *sqd) |
| 6628 | { |
| 6629 | for (;;) { |
| 6630 | /* |
| 6631 | * TASK_PARKED is a special state; we must serialize against |
| 6632 | * possible pending wakeups to avoid store-store collisions on |
| 6633 | * task->state. |
| 6634 | * |
| 6635 | * Such a collision might possibly result in the task state |
| 6636 | * changin from TASK_PARKED and us failing the |
| 6637 | * wait_task_inactive() in kthread_park(). |
| 6638 | */ |
| 6639 | set_special_state(TASK_PARKED); |
| 6640 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) |
| 6641 | break; |
| 6642 | |
| 6643 | /* |
| 6644 | * Thread is going to call schedule(), do not preempt it, |
| 6645 | * or the caller of kthread_park() may spend more time in |
| 6646 | * wait_task_inactive(). |
| 6647 | */ |
| 6648 | preempt_disable(); |
| 6649 | complete(&sqd->completion); |
| 6650 | schedule_preempt_disabled(); |
| 6651 | preempt_enable(); |
| 6652 | } |
| 6653 | __set_current_state(TASK_RUNNING); |
| 6654 | } |
| 6655 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6656 | static int io_sq_thread(void *data) |
| 6657 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6658 | struct io_sq_data *sqd = data; |
| 6659 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 6660 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6661 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6662 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6663 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6664 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
| 6665 | set_task_comm(current, buf); |
| 6666 | sqd->thread = current; |
| 6667 | current->pf_io_worker = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6668 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6669 | if (sqd->sq_cpu != -1) |
| 6670 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 6671 | else |
| 6672 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 6673 | current->flags |= PF_NO_SETAFFINITY; |
| 6674 | |
| 6675 | complete(&sqd->completion); |
| 6676 | |
| 6677 | wait_for_completion(&sqd->startup); |
| 6678 | |
| 6679 | while (!io_sq_thread_should_stop(sqd)) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6680 | int ret; |
| 6681 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6682 | |
| 6683 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6684 | * Any changes to the sqd lists are synchronized through the |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6685 | * thread parking. This synchronizes the thread vs users, |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6686 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6687 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6688 | if (io_sq_thread_should_park(sqd)) { |
| 6689 | io_sq_thread_parkme(sqd); |
| 6690 | continue; |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 6691 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6692 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6693 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6694 | timeout = jiffies + sqd->sq_thread_idle; |
| 6695 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6696 | if (fatal_signal_pending(current)) |
| 6697 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6698 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6699 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6700 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6701 | ret = __io_sq_thread(ctx, cap_entries); |
| 6702 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 6703 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6704 | } |
| 6705 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6706 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6707 | io_run_task_work(); |
| 6708 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6709 | if (sqt_spin) |
| 6710 | timeout = jiffies + sqd->sq_thread_idle; |
| 6711 | continue; |
| 6712 | } |
| 6713 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6714 | needs_sched = true; |
| 6715 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 6716 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6717 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6718 | !list_empty_careful(&ctx->iopoll_list)) { |
| 6719 | needs_sched = false; |
| 6720 | break; |
| 6721 | } |
| 6722 | if (io_sqring_entries(ctx)) { |
| 6723 | needs_sched = false; |
| 6724 | break; |
| 6725 | } |
| 6726 | } |
| 6727 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6728 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6729 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6730 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6731 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6732 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6733 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6734 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6735 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6736 | |
| 6737 | finish_wait(&sqd->wait, &wait); |
| 6738 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6739 | } |
| 6740 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6741 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6742 | io_uring_cancel_sqpoll(ctx); |
| 6743 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6744 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6745 | |
Jens Axboe | 8629397 | 2021-02-26 13:46:49 -0700 | [diff] [blame] | 6746 | if (io_sq_thread_should_park(sqd)) |
| 6747 | io_sq_thread_parkme(sqd); |
| 6748 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6749 | /* |
| 6750 | * Clear thread under lock so that concurrent parks work correctly |
| 6751 | */ |
Jens Axboe | 8629397 | 2021-02-26 13:46:49 -0700 | [diff] [blame] | 6752 | complete(&sqd->completion); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6753 | mutex_lock(&sqd->lock); |
| 6754 | sqd->thread = NULL; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 6755 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6756 | ctx->sqo_exec = 1; |
| 6757 | io_ring_set_wakeup_flag(ctx); |
| 6758 | } |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6759 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6760 | complete(&sqd->exited); |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 6761 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6762 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6763 | } |
| 6764 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6765 | struct io_wait_queue { |
| 6766 | struct wait_queue_entry wq; |
| 6767 | struct io_ring_ctx *ctx; |
| 6768 | unsigned to_wait; |
| 6769 | unsigned nr_timeouts; |
| 6770 | }; |
| 6771 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6772 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6773 | { |
| 6774 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6775 | |
| 6776 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6777 | * 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] | 6778 | * started waiting. For timeouts, we always want to return to userspace, |
| 6779 | * regardless of event count. |
| 6780 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6781 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6782 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6783 | } |
| 6784 | |
| 6785 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6786 | int wake_flags, void *key) |
| 6787 | { |
| 6788 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6789 | wq); |
| 6790 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6791 | /* |
| 6792 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 6793 | * the task, and the next invocation will do it. |
| 6794 | */ |
| 6795 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 6796 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6797 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6798 | } |
| 6799 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6800 | static int io_run_task_work_sig(void) |
| 6801 | { |
| 6802 | if (io_run_task_work()) |
| 6803 | return 1; |
| 6804 | if (!signal_pending(current)) |
| 6805 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 6806 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 6807 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6808 | return -EINTR; |
| 6809 | } |
| 6810 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6811 | /* when returns >0, the caller should retry */ |
| 6812 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 6813 | struct io_wait_queue *iowq, |
| 6814 | signed long *timeout) |
| 6815 | { |
| 6816 | int ret; |
| 6817 | |
| 6818 | /* make sure we run task_work before checking for signals */ |
| 6819 | ret = io_run_task_work_sig(); |
| 6820 | if (ret || io_should_wake(iowq)) |
| 6821 | return ret; |
| 6822 | /* let the caller flush overflows, retry */ |
| 6823 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 6824 | return 1; |
| 6825 | |
| 6826 | *timeout = schedule_timeout(*timeout); |
| 6827 | return !*timeout ? -ETIME : 1; |
| 6828 | } |
| 6829 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6830 | /* |
| 6831 | * Wait until events become available, if we don't already have some. The |
| 6832 | * application must reap them itself, as they reside on the shared cq ring. |
| 6833 | */ |
| 6834 | 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] | 6835 | const sigset_t __user *sig, size_t sigsz, |
| 6836 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6837 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6838 | struct io_wait_queue iowq = { |
| 6839 | .wq = { |
| 6840 | .private = current, |
| 6841 | .func = io_wake_function, |
| 6842 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6843 | }, |
| 6844 | .ctx = ctx, |
| 6845 | .to_wait = min_events, |
| 6846 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6847 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6848 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 6849 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6850 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6851 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6852 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 6853 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6854 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6855 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6856 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6857 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6858 | |
| 6859 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6860 | #ifdef CONFIG_COMPAT |
| 6861 | if (in_compat_syscall()) |
| 6862 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6863 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6864 | else |
| 6865 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6866 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6867 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6868 | if (ret) |
| 6869 | return ret; |
| 6870 | } |
| 6871 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6872 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6873 | struct timespec64 ts; |
| 6874 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6875 | if (get_timespec64(&ts, uts)) |
| 6876 | return -EFAULT; |
| 6877 | timeout = timespec64_to_jiffies(&ts); |
| 6878 | } |
| 6879 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6880 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6881 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6882 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6883 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6884 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6885 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6886 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 6887 | finish_wait(&ctx->wait, &iowq.wq); |
| 6888 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6889 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6890 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6891 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6892 | 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] | 6893 | } |
| 6894 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6895 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6896 | { |
| 6897 | #if defined(CONFIG_UNIX) |
| 6898 | if (ctx->ring_sock) { |
| 6899 | struct sock *sock = ctx->ring_sock->sk; |
| 6900 | struct sk_buff *skb; |
| 6901 | |
| 6902 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6903 | kfree_skb(skb); |
| 6904 | } |
| 6905 | #else |
| 6906 | int i; |
| 6907 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6908 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6909 | struct file *file; |
| 6910 | |
| 6911 | file = io_file_from_index(ctx, i); |
| 6912 | if (file) |
| 6913 | fput(file); |
| 6914 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6915 | #endif |
| 6916 | } |
| 6917 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 6918 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6919 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6920 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6921 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6922 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6923 | complete(&data->done); |
| 6924 | } |
| 6925 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6926 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 6927 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6928 | spin_lock_bh(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 6929 | } |
| 6930 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6931 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6932 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6933 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 6934 | } |
| 6935 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 6936 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 6937 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6938 | struct fixed_rsrc_ref_node *ref_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6939 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6940 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6941 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 6942 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6943 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6944 | percpu_ref_get(&rsrc_data->refs); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6945 | } |
| 6946 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6947 | 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] | 6948 | { |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6949 | struct fixed_rsrc_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6950 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6951 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 6952 | ref_node = data->node; |
Pavel Begunkov | e6cb007 | 2021-02-20 18:03:47 +0000 | [diff] [blame] | 6953 | data->node = NULL; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6954 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6955 | if (ref_node) |
| 6956 | percpu_ref_kill(&ref_node->refs); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6957 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6958 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6959 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 6960 | struct io_ring_ctx *ctx, |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6961 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 6962 | struct io_rsrc_put *prsrc)) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6963 | { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6964 | struct fixed_rsrc_ref_node *backup_node; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6965 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6966 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6967 | if (data->quiesce) |
| 6968 | return -ENXIO; |
| 6969 | |
| 6970 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 6971 | do { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6972 | ret = -ENOMEM; |
| 6973 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 6974 | if (!backup_node) |
| 6975 | break; |
| 6976 | backup_node->rsrc_data = data; |
| 6977 | backup_node->rsrc_put = rsrc_put; |
| 6978 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6979 | io_sqe_rsrc_kill_node(ctx, data); |
| 6980 | percpu_ref_kill(&data->refs); |
| 6981 | flush_delayed_work(&ctx->rsrc_put_work); |
| 6982 | |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 6983 | ret = wait_for_completion_interruptible(&data->done); |
| 6984 | if (!ret) |
| 6985 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6986 | |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 6987 | percpu_ref_resurrect(&data->refs); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6988 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
| 6989 | backup_node = NULL; |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 6990 | reinit_completion(&data->done); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6991 | mutex_unlock(&ctx->uring_lock); |
| 6992 | ret = io_run_task_work_sig(); |
| 6993 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6994 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6995 | data->quiesce = false; |
| 6996 | |
| 6997 | if (backup_node) |
| 6998 | destroy_fixed_rsrc_ref_node(backup_node); |
| 6999 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7000 | } |
| 7001 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7002 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
| 7003 | { |
| 7004 | struct fixed_rsrc_data *data; |
| 7005 | |
| 7006 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7007 | if (!data) |
| 7008 | return NULL; |
| 7009 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7010 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7011 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7012 | kfree(data); |
| 7013 | return NULL; |
| 7014 | } |
| 7015 | data->ctx = ctx; |
| 7016 | init_completion(&data->done); |
| 7017 | return data; |
| 7018 | } |
| 7019 | |
| 7020 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) |
| 7021 | { |
| 7022 | percpu_ref_exit(&data->refs); |
| 7023 | kfree(data->table); |
| 7024 | kfree(data); |
| 7025 | } |
| 7026 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7027 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7028 | { |
| 7029 | struct fixed_rsrc_data *data = ctx->file_data; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7030 | unsigned nr_tables, i; |
| 7031 | int ret; |
| 7032 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7033 | /* |
| 7034 | * percpu_ref_is_dying() is to stop parallel files unregister |
| 7035 | * Since we possibly drop uring lock later in this function to |
| 7036 | * run task work. |
| 7037 | */ |
| 7038 | if (!data || percpu_ref_is_dying(&data->refs)) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7039 | return -ENXIO; |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7040 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7041 | if (ret) |
| 7042 | return ret; |
| 7043 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7044 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7045 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7046 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7047 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7048 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7049 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7050 | ctx->nr_user_files = 0; |
| 7051 | return 0; |
| 7052 | } |
| 7053 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7054 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7055 | __releases(&sqd->lock) |
| 7056 | { |
| 7057 | if (!sqd->thread) |
| 7058 | return; |
| 7059 | if (sqd->thread == current) |
| 7060 | return; |
| 7061 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 7062 | wake_up_state(sqd->thread, TASK_PARKED); |
| 7063 | mutex_unlock(&sqd->lock); |
| 7064 | } |
| 7065 | |
| 7066 | static bool io_sq_thread_park(struct io_sq_data *sqd) |
| 7067 | __acquires(&sqd->lock) |
| 7068 | { |
| 7069 | if (sqd->thread == current) |
| 7070 | return true; |
| 7071 | mutex_lock(&sqd->lock); |
| 7072 | if (!sqd->thread) { |
| 7073 | mutex_unlock(&sqd->lock); |
| 7074 | return false; |
| 7075 | } |
| 7076 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 7077 | wake_up_process(sqd->thread); |
| 7078 | wait_for_completion(&sqd->completion); |
| 7079 | return true; |
| 7080 | } |
| 7081 | |
| 7082 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7083 | { |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 7084 | if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7085 | return; |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 7086 | mutex_lock(&sqd->lock); |
| 7087 | if (sqd->thread) { |
| 7088 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7089 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); |
| 7090 | wake_up_process(sqd->thread); |
| 7091 | mutex_unlock(&sqd->lock); |
| 7092 | wait_for_completion(&sqd->exited); |
| 7093 | WARN_ON_ONCE(sqd->thread); |
| 7094 | } else { |
| 7095 | mutex_unlock(&sqd->lock); |
| 7096 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7097 | } |
| 7098 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7099 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7100 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7101 | if (refcount_dec_and_test(&sqd->refs)) { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7102 | io_sq_thread_stop(sqd); |
| 7103 | kfree(sqd); |
| 7104 | } |
| 7105 | } |
| 7106 | |
| 7107 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7108 | { |
| 7109 | struct io_sq_data *sqd = ctx->sq_data; |
| 7110 | |
| 7111 | if (sqd) { |
Jens Axboe | eb85890 | 2021-02-25 10:13:29 -0700 | [diff] [blame] | 7112 | complete(&sqd->startup); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7113 | if (sqd->thread) { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7114 | wait_for_completion(&ctx->sq_thread_comp); |
| 7115 | io_sq_thread_park(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7116 | } |
| 7117 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7118 | mutex_lock(&sqd->ctx_lock); |
| 7119 | list_del(&ctx->sqd_list); |
| 7120 | io_sqd_update_thread_idle(sqd); |
| 7121 | mutex_unlock(&sqd->ctx_lock); |
| 7122 | |
| 7123 | if (sqd->thread) |
| 7124 | io_sq_thread_unpark(sqd); |
| 7125 | |
| 7126 | io_put_sq_data(sqd); |
| 7127 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7128 | } |
| 7129 | } |
| 7130 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7131 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7132 | { |
| 7133 | struct io_ring_ctx *ctx_attach; |
| 7134 | struct io_sq_data *sqd; |
| 7135 | struct fd f; |
| 7136 | |
| 7137 | f = fdget(p->wq_fd); |
| 7138 | if (!f.file) |
| 7139 | return ERR_PTR(-ENXIO); |
| 7140 | if (f.file->f_op != &io_uring_fops) { |
| 7141 | fdput(f); |
| 7142 | return ERR_PTR(-EINVAL); |
| 7143 | } |
| 7144 | |
| 7145 | ctx_attach = f.file->private_data; |
| 7146 | sqd = ctx_attach->sq_data; |
| 7147 | if (!sqd) { |
| 7148 | fdput(f); |
| 7149 | return ERR_PTR(-EINVAL); |
| 7150 | } |
| 7151 | |
| 7152 | refcount_inc(&sqd->refs); |
| 7153 | fdput(f); |
| 7154 | return sqd; |
| 7155 | } |
| 7156 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7157 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7158 | { |
| 7159 | struct io_sq_data *sqd; |
| 7160 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7161 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7162 | return io_attach_sq_data(p); |
| 7163 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7164 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7165 | if (!sqd) |
| 7166 | return ERR_PTR(-ENOMEM); |
| 7167 | |
| 7168 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7169 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7170 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7171 | mutex_init(&sqd->ctx_lock); |
| 7172 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7173 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7174 | init_completion(&sqd->startup); |
| 7175 | init_completion(&sqd->completion); |
| 7176 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7177 | return sqd; |
| 7178 | } |
| 7179 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7180 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7181 | /* |
| 7182 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7183 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7184 | * loops in the file referencing. |
| 7185 | */ |
| 7186 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7187 | { |
| 7188 | struct sock *sk = ctx->ring_sock->sk; |
| 7189 | struct scm_fp_list *fpl; |
| 7190 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7191 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7192 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7193 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7194 | if (!fpl) |
| 7195 | return -ENOMEM; |
| 7196 | |
| 7197 | skb = alloc_skb(0, GFP_KERNEL); |
| 7198 | if (!skb) { |
| 7199 | kfree(fpl); |
| 7200 | return -ENOMEM; |
| 7201 | } |
| 7202 | |
| 7203 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7204 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7205 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7206 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7207 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7208 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7209 | |
| 7210 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7211 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7212 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7213 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7214 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7215 | } |
| 7216 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7217 | if (nr_files) { |
| 7218 | fpl->max = SCM_MAX_FD; |
| 7219 | fpl->count = nr_files; |
| 7220 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7221 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7222 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7223 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7224 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7225 | for (i = 0; i < nr_files; i++) |
| 7226 | fput(fpl->fp[i]); |
| 7227 | } else { |
| 7228 | kfree_skb(skb); |
| 7229 | kfree(fpl); |
| 7230 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7231 | |
| 7232 | return 0; |
| 7233 | } |
| 7234 | |
| 7235 | /* |
| 7236 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7237 | * causes regular reference counting to break down. We rely on the UNIX |
| 7238 | * garbage collection to take care of this problem for us. |
| 7239 | */ |
| 7240 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7241 | { |
| 7242 | unsigned left, total; |
| 7243 | int ret = 0; |
| 7244 | |
| 7245 | total = 0; |
| 7246 | left = ctx->nr_user_files; |
| 7247 | while (left) { |
| 7248 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7249 | |
| 7250 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7251 | if (ret) |
| 7252 | break; |
| 7253 | left -= this_files; |
| 7254 | total += this_files; |
| 7255 | } |
| 7256 | |
| 7257 | if (!ret) |
| 7258 | return 0; |
| 7259 | |
| 7260 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7261 | struct file *file = io_file_from_index(ctx, total); |
| 7262 | |
| 7263 | if (file) |
| 7264 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7265 | total++; |
| 7266 | } |
| 7267 | |
| 7268 | return ret; |
| 7269 | } |
| 7270 | #else |
| 7271 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7272 | { |
| 7273 | return 0; |
| 7274 | } |
| 7275 | #endif |
| 7276 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7277 | 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] | 7278 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7279 | { |
| 7280 | int i; |
| 7281 | |
| 7282 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7283 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7284 | unsigned this_files; |
| 7285 | |
| 7286 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7287 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7288 | GFP_KERNEL); |
| 7289 | if (!table->files) |
| 7290 | break; |
| 7291 | nr_files -= this_files; |
| 7292 | } |
| 7293 | |
| 7294 | if (i == nr_tables) |
| 7295 | return 0; |
| 7296 | |
| 7297 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7298 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7299 | kfree(table->files); |
| 7300 | } |
| 7301 | return 1; |
| 7302 | } |
| 7303 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7304 | 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] | 7305 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7306 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7307 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7308 | struct sock *sock = ctx->ring_sock->sk; |
| 7309 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7310 | struct sk_buff *skb; |
| 7311 | int i; |
| 7312 | |
| 7313 | __skb_queue_head_init(&list); |
| 7314 | |
| 7315 | /* |
| 7316 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7317 | * remove this entry and rearrange the file array. |
| 7318 | */ |
| 7319 | skb = skb_dequeue(head); |
| 7320 | while (skb) { |
| 7321 | struct scm_fp_list *fp; |
| 7322 | |
| 7323 | fp = UNIXCB(skb).fp; |
| 7324 | for (i = 0; i < fp->count; i++) { |
| 7325 | int left; |
| 7326 | |
| 7327 | if (fp->fp[i] != file) |
| 7328 | continue; |
| 7329 | |
| 7330 | unix_notinflight(fp->user, fp->fp[i]); |
| 7331 | left = fp->count - 1 - i; |
| 7332 | if (left) { |
| 7333 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7334 | left * sizeof(struct file *)); |
| 7335 | } |
| 7336 | fp->count--; |
| 7337 | if (!fp->count) { |
| 7338 | kfree_skb(skb); |
| 7339 | skb = NULL; |
| 7340 | } else { |
| 7341 | __skb_queue_tail(&list, skb); |
| 7342 | } |
| 7343 | fput(file); |
| 7344 | file = NULL; |
| 7345 | break; |
| 7346 | } |
| 7347 | |
| 7348 | if (!file) |
| 7349 | break; |
| 7350 | |
| 7351 | __skb_queue_tail(&list, skb); |
| 7352 | |
| 7353 | skb = skb_dequeue(head); |
| 7354 | } |
| 7355 | |
| 7356 | if (skb_peek(&list)) { |
| 7357 | spin_lock_irq(&head->lock); |
| 7358 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7359 | __skb_queue_tail(head, skb); |
| 7360 | spin_unlock_irq(&head->lock); |
| 7361 | } |
| 7362 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7363 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7364 | #endif |
| 7365 | } |
| 7366 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7367 | 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] | 7368 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7369 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7370 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7371 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7372 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7373 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7374 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7375 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7376 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7377 | } |
| 7378 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7379 | percpu_ref_exit(&ref_node->refs); |
| 7380 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7381 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7382 | } |
| 7383 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7384 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7385 | { |
| 7386 | struct io_ring_ctx *ctx; |
| 7387 | struct llist_node *node; |
| 7388 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7389 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7390 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7391 | |
| 7392 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7393 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7394 | struct llist_node *next = node->next; |
| 7395 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7396 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7397 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7398 | node = next; |
| 7399 | } |
| 7400 | } |
| 7401 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7402 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7403 | unsigned i) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7404 | { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7405 | struct fixed_rsrc_table *table; |
| 7406 | |
| 7407 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7408 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7409 | } |
| 7410 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7411 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7412 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7413 | struct fixed_rsrc_ref_node *ref_node; |
| 7414 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7415 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7416 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7417 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7418 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7419 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7420 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7421 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7422 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7423 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7424 | ref_node->done = true; |
| 7425 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7426 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7427 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7428 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7429 | /* recycle ref nodes in order */ |
| 7430 | if (!ref_node->done) |
| 7431 | break; |
| 7432 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7433 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7434 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7435 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7436 | |
| 7437 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7438 | delay = 0; |
| 7439 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7440 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7441 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7442 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7443 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7444 | } |
| 7445 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7446 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7447 | struct io_ring_ctx *ctx) |
| 7448 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7449 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7450 | |
| 7451 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7452 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7453 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7454 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7455 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7456 | 0, GFP_KERNEL)) { |
| 7457 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7458 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7459 | } |
| 7460 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7461 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7462 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7463 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7464 | } |
| 7465 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7466 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7467 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7468 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7469 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7470 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7471 | } |
| 7472 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7473 | 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] | 7474 | { |
| 7475 | percpu_ref_exit(&ref_node->refs); |
| 7476 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7477 | } |
| 7478 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7479 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7480 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7481 | unsigned nr_args) |
| 7482 | { |
| 7483 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7484 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7485 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7486 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7487 | struct fixed_rsrc_ref_node *ref_node; |
| 7488 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7489 | |
| 7490 | if (ctx->file_data) |
| 7491 | return -EBUSY; |
| 7492 | if (!nr_args) |
| 7493 | return -EINVAL; |
| 7494 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7495 | return -EMFILE; |
| 7496 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7497 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7498 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7499 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7500 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7501 | |
| 7502 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7503 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7504 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7505 | if (!file_data->table) |
| 7506 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7507 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7508 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7509 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7510 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7511 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7512 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7513 | ret = -EFAULT; |
| 7514 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7515 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7516 | /* allow sparse sets */ |
| 7517 | if (fd == -1) |
| 7518 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7519 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7520 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7521 | ret = -EBADF; |
| 7522 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7523 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7524 | |
| 7525 | /* |
| 7526 | * Don't allow io_uring instances to be registered. If UNIX |
| 7527 | * isn't enabled, then this causes a reference cycle and this |
| 7528 | * instance can never get freed. If UNIX is enabled we'll |
| 7529 | * handle it just fine, but there's still no point in allowing |
| 7530 | * a ring fd as it doesn't support regular read/write anyway. |
| 7531 | */ |
| 7532 | if (file->f_op == &io_uring_fops) { |
| 7533 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7534 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7535 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7536 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7537 | } |
| 7538 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7539 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7540 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7541 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7542 | return ret; |
| 7543 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7544 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7545 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7546 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7547 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7548 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7549 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7550 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7551 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7552 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7553 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7554 | out_fput: |
| 7555 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7556 | file = io_file_from_index(ctx, i); |
| 7557 | if (file) |
| 7558 | fput(file); |
| 7559 | } |
| 7560 | for (i = 0; i < nr_tables; i++) |
| 7561 | kfree(file_data->table[i].files); |
| 7562 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7563 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7564 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7565 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7566 | return ret; |
| 7567 | } |
| 7568 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7569 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7570 | int index) |
| 7571 | { |
| 7572 | #if defined(CONFIG_UNIX) |
| 7573 | struct sock *sock = ctx->ring_sock->sk; |
| 7574 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7575 | struct sk_buff *skb; |
| 7576 | |
| 7577 | /* |
| 7578 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7579 | * file set. If there's no room, fall back to allocating a new skb |
| 7580 | * and filling it in. |
| 7581 | */ |
| 7582 | spin_lock_irq(&head->lock); |
| 7583 | skb = skb_peek(head); |
| 7584 | if (skb) { |
| 7585 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7586 | |
| 7587 | if (fpl->count < SCM_MAX_FD) { |
| 7588 | __skb_unlink(skb, head); |
| 7589 | spin_unlock_irq(&head->lock); |
| 7590 | fpl->fp[fpl->count] = get_file(file); |
| 7591 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7592 | fpl->count++; |
| 7593 | spin_lock_irq(&head->lock); |
| 7594 | __skb_queue_head(head, skb); |
| 7595 | } else { |
| 7596 | skb = NULL; |
| 7597 | } |
| 7598 | } |
| 7599 | spin_unlock_irq(&head->lock); |
| 7600 | |
| 7601 | if (skb) { |
| 7602 | fput(file); |
| 7603 | return 0; |
| 7604 | } |
| 7605 | |
| 7606 | return __io_sqe_files_scm(ctx, 1, index); |
| 7607 | #else |
| 7608 | return 0; |
| 7609 | #endif |
| 7610 | } |
| 7611 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7612 | 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] | 7613 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7614 | struct io_rsrc_put *prsrc; |
| 7615 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7616 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7617 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7618 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7619 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7620 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7621 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7622 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7623 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7624 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7625 | } |
| 7626 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7627 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7628 | struct file *file) |
| 7629 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7630 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7631 | } |
| 7632 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7633 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7634 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7635 | unsigned nr_args) |
| 7636 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7637 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7638 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7639 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7640 | __s32 __user *fds; |
| 7641 | int fd, i, err; |
| 7642 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7643 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7644 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7645 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7646 | return -EOVERFLOW; |
| 7647 | if (done > ctx->nr_user_files) |
| 7648 | return -EINVAL; |
| 7649 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7650 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7651 | if (!ref_node) |
| 7652 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7653 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7654 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7655 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7656 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7657 | err = 0; |
| 7658 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7659 | err = -EFAULT; |
| 7660 | break; |
| 7661 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 7662 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 7663 | continue; |
| 7664 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7665 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7666 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 7667 | |
| 7668 | if (*file_slot) { |
| 7669 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7670 | if (err) |
| 7671 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7672 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7673 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7674 | } |
| 7675 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7676 | file = fget(fd); |
| 7677 | if (!file) { |
| 7678 | err = -EBADF; |
| 7679 | break; |
| 7680 | } |
| 7681 | /* |
| 7682 | * Don't allow io_uring instances to be registered. If |
| 7683 | * UNIX isn't enabled, then this causes a reference |
| 7684 | * cycle and this instance can never get freed. If UNIX |
| 7685 | * is enabled we'll handle it just fine, but there's |
| 7686 | * still no point in allowing a ring fd as it doesn't |
| 7687 | * support regular read/write anyway. |
| 7688 | */ |
| 7689 | if (file->f_op == &io_uring_fops) { |
| 7690 | fput(file); |
| 7691 | err = -EBADF; |
| 7692 | break; |
| 7693 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 7694 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7695 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7696 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 7697 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7698 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7699 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7700 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7701 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7702 | } |
| 7703 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7704 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 7705 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7706 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7707 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7708 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7709 | |
| 7710 | return done ? done : err; |
| 7711 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7712 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7713 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7714 | unsigned nr_args) |
| 7715 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7716 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7717 | |
| 7718 | if (!ctx->file_data) |
| 7719 | return -ENXIO; |
| 7720 | if (!nr_args) |
| 7721 | return -EINVAL; |
| 7722 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7723 | return -EFAULT; |
| 7724 | if (up.resv) |
| 7725 | return -EINVAL; |
| 7726 | |
| 7727 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7728 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7729 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7730 | 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] | 7731 | { |
| 7732 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7733 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7734 | req = io_put_req_find_next(req); |
| 7735 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7736 | } |
| 7737 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7738 | 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] | 7739 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7740 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7741 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7742 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7743 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 7744 | hash = ctx->hash_map; |
| 7745 | if (!hash) { |
| 7746 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
| 7747 | if (!hash) |
| 7748 | return ERR_PTR(-ENOMEM); |
| 7749 | refcount_set(&hash->refs, 1); |
| 7750 | init_waitqueue_head(&hash->wait); |
| 7751 | ctx->hash_map = hash; |
| 7752 | } |
| 7753 | |
| 7754 | data.hash = hash; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7755 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7756 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7757 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7758 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7759 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7760 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7761 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7762 | } |
| 7763 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7764 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 7765 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7766 | { |
| 7767 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7768 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7769 | |
| 7770 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 7771 | if (unlikely(!tctx)) |
| 7772 | return -ENOMEM; |
| 7773 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7774 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 7775 | if (unlikely(ret)) { |
| 7776 | kfree(tctx); |
| 7777 | return ret; |
| 7778 | } |
| 7779 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7780 | tctx->io_wq = io_init_wq_offload(ctx); |
| 7781 | if (IS_ERR(tctx->io_wq)) { |
| 7782 | ret = PTR_ERR(tctx->io_wq); |
| 7783 | percpu_counter_destroy(&tctx->inflight); |
| 7784 | kfree(tctx); |
| 7785 | return ret; |
| 7786 | } |
| 7787 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7788 | xa_init(&tctx->xa); |
| 7789 | init_waitqueue_head(&tctx->wait); |
| 7790 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 7791 | atomic_set(&tctx->in_idle, 0); |
| 7792 | tctx->sqpoll = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7793 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 7794 | spin_lock_init(&tctx->task_lock); |
| 7795 | INIT_WQ_LIST(&tctx->task_list); |
| 7796 | tctx->task_state = 0; |
| 7797 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7798 | return 0; |
| 7799 | } |
| 7800 | |
| 7801 | void __io_uring_free(struct task_struct *tsk) |
| 7802 | { |
| 7803 | struct io_uring_task *tctx = tsk->io_uring; |
| 7804 | |
| 7805 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 7806 | WARN_ON_ONCE(tctx->io_wq); |
| 7807 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7808 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7809 | kfree(tctx); |
| 7810 | tsk->io_uring = NULL; |
| 7811 | } |
| 7812 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7813 | static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx) |
| 7814 | { |
| 7815 | int ret; |
| 7816 | |
| 7817 | clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7818 | reinit_completion(&sqd->completion); |
Pavel Begunkov | 70aacfe | 2021-03-01 13:02:15 +0000 | [diff] [blame^] | 7819 | ctx->sqo_exec = 0; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7820 | sqd->task_pid = current->pid; |
| 7821 | current->flags |= PF_IO_WORKER; |
| 7822 | ret = io_wq_fork_thread(io_sq_thread, sqd); |
| 7823 | current->flags &= ~PF_IO_WORKER; |
| 7824 | if (ret < 0) { |
| 7825 | sqd->thread = NULL; |
| 7826 | return ret; |
| 7827 | } |
| 7828 | wait_for_completion(&sqd->completion); |
| 7829 | return io_uring_alloc_task_context(sqd->thread, ctx); |
| 7830 | } |
| 7831 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7832 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 7833 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7834 | { |
| 7835 | int ret; |
| 7836 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7837 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 7838 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 7839 | IORING_SETUP_ATTACH_WQ) { |
| 7840 | struct fd f; |
| 7841 | |
| 7842 | f = fdget(p->wq_fd); |
| 7843 | if (!f.file) |
| 7844 | return -ENXIO; |
| 7845 | if (f.file->f_op != &io_uring_fops) { |
| 7846 | fdput(f); |
| 7847 | return -EINVAL; |
| 7848 | } |
| 7849 | fdput(f); |
| 7850 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7851 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7852 | struct io_sq_data *sqd; |
| 7853 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7854 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 7855 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7856 | goto err; |
| 7857 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7858 | sqd = io_get_sq_data(p); |
| 7859 | if (IS_ERR(sqd)) { |
| 7860 | ret = PTR_ERR(sqd); |
| 7861 | goto err; |
| 7862 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7863 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7864 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7865 | io_sq_thread_park(sqd); |
| 7866 | mutex_lock(&sqd->ctx_lock); |
| 7867 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 7868 | mutex_unlock(&sqd->ctx_lock); |
| 7869 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7870 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7871 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7872 | if (!ctx->sq_thread_idle) |
| 7873 | ctx->sq_thread_idle = HZ; |
| 7874 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7875 | if (sqd->thread) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7876 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7877 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7878 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7879 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7880 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7881 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7882 | if (cpu >= nr_cpu_ids) |
| 7883 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7884 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7885 | goto err; |
| 7886 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7887 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7888 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7889 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7890 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7891 | |
| 7892 | sqd->task_pid = current->pid; |
| 7893 | current->flags |= PF_IO_WORKER; |
| 7894 | ret = io_wq_fork_thread(io_sq_thread, sqd); |
| 7895 | current->flags &= ~PF_IO_WORKER; |
| 7896 | if (ret < 0) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7897 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7898 | goto err; |
| 7899 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7900 | wait_for_completion(&sqd->completion); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7901 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7902 | if (ret) |
| 7903 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7904 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7905 | /* Can't have SQ_AFF without SQPOLL */ |
| 7906 | ret = -EINVAL; |
| 7907 | goto err; |
| 7908 | } |
| 7909 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7910 | return 0; |
| 7911 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7912 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7913 | return ret; |
| 7914 | } |
| 7915 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7916 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 7917 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7918 | struct io_sq_data *sqd = ctx->sq_data; |
| 7919 | |
Jens Axboe | 3ebba79 | 2021-02-28 15:32:18 -0700 | [diff] [blame] | 7920 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
Jens Axboe | eb85890 | 2021-02-25 10:13:29 -0700 | [diff] [blame] | 7921 | if (ctx->flags & IORING_SETUP_SQPOLL) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7922 | complete(&sqd->startup); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7923 | } |
| 7924 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7925 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7926 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7927 | { |
| 7928 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7929 | } |
| 7930 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7931 | static inline int __io_account_mem(struct user_struct *user, |
| 7932 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7933 | { |
| 7934 | unsigned long page_limit, cur_pages, new_pages; |
| 7935 | |
| 7936 | /* Don't allow more pages than we can safely lock */ |
| 7937 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7938 | |
| 7939 | do { |
| 7940 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7941 | new_pages = cur_pages + nr_pages; |
| 7942 | if (new_pages > page_limit) |
| 7943 | return -ENOMEM; |
| 7944 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7945 | new_pages) != cur_pages); |
| 7946 | |
| 7947 | return 0; |
| 7948 | } |
| 7949 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7950 | 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] | 7951 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7952 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7953 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7954 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7955 | if (ctx->mm_account) |
| 7956 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7957 | } |
| 7958 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7959 | 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] | 7960 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7961 | int ret; |
| 7962 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7963 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7964 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7965 | if (ret) |
| 7966 | return ret; |
| 7967 | } |
| 7968 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7969 | if (ctx->mm_account) |
| 7970 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7971 | |
| 7972 | return 0; |
| 7973 | } |
| 7974 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7975 | static void io_mem_free(void *ptr) |
| 7976 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7977 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7978 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7979 | if (!ptr) |
| 7980 | return; |
| 7981 | |
| 7982 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7983 | if (put_page_testzero(page)) |
| 7984 | free_compound_page(page); |
| 7985 | } |
| 7986 | |
| 7987 | static void *io_mem_alloc(size_t size) |
| 7988 | { |
| 7989 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7990 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7991 | |
| 7992 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7993 | } |
| 7994 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7995 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7996 | size_t *sq_offset) |
| 7997 | { |
| 7998 | struct io_rings *rings; |
| 7999 | size_t off, sq_array_size; |
| 8000 | |
| 8001 | off = struct_size(rings, cqes, cq_entries); |
| 8002 | if (off == SIZE_MAX) |
| 8003 | return SIZE_MAX; |
| 8004 | |
| 8005 | #ifdef CONFIG_SMP |
| 8006 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8007 | if (off == 0) |
| 8008 | return SIZE_MAX; |
| 8009 | #endif |
| 8010 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8011 | if (sq_offset) |
| 8012 | *sq_offset = off; |
| 8013 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8014 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8015 | if (sq_array_size == SIZE_MAX) |
| 8016 | return SIZE_MAX; |
| 8017 | |
| 8018 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8019 | return SIZE_MAX; |
| 8020 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8021 | return off; |
| 8022 | } |
| 8023 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8024 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8025 | { |
| 8026 | int i, j; |
| 8027 | |
| 8028 | if (!ctx->user_bufs) |
| 8029 | return -ENXIO; |
| 8030 | |
| 8031 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8032 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8033 | |
| 8034 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8035 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8036 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8037 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8038 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8039 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8040 | imu->nr_bvecs = 0; |
| 8041 | } |
| 8042 | |
| 8043 | kfree(ctx->user_bufs); |
| 8044 | ctx->user_bufs = NULL; |
| 8045 | ctx->nr_user_bufs = 0; |
| 8046 | return 0; |
| 8047 | } |
| 8048 | |
| 8049 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8050 | void __user *arg, unsigned index) |
| 8051 | { |
| 8052 | struct iovec __user *src; |
| 8053 | |
| 8054 | #ifdef CONFIG_COMPAT |
| 8055 | if (ctx->compat) { |
| 8056 | struct compat_iovec __user *ciovs; |
| 8057 | struct compat_iovec ciov; |
| 8058 | |
| 8059 | ciovs = (struct compat_iovec __user *) arg; |
| 8060 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8061 | return -EFAULT; |
| 8062 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8063 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8064 | dst->iov_len = ciov.iov_len; |
| 8065 | return 0; |
| 8066 | } |
| 8067 | #endif |
| 8068 | src = (struct iovec __user *) arg; |
| 8069 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8070 | return -EFAULT; |
| 8071 | return 0; |
| 8072 | } |
| 8073 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8074 | /* |
| 8075 | * Not super efficient, but this is just a registration time. And we do cache |
| 8076 | * the last compound head, so generally we'll only do a full search if we don't |
| 8077 | * match that one. |
| 8078 | * |
| 8079 | * We check if the given compound head page has already been accounted, to |
| 8080 | * avoid double accounting it. This allows us to account the full size of the |
| 8081 | * page, not just the constituent pages of a huge page. |
| 8082 | */ |
| 8083 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8084 | int nr_pages, struct page *hpage) |
| 8085 | { |
| 8086 | int i, j; |
| 8087 | |
| 8088 | /* check current page array */ |
| 8089 | for (i = 0; i < nr_pages; i++) { |
| 8090 | if (!PageCompound(pages[i])) |
| 8091 | continue; |
| 8092 | if (compound_head(pages[i]) == hpage) |
| 8093 | return true; |
| 8094 | } |
| 8095 | |
| 8096 | /* check previously registered pages */ |
| 8097 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8098 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8099 | |
| 8100 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8101 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8102 | continue; |
| 8103 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8104 | return true; |
| 8105 | } |
| 8106 | } |
| 8107 | |
| 8108 | return false; |
| 8109 | } |
| 8110 | |
| 8111 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8112 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8113 | struct page **last_hpage) |
| 8114 | { |
| 8115 | int i, ret; |
| 8116 | |
| 8117 | for (i = 0; i < nr_pages; i++) { |
| 8118 | if (!PageCompound(pages[i])) { |
| 8119 | imu->acct_pages++; |
| 8120 | } else { |
| 8121 | struct page *hpage; |
| 8122 | |
| 8123 | hpage = compound_head(pages[i]); |
| 8124 | if (hpage == *last_hpage) |
| 8125 | continue; |
| 8126 | *last_hpage = hpage; |
| 8127 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8128 | continue; |
| 8129 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8130 | } |
| 8131 | } |
| 8132 | |
| 8133 | if (!imu->acct_pages) |
| 8134 | return 0; |
| 8135 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8136 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8137 | if (ret) |
| 8138 | imu->acct_pages = 0; |
| 8139 | return ret; |
| 8140 | } |
| 8141 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8142 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8143 | struct io_mapped_ubuf *imu, |
| 8144 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8145 | { |
| 8146 | struct vm_area_struct **vmas = NULL; |
| 8147 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8148 | unsigned long off, start, end, ubuf; |
| 8149 | size_t size; |
| 8150 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8151 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8152 | ubuf = (unsigned long) iov->iov_base; |
| 8153 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8154 | start = ubuf >> PAGE_SHIFT; |
| 8155 | nr_pages = end - start; |
| 8156 | |
| 8157 | ret = -ENOMEM; |
| 8158 | |
| 8159 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8160 | if (!pages) |
| 8161 | goto done; |
| 8162 | |
| 8163 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8164 | GFP_KERNEL); |
| 8165 | if (!vmas) |
| 8166 | goto done; |
| 8167 | |
| 8168 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8169 | GFP_KERNEL); |
| 8170 | if (!imu->bvec) |
| 8171 | goto done; |
| 8172 | |
| 8173 | ret = 0; |
| 8174 | mmap_read_lock(current->mm); |
| 8175 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8176 | pages, vmas); |
| 8177 | if (pret == nr_pages) { |
| 8178 | /* don't support file backed memory */ |
| 8179 | for (i = 0; i < nr_pages; i++) { |
| 8180 | struct vm_area_struct *vma = vmas[i]; |
| 8181 | |
| 8182 | if (vma->vm_file && |
| 8183 | !is_file_hugepages(vma->vm_file)) { |
| 8184 | ret = -EOPNOTSUPP; |
| 8185 | break; |
| 8186 | } |
| 8187 | } |
| 8188 | } else { |
| 8189 | ret = pret < 0 ? pret : -EFAULT; |
| 8190 | } |
| 8191 | mmap_read_unlock(current->mm); |
| 8192 | if (ret) { |
| 8193 | /* |
| 8194 | * if we did partial map, or found file backed vmas, |
| 8195 | * release any pages we did get |
| 8196 | */ |
| 8197 | if (pret > 0) |
| 8198 | unpin_user_pages(pages, pret); |
| 8199 | kvfree(imu->bvec); |
| 8200 | goto done; |
| 8201 | } |
| 8202 | |
| 8203 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8204 | if (ret) { |
| 8205 | unpin_user_pages(pages, pret); |
| 8206 | kvfree(imu->bvec); |
| 8207 | goto done; |
| 8208 | } |
| 8209 | |
| 8210 | off = ubuf & ~PAGE_MASK; |
| 8211 | size = iov->iov_len; |
| 8212 | for (i = 0; i < nr_pages; i++) { |
| 8213 | size_t vec_len; |
| 8214 | |
| 8215 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8216 | imu->bvec[i].bv_page = pages[i]; |
| 8217 | imu->bvec[i].bv_len = vec_len; |
| 8218 | imu->bvec[i].bv_offset = off; |
| 8219 | off = 0; |
| 8220 | size -= vec_len; |
| 8221 | } |
| 8222 | /* store original address for later verification */ |
| 8223 | imu->ubuf = ubuf; |
| 8224 | imu->len = iov->iov_len; |
| 8225 | imu->nr_bvecs = nr_pages; |
| 8226 | ret = 0; |
| 8227 | done: |
| 8228 | kvfree(pages); |
| 8229 | kvfree(vmas); |
| 8230 | return ret; |
| 8231 | } |
| 8232 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8233 | 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] | 8234 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8235 | if (ctx->user_bufs) |
| 8236 | return -EBUSY; |
| 8237 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8238 | return -EINVAL; |
| 8239 | |
| 8240 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8241 | GFP_KERNEL); |
| 8242 | if (!ctx->user_bufs) |
| 8243 | return -ENOMEM; |
| 8244 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8245 | return 0; |
| 8246 | } |
| 8247 | |
| 8248 | static int io_buffer_validate(struct iovec *iov) |
| 8249 | { |
| 8250 | /* |
| 8251 | * Don't impose further limits on the size and buffer |
| 8252 | * constraints here, we'll -EINVAL later when IO is |
| 8253 | * submitted if they are wrong. |
| 8254 | */ |
| 8255 | if (!iov->iov_base || !iov->iov_len) |
| 8256 | return -EFAULT; |
| 8257 | |
| 8258 | /* arbitrary limit, but we need something */ |
| 8259 | if (iov->iov_len > SZ_1G) |
| 8260 | return -EFAULT; |
| 8261 | |
| 8262 | return 0; |
| 8263 | } |
| 8264 | |
| 8265 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8266 | unsigned int nr_args) |
| 8267 | { |
| 8268 | int i, ret; |
| 8269 | struct iovec iov; |
| 8270 | struct page *last_hpage = NULL; |
| 8271 | |
| 8272 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8273 | if (ret) |
| 8274 | return ret; |
| 8275 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8276 | for (i = 0; i < nr_args; i++) { |
| 8277 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8278 | |
| 8279 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 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 | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8283 | ret = io_buffer_validate(&iov); |
| 8284 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8285 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8286 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8287 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8288 | if (ret) |
| 8289 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8290 | |
| 8291 | ctx->nr_user_bufs++; |
| 8292 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8293 | |
| 8294 | if (ret) |
| 8295 | io_sqe_buffers_unregister(ctx); |
| 8296 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8297 | return ret; |
| 8298 | } |
| 8299 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8300 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8301 | { |
| 8302 | __s32 __user *fds = arg; |
| 8303 | int fd; |
| 8304 | |
| 8305 | if (ctx->cq_ev_fd) |
| 8306 | return -EBUSY; |
| 8307 | |
| 8308 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8309 | return -EFAULT; |
| 8310 | |
| 8311 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8312 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8313 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8314 | ctx->cq_ev_fd = NULL; |
| 8315 | return ret; |
| 8316 | } |
| 8317 | |
| 8318 | return 0; |
| 8319 | } |
| 8320 | |
| 8321 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8322 | { |
| 8323 | if (ctx->cq_ev_fd) { |
| 8324 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8325 | ctx->cq_ev_fd = NULL; |
| 8326 | return 0; |
| 8327 | } |
| 8328 | |
| 8329 | return -ENXIO; |
| 8330 | } |
| 8331 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8332 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8333 | { |
| 8334 | struct io_ring_ctx *ctx = data; |
| 8335 | struct io_buffer *buf = p; |
| 8336 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8337 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8338 | return 0; |
| 8339 | } |
| 8340 | |
| 8341 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8342 | { |
| 8343 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8344 | idr_destroy(&ctx->io_buffer_idr); |
| 8345 | } |
| 8346 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8347 | 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] | 8348 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8349 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8350 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8351 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8352 | if (tsk && req->task != tsk) |
| 8353 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8354 | list_del(&req->compl.list); |
| 8355 | kmem_cache_free(req_cachep, req); |
| 8356 | } |
| 8357 | } |
| 8358 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8359 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8360 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8361 | struct io_submit_state *submit_state = &ctx->submit_state; |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8362 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8363 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8364 | mutex_lock(&ctx->uring_lock); |
| 8365 | |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8366 | if (submit_state->free_reqs) { |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8367 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8368 | submit_state->reqs); |
Pavel Begunkov | 8e5c66c | 2021-02-22 11:45:55 +0000 | [diff] [blame] | 8369 | submit_state->free_reqs = 0; |
| 8370 | } |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8371 | |
| 8372 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8373 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 8374 | cs->locked_free_nr = 0; |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8375 | spin_unlock_irq(&ctx->completion_lock); |
| 8376 | |
Pavel Begunkov | e5547d2 | 2021-02-23 22:17:20 +0000 | [diff] [blame] | 8377 | io_req_cache_free(&cs->free_list, NULL); |
| 8378 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8379 | mutex_unlock(&ctx->uring_lock); |
| 8380 | } |
| 8381 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8382 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8383 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8384 | /* |
| 8385 | * Some may use context even when all refs and requests have been put, |
| 8386 | * and they are free to do so while still holding uring_lock, see |
| 8387 | * __io_req_task_submit(). Wait for them to finish. |
| 8388 | */ |
| 8389 | mutex_lock(&ctx->uring_lock); |
| 8390 | mutex_unlock(&ctx->uring_lock); |
| 8391 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8392 | io_sq_thread_finish(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8393 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8394 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8395 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8396 | mmdrop(ctx->mm_account); |
| 8397 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8398 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8399 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8400 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8401 | io_sqe_files_unregister(ctx); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8402 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8403 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8404 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8405 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8406 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8407 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8408 | if (ctx->ring_sock) { |
| 8409 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8410 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8411 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8412 | #endif |
| 8413 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8414 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8415 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8416 | |
| 8417 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8418 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 8419 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8420 | if (ctx->hash_map) |
| 8421 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8422 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8423 | kfree(ctx); |
| 8424 | } |
| 8425 | |
| 8426 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8427 | { |
| 8428 | struct io_ring_ctx *ctx = file->private_data; |
| 8429 | __poll_t mask = 0; |
| 8430 | |
| 8431 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8432 | /* |
| 8433 | * synchronizes with barrier from wq_has_sleeper call in |
| 8434 | * io_commit_cqring |
| 8435 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8436 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8437 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8438 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8439 | |
| 8440 | /* |
| 8441 | * Don't flush cqring overflow list here, just do a simple check. |
| 8442 | * Otherwise there could possible be ABBA deadlock: |
| 8443 | * CPU0 CPU1 |
| 8444 | * ---- ---- |
| 8445 | * lock(&ctx->uring_lock); |
| 8446 | * lock(&ep->mtx); |
| 8447 | * lock(&ctx->uring_lock); |
| 8448 | * lock(&ep->mtx); |
| 8449 | * |
| 8450 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8451 | * pushs them to do the flush. |
| 8452 | */ |
| 8453 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8454 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8455 | |
| 8456 | return mask; |
| 8457 | } |
| 8458 | |
| 8459 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8460 | { |
| 8461 | struct io_ring_ctx *ctx = file->private_data; |
| 8462 | |
| 8463 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8464 | } |
| 8465 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8466 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8467 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8468 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8469 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8470 | creds = idr_remove(&ctx->personality_idr, id); |
| 8471 | if (creds) { |
| 8472 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8473 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8474 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8475 | |
| 8476 | return -EINVAL; |
| 8477 | } |
| 8478 | |
| 8479 | static int io_remove_personalities(int id, void *p, void *data) |
| 8480 | { |
| 8481 | struct io_ring_ctx *ctx = data; |
| 8482 | |
| 8483 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8484 | return 0; |
| 8485 | } |
| 8486 | |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8487 | static bool io_run_ctx_fallback(struct io_ring_ctx *ctx) |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8488 | { |
Pavel Begunkov | 28c4721 | 2021-02-28 22:04:54 +0000 | [diff] [blame] | 8489 | struct callback_head *work, *next; |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8490 | bool executed = false; |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8491 | |
| 8492 | do { |
Pavel Begunkov | 28c4721 | 2021-02-28 22:04:54 +0000 | [diff] [blame] | 8493 | work = xchg(&ctx->exit_task_work, NULL); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 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); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8522 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8523 | io_ring_ctx_free(ctx); |
| 8524 | } |
| 8525 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8526 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8527 | { |
| 8528 | mutex_lock(&ctx->uring_lock); |
| 8529 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8530 | /* if force is set, the ring is going away. always drop after that */ |
| 8531 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8532 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8533 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8534 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8535 | mutex_unlock(&ctx->uring_lock); |
| 8536 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8537 | io_kill_timeouts(ctx, NULL, NULL); |
| 8538 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8539 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8540 | /* 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] | 8541 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8542 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8543 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8544 | /* |
| 8545 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8546 | * if we're exiting a ton of rings at the same time. It just adds |
| 8547 | * noise and overhead, there's no discernable change in runtime |
| 8548 | * over using system_wq. |
| 8549 | */ |
| 8550 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8551 | } |
| 8552 | |
| 8553 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8554 | { |
| 8555 | struct io_ring_ctx *ctx = file->private_data; |
| 8556 | |
| 8557 | file->private_data = NULL; |
| 8558 | io_ring_ctx_wait_and_kill(ctx); |
| 8559 | return 0; |
| 8560 | } |
| 8561 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8562 | struct io_task_cancel { |
| 8563 | struct task_struct *task; |
| 8564 | struct files_struct *files; |
| 8565 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8566 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8567 | 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] | 8568 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8569 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8570 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8571 | bool ret; |
| 8572 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8573 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8574 | unsigned long flags; |
| 8575 | struct io_ring_ctx *ctx = req->ctx; |
| 8576 | |
| 8577 | /* protect against races with linked timeouts */ |
| 8578 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8579 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8580 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8581 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8582 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8583 | } |
| 8584 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8585 | } |
| 8586 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8587 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8588 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8589 | struct files_struct *files) |
| 8590 | { |
| 8591 | struct io_defer_entry *de = NULL; |
| 8592 | LIST_HEAD(list); |
| 8593 | |
| 8594 | spin_lock_irq(&ctx->completion_lock); |
| 8595 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8596 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8597 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8598 | break; |
| 8599 | } |
| 8600 | } |
| 8601 | spin_unlock_irq(&ctx->completion_lock); |
| 8602 | |
| 8603 | while (!list_empty(&list)) { |
| 8604 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8605 | list_del_init(&de->list); |
| 8606 | req_set_fail_links(de->req); |
| 8607 | io_put_req(de->req); |
| 8608 | io_req_complete(de->req, -ECANCELED); |
| 8609 | kfree(de); |
| 8610 | } |
| 8611 | } |
| 8612 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8613 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8614 | struct task_struct *task, |
| 8615 | struct files_struct *files) |
| 8616 | { |
| 8617 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8618 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8619 | |
| 8620 | while (1) { |
| 8621 | enum io_wq_cancel cret; |
| 8622 | bool ret = false; |
| 8623 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8624 | if (tctx && tctx->io_wq) { |
| 8625 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8626 | &cancel, true); |
| 8627 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8628 | } |
| 8629 | |
| 8630 | /* SQPOLL thread does its own polling */ |
| 8631 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8632 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8633 | io_iopoll_try_reap_events(ctx); |
| 8634 | ret = true; |
| 8635 | } |
| 8636 | } |
| 8637 | |
| 8638 | ret |= io_poll_remove_all(ctx, task, files); |
| 8639 | ret |= io_kill_timeouts(ctx, task, files); |
| 8640 | ret |= io_run_task_work(); |
Pavel Begunkov | ba50a03 | 2021-02-26 15:47:56 +0000 | [diff] [blame] | 8641 | ret |= io_run_ctx_fallback(ctx); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8642 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8643 | if (!ret) |
| 8644 | break; |
| 8645 | cond_resched(); |
| 8646 | } |
| 8647 | } |
| 8648 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8649 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8650 | struct task_struct *task, |
| 8651 | struct files_struct *files) |
| 8652 | { |
| 8653 | struct io_kiocb *req; |
| 8654 | int cnt = 0; |
| 8655 | |
| 8656 | spin_lock_irq(&ctx->inflight_lock); |
| 8657 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8658 | cnt += io_match_task(req, task, files); |
| 8659 | spin_unlock_irq(&ctx->inflight_lock); |
| 8660 | return cnt; |
| 8661 | } |
| 8662 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8663 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8664 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8665 | struct files_struct *files) |
| 8666 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8667 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8668 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8669 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8670 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8671 | inflight = io_uring_count_inflight(ctx, task, files); |
| 8672 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8673 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8674 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8675 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8676 | |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 8677 | if (ctx->sq_data) |
| 8678 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8679 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 8680 | TASK_UNINTERRUPTIBLE); |
| 8681 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 8682 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8683 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 8684 | if (ctx->sq_data) |
| 8685 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8686 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8687 | } |
| 8688 | |
| 8689 | /* |
| 8690 | * We need to iteratively cancel requests, in case a request has dependent |
| 8691 | * hard links. These persist even for failure of cancelations, hence keep |
| 8692 | * looping until none are found. |
| 8693 | */ |
| 8694 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8695 | struct files_struct *files) |
| 8696 | { |
| 8697 | struct task_struct *task = current; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8698 | bool did_park = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8699 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8700 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | 70aacfe | 2021-03-01 13:02:15 +0000 | [diff] [blame^] | 8701 | /* never started, nothing to cancel */ |
| 8702 | if (ctx->flags & IORING_SETUP_R_DISABLED) { |
| 8703 | io_sq_offload_start(ctx); |
| 8704 | return; |
| 8705 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8706 | did_park = io_sq_thread_park(ctx->sq_data); |
| 8707 | if (did_park) { |
| 8708 | task = ctx->sq_data->thread; |
| 8709 | atomic_inc(&task->io_uring->in_idle); |
| 8710 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8711 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8712 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8713 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8714 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 8715 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8716 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8717 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8718 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8719 | if (did_park) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8720 | atomic_dec(&task->io_uring->in_idle); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8721 | io_sq_thread_unpark(ctx->sq_data); |
| 8722 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8723 | } |
| 8724 | |
| 8725 | /* |
| 8726 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8727 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8728 | 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] | 8729 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8730 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8731 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8732 | |
| 8733 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8734 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8735 | if (unlikely(ret)) |
| 8736 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8737 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8738 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8739 | if (tctx->last != file) { |
| 8740 | void *old = xa_load(&tctx->xa, (unsigned long)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 | if (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8743 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8744 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 8745 | file, GFP_KERNEL)); |
| 8746 | if (ret) { |
| 8747 | fput(file); |
| 8748 | return ret; |
| 8749 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 8750 | |
| 8751 | /* one and only SQPOLL file note, held by sqo_task */ |
| 8752 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 8753 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8754 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8755 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8756 | } |
| 8757 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8758 | /* |
| 8759 | * This is race safe in that the task itself is doing this, hence it |
| 8760 | * cannot be going through the exit/cancel paths at the same time. |
| 8761 | * This cannot be modified while exit/cancel is running. |
| 8762 | */ |
| 8763 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 8764 | tctx->sqpoll = true; |
| 8765 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8766 | return 0; |
| 8767 | } |
| 8768 | |
| 8769 | /* |
| 8770 | * Remove this io_uring_file -> task mapping. |
| 8771 | */ |
| 8772 | static void io_uring_del_task_file(struct file *file) |
| 8773 | { |
| 8774 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8775 | |
| 8776 | if (tctx->last == file) |
| 8777 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 8778 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8779 | if (file) |
| 8780 | fput(file); |
| 8781 | } |
| 8782 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8783 | static void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8784 | { |
| 8785 | struct file *file; |
| 8786 | unsigned long index; |
| 8787 | |
| 8788 | xa_for_each(&tctx->xa, index, file) |
| 8789 | io_uring_del_task_file(file); |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8790 | if (tctx->io_wq) { |
| 8791 | io_wq_put_and_exit(tctx->io_wq); |
| 8792 | tctx->io_wq = NULL; |
| 8793 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8794 | } |
| 8795 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8796 | void __io_uring_files_cancel(struct files_struct *files) |
| 8797 | { |
| 8798 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 8799 | struct file *file; |
| 8800 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8801 | |
| 8802 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8803 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8804 | xa_for_each(&tctx->xa, index, file) |
| 8805 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8806 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8807 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8808 | if (files) |
| 8809 | io_uring_clean_tctx(tctx); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 8813 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8814 | return percpu_counter_sum(&tctx->inflight); |
| 8815 | } |
| 8816 | |
| 8817 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 8818 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8819 | struct io_sq_data *sqd = ctx->sq_data; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8820 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8821 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8822 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8823 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8824 | if (!sqd) |
| 8825 | return; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8826 | if (!io_sq_thread_park(sqd)) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8827 | return; |
| 8828 | tctx = ctx->sq_data->thread->io_uring; |
Jens Axboe | e54945a | 2021-02-26 11:27:15 -0700 | [diff] [blame] | 8829 | /* can happen on fork/alloc failure, just ignore that state */ |
| 8830 | if (!tctx) { |
| 8831 | io_sq_thread_unpark(sqd); |
| 8832 | return; |
| 8833 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8834 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8835 | atomic_inc(&tctx->in_idle); |
| 8836 | do { |
| 8837 | /* read completions before cancelations */ |
| 8838 | inflight = tctx_inflight(tctx); |
| 8839 | if (!inflight) |
| 8840 | break; |
| 8841 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8842 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8843 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8844 | /* |
| 8845 | * If we've seen completions, retry without waiting. This |
| 8846 | * avoids a race where a completion comes in before we did |
| 8847 | * prepare_to_wait(). |
| 8848 | */ |
| 8849 | if (inflight == tctx_inflight(tctx)) |
| 8850 | schedule(); |
| 8851 | finish_wait(&tctx->wait, &wait); |
| 8852 | } while (1); |
| 8853 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8854 | io_sq_thread_unpark(sqd); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8855 | } |
| 8856 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8857 | /* |
| 8858 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 8859 | * requests. |
| 8860 | */ |
| 8861 | void __io_uring_task_cancel(void) |
| 8862 | { |
| 8863 | struct io_uring_task *tctx = current->io_uring; |
| 8864 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8865 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8866 | |
| 8867 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8868 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8869 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8870 | if (tctx->sqpoll) { |
| 8871 | struct file *file; |
| 8872 | unsigned long index; |
| 8873 | |
| 8874 | xa_for_each(&tctx->xa, index, file) |
| 8875 | io_uring_cancel_sqpoll(file->private_data); |
| 8876 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 8877 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8878 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8879 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8880 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8881 | if (!inflight) |
| 8882 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8883 | __io_uring_files_cancel(NULL); |
| 8884 | |
| 8885 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8886 | |
| 8887 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 8888 | * If we've seen completions, retry without waiting. This |
| 8889 | * avoids a race where a completion comes in before we did |
| 8890 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8891 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 8892 | if (inflight == tctx_inflight(tctx)) |
| 8893 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 8894 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8895 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8896 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8897 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8898 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 8899 | io_uring_clean_tctx(tctx); |
| 8900 | /* all current's requests should be gone, we can kill tctx */ |
| 8901 | __io_uring_free(current); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8902 | } |
| 8903 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8904 | static void *io_uring_validate_mmap_request(struct file *file, |
| 8905 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8906 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8907 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8908 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8909 | struct page *page; |
| 8910 | void *ptr; |
| 8911 | |
| 8912 | switch (offset) { |
| 8913 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8914 | case IORING_OFF_CQ_RING: |
| 8915 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8916 | break; |
| 8917 | case IORING_OFF_SQES: |
| 8918 | ptr = ctx->sq_sqes; |
| 8919 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8920 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8921 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8922 | } |
| 8923 | |
| 8924 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 8925 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8926 | return ERR_PTR(-EINVAL); |
| 8927 | |
| 8928 | return ptr; |
| 8929 | } |
| 8930 | |
| 8931 | #ifdef CONFIG_MMU |
| 8932 | |
| 8933 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8934 | { |
| 8935 | size_t sz = vma->vm_end - vma->vm_start; |
| 8936 | unsigned long pfn; |
| 8937 | void *ptr; |
| 8938 | |
| 8939 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 8940 | if (IS_ERR(ptr)) |
| 8941 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8942 | |
| 8943 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 8944 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 8945 | } |
| 8946 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8947 | #else /* !CONFIG_MMU */ |
| 8948 | |
| 8949 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8950 | { |
| 8951 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 8952 | } |
| 8953 | |
| 8954 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 8955 | { |
| 8956 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 8957 | } |
| 8958 | |
| 8959 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 8960 | unsigned long addr, unsigned long len, |
| 8961 | unsigned long pgoff, unsigned long flags) |
| 8962 | { |
| 8963 | void *ptr; |
| 8964 | |
| 8965 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 8966 | if (IS_ERR(ptr)) |
| 8967 | return PTR_ERR(ptr); |
| 8968 | |
| 8969 | return (unsigned long) ptr; |
| 8970 | } |
| 8971 | |
| 8972 | #endif /* !CONFIG_MMU */ |
| 8973 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8974 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8975 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8976 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8977 | DEFINE_WAIT(wait); |
| 8978 | |
| 8979 | do { |
| 8980 | if (!io_sqring_full(ctx)) |
| 8981 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8982 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 8983 | |
| 8984 | if (!io_sqring_full(ctx)) |
| 8985 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8986 | schedule(); |
| 8987 | } while (!signal_pending(current)); |
| 8988 | |
| 8989 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8990 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8991 | } |
| 8992 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 8993 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 8994 | struct __kernel_timespec __user **ts, |
| 8995 | const sigset_t __user **sig) |
| 8996 | { |
| 8997 | struct io_uring_getevents_arg arg; |
| 8998 | |
| 8999 | /* |
| 9000 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9001 | * is just a pointer to the sigset_t. |
| 9002 | */ |
| 9003 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9004 | *sig = (const sigset_t __user *) argp; |
| 9005 | *ts = NULL; |
| 9006 | return 0; |
| 9007 | } |
| 9008 | |
| 9009 | /* |
| 9010 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9011 | * timespec and sigset_t pointers if good. |
| 9012 | */ |
| 9013 | if (*argsz != sizeof(arg)) |
| 9014 | return -EINVAL; |
| 9015 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9016 | return -EFAULT; |
| 9017 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9018 | *argsz = arg.sigmask_sz; |
| 9019 | *ts = u64_to_user_ptr(arg.ts); |
| 9020 | return 0; |
| 9021 | } |
| 9022 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9023 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9024 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9025 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9026 | { |
| 9027 | struct io_ring_ctx *ctx; |
| 9028 | long ret = -EBADF; |
| 9029 | int submitted = 0; |
| 9030 | struct fd f; |
| 9031 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9032 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9033 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9034 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9035 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9036 | return -EINVAL; |
| 9037 | |
| 9038 | f = fdget(fd); |
| 9039 | if (!f.file) |
| 9040 | return -EBADF; |
| 9041 | |
| 9042 | ret = -EOPNOTSUPP; |
| 9043 | if (f.file->f_op != &io_uring_fops) |
| 9044 | goto out_fput; |
| 9045 | |
| 9046 | ret = -ENXIO; |
| 9047 | ctx = f.file->private_data; |
| 9048 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9049 | goto out_fput; |
| 9050 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9051 | ret = -EBADFD; |
| 9052 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9053 | goto out; |
| 9054 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9055 | /* |
| 9056 | * For SQ polling, the thread will do all submissions and completions. |
| 9057 | * Just return the requested submit count, and wake the thread if |
| 9058 | * we were asked to. |
| 9059 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9060 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9061 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9062 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9063 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9064 | if (unlikely(ctx->sqo_exec)) { |
| 9065 | ret = io_sq_thread_fork(ctx->sq_data, ctx); |
| 9066 | if (ret) |
| 9067 | goto out; |
| 9068 | ctx->sqo_exec = 0; |
| 9069 | } |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9070 | ret = -EOWNERDEAD; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9071 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9072 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9073 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9074 | ret = io_sqpoll_wait_sq(ctx); |
| 9075 | if (ret) |
| 9076 | goto out; |
| 9077 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9078 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9079 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9080 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9081 | if (unlikely(ret)) |
| 9082 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9083 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9084 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9085 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9086 | |
| 9087 | if (submitted != to_submit) |
| 9088 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9089 | } |
| 9090 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9091 | const sigset_t __user *sig; |
| 9092 | struct __kernel_timespec __user *ts; |
| 9093 | |
| 9094 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9095 | if (unlikely(ret)) |
| 9096 | goto out; |
| 9097 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9098 | min_complete = min(min_complete, ctx->cq_entries); |
| 9099 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9100 | /* |
| 9101 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9102 | * space applications don't need to do io completion events |
| 9103 | * polling again, they can rely on io_sq_thread to do polling |
| 9104 | * work, which can reduce cpu usage and uring_lock contention. |
| 9105 | */ |
| 9106 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9107 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9108 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9109 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9110 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9111 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9112 | } |
| 9113 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9114 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9115 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9116 | out_fput: |
| 9117 | fdput(f); |
| 9118 | return submitted ? submitted : ret; |
| 9119 | } |
| 9120 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9121 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9122 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9123 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9124 | const struct cred *cred = p; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9125 | struct seq_file *m = data; |
| 9126 | struct user_namespace *uns = seq_user_ns(m); |
| 9127 | struct group_info *gi; |
| 9128 | kernel_cap_t cap; |
| 9129 | unsigned __capi; |
| 9130 | int g; |
| 9131 | |
| 9132 | seq_printf(m, "%5d\n", id); |
| 9133 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9134 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9135 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9136 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9137 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9138 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9139 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9140 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9141 | seq_puts(m, "\n\tGroups:\t"); |
| 9142 | gi = cred->group_info; |
| 9143 | for (g = 0; g < gi->ngroups; g++) { |
| 9144 | seq_put_decimal_ull(m, g ? " " : "", |
| 9145 | from_kgid_munged(uns, gi->gid[g])); |
| 9146 | } |
| 9147 | seq_puts(m, "\n\tCapEff:\t"); |
| 9148 | cap = cred->cap_effective; |
| 9149 | CAP_FOR_EACH_U32(__capi) |
| 9150 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9151 | seq_putc(m, '\n'); |
| 9152 | return 0; |
| 9153 | } |
| 9154 | |
| 9155 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9156 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9157 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9158 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9159 | int i; |
| 9160 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9161 | /* |
| 9162 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9163 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9164 | * cases. If we fail to get the lock, we just don't iterate any |
| 9165 | * structures that could be going away outside the io_uring mutex. |
| 9166 | */ |
| 9167 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9168 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9169 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9170 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 9171 | if (!sq->thread) |
| 9172 | sq = NULL; |
| 9173 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9174 | |
| 9175 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9176 | 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] | 9177 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9178 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9179 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9180 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9181 | if (f) |
| 9182 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9183 | else |
| 9184 | seq_printf(m, "%5u: <none>\n", i); |
| 9185 | } |
| 9186 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9187 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9188 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9189 | |
| 9190 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9191 | (unsigned int) buf->len); |
| 9192 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9193 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9194 | seq_printf(m, "Personalities:\n"); |
| 9195 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9196 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9197 | seq_printf(m, "PollList:\n"); |
| 9198 | spin_lock_irq(&ctx->completion_lock); |
| 9199 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9200 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9201 | struct io_kiocb *req; |
| 9202 | |
| 9203 | hlist_for_each_entry(req, list, hash_node) |
| 9204 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9205 | req->task->task_works != NULL); |
| 9206 | } |
| 9207 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9208 | if (has_lock) |
| 9209 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9210 | } |
| 9211 | |
| 9212 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9213 | { |
| 9214 | struct io_ring_ctx *ctx = f->private_data; |
| 9215 | |
| 9216 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9217 | __io_uring_show_fdinfo(ctx, m); |
| 9218 | percpu_ref_put(&ctx->refs); |
| 9219 | } |
| 9220 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9221 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9222 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9223 | static const struct file_operations io_uring_fops = { |
| 9224 | .release = io_uring_release, |
| 9225 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9226 | #ifndef CONFIG_MMU |
| 9227 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9228 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9229 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9230 | .poll = io_uring_poll, |
| 9231 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9232 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9233 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9234 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9235 | }; |
| 9236 | |
| 9237 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9238 | struct io_uring_params *p) |
| 9239 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9240 | struct io_rings *rings; |
| 9241 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9242 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9243 | /* make sure these are sane, as we already accounted them */ |
| 9244 | ctx->sq_entries = p->sq_entries; |
| 9245 | ctx->cq_entries = p->cq_entries; |
| 9246 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9247 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9248 | if (size == SIZE_MAX) |
| 9249 | return -EOVERFLOW; |
| 9250 | |
| 9251 | rings = io_mem_alloc(size); |
| 9252 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9253 | return -ENOMEM; |
| 9254 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9255 | ctx->rings = rings; |
| 9256 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9257 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9258 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9259 | rings->sq_ring_entries = p->sq_entries; |
| 9260 | rings->cq_ring_entries = p->cq_entries; |
| 9261 | ctx->sq_mask = rings->sq_ring_mask; |
| 9262 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9263 | |
| 9264 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9265 | if (size == SIZE_MAX) { |
| 9266 | io_mem_free(ctx->rings); |
| 9267 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9268 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9269 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9270 | |
| 9271 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9272 | if (!ctx->sq_sqes) { |
| 9273 | io_mem_free(ctx->rings); |
| 9274 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9275 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9276 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9277 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9278 | return 0; |
| 9279 | } |
| 9280 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9281 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9282 | { |
| 9283 | int ret, fd; |
| 9284 | |
| 9285 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9286 | if (fd < 0) |
| 9287 | return fd; |
| 9288 | |
| 9289 | ret = io_uring_add_task_file(ctx, file); |
| 9290 | if (ret) { |
| 9291 | put_unused_fd(fd); |
| 9292 | return ret; |
| 9293 | } |
| 9294 | fd_install(fd, file); |
| 9295 | return fd; |
| 9296 | } |
| 9297 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9298 | /* |
| 9299 | * Allocate an anonymous fd, this is what constitutes the application |
| 9300 | * visible backing of an io_uring instance. The application mmaps this |
| 9301 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9302 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9303 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9304 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9305 | { |
| 9306 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9307 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9308 | int ret; |
| 9309 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9310 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9311 | &ctx->ring_sock); |
| 9312 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9313 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9314 | #endif |
| 9315 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9316 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9317 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9318 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9319 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9320 | sock_release(ctx->ring_sock); |
| 9321 | ctx->ring_sock = NULL; |
| 9322 | } else { |
| 9323 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9324 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9325 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9326 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9327 | } |
| 9328 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9329 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9330 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9331 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9332 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9333 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9334 | int ret; |
| 9335 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9336 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9337 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9338 | if (entries > IORING_MAX_ENTRIES) { |
| 9339 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9340 | return -EINVAL; |
| 9341 | entries = IORING_MAX_ENTRIES; |
| 9342 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9343 | |
| 9344 | /* |
| 9345 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9346 | * application to drive a higher depth than the size of the SQ ring, |
| 9347 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9348 | * some flexibility in overcommitting a bit. If the application has |
| 9349 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9350 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9351 | */ |
| 9352 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9353 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9354 | /* |
| 9355 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9356 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9357 | * any cq vs sq ring sizing. |
| 9358 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9359 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9360 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9361 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9362 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9363 | return -EINVAL; |
| 9364 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9365 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9366 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9367 | if (p->cq_entries < p->sq_entries) |
| 9368 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9369 | } else { |
| 9370 | p->cq_entries = 2 * p->sq_entries; |
| 9371 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9372 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9373 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9374 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9375 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9376 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9377 | if (!capable(CAP_IPC_LOCK)) |
| 9378 | ctx->user = get_uid(current_user()); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9379 | ctx->sqo_task = current; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9380 | |
| 9381 | /* |
| 9382 | * This is just grabbed for accounting purposes. When a process exits, |
| 9383 | * the mm is exited and dropped before the files, hence we need to hang |
| 9384 | * on to this mm purely for the purposes of being able to unaccount |
| 9385 | * memory (locked/pinned vm). It's not used for anything else. |
| 9386 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9387 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9388 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9389 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9390 | ret = io_allocate_scq_urings(ctx, p); |
| 9391 | if (ret) |
| 9392 | goto err; |
| 9393 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9394 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9395 | if (ret) |
| 9396 | goto err; |
| 9397 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9398 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9399 | io_sq_offload_start(ctx); |
| 9400 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9401 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9402 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9403 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9404 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9405 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9406 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9407 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9408 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9409 | |
| 9410 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9411 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9412 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9413 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9414 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9415 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9416 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9417 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9418 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9419 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9420 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9421 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9422 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Jens Axboe | 1c0aa1f | 2021-02-20 11:55:28 -0700 | [diff] [blame] | 9423 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9424 | |
| 9425 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9426 | ret = -EFAULT; |
| 9427 | goto err; |
| 9428 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9429 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9430 | file = io_uring_get_file(ctx); |
| 9431 | if (IS_ERR(file)) { |
| 9432 | ret = PTR_ERR(file); |
| 9433 | goto err; |
| 9434 | } |
| 9435 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9436 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9437 | * Install ring fd as the very last thing, so we don't risk someone |
| 9438 | * having closed it before we finish setup |
| 9439 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9440 | ret = io_uring_install_fd(ctx, file); |
| 9441 | if (ret < 0) { |
| 9442 | /* fput will clean it up */ |
| 9443 | fput(file); |
| 9444 | return ret; |
| 9445 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9446 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9447 | 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] | 9448 | return ret; |
| 9449 | err: |
| 9450 | io_ring_ctx_wait_and_kill(ctx); |
| 9451 | return ret; |
| 9452 | } |
| 9453 | |
| 9454 | /* |
| 9455 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9456 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9457 | * params structure passed in. |
| 9458 | */ |
| 9459 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9460 | { |
| 9461 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9462 | int i; |
| 9463 | |
| 9464 | if (copy_from_user(&p, params, sizeof(p))) |
| 9465 | return -EFAULT; |
| 9466 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9467 | if (p.resv[i]) |
| 9468 | return -EINVAL; |
| 9469 | } |
| 9470 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9471 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9472 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9473 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9474 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9475 | return -EINVAL; |
| 9476 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9477 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9478 | } |
| 9479 | |
| 9480 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9481 | struct io_uring_params __user *, params) |
| 9482 | { |
| 9483 | return io_uring_setup(entries, params); |
| 9484 | } |
| 9485 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9486 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9487 | { |
| 9488 | struct io_uring_probe *p; |
| 9489 | size_t size; |
| 9490 | int i, ret; |
| 9491 | |
| 9492 | size = struct_size(p, ops, nr_args); |
| 9493 | if (size == SIZE_MAX) |
| 9494 | return -EOVERFLOW; |
| 9495 | p = kzalloc(size, GFP_KERNEL); |
| 9496 | if (!p) |
| 9497 | return -ENOMEM; |
| 9498 | |
| 9499 | ret = -EFAULT; |
| 9500 | if (copy_from_user(p, arg, size)) |
| 9501 | goto out; |
| 9502 | ret = -EINVAL; |
| 9503 | if (memchr_inv(p, 0, size)) |
| 9504 | goto out; |
| 9505 | |
| 9506 | p->last_op = IORING_OP_LAST - 1; |
| 9507 | if (nr_args > IORING_OP_LAST) |
| 9508 | nr_args = IORING_OP_LAST; |
| 9509 | |
| 9510 | for (i = 0; i < nr_args; i++) { |
| 9511 | p->ops[i].op = i; |
| 9512 | if (!io_op_defs[i].not_supported) |
| 9513 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9514 | } |
| 9515 | p->ops_len = i; |
| 9516 | |
| 9517 | ret = 0; |
| 9518 | if (copy_to_user(arg, p, size)) |
| 9519 | ret = -EFAULT; |
| 9520 | out: |
| 9521 | kfree(p); |
| 9522 | return ret; |
| 9523 | } |
| 9524 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9525 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9526 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9527 | const struct cred *creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9528 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9529 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9530 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9531 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9532 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 9533 | USHRT_MAX, GFP_KERNEL); |
| 9534 | if (ret < 0) |
| 9535 | put_cred(creds); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9536 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9537 | } |
| 9538 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9539 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9540 | unsigned int nr_args) |
| 9541 | { |
| 9542 | struct io_uring_restriction *res; |
| 9543 | size_t size; |
| 9544 | int i, ret; |
| 9545 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9546 | /* Restrictions allowed only if rings started disabled */ |
| 9547 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9548 | return -EBADFD; |
| 9549 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9550 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9551 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9552 | return -EBUSY; |
| 9553 | |
| 9554 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9555 | return -EINVAL; |
| 9556 | |
| 9557 | size = array_size(nr_args, sizeof(*res)); |
| 9558 | if (size == SIZE_MAX) |
| 9559 | return -EOVERFLOW; |
| 9560 | |
| 9561 | res = memdup_user(arg, size); |
| 9562 | if (IS_ERR(res)) |
| 9563 | return PTR_ERR(res); |
| 9564 | |
| 9565 | ret = 0; |
| 9566 | |
| 9567 | for (i = 0; i < nr_args; i++) { |
| 9568 | switch (res[i].opcode) { |
| 9569 | case IORING_RESTRICTION_REGISTER_OP: |
| 9570 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9571 | ret = -EINVAL; |
| 9572 | goto out; |
| 9573 | } |
| 9574 | |
| 9575 | __set_bit(res[i].register_op, |
| 9576 | ctx->restrictions.register_op); |
| 9577 | break; |
| 9578 | case IORING_RESTRICTION_SQE_OP: |
| 9579 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9580 | ret = -EINVAL; |
| 9581 | goto out; |
| 9582 | } |
| 9583 | |
| 9584 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9585 | break; |
| 9586 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9587 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9588 | break; |
| 9589 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9590 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9591 | break; |
| 9592 | default: |
| 9593 | ret = -EINVAL; |
| 9594 | goto out; |
| 9595 | } |
| 9596 | } |
| 9597 | |
| 9598 | out: |
| 9599 | /* Reset all restrictions if an error happened */ |
| 9600 | if (ret != 0) |
| 9601 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9602 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9603 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9604 | |
| 9605 | kfree(res); |
| 9606 | return ret; |
| 9607 | } |
| 9608 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9609 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9610 | { |
| 9611 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9612 | return -EBADFD; |
| 9613 | |
| 9614 | if (ctx->restrictions.registered) |
| 9615 | ctx->restricted = 1; |
| 9616 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9617 | io_sq_offload_start(ctx); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9618 | return 0; |
| 9619 | } |
| 9620 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9621 | static bool io_register_op_must_quiesce(int op) |
| 9622 | { |
| 9623 | switch (op) { |
| 9624 | case IORING_UNREGISTER_FILES: |
| 9625 | case IORING_REGISTER_FILES_UPDATE: |
| 9626 | case IORING_REGISTER_PROBE: |
| 9627 | case IORING_REGISTER_PERSONALITY: |
| 9628 | case IORING_UNREGISTER_PERSONALITY: |
| 9629 | return false; |
| 9630 | default: |
| 9631 | return true; |
| 9632 | } |
| 9633 | } |
| 9634 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9635 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9636 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9637 | __releases(ctx->uring_lock) |
| 9638 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9639 | { |
| 9640 | int ret; |
| 9641 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9642 | /* |
| 9643 | * We're inside the ring mutex, if the ref is already dying, then |
| 9644 | * someone else killed the ctx or is already going through |
| 9645 | * io_uring_register(). |
| 9646 | */ |
| 9647 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9648 | return -ENXIO; |
| 9649 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9650 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9651 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9652 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9653 | /* |
| 9654 | * Drop uring mutex before waiting for references to exit. If |
| 9655 | * another thread is currently inside io_uring_enter() it might |
| 9656 | * need to grab the uring_lock to make progress. If we hold it |
| 9657 | * here across the drain wait, then we can deadlock. It's safe |
| 9658 | * to drop the mutex here, since no new references will come in |
| 9659 | * after we've killed the percpu ref. |
| 9660 | */ |
| 9661 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9662 | do { |
| 9663 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 9664 | if (!ret) |
| 9665 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 9666 | ret = io_run_task_work_sig(); |
| 9667 | if (ret < 0) |
| 9668 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9669 | } while (1); |
| 9670 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9671 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9672 | |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9673 | if (ret) { |
| 9674 | percpu_ref_resurrect(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9675 | goto out_quiesce; |
| 9676 | } |
| 9677 | } |
| 9678 | |
| 9679 | if (ctx->restricted) { |
| 9680 | if (opcode >= IORING_REGISTER_LAST) { |
| 9681 | ret = -EINVAL; |
| 9682 | goto out; |
| 9683 | } |
| 9684 | |
| 9685 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 9686 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9687 | goto out; |
| 9688 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9689 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9690 | |
| 9691 | switch (opcode) { |
| 9692 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9693 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9694 | break; |
| 9695 | case IORING_UNREGISTER_BUFFERS: |
| 9696 | ret = -EINVAL; |
| 9697 | if (arg || nr_args) |
| 9698 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9699 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9700 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 9701 | case IORING_REGISTER_FILES: |
| 9702 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 9703 | break; |
| 9704 | case IORING_UNREGISTER_FILES: |
| 9705 | ret = -EINVAL; |
| 9706 | if (arg || nr_args) |
| 9707 | break; |
| 9708 | ret = io_sqe_files_unregister(ctx); |
| 9709 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 9710 | case IORING_REGISTER_FILES_UPDATE: |
| 9711 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 9712 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9713 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9714 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9715 | ret = -EINVAL; |
| 9716 | if (nr_args != 1) |
| 9717 | break; |
| 9718 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9719 | if (ret) |
| 9720 | break; |
| 9721 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 9722 | ctx->eventfd_async = 1; |
| 9723 | else |
| 9724 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9725 | break; |
| 9726 | case IORING_UNREGISTER_EVENTFD: |
| 9727 | ret = -EINVAL; |
| 9728 | if (arg || nr_args) |
| 9729 | break; |
| 9730 | ret = io_eventfd_unregister(ctx); |
| 9731 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9732 | case IORING_REGISTER_PROBE: |
| 9733 | ret = -EINVAL; |
| 9734 | if (!arg || nr_args > 256) |
| 9735 | break; |
| 9736 | ret = io_probe(ctx, arg, nr_args); |
| 9737 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9738 | case IORING_REGISTER_PERSONALITY: |
| 9739 | ret = -EINVAL; |
| 9740 | if (arg || nr_args) |
| 9741 | break; |
| 9742 | ret = io_register_personality(ctx); |
| 9743 | break; |
| 9744 | case IORING_UNREGISTER_PERSONALITY: |
| 9745 | ret = -EINVAL; |
| 9746 | if (arg) |
| 9747 | break; |
| 9748 | ret = io_unregister_personality(ctx, nr_args); |
| 9749 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9750 | case IORING_REGISTER_ENABLE_RINGS: |
| 9751 | ret = -EINVAL; |
| 9752 | if (arg || nr_args) |
| 9753 | break; |
| 9754 | ret = io_register_enable_rings(ctx); |
| 9755 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9756 | case IORING_REGISTER_RESTRICTIONS: |
| 9757 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 9758 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9759 | default: |
| 9760 | ret = -EINVAL; |
| 9761 | break; |
| 9762 | } |
| 9763 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9764 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9765 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9766 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9767 | percpu_ref_reinit(&ctx->refs); |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9768 | out_quiesce: |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9769 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9770 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9771 | return ret; |
| 9772 | } |
| 9773 | |
| 9774 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 9775 | void __user *, arg, unsigned int, nr_args) |
| 9776 | { |
| 9777 | struct io_ring_ctx *ctx; |
| 9778 | long ret = -EBADF; |
| 9779 | struct fd f; |
| 9780 | |
| 9781 | f = fdget(fd); |
| 9782 | if (!f.file) |
| 9783 | return -EBADF; |
| 9784 | |
| 9785 | ret = -EOPNOTSUPP; |
| 9786 | if (f.file->f_op != &io_uring_fops) |
| 9787 | goto out_fput; |
| 9788 | |
| 9789 | ctx = f.file->private_data; |
| 9790 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 9791 | io_run_task_work(); |
| 9792 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9793 | mutex_lock(&ctx->uring_lock); |
| 9794 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 9795 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9796 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 9797 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9798 | out_fput: |
| 9799 | fdput(f); |
| 9800 | return ret; |
| 9801 | } |
| 9802 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9803 | static int __init io_uring_init(void) |
| 9804 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9805 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 9806 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 9807 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 9808 | } while (0) |
| 9809 | |
| 9810 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 9811 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 9812 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 9813 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 9814 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 9815 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 9816 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 9817 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 9818 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 9819 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9820 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9821 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 9822 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 9823 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 9824 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 9825 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9826 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 9827 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9828 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 9829 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 9830 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 9831 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 9832 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 9833 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 9834 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 9835 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9836 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9837 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 9838 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 9839 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9840 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9841 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 9842 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 9843 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 9844 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 9845 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9846 | return 0; |
| 9847 | }; |
| 9848 | __initcall(io_uring_init); |