Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 47 | #include <net/compat.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 48 | #include <linux/refcount.h> |
| 49 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 50 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/sched/signal.h> |
| 53 | #include <linux/fs.h> |
| 54 | #include <linux/file.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/mm.h> |
| 57 | #include <linux/mman.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 73 | #include <linux/namei.h> |
| 74 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 75 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 76 | #include <linux/eventpoll.h> |
Jens Axboe | ff002b3 | 2020-02-07 16:05:21 -0700 | [diff] [blame] | 77 | #include <linux/fs_struct.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 78 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 79 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 80 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 81 | #include <linux/io_uring.h> |
Dennis Zhou | 91d8f51 | 2020-09-16 13:41:05 -0700 | [diff] [blame] | 82 | #include <linux/blk-cgroup.h> |
Jens Axboe | 4ea33a9 | 2020-10-15 13:46:44 -0600 | [diff] [blame] | 83 | #include <linux/audit.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 85 | #define CREATE_TRACE_POINTS |
| 86 | #include <trace/events/io_uring.h> |
| 87 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 88 | #include <uapi/linux/io_uring.h> |
| 89 | |
| 90 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 91 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 92 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 93 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 98 | */ |
| 99 | #define IORING_FILE_TABLE_SHIFT 9 |
| 100 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 101 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 102 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 103 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 104 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 105 | |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 106 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
| 107 | IOSQE_IO_HARDLINK | IOSQE_ASYNC | \ |
| 108 | IOSQE_BUFFER_SELECT) |
| 109 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 110 | struct io_uring { |
| 111 | u32 head ____cacheline_aligned_in_smp; |
| 112 | u32 tail ____cacheline_aligned_in_smp; |
| 113 | }; |
| 114 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 115 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 116 | * This data is shared with the application through the mmap at offsets |
| 117 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 118 | * |
| 119 | * The offsets to the member fields are published through struct |
| 120 | * io_sqring_offsets when calling io_uring_setup. |
| 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 123 | /* |
| 124 | * Head and tail offsets into the ring; the offsets need to be |
| 125 | * masked to get valid indices. |
| 126 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 127 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 128 | * and the application controls tail of the sq ring and the head of the |
| 129 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 130 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 131 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 132 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 133 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 134 | * ring_entries - 1) |
| 135 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 136 | u32 sq_ring_mask, cq_ring_mask; |
| 137 | /* Ring sizes (constant, power of 2) */ |
| 138 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Number of invalid entries dropped by the kernel due to |
| 141 | * invalid index stored in array |
| 142 | * |
| 143 | * Written by the kernel, shouldn't be modified by the |
| 144 | * application (i.e. get number of "new events" by comparing to |
| 145 | * cached value). |
| 146 | * |
| 147 | * After a new SQ head value was read by the application this |
| 148 | * counter includes all submissions that were dropped reaching |
| 149 | * the new SQ head (and possibly more). |
| 150 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 151 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 152 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 153 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 154 | * |
| 155 | * Written by the kernel, shouldn't be modified by the |
| 156 | * application. |
| 157 | * |
| 158 | * The application needs a full memory barrier before checking |
| 159 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 160 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 161 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 163 | * Runtime CQ flags |
| 164 | * |
| 165 | * Written by the application, shouldn't be modified by the |
| 166 | * kernel. |
| 167 | */ |
| 168 | u32 cq_flags; |
| 169 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 170 | * Number of completion events lost because the queue was full; |
| 171 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 172 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 173 | * the completion queue. |
| 174 | * |
| 175 | * Written by the kernel, shouldn't be modified by the |
| 176 | * application (i.e. get number of "new events" by comparing to |
| 177 | * cached value). |
| 178 | * |
| 179 | * As completion events come in out of order this counter is not |
| 180 | * ordered with any other data. |
| 181 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 182 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Ring buffer of completion events. |
| 185 | * |
| 186 | * The kernel writes completion events fresh every time they are |
| 187 | * produced, so the application is allowed to modify pending |
| 188 | * entries. |
| 189 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 190 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 193 | enum io_uring_cmd_flags { |
| 194 | IO_URING_F_NONBLOCK = 1, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 195 | IO_URING_F_COMPLETE_DEFER = 2, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 198 | struct io_mapped_ubuf { |
| 199 | u64 ubuf; |
| 200 | size_t len; |
| 201 | struct bio_vec *bvec; |
| 202 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 203 | unsigned long acct_pages; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 206 | struct io_ring_ctx; |
| 207 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 208 | struct io_rsrc_put { |
| 209 | struct list_head list; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 210 | union { |
| 211 | void *rsrc; |
| 212 | struct file *file; |
| 213 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | struct fixed_rsrc_table { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 217 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 220 | struct fixed_rsrc_ref_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 221 | struct percpu_ref refs; |
| 222 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 223 | struct list_head rsrc_list; |
| 224 | struct fixed_rsrc_data *rsrc_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 225 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 226 | struct io_rsrc_put *prsrc); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 227 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 228 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 229 | }; |
| 230 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 231 | struct fixed_rsrc_data { |
| 232 | struct fixed_rsrc_table *table; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 233 | struct io_ring_ctx *ctx; |
| 234 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 235 | struct fixed_rsrc_ref_node *node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 236 | struct percpu_ref refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 237 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 238 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 241 | struct io_buffer { |
| 242 | struct list_head list; |
| 243 | __u64 addr; |
| 244 | __s32 len; |
| 245 | __u16 bid; |
| 246 | }; |
| 247 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 248 | struct io_restriction { |
| 249 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 250 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 251 | u8 sqe_flags_allowed; |
| 252 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 253 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 254 | }; |
| 255 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 256 | enum { |
| 257 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 258 | IO_SQ_THREAD_SHOULD_PARK, |
| 259 | }; |
| 260 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 261 | struct io_sq_data { |
| 262 | refcount_t refs; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 263 | struct mutex lock; |
| 264 | |
| 265 | /* ctx's that are using this sqd */ |
| 266 | struct list_head ctx_list; |
| 267 | struct list_head ctx_new_list; |
| 268 | struct mutex ctx_lock; |
| 269 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 270 | struct task_struct *thread; |
| 271 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 272 | |
| 273 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 274 | int sq_cpu; |
| 275 | pid_t task_pid; |
| 276 | |
| 277 | unsigned long state; |
| 278 | struct completion startup; |
| 279 | struct completion completion; |
| 280 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 281 | }; |
| 282 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 283 | #define IO_IOPOLL_BATCH 8 |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 284 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 285 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 286 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 287 | |
| 288 | struct io_comp_state { |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 289 | struct io_kiocb *reqs[IO_COMPL_BATCH]; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 290 | unsigned int nr; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 291 | unsigned int locked_free_nr; |
| 292 | /* inline/task_work completion list, under ->uring_lock */ |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 293 | struct list_head free_list; |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 294 | /* IRQ completion list, under ->completion_lock */ |
| 295 | struct list_head locked_free_list; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 298 | struct io_submit_link { |
| 299 | struct io_kiocb *head; |
| 300 | struct io_kiocb *last; |
| 301 | }; |
| 302 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 303 | struct io_submit_state { |
| 304 | struct blk_plug plug; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 305 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * io_kiocb alloc cache |
| 309 | */ |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 310 | void *reqs[IO_REQ_CACHE_SIZE]; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 311 | unsigned int free_reqs; |
| 312 | |
| 313 | bool plug_started; |
| 314 | |
| 315 | /* |
| 316 | * Batch completion logic |
| 317 | */ |
| 318 | struct io_comp_state comp; |
| 319 | |
| 320 | /* |
| 321 | * File reference cache |
| 322 | */ |
| 323 | struct file *file; |
| 324 | unsigned int fd; |
| 325 | unsigned int file_refs; |
| 326 | unsigned int ios_left; |
| 327 | }; |
| 328 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 329 | struct io_ring_ctx { |
| 330 | struct { |
| 331 | struct percpu_ref refs; |
| 332 | } ____cacheline_aligned_in_smp; |
| 333 | |
| 334 | struct { |
| 335 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 336 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 337 | unsigned int cq_overflow_flushed: 1; |
| 338 | unsigned int drain_next: 1; |
| 339 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 340 | unsigned int restricted: 1; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 341 | unsigned int sqo_dead: 1; |
Jens Axboe | 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 | |
| 363 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 364 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 365 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 366 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 367 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 368 | } ____cacheline_aligned_in_smp; |
| 369 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 370 | struct { |
| 371 | struct mutex uring_lock; |
| 372 | wait_queue_head_t wait; |
| 373 | } ____cacheline_aligned_in_smp; |
| 374 | |
| 375 | struct io_submit_state submit_state; |
| 376 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 377 | struct io_rings *rings; |
| 378 | |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 379 | /* |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 380 | * For SQPOLL usage |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 381 | */ |
| 382 | struct task_struct *sqo_task; |
| 383 | |
| 384 | /* Only used for accounting purposes */ |
| 385 | struct mm_struct *mm_account; |
| 386 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 387 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 388 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 389 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 390 | struct list_head sqd_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 391 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 392 | /* |
| 393 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 394 | * readers must ensure that ->refs is alive as long as the file* is |
| 395 | * used. Only updated through io_uring_register(2). |
| 396 | */ |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 397 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 398 | unsigned nr_user_files; |
| 399 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 400 | /* if used, fixed mapped user buffers */ |
| 401 | unsigned nr_user_bufs; |
| 402 | struct io_mapped_ubuf *user_bufs; |
| 403 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 404 | struct user_struct *user; |
| 405 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 406 | struct completion ref_comp; |
| 407 | struct completion sq_thread_comp; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 408 | |
| 409 | #if defined(CONFIG_UNIX) |
| 410 | struct socket *ring_sock; |
| 411 | #endif |
| 412 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 413 | struct idr io_buffer_idr; |
| 414 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 415 | struct idr personality_idr; |
| 416 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 417 | struct { |
| 418 | unsigned cached_cq_tail; |
| 419 | unsigned cq_entries; |
| 420 | unsigned cq_mask; |
| 421 | atomic_t cq_timeouts; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 422 | unsigned cq_last_tm_flush; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 423 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 424 | struct wait_queue_head cq_wait; |
| 425 | struct fasync_struct *cq_fasync; |
| 426 | struct eventfd_ctx *cq_ev_fd; |
| 427 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 428 | |
| 429 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 430 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 431 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 432 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 433 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 434 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 435 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 436 | * manipulate the list, hence no extra locking is needed there. |
| 437 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 438 | struct list_head iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 439 | struct hlist_head *cancel_hash; |
| 440 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 441 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 442 | |
| 443 | spinlock_t inflight_lock; |
| 444 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 445 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 446 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 447 | struct delayed_work rsrc_put_work; |
| 448 | struct llist_head rsrc_put_llist; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 449 | struct list_head rsrc_ref_list; |
| 450 | spinlock_t rsrc_ref_lock; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 451 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 452 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 453 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 454 | /* exit task_work */ |
| 455 | struct callback_head *exit_task_work; |
| 456 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 457 | /* Keep this last, we don't need it for the fast path */ |
| 458 | struct work_struct exit_work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 459 | }; |
| 460 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 461 | /* |
| 462 | * First field must be the file pointer in all the |
| 463 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 464 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 465 | struct io_poll_iocb { |
| 466 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 467 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 468 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 469 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 470 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 471 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 474 | struct io_poll_remove { |
| 475 | struct file *file; |
| 476 | u64 addr; |
| 477 | }; |
| 478 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 479 | struct io_close { |
| 480 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 481 | int fd; |
| 482 | }; |
| 483 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 484 | struct io_timeout_data { |
| 485 | struct io_kiocb *req; |
| 486 | struct hrtimer timer; |
| 487 | struct timespec64 ts; |
| 488 | enum hrtimer_mode mode; |
| 489 | }; |
| 490 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 491 | struct io_accept { |
| 492 | struct file *file; |
| 493 | struct sockaddr __user *addr; |
| 494 | int __user *addr_len; |
| 495 | int flags; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 496 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | struct io_sync { |
| 500 | struct file *file; |
| 501 | loff_t len; |
| 502 | loff_t off; |
| 503 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 504 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 505 | }; |
| 506 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 507 | struct io_cancel { |
| 508 | struct file *file; |
| 509 | u64 addr; |
| 510 | }; |
| 511 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 512 | struct io_timeout { |
| 513 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 514 | u32 off; |
| 515 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 516 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 517 | /* head of the link, used by linked timeouts only */ |
| 518 | struct io_kiocb *head; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 519 | }; |
| 520 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 521 | struct io_timeout_rem { |
| 522 | struct file *file; |
| 523 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 524 | |
| 525 | /* timeout update */ |
| 526 | struct timespec64 ts; |
| 527 | u32 flags; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 528 | }; |
| 529 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 530 | struct io_rw { |
| 531 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 532 | struct kiocb kiocb; |
| 533 | u64 addr; |
| 534 | u64 len; |
| 535 | }; |
| 536 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 537 | struct io_connect { |
| 538 | struct file *file; |
| 539 | struct sockaddr __user *addr; |
| 540 | int addr_len; |
| 541 | }; |
| 542 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 543 | struct io_sr_msg { |
| 544 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 545 | union { |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 546 | struct user_msghdr __user *umsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 547 | void __user *buf; |
| 548 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 549 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 550 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 551 | size_t len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 552 | struct io_buffer *kbuf; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 553 | }; |
| 554 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 555 | struct io_open { |
| 556 | struct file *file; |
| 557 | int dfd; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 558 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 559 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 560 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 561 | }; |
| 562 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 563 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 564 | struct file *file; |
| 565 | u64 arg; |
| 566 | u32 nr_args; |
| 567 | u32 offset; |
| 568 | }; |
| 569 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 570 | struct io_fadvise { |
| 571 | struct file *file; |
| 572 | u64 offset; |
| 573 | u32 len; |
| 574 | u32 advice; |
| 575 | }; |
| 576 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 577 | struct io_madvise { |
| 578 | struct file *file; |
| 579 | u64 addr; |
| 580 | u32 len; |
| 581 | u32 advice; |
| 582 | }; |
| 583 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 584 | struct io_epoll { |
| 585 | struct file *file; |
| 586 | int epfd; |
| 587 | int op; |
| 588 | int fd; |
| 589 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 590 | }; |
| 591 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 592 | struct io_splice { |
| 593 | struct file *file_out; |
| 594 | struct file *file_in; |
| 595 | loff_t off_out; |
| 596 | loff_t off_in; |
| 597 | u64 len; |
| 598 | unsigned int flags; |
| 599 | }; |
| 600 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 601 | struct io_provide_buf { |
| 602 | struct file *file; |
| 603 | __u64 addr; |
| 604 | __s32 len; |
| 605 | __u32 bgid; |
| 606 | __u16 nbufs; |
| 607 | __u16 bid; |
| 608 | }; |
| 609 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 610 | struct io_statx { |
| 611 | struct file *file; |
| 612 | int dfd; |
| 613 | unsigned int mask; |
| 614 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 615 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 616 | struct statx __user *buffer; |
| 617 | }; |
| 618 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 619 | struct io_shutdown { |
| 620 | struct file *file; |
| 621 | int how; |
| 622 | }; |
| 623 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 624 | struct io_rename { |
| 625 | struct file *file; |
| 626 | int old_dfd; |
| 627 | int new_dfd; |
| 628 | struct filename *oldpath; |
| 629 | struct filename *newpath; |
| 630 | int flags; |
| 631 | }; |
| 632 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 633 | struct io_unlink { |
| 634 | struct file *file; |
| 635 | int dfd; |
| 636 | int flags; |
| 637 | struct filename *filename; |
| 638 | }; |
| 639 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 640 | struct io_completion { |
| 641 | struct file *file; |
| 642 | struct list_head list; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 643 | int cflags; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 644 | }; |
| 645 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 646 | struct io_async_connect { |
| 647 | struct sockaddr_storage address; |
| 648 | }; |
| 649 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 650 | struct io_async_msghdr { |
| 651 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 652 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 653 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 654 | struct sockaddr __user *uaddr; |
| 655 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 656 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 657 | }; |
| 658 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 659 | struct io_async_rw { |
| 660 | struct iovec fast_iov[UIO_FASTIOV]; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 661 | const struct iovec *free_iovec; |
| 662 | struct iov_iter iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 663 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 664 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 665 | }; |
| 666 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 667 | enum { |
| 668 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 669 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 670 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 671 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 672 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 673 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 674 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 675 | REQ_F_FAIL_LINK_BIT, |
| 676 | REQ_F_INFLIGHT_BIT, |
| 677 | REQ_F_CUR_POS_BIT, |
| 678 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 679 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 680 | REQ_F_ISREG_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 681 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 682 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 683 | REQ_F_BUFFER_SELECTED_BIT, |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 684 | REQ_F_NO_FILE_TABLE_BIT, |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 685 | REQ_F_WORK_INITIALIZED_BIT, |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 686 | REQ_F_LTIMEOUT_ACTIVE_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 687 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 688 | |
| 689 | /* not a real bit, just to check we're not overflowing the space */ |
| 690 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | enum { |
| 694 | /* ctx owns file */ |
| 695 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 696 | /* drain existing IO first */ |
| 697 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 698 | /* linked sqes */ |
| 699 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 700 | /* doesn't sever on completion < 0 */ |
| 701 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 702 | /* IOSQE_ASYNC */ |
| 703 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 704 | /* IOSQE_BUFFER_SELECT */ |
| 705 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 706 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 707 | /* fail rest of links */ |
| 708 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 709 | /* on inflight list */ |
| 710 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 711 | /* read/write uses file position */ |
| 712 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 713 | /* must not punt to workers */ |
| 714 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 715 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 716 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 717 | /* regular file */ |
| 718 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 719 | /* needs cleanup */ |
| 720 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 721 | /* already went through poll handler */ |
| 722 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 723 | /* buffer already selected */ |
| 724 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 725 | /* doesn't need file table for this request */ |
| 726 | REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 727 | /* io_wq_work is initialized */ |
| 728 | REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 729 | /* linked timeout is active, i.e. prepared by link's head */ |
| 730 | REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 731 | /* completion is deferred through io_comp_state */ |
| 732 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 733 | }; |
| 734 | |
| 735 | struct async_poll { |
| 736 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 737 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 738 | }; |
| 739 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 740 | struct io_task_work { |
| 741 | struct io_wq_work_node node; |
| 742 | task_work_func_t func; |
| 743 | }; |
| 744 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 745 | /* |
| 746 | * NOTE! Each of the iocb union members has the file pointer |
| 747 | * as the first entry in their struct definition. So you can |
| 748 | * access the file pointer through any of the sub-structs, |
| 749 | * or directly as just 'ki_filp' in this struct. |
| 750 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 751 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 752 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 753 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 754 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 755 | struct io_poll_iocb poll; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 756 | struct io_poll_remove poll_remove; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 757 | struct io_accept accept; |
| 758 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 759 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 760 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 761 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 762 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 763 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 764 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 765 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 766 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 767 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 768 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 769 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 770 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 771 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 772 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 773 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 774 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 775 | struct io_unlink unlink; |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 776 | /* use only after cleaning per-op data, see io_clean_op() */ |
| 777 | struct io_completion compl; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 778 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 779 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 780 | /* opcode allocated if it needs to store data for async defer */ |
| 781 | void *async_data; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 782 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 783 | /* polled IO has completed */ |
| 784 | u8 iopoll_completed; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 785 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 786 | u16 buf_index; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 787 | u32 result; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 788 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 789 | struct io_ring_ctx *ctx; |
| 790 | unsigned int flags; |
| 791 | refcount_t refs; |
| 792 | struct task_struct *task; |
| 793 | u64 user_data; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 794 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 795 | struct io_kiocb *link; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 796 | struct percpu_ref *fixed_rsrc_refs; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 797 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 798 | /* |
| 799 | * 1. used with ctx->iopoll_list with reads/writes |
| 800 | * 2. to track reqs with ->files (see io_op_def::file_table) |
| 801 | */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 802 | struct list_head inflight_entry; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 803 | union { |
| 804 | struct io_task_work io_task_work; |
| 805 | struct callback_head task_work; |
| 806 | }; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 807 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 808 | struct hlist_node hash_node; |
| 809 | struct async_poll *apoll; |
| 810 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 811 | }; |
| 812 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 813 | struct io_defer_entry { |
| 814 | struct list_head list; |
| 815 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 816 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 817 | }; |
| 818 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 819 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 820 | /* needs req->file assigned */ |
| 821 | unsigned needs_file : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 822 | /* hash wq insertion if file is a regular file */ |
| 823 | unsigned hash_reg_file : 1; |
| 824 | /* unbound wq insertion if file is a non-regular file */ |
| 825 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 826 | /* opcode is not supported by this kernel */ |
| 827 | unsigned not_supported : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 828 | /* set if opcode supports polled "wait" */ |
| 829 | unsigned pollin : 1; |
| 830 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 831 | /* op supports buffer selection */ |
| 832 | unsigned buffer_select : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 833 | /* must always have async data allocated */ |
| 834 | unsigned needs_async_data : 1; |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 835 | /* should block plug */ |
| 836 | unsigned plug : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 837 | /* size of async data needed, if any */ |
| 838 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 839 | }; |
| 840 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 841 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 842 | [IORING_OP_NOP] = {}, |
| 843 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 844 | .needs_file = 1, |
| 845 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 846 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 847 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 848 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 849 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 850 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 851 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 852 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 853 | .needs_file = 1, |
| 854 | .hash_reg_file = 1, |
| 855 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 856 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 857 | .needs_async_data = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 858 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 859 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 860 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 861 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 862 | .needs_file = 1, |
| 863 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 864 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 865 | .needs_file = 1, |
| 866 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 867 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 868 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 869 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 870 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 871 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 872 | .needs_file = 1, |
| 873 | .hash_reg_file = 1, |
| 874 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 875 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 876 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 877 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 878 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 879 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 880 | .needs_file = 1, |
| 881 | .unbound_nonreg_file = 1, |
| 882 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 883 | [IORING_OP_POLL_REMOVE] = {}, |
| 884 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 885 | .needs_file = 1, |
| 886 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 887 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 888 | .needs_file = 1, |
| 889 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 890 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 891 | .needs_async_data = 1, |
| 892 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 893 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 894 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 895 | .needs_file = 1, |
| 896 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 897 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 898 | .buffer_select = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 899 | .needs_async_data = 1, |
| 900 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 901 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 902 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 903 | .needs_async_data = 1, |
| 904 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 905 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 906 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 907 | /* used by timeout updates' prep() */ |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 908 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 909 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 910 | .needs_file = 1, |
| 911 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 912 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 913 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 914 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 915 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 916 | .needs_async_data = 1, |
| 917 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 918 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 919 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 920 | .needs_file = 1, |
| 921 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 922 | .pollout = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 923 | .needs_async_data = 1, |
| 924 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 925 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 926 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 927 | .needs_file = 1, |
| 928 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 929 | [IORING_OP_OPENAT] = {}, |
| 930 | [IORING_OP_CLOSE] = {}, |
| 931 | [IORING_OP_FILES_UPDATE] = {}, |
| 932 | [IORING_OP_STATX] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 933 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 934 | .needs_file = 1, |
| 935 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 936 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 937 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 938 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 939 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 940 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 941 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 942 | .needs_file = 1, |
| 943 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 944 | .pollout = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 945 | .plug = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 946 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 947 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 948 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 949 | .needs_file = 1, |
| 950 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 951 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 952 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 953 | .needs_file = 1, |
| 954 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 955 | .pollout = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 956 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 957 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 958 | .needs_file = 1, |
| 959 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 960 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 961 | .buffer_select = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 962 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 963 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 964 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 965 | [IORING_OP_EPOLL_CTL] = { |
| 966 | .unbound_nonreg_file = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 967 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 968 | [IORING_OP_SPLICE] = { |
| 969 | .needs_file = 1, |
| 970 | .hash_reg_file = 1, |
| 971 | .unbound_nonreg_file = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 972 | }, |
| 973 | [IORING_OP_PROVIDE_BUFFERS] = {}, |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 974 | [IORING_OP_REMOVE_BUFFERS] = {}, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 975 | [IORING_OP_TEE] = { |
| 976 | .needs_file = 1, |
| 977 | .hash_reg_file = 1, |
| 978 | .unbound_nonreg_file = 1, |
| 979 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 980 | [IORING_OP_SHUTDOWN] = { |
| 981 | .needs_file = 1, |
| 982 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 983 | [IORING_OP_RENAMEAT] = {}, |
| 984 | [IORING_OP_UNLINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 985 | }; |
| 986 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 987 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 988 | struct task_struct *task, |
| 989 | struct files_struct *files); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 990 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 991 | 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] | 992 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 993 | struct io_ring_ctx *ctx); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 994 | 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] | 995 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 996 | static bool io_rw_reissue(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 997 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 998 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 999 | static void io_put_req_deferred(struct io_kiocb *req, int nr); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1000 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1001 | static void io_dismantle_req(struct io_kiocb *req); |
| 1002 | static void io_put_task(struct task_struct *task, int nr); |
| 1003 | static void io_queue_next(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1004 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1005 | static void __io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1006 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1007 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1008 | struct io_uring_rsrc_update *ip, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1009 | unsigned nr_args); |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1010 | static void __io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1011 | static struct file *io_file_get(struct io_submit_state *state, |
| 1012 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1013 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1014 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1015 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 1016 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 1017 | struct iov_iter *iter, bool needs_lock); |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 1018 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 1019 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 1020 | struct iov_iter *iter, bool force); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1021 | static void io_req_task_queue(struct io_kiocb *req); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1022 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 1023 | struct io_ring_ctx *ctx); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1024 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1025 | static struct kmem_cache *req_cachep; |
| 1026 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1027 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1028 | |
| 1029 | struct sock *io_uring_get_socket(struct file *file) |
| 1030 | { |
| 1031 | #if defined(CONFIG_UNIX) |
| 1032 | if (file->f_op == &io_uring_fops) { |
| 1033 | struct io_ring_ctx *ctx = file->private_data; |
| 1034 | |
| 1035 | return ctx->ring_sock->sk; |
| 1036 | } |
| 1037 | #endif |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1041 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1042 | #define io_for_each_link(pos, head) \ |
| 1043 | for (pos = (head); pos; pos = pos->link) |
| 1044 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1045 | static inline void io_clean_op(struct io_kiocb *req) |
| 1046 | { |
Pavel Begunkov | 9d5c819 | 2021-01-24 15:08:14 +0000 | [diff] [blame] | 1047 | if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1048 | __io_clean_op(req); |
| 1049 | } |
| 1050 | |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1051 | static inline void io_set_resource_node(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1052 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1053 | struct io_ring_ctx *ctx = req->ctx; |
| 1054 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1055 | if (!req->fixed_rsrc_refs) { |
| 1056 | req->fixed_rsrc_refs = &ctx->file_data->node->refs; |
| 1057 | percpu_ref_get(req->fixed_rsrc_refs); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
Pavel Begunkov | 88f171a | 2021-02-20 18:03:50 +0000 | [diff] [blame] | 1061 | static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1062 | { |
| 1063 | if (!percpu_ref_tryget(ref)) { |
| 1064 | /* already at zero, wait for ->release() */ |
| 1065 | if (!try_wait_for_completion(compl)) |
| 1066 | synchronize_rcu(); |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | percpu_ref_resurrect(ref); |
| 1071 | reinit_completion(compl); |
| 1072 | percpu_ref_put(ref); |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1076 | static bool io_match_task(struct io_kiocb *head, |
| 1077 | struct task_struct *task, |
| 1078 | struct files_struct *files) |
| 1079 | { |
| 1080 | struct io_kiocb *req; |
| 1081 | |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1082 | if (task && head->task != task) { |
| 1083 | /* in terms of cancelation, always match if req task is dead */ |
| 1084 | if (head->task->flags & PF_EXITING) |
| 1085 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1086 | return false; |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 1087 | } |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1088 | if (!files) |
| 1089 | return true; |
| 1090 | |
| 1091 | io_for_each_link(req, head) { |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1092 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| 1093 | continue; |
| 1094 | if (req->file && req->file->f_op == &io_uring_fops) |
| 1095 | return true; |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 1096 | if (req->task->files == files) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1097 | return true; |
| 1098 | } |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1102 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1103 | { |
| 1104 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1105 | req->flags |= REQ_F_FAIL_LINK; |
| 1106 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1107 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1108 | static inline void __io_req_init_async(struct io_kiocb *req) |
| 1109 | { |
| 1110 | memset(&req->work, 0, sizeof(req->work)); |
| 1111 | req->flags |= REQ_F_WORK_INITIALIZED; |
| 1112 | } |
| 1113 | |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1114 | /* |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1115 | * Note: must call io_req_init_async() for the first time you |
| 1116 | * touch any members of io_wq_work. |
| 1117 | */ |
| 1118 | static inline void io_req_init_async(struct io_kiocb *req) |
| 1119 | { |
| 1120 | if (req->flags & REQ_F_WORK_INITIALIZED) |
| 1121 | return; |
| 1122 | |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 1123 | __io_req_init_async(req); |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1126 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 1127 | { |
| 1128 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1129 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1130 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1133 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1134 | { |
| 1135 | return !req->timeout.off; |
| 1136 | } |
| 1137 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1138 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 1139 | { |
| 1140 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1141 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1142 | |
| 1143 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1144 | if (!ctx) |
| 1145 | return NULL; |
| 1146 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1147 | /* |
| 1148 | * Use 5 bits less than the max cq entries, that should give us around |
| 1149 | * 32 entries per hash list if totally full and uniformly spread. |
| 1150 | */ |
| 1151 | hash_bits = ilog2(p->cq_entries); |
| 1152 | hash_bits -= 5; |
| 1153 | if (hash_bits <= 0) |
| 1154 | hash_bits = 1; |
| 1155 | ctx->cancel_hash_bits = hash_bits; |
| 1156 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1157 | GFP_KERNEL); |
| 1158 | if (!ctx->cancel_hash) |
| 1159 | goto err; |
| 1160 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1161 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1162 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1163 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1164 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1165 | |
| 1166 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1167 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1168 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1169 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1170 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1171 | init_completion(&ctx->ref_comp); |
| 1172 | init_completion(&ctx->sq_thread_comp); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 1173 | idr_init(&ctx->io_buffer_idr); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 1174 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1175 | mutex_init(&ctx->uring_lock); |
| 1176 | init_waitqueue_head(&ctx->wait); |
| 1177 | spin_lock_init(&ctx->completion_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 1178 | INIT_LIST_HEAD(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1179 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1180 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1181 | spin_lock_init(&ctx->inflight_lock); |
| 1182 | INIT_LIST_HEAD(&ctx->inflight_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1183 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1184 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1185 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1186 | init_llist_head(&ctx->rsrc_put_llist); |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 1187 | INIT_LIST_HEAD(&ctx->submit_state.comp.free_list); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1188 | INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1189 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1190 | err: |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1191 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1192 | kfree(ctx); |
| 1193 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1196 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1197 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1198 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1199 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1200 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1201 | return seq != ctx->cached_cq_tail |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1202 | + READ_ONCE(ctx->cached_cq_overflow); |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1203 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1204 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1205 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1206 | } |
| 1207 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1208 | static void io_req_clean_work(struct io_kiocb *req) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1209 | { |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 1210 | if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1211 | return; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1212 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 1213 | if (req->work.creds) { |
| 1214 | put_cred(req->work.creds); |
| 1215 | req->work.creds = NULL; |
| 1216 | } |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1217 | if (req->flags & REQ_F_INFLIGHT) { |
| 1218 | struct io_ring_ctx *ctx = req->ctx; |
| 1219 | struct io_uring_task *tctx = req->task->io_uring; |
| 1220 | unsigned long flags; |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1221 | |
Pavel Begunkov | 34e08fe | 2021-02-01 18:59:53 +0000 | [diff] [blame] | 1222 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1223 | list_del(&req->inflight_entry); |
| 1224 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1225 | req->flags &= ~REQ_F_INFLIGHT; |
| 1226 | if (atomic_read(&tctx->in_idle)) |
| 1227 | wake_up(&tctx->wait); |
| 1228 | } |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1229 | |
Pavel Begunkov | e86d004 | 2021-02-01 18:59:54 +0000 | [diff] [blame] | 1230 | req->flags &= ~REQ_F_WORK_INITIALIZED; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 1231 | } |
| 1232 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1233 | static void io_req_track_inflight(struct io_kiocb *req) |
| 1234 | { |
| 1235 | struct io_ring_ctx *ctx = req->ctx; |
| 1236 | |
| 1237 | if (!(req->flags & REQ_F_INFLIGHT)) { |
| 1238 | io_req_init_async(req); |
| 1239 | req->flags |= REQ_F_INFLIGHT; |
| 1240 | |
| 1241 | spin_lock_irq(&ctx->inflight_lock); |
| 1242 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 1243 | spin_unlock_irq(&ctx->inflight_lock); |
| 1244 | } |
| 1245 | } |
| 1246 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1247 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1248 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1249 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1250 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1251 | |
Pavel Begunkov | 16d5980 | 2020-07-12 16:16:47 +0300 | [diff] [blame] | 1252 | io_req_init_async(req); |
| 1253 | |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1254 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1255 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1256 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1257 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1258 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1259 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1260 | } else { |
| 1261 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1262 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1263 | } |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 1264 | if (!req->work.creds) |
| 1265 | req->work.creds = get_current_cred(); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1266 | } |
| 1267 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1268 | static void io_prep_async_link(struct io_kiocb *req) |
| 1269 | { |
| 1270 | struct io_kiocb *cur; |
| 1271 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1272 | io_for_each_link(cur, req) |
| 1273 | io_prep_async_work(cur); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1274 | } |
| 1275 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1276 | static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1277 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1278 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1279 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1280 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1281 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1282 | BUG_ON(!tctx); |
| 1283 | BUG_ON(!tctx->io_wq); |
| 1284 | |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1285 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1286 | &req->work, req->flags); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1287 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1288 | return link; |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 1289 | } |
| 1290 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1291 | static void io_queue_async_work(struct io_kiocb *req) |
| 1292 | { |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1293 | struct io_kiocb *link; |
| 1294 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1295 | /* init ->work of the whole link before punting */ |
| 1296 | io_prep_async_link(req); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1297 | link = __io_queue_async_work(req); |
| 1298 | |
| 1299 | if (link) |
| 1300 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1301 | } |
| 1302 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1303 | static void io_kill_timeout(struct io_kiocb *req) |
| 1304 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1305 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1306 | int ret; |
| 1307 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1308 | ret = hrtimer_try_to_cancel(&io->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1309 | if (ret != -1) { |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1310 | atomic_set(&req->ctx->cq_timeouts, |
| 1311 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1312 | list_del_init(&req->timeout.list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1313 | io_cqring_fill_event(req, 0); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1314 | io_put_req_deferred(req, 1); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1318 | /* |
| 1319 | * Returns true if we found and killed one or more timeouts |
| 1320 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1321 | static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 1322 | struct files_struct *files) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1323 | { |
| 1324 | struct io_kiocb *req, *tmp; |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1325 | int canceled = 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1326 | |
| 1327 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1328 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 1329 | if (io_match_task(req, tsk, files)) { |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1330 | io_kill_timeout(req); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1331 | canceled++; |
| 1332 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 1333 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1334 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 1335 | return canceled != 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1336 | } |
| 1337 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1338 | static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| 1339 | { |
| 1340 | do { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1341 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1342 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1343 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1344 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1345 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1346 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1347 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1348 | kfree(de); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1349 | } while (!list_empty(&ctx->defer_list)); |
| 1350 | } |
| 1351 | |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1352 | static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| 1353 | { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1354 | u32 seq; |
| 1355 | |
| 1356 | if (list_empty(&ctx->timeout_list)) |
| 1357 | return; |
| 1358 | |
| 1359 | seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 1360 | |
| 1361 | do { |
| 1362 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1363 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1364 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1365 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1366 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1367 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1368 | |
| 1369 | /* |
| 1370 | * Since seq can easily wrap around over time, subtract |
| 1371 | * the last seq at which timeouts were flushed before comparing. |
| 1372 | * Assuming not more than 2^31-1 events have happened since, |
| 1373 | * these subtractions won't have wrapped, so we can check if |
| 1374 | * target is in [last_seq, current_seq] by comparing the two. |
| 1375 | */ |
| 1376 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1377 | events_got = seq - ctx->cq_last_tm_flush; |
| 1378 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1379 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1380 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1381 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1382 | io_kill_timeout(req); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1383 | } while (!list_empty(&ctx->timeout_list)); |
| 1384 | |
| 1385 | ctx->cq_last_tm_flush = seq; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1386 | } |
| 1387 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1388 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1389 | { |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1390 | io_flush_timeouts(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1391 | |
| 1392 | /* order cqe stores with ring update */ |
| 1393 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1394 | |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1395 | if (unlikely(!list_empty(&ctx->defer_list))) |
| 1396 | __io_queue_deferred(ctx); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1397 | } |
| 1398 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1399 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1400 | { |
| 1401 | struct io_rings *r = ctx->rings; |
| 1402 | |
| 1403 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| 1404 | } |
| 1405 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1406 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1407 | { |
| 1408 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1409 | } |
| 1410 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1411 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 1412 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1413 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1414 | unsigned tail; |
| 1415 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1416 | /* |
| 1417 | * writes to the cq entry need to come after reading head; the |
| 1418 | * control dependency is enough as we're using WRITE_ONCE to |
| 1419 | * fill the cq entry |
| 1420 | */ |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1421 | if (__io_cqring_events(ctx) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1422 | return NULL; |
| 1423 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1424 | tail = ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1425 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1428 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1429 | { |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1430 | if (!ctx->cq_ev_fd) |
| 1431 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1432 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1433 | return false; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1434 | if (!ctx->eventfd_async) |
| 1435 | return true; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1436 | return io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1439 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1440 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1441 | /* see waitqueue_active() comment */ |
| 1442 | smp_mb(); |
| 1443 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1444 | if (waitqueue_active(&ctx->wait)) |
| 1445 | wake_up(&ctx->wait); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 1446 | if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| 1447 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1448 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1449 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1450 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1451 | wake_up_interruptible(&ctx->cq_wait); |
| 1452 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1453 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1454 | } |
| 1455 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1456 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1457 | { |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1458 | /* see waitqueue_active() comment */ |
| 1459 | smp_mb(); |
| 1460 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1461 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 1462 | if (waitqueue_active(&ctx->wait)) |
| 1463 | wake_up(&ctx->wait); |
| 1464 | } |
| 1465 | if (io_should_trigger_evfd(ctx)) |
| 1466 | eventfd_signal(ctx->cq_ev_fd, 1); |
Pavel Begunkov | b1445e5 | 2021-01-07 03:15:43 +0000 | [diff] [blame] | 1467 | if (waitqueue_active(&ctx->cq_wait)) { |
Pavel Begunkov | 4aa84f2 | 2021-01-07 03:15:42 +0000 | [diff] [blame] | 1468 | wake_up_interruptible(&ctx->cq_wait); |
| 1469 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 1470 | } |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1473 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1474 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1475 | struct task_struct *tsk, |
| 1476 | struct files_struct *files) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1477 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1478 | struct io_rings *rings = ctx->rings; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1479 | struct io_kiocb *req, *tmp; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1480 | struct io_uring_cqe *cqe; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1481 | unsigned long flags; |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1482 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1483 | LIST_HEAD(list); |
| 1484 | |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1485 | if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries) |
| 1486 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1487 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1488 | posted = false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1489 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1490 | 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] | 1491 | if (!io_match_task(req, tsk, files)) |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1492 | continue; |
| 1493 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1494 | cqe = io_get_cqring(ctx); |
| 1495 | if (!cqe && !force) |
| 1496 | break; |
| 1497 | |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1498 | list_move(&req->compl.list, &list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1499 | if (cqe) { |
| 1500 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1501 | WRITE_ONCE(cqe->res, req->result); |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1502 | WRITE_ONCE(cqe->flags, req->compl.cflags); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1503 | } else { |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1504 | ctx->cached_cq_overflow++; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1505 | WRITE_ONCE(ctx->rings->cq_overflow, |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1506 | ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1507 | } |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1508 | posted = true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1511 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1512 | if (all_flushed) { |
| 1513 | clear_bit(0, &ctx->sq_check_overflow); |
| 1514 | clear_bit(0, &ctx->cq_check_overflow); |
| 1515 | ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| 1516 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1517 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1518 | if (posted) |
| 1519 | io_commit_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1520 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1521 | if (posted) |
| 1522 | io_cqring_ev_posted(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1523 | |
| 1524 | while (!list_empty(&list)) { |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1525 | req = list_first_entry(&list, struct io_kiocb, compl.list); |
| 1526 | list_del(&req->compl.list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1527 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1528 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1529 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1530 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1533 | static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| 1534 | struct task_struct *tsk, |
| 1535 | struct files_struct *files) |
| 1536 | { |
| 1537 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1538 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1539 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1540 | mutex_lock(&ctx->uring_lock); |
| 1541 | __io_cqring_overflow_flush(ctx, force, tsk, files); |
| 1542 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1543 | mutex_unlock(&ctx->uring_lock); |
| 1544 | } |
| 1545 | } |
| 1546 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1547 | 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] | 1548 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1549 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1550 | struct io_uring_cqe *cqe; |
| 1551 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1552 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1553 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1554 | /* |
| 1555 | * If we can't get a cq entry, userspace overflowed the |
| 1556 | * submission (by quite a lot). Increment the overflow count in |
| 1557 | * the ring. |
| 1558 | */ |
| 1559 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1560 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1561 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1562 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1563 | WRITE_ONCE(cqe->flags, cflags); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 1564 | } else if (ctx->cq_overflow_flushed || |
| 1565 | atomic_read(&req->task->io_uring->in_idle)) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 1566 | /* |
| 1567 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1568 | * then we cannot store the request for later flushing, we need |
| 1569 | * to drop it on the floor. |
| 1570 | */ |
Pavel Begunkov | 2c3bac6d | 2020-10-18 10:17:40 +0100 | [diff] [blame] | 1571 | ctx->cached_cq_overflow++; |
| 1572 | WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1573 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1574 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1575 | set_bit(0, &ctx->sq_check_overflow); |
| 1576 | set_bit(0, &ctx->cq_check_overflow); |
Xiaoguang Wang | 6d5f904 | 2020-07-09 09:15:29 +0800 | [diff] [blame] | 1577 | ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1578 | } |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1579 | io_clean_op(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1580 | req->result = res; |
Pavel Begunkov | 0f7e466 | 2020-07-13 23:37:16 +0300 | [diff] [blame] | 1581 | req->compl.cflags = cflags; |
Pavel Begunkov | 40d8ddd | 2020-07-13 23:37:11 +0300 | [diff] [blame] | 1582 | refcount_inc(&req->refs); |
| 1583 | list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1587 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| 1588 | { |
| 1589 | __io_cqring_fill_event(req, res, 0); |
| 1590 | } |
| 1591 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1592 | static inline void io_req_complete_post(struct io_kiocb *req, long res, |
| 1593 | unsigned int cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1594 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1595 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1596 | unsigned long flags; |
| 1597 | |
| 1598 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1599 | __io_cqring_fill_event(req, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1600 | io_commit_cqring(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1601 | /* |
| 1602 | * If we're the last reference to this request, add to our locked |
| 1603 | * free_list cache. |
| 1604 | */ |
| 1605 | if (refcount_dec_and_test(&req->refs)) { |
| 1606 | struct io_comp_state *cs = &ctx->submit_state.comp; |
| 1607 | |
| 1608 | io_dismantle_req(req); |
| 1609 | io_put_task(req->task, 1); |
| 1610 | list_add(&req->compl.list, &cs->locked_free_list); |
| 1611 | cs->locked_free_nr++; |
| 1612 | } else |
| 1613 | req = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1614 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1615 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1616 | io_cqring_ev_posted(ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1617 | if (req) { |
| 1618 | io_queue_next(req); |
| 1619 | percpu_ref_put(&ctx->refs); |
| 1620 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1623 | static void io_req_complete_state(struct io_kiocb *req, long res, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1624 | unsigned int cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1625 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1626 | io_clean_op(req); |
| 1627 | req->result = res; |
| 1628 | req->compl.cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1629 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1630 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1631 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1632 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
| 1633 | long res, unsigned cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1634 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1635 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1636 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1637 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1638 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1639 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1640 | |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1641 | static inline void io_req_complete(struct io_kiocb *req, long res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1642 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1643 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1646 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1647 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1648 | struct io_submit_state *state = &ctx->submit_state; |
| 1649 | struct io_comp_state *cs = &state->comp; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1650 | struct io_kiocb *req = NULL; |
| 1651 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1652 | /* |
| 1653 | * If we have more than a batch's worth of requests in our IRQ side |
| 1654 | * locked cache, grab the lock and move them over to our submission |
| 1655 | * side cache. |
| 1656 | */ |
| 1657 | if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) { |
| 1658 | spin_lock_irq(&ctx->completion_lock); |
| 1659 | list_splice_init(&cs->locked_free_list, &cs->free_list); |
| 1660 | cs->locked_free_nr = 0; |
| 1661 | spin_unlock_irq(&ctx->completion_lock); |
| 1662 | } |
| 1663 | |
| 1664 | while (!list_empty(&cs->free_list)) { |
| 1665 | req = list_first_entry(&cs->free_list, struct io_kiocb, |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1666 | compl.list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1667 | list_del(&req->compl.list); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1668 | state->reqs[state->free_reqs++] = req; |
| 1669 | if (state->free_reqs == ARRAY_SIZE(state->reqs)) |
| 1670 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1671 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1672 | |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1673 | return req != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1676 | static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1677 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 1678 | struct io_submit_state *state = &ctx->submit_state; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1679 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1680 | BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1681 | |
Pavel Begunkov | f6b6c7d | 2020-06-21 13:09:53 +0300 | [diff] [blame] | 1682 | if (!state->free_reqs) { |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1683 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1684 | int ret; |
| 1685 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1686 | if (io_flush_cached_reqs(ctx)) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1687 | goto got_req; |
| 1688 | |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 1689 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH, |
| 1690 | state->reqs); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1691 | |
| 1692 | /* |
| 1693 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1694 | * retry single alloc to be on the safe side. |
| 1695 | */ |
| 1696 | if (unlikely(ret <= 0)) { |
| 1697 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1698 | if (!state->reqs[0]) |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1699 | return NULL; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1700 | ret = 1; |
| 1701 | } |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1702 | state->free_reqs = ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1703 | } |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1704 | got_req: |
Pavel Begunkov | 291b282 | 2020-09-30 22:57:01 +0300 | [diff] [blame] | 1705 | state->free_reqs--; |
| 1706 | return state->reqs[state->free_reqs]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1709 | static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| 1710 | bool fixed) |
| 1711 | { |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1712 | if (!fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1713 | fput(file); |
| 1714 | } |
| 1715 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1716 | static void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1717 | { |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 1718 | io_clean_op(req); |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 1719 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1720 | if (req->async_data) |
| 1721 | kfree(req->async_data); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 1722 | if (req->file) |
| 1723 | io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1724 | if (req->fixed_rsrc_refs) |
| 1725 | percpu_ref_put(req->fixed_rsrc_refs); |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 1726 | io_req_clean_work(req); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1727 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1728 | |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1729 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1730 | { |
| 1731 | struct io_uring_task *tctx = task->io_uring; |
| 1732 | |
| 1733 | percpu_counter_sub(&tctx->inflight, nr); |
| 1734 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1735 | wake_up(&tctx->wait); |
| 1736 | put_task_struct_many(task, nr); |
| 1737 | } |
| 1738 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1739 | static void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1740 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 1741 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1742 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1743 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 1744 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 1745 | |
Pavel Begunkov | 3893f39 | 2021-02-10 00:03:15 +0000 | [diff] [blame] | 1746 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 1747 | percpu_ref_put(&ctx->refs); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1748 | } |
| 1749 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1750 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 1751 | { |
| 1752 | struct io_kiocb *nxt = req->link; |
| 1753 | |
| 1754 | req->link = nxt->link; |
| 1755 | nxt->link = NULL; |
| 1756 | } |
| 1757 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1758 | static void io_kill_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1759 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1760 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1761 | struct io_kiocb *link; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1762 | bool cancelled = false; |
| 1763 | unsigned long flags; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1764 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1765 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1766 | link = req->link; |
| 1767 | |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 1768 | /* |
| 1769 | * Can happen if a linked timeout fired and link had been like |
| 1770 | * req -> link t-out -> link t-out [-> ...] |
| 1771 | */ |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1772 | if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| 1773 | struct io_timeout_data *io = link->async_data; |
| 1774 | int ret; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1775 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1776 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 1777 | link->timeout.head = NULL; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1778 | ret = hrtimer_try_to_cancel(&io->timer); |
| 1779 | if (ret != -1) { |
| 1780 | io_cqring_fill_event(link, -ECANCELED); |
| 1781 | io_commit_cqring(ctx); |
| 1782 | cancelled = true; |
| 1783 | } |
| 1784 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1785 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1786 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Jens Axboe | ab0b645 | 2020-06-30 08:43:15 -0600 | [diff] [blame] | 1787 | |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1788 | if (cancelled) { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1789 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 1790 | io_put_req(link); |
| 1791 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1792 | } |
| 1793 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1794 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1795 | static void io_fail_links(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1796 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1797 | struct io_kiocb *link, *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1798 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1799 | unsigned long flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1800 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 1801 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1802 | link = req->link; |
| 1803 | req->link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1804 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1805 | while (link) { |
| 1806 | nxt = link->link; |
| 1807 | link->link = NULL; |
| 1808 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1809 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1810 | io_cqring_fill_event(link, -ECANCELED); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1811 | |
| 1812 | /* |
| 1813 | * It's ok to free under spinlock as they're not linked anymore, |
| 1814 | * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| 1815 | * work.fs->lock. |
| 1816 | */ |
| 1817 | if (link->flags & REQ_F_WORK_INITIALIZED) |
| 1818 | io_put_req_deferred(link, 2); |
| 1819 | else |
| 1820 | io_double_put_req(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1821 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1822 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1823 | io_commit_cqring(ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 1824 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1825 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1826 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1827 | } |
| 1828 | |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1829 | static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1830 | { |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 1831 | if (req->flags & REQ_F_LINK_TIMEOUT) |
| 1832 | io_kill_linked_timeout(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1833 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1834 | /* |
| 1835 | * If LINK is set, we have dependent requests in this chain. If we |
| 1836 | * didn't fail this request, queue the first one up, moving any other |
| 1837 | * dependencies to the next request. In case of failure, fail the rest |
| 1838 | * of the chain. |
| 1839 | */ |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1840 | if (likely(!(req->flags & REQ_F_FAIL_LINK))) { |
| 1841 | struct io_kiocb *nxt = req->link; |
| 1842 | |
| 1843 | req->link = NULL; |
| 1844 | return nxt; |
| 1845 | } |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 1846 | io_fail_links(req); |
| 1847 | return NULL; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1848 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1849 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1850 | 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] | 1851 | { |
Pavel Begunkov | cdbff98 | 2021-02-12 18:41:16 +0000 | [diff] [blame] | 1852 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
Pavel Begunkov | 3fa5e0f | 2020-06-30 15:20:43 +0300 | [diff] [blame] | 1853 | return NULL; |
| 1854 | return __io_req_find_next(req); |
| 1855 | } |
| 1856 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1857 | static bool __tctx_task_work(struct io_uring_task *tctx) |
| 1858 | { |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1859 | struct io_ring_ctx *ctx = NULL; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1860 | struct io_wq_work_list list; |
| 1861 | struct io_wq_work_node *node; |
| 1862 | |
| 1863 | if (wq_list_empty(&tctx->task_list)) |
| 1864 | return false; |
| 1865 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1866 | spin_lock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1867 | list = tctx->task_list; |
| 1868 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1869 | spin_unlock_irq(&tctx->task_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1870 | |
| 1871 | node = list.first; |
| 1872 | while (node) { |
| 1873 | struct io_wq_work_node *next = node->next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1874 | struct io_ring_ctx *this_ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1875 | struct io_kiocb *req; |
| 1876 | |
| 1877 | req = container_of(node, struct io_kiocb, io_task_work.node); |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1878 | this_ctx = req->ctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1879 | req->task_work.func(&req->task_work); |
| 1880 | node = next; |
Jens Axboe | 65453d1 | 2021-02-10 00:03:21 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (!ctx) { |
| 1883 | ctx = this_ctx; |
| 1884 | } else if (ctx != this_ctx) { |
| 1885 | mutex_lock(&ctx->uring_lock); |
| 1886 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1887 | mutex_unlock(&ctx->uring_lock); |
| 1888 | ctx = this_ctx; |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | if (ctx && ctx->submit_state.comp.nr) { |
| 1893 | mutex_lock(&ctx->uring_lock); |
| 1894 | io_submit_flush_completions(&ctx->submit_state.comp, ctx); |
| 1895 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | return list.first != NULL; |
| 1899 | } |
| 1900 | |
| 1901 | static void tctx_task_work(struct callback_head *cb) |
| 1902 | { |
| 1903 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work); |
| 1904 | |
| 1905 | while (__tctx_task_work(tctx)) |
| 1906 | cond_resched(); |
| 1907 | |
| 1908 | clear_bit(0, &tctx->task_state); |
| 1909 | } |
| 1910 | |
| 1911 | static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req, |
| 1912 | enum task_work_notify_mode notify) |
| 1913 | { |
| 1914 | struct io_uring_task *tctx = tsk->io_uring; |
| 1915 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1916 | unsigned long flags; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1917 | int ret; |
| 1918 | |
| 1919 | WARN_ON_ONCE(!tctx); |
| 1920 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1921 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1922 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1923 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1924 | |
| 1925 | /* task_work already pending, we're done */ |
| 1926 | if (test_bit(0, &tctx->task_state) || |
| 1927 | test_and_set_bit(0, &tctx->task_state)) |
| 1928 | return 0; |
| 1929 | |
| 1930 | if (!task_work_add(tsk, &tctx->task_work, notify)) |
| 1931 | return 0; |
| 1932 | |
| 1933 | /* |
| 1934 | * Slow path - we failed, find and delete work. if the work is not |
| 1935 | * in the list, it got run and we're fine. |
| 1936 | */ |
| 1937 | ret = 0; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1938 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1939 | wq_list_for_each(node, prev, &tctx->task_list) { |
| 1940 | if (&req->io_task_work.node == node) { |
| 1941 | wq_list_del(&tctx->task_list, node, prev); |
| 1942 | ret = 1; |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 1946 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1947 | clear_bit(0, &tctx->task_state); |
| 1948 | return ret; |
| 1949 | } |
| 1950 | |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1951 | static int io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1952 | { |
| 1953 | struct task_struct *tsk = req->task; |
| 1954 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 1955 | enum task_work_notify_mode notify; |
| 1956 | int ret; |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1957 | |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 1958 | if (tsk->flags & PF_EXITING) |
| 1959 | return -ESRCH; |
| 1960 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1961 | /* |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1962 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 1963 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 1964 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 1965 | * will do the job. |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1966 | */ |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 1967 | notify = TWA_NONE; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 1968 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1969 | notify = TWA_SIGNAL; |
| 1970 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1971 | ret = io_task_work_add(tsk, req, notify); |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1972 | if (!ret) |
| 1973 | wake_up_process(tsk); |
Jens Axboe | 0ba9c9e | 2020-08-06 19:41:50 -0600 | [diff] [blame] | 1974 | |
Jens Axboe | c2c4c83 | 2020-07-01 15:37:11 -0600 | [diff] [blame] | 1975 | return ret; |
| 1976 | } |
| 1977 | |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1978 | static void io_req_task_work_add_fallback(struct io_kiocb *req, |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 1979 | task_work_func_t cb) |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1980 | { |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 1981 | struct io_ring_ctx *ctx = req->ctx; |
| 1982 | struct callback_head *head; |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1983 | |
| 1984 | init_task_work(&req->task_work, cb); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 1985 | do { |
| 1986 | head = READ_ONCE(ctx->exit_task_work); |
| 1987 | req->task_work.next = head; |
| 1988 | } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1991 | static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| 1992 | { |
| 1993 | struct io_ring_ctx *ctx = req->ctx; |
| 1994 | |
| 1995 | spin_lock_irq(&ctx->completion_lock); |
| 1996 | io_cqring_fill_event(req, error); |
| 1997 | io_commit_cqring(ctx); |
| 1998 | spin_unlock_irq(&ctx->completion_lock); |
| 1999 | |
| 2000 | io_cqring_ev_posted(ctx); |
| 2001 | req_set_fail_links(req); |
| 2002 | io_double_put_req(req); |
| 2003 | } |
| 2004 | |
| 2005 | static void io_req_task_cancel(struct callback_head *cb) |
| 2006 | { |
| 2007 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2008 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2009 | |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2010 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2011 | __io_req_task_cancel(req, req->result); |
Pavel Begunkov | 792bb6e | 2021-02-18 22:32:51 +0000 | [diff] [blame] | 2012 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2013 | percpu_ref_put(&ctx->refs); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | static void __io_req_task_submit(struct io_kiocb *req) |
| 2017 | { |
| 2018 | struct io_ring_ctx *ctx = req->ctx; |
| 2019 | |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2020 | /* ctx stays valid until unlock, even if we drop all ours ctx->refs */ |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2021 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 2022 | if (!ctx->sqo_dead && !(current->flags & PF_EXITING)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2023 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2024 | else |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2025 | __io_req_task_cancel(req, -EFAULT); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2026 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2027 | } |
| 2028 | |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2029 | static void io_req_task_submit(struct callback_head *cb) |
| 2030 | { |
| 2031 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2032 | |
| 2033 | __io_req_task_submit(req); |
| 2034 | } |
| 2035 | |
| 2036 | static void io_req_task_queue(struct io_kiocb *req) |
| 2037 | { |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2038 | int ret; |
| 2039 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2040 | req->task_work.func = io_req_task_submit; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2041 | ret = io_req_task_work_add(req); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2042 | if (unlikely(ret)) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2043 | req->result = -ECANCELED; |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 2044 | percpu_ref_get(&req->ctx->refs); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2045 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2046 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2047 | } |
| 2048 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2049 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2050 | { |
| 2051 | percpu_ref_get(&req->ctx->refs); |
| 2052 | req->result = ret; |
| 2053 | req->task_work.func = io_req_task_cancel; |
| 2054 | |
| 2055 | if (unlikely(io_req_task_work_add(req))) |
| 2056 | io_req_task_work_add_fallback(req, io_req_task_cancel); |
| 2057 | } |
| 2058 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2059 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2060 | { |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2061 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2062 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2063 | if (nxt) |
| 2064 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2065 | } |
| 2066 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2067 | static void io_free_req(struct io_kiocb *req) |
| 2068 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2069 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2070 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2071 | } |
| 2072 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2073 | struct req_batch { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2074 | struct task_struct *task; |
| 2075 | int task_refs; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 2076 | int ctx_refs; |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2077 | }; |
| 2078 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2079 | static inline void io_init_req_batch(struct req_batch *rb) |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2080 | { |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2081 | rb->task_refs = 0; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2082 | rb->ctx_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2083 | rb->task = NULL; |
| 2084 | } |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 2085 | |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2086 | static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| 2087 | struct req_batch *rb) |
| 2088 | { |
Pavel Begunkov | 6e833d5 | 2021-02-11 18:28:20 +0000 | [diff] [blame] | 2089 | if (rb->task) |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2090 | io_put_task(rb->task, rb->task_refs); |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2091 | if (rb->ctx_refs) |
| 2092 | percpu_ref_put_many(&ctx->refs, rb->ctx_refs); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2093 | } |
| 2094 | |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2095 | static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req, |
| 2096 | struct io_submit_state *state) |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2097 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2098 | io_queue_next(req); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2099 | |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2100 | if (req->task != rb->task) { |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2101 | if (rb->task) |
| 2102 | io_put_task(rb->task, rb->task_refs); |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2103 | rb->task = req->task; |
| 2104 | rb->task_refs = 0; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2105 | } |
Jens Axboe | e3bc8e9 | 2020-09-24 08:45:57 -0600 | [diff] [blame] | 2106 | rb->task_refs++; |
Pavel Begunkov | 9ae7246 | 2021-02-10 00:03:16 +0000 | [diff] [blame] | 2107 | rb->ctx_refs++; |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2108 | |
Pavel Begunkov | 4edf20f | 2020-10-13 09:43:59 +0100 | [diff] [blame] | 2109 | io_dismantle_req(req); |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2110 | if (state->free_reqs != ARRAY_SIZE(state->reqs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2111 | state->reqs[state->free_reqs++] = req; |
Pavel Begunkov | bd75904 | 2021-02-12 03:23:50 +0000 | [diff] [blame] | 2112 | else |
| 2113 | list_add(&req->compl.list, &state->comp.free_list); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2114 | } |
| 2115 | |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2116 | static void io_submit_flush_completions(struct io_comp_state *cs, |
| 2117 | struct io_ring_ctx *ctx) |
| 2118 | { |
| 2119 | int i, nr = cs->nr; |
| 2120 | struct io_kiocb *req; |
| 2121 | struct req_batch rb; |
| 2122 | |
| 2123 | io_init_req_batch(&rb); |
| 2124 | spin_lock_irq(&ctx->completion_lock); |
| 2125 | for (i = 0; i < nr; i++) { |
| 2126 | req = cs->reqs[i]; |
| 2127 | __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| 2128 | } |
| 2129 | io_commit_cqring(ctx); |
| 2130 | spin_unlock_irq(&ctx->completion_lock); |
| 2131 | |
| 2132 | io_cqring_ev_posted(ctx); |
| 2133 | for (i = 0; i < nr; i++) { |
| 2134 | req = cs->reqs[i]; |
| 2135 | |
| 2136 | /* submission and completion refs */ |
| 2137 | if (refcount_sub_and_test(2, &req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2138 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | io_req_free_batch_finish(ctx, &rb); |
| 2142 | cs->nr = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2143 | } |
| 2144 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2145 | /* |
| 2146 | * Drop reference to request, return next in chain (if there is one) if this |
| 2147 | * was the last reference to this request. |
| 2148 | */ |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2149 | 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] | 2150 | { |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2151 | struct io_kiocb *nxt = NULL; |
| 2152 | |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2153 | if (refcount_dec_and_test(&req->refs)) { |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2154 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2155 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2156 | } |
Pavel Begunkov | 9b5f7bd | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2157 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2160 | static void io_put_req(struct io_kiocb *req) |
| 2161 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2162 | if (refcount_dec_and_test(&req->refs)) |
| 2163 | io_free_req(req); |
| 2164 | } |
| 2165 | |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2166 | static void io_put_req_deferred_cb(struct callback_head *cb) |
| 2167 | { |
| 2168 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 2169 | |
| 2170 | io_free_req(req); |
| 2171 | } |
| 2172 | |
| 2173 | static void io_free_req_deferred(struct io_kiocb *req) |
| 2174 | { |
| 2175 | int ret; |
| 2176 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2177 | req->task_work.func = io_put_req_deferred_cb; |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 2178 | ret = io_req_task_work_add(req); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2179 | if (unlikely(ret)) |
| 2180 | io_req_task_work_add_fallback(req, io_put_req_deferred_cb); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| 2184 | { |
| 2185 | if (refcount_sub_and_test(refs, &req->refs)) |
| 2186 | io_free_req_deferred(req); |
| 2187 | } |
| 2188 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 2189 | static void io_double_put_req(struct io_kiocb *req) |
| 2190 | { |
| 2191 | /* drop both submit and complete references */ |
| 2192 | if (refcount_sub_and_test(2, &req->refs)) |
| 2193 | io_free_req(req); |
| 2194 | } |
| 2195 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2196 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2197 | { |
| 2198 | /* See comment at the top of this file */ |
| 2199 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2200 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2201 | } |
| 2202 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2203 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2204 | { |
| 2205 | struct io_rings *rings = ctx->rings; |
| 2206 | |
| 2207 | /* make sure SQ entry isn't read before tail */ |
| 2208 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2209 | } |
| 2210 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2211 | 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] | 2212 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2213 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2214 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2215 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2216 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2217 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2218 | kfree(kbuf); |
| 2219 | return cflags; |
| 2220 | } |
| 2221 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2222 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2223 | { |
| 2224 | struct io_buffer *kbuf; |
| 2225 | |
| 2226 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2227 | return io_put_kbuf(req, kbuf); |
| 2228 | } |
| 2229 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2230 | static inline bool io_run_task_work(void) |
| 2231 | { |
Jens Axboe | 6200b0a | 2020-09-13 14:38:30 -0600 | [diff] [blame] | 2232 | /* |
| 2233 | * Not safe to run on exiting task, and the task_work handling will |
| 2234 | * not add work to such a task. |
| 2235 | */ |
| 2236 | if (unlikely(current->flags & PF_EXITING)) |
| 2237 | return false; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2238 | if (current->task_works) { |
| 2239 | __set_current_state(TASK_RUNNING); |
| 2240 | task_work_run(); |
| 2241 | return true; |
| 2242 | } |
| 2243 | |
| 2244 | return false; |
| 2245 | } |
| 2246 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2247 | /* |
| 2248 | * Find and free completed poll iocbs |
| 2249 | */ |
| 2250 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2251 | struct list_head *done) |
| 2252 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 2253 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2254 | struct io_kiocb *req; |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2255 | |
| 2256 | /* order with ->result store in io_complete_rw_iopoll() */ |
| 2257 | smp_rmb(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2258 | |
Pavel Begunkov | 5af1d13 | 2020-07-18 11:32:52 +0300 | [diff] [blame] | 2259 | io_init_req_batch(&rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2260 | while (!list_empty(done)) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2261 | int cflags = 0; |
| 2262 | |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2263 | req = list_first_entry(done, struct io_kiocb, inflight_entry); |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2264 | list_del(&req->inflight_entry); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2265 | |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2266 | if (READ_ONCE(req->result) == -EAGAIN) { |
| 2267 | req->iopoll_completed = 0; |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2268 | if (io_rw_reissue(req)) |
Pavel Begunkov | f161340 | 2021-02-11 18:28:21 +0000 | [diff] [blame] | 2269 | continue; |
| 2270 | } |
| 2271 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2272 | if (req->flags & REQ_F_BUFFER_SELECTED) |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2273 | cflags = io_put_rw_kbuf(req); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2274 | |
| 2275 | __io_cqring_fill_event(req, req->result, cflags); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2276 | (*nr_events)++; |
| 2277 | |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2278 | if (refcount_dec_and_test(&req->refs)) |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 2279 | io_req_free_batch(&rb, req, &ctx->submit_state); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2280 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2281 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2282 | io_commit_cqring(ctx); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 2283 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 2d6500d | 2020-06-28 12:52:33 +0300 | [diff] [blame] | 2284 | io_req_free_batch_finish(ctx, &rb); |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2287 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2288 | long min) |
| 2289 | { |
| 2290 | struct io_kiocb *req, *tmp; |
| 2291 | LIST_HEAD(done); |
| 2292 | bool spin; |
| 2293 | int ret; |
| 2294 | |
| 2295 | /* |
| 2296 | * Only spin for completions if we don't have multiple devices hanging |
| 2297 | * off our complete list, and we're under the requested amount. |
| 2298 | */ |
| 2299 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 2300 | |
| 2301 | ret = 0; |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2302 | list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2303 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2304 | |
| 2305 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2306 | * Move completed and retryable entries to our local lists. |
| 2307 | * If we find a request that requires polling, break out |
| 2308 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2309 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2310 | if (READ_ONCE(req->iopoll_completed)) { |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2311 | list_move_tail(&req->inflight_entry, &done); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2312 | continue; |
| 2313 | } |
| 2314 | if (!list_empty(&done)) |
| 2315 | break; |
| 2316 | |
| 2317 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 2318 | if (ret < 0) |
| 2319 | break; |
| 2320 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2321 | /* iopoll may have completed current req */ |
| 2322 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2323 | list_move_tail(&req->inflight_entry, &done); |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2324 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2325 | if (ret && spin) |
| 2326 | spin = false; |
| 2327 | ret = 0; |
| 2328 | } |
| 2329 | |
| 2330 | if (!list_empty(&done)) |
| 2331 | io_iopoll_complete(ctx, nr_events, &done); |
| 2332 | |
| 2333 | return ret; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 2337 | * 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] | 2338 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 2339 | * as a non-spinning completion check. |
| 2340 | */ |
| 2341 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 2342 | long min) |
| 2343 | { |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2344 | while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2345 | int ret; |
| 2346 | |
| 2347 | ret = io_do_iopoll(ctx, nr_events, min); |
| 2348 | if (ret < 0) |
| 2349 | return ret; |
Pavel Begunkov | eba0a4d | 2020-07-06 17:59:30 +0300 | [diff] [blame] | 2350 | if (*nr_events >= min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | return 1; |
| 2355 | } |
| 2356 | |
| 2357 | /* |
| 2358 | * We can't just wait for polled events to come to us, we have to actively |
| 2359 | * find and complete them. |
| 2360 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2361 | static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2362 | { |
| 2363 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2364 | return; |
| 2365 | |
| 2366 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2367 | while (!list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2368 | unsigned int nr_events = 0; |
| 2369 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2370 | io_do_iopoll(ctx, &nr_events, 0); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2371 | |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2372 | /* let it sleep and repeat later if can't complete a request */ |
| 2373 | if (nr_events == 0) |
| 2374 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2375 | /* |
| 2376 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2377 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2378 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2379 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2380 | if (need_resched()) { |
| 2381 | mutex_unlock(&ctx->uring_lock); |
| 2382 | cond_resched(); |
| 2383 | mutex_lock(&ctx->uring_lock); |
| 2384 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2385 | } |
| 2386 | mutex_unlock(&ctx->uring_lock); |
| 2387 | } |
| 2388 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2389 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2390 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2391 | unsigned int nr_events = 0; |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 2392 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2393 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2394 | /* |
| 2395 | * We disallow the app entering submit/complete with polling, but we |
| 2396 | * still need to lock the ring to prevent racing with polled issue |
| 2397 | * that got punted to a workqueue. |
| 2398 | */ |
| 2399 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2400 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2401 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2402 | * Don't enter poll loop if we already have events pending. |
| 2403 | * If we do, we can potentially be spinning for commands that |
| 2404 | * already triggered a CQE (eg in error). |
| 2405 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2406 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 2407 | __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 2408 | if (io_cqring_events(ctx)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2409 | break; |
| 2410 | |
| 2411 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2412 | * If a submit got punted to a workqueue, we can have the |
| 2413 | * application entering polling for a command before it gets |
| 2414 | * issued. That app will hold the uring_lock for the duration |
| 2415 | * of the poll right here, so we need to take a breather every |
| 2416 | * now and then to ensure that the issue has a chance to add |
| 2417 | * the poll to the issued list. Otherwise we can spin here |
| 2418 | * forever, while the workqueue is stuck trying to acquire the |
| 2419 | * very same mutex. |
| 2420 | */ |
| 2421 | if (!(++iters & 7)) { |
| 2422 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2423 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2424 | mutex_lock(&ctx->uring_lock); |
| 2425 | } |
| 2426 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2427 | ret = io_iopoll_getevents(ctx, &nr_events, min); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2428 | if (ret <= 0) |
| 2429 | break; |
| 2430 | ret = 0; |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2431 | } while (min && !nr_events && !need_resched()); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2432 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2433 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2434 | return ret; |
| 2435 | } |
| 2436 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2437 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2438 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2439 | /* |
| 2440 | * Tell lockdep we inherited freeze protection from submission |
| 2441 | * thread. |
| 2442 | */ |
| 2443 | if (req->flags & REQ_F_ISREG) { |
| 2444 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2445 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2446 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2447 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2448 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2451 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2452 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2453 | { |
| 2454 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Colin Ian King | 4a24547 | 2021-02-10 20:00:07 +0000 | [diff] [blame] | 2455 | int rw, ret; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2456 | struct iov_iter iter; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2457 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2458 | /* already prepared */ |
| 2459 | if (req->async_data) |
| 2460 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2461 | |
| 2462 | switch (req->opcode) { |
| 2463 | case IORING_OP_READV: |
| 2464 | case IORING_OP_READ_FIXED: |
| 2465 | case IORING_OP_READ: |
| 2466 | rw = READ; |
| 2467 | break; |
| 2468 | case IORING_OP_WRITEV: |
| 2469 | case IORING_OP_WRITE_FIXED: |
| 2470 | case IORING_OP_WRITE: |
| 2471 | rw = WRITE; |
| 2472 | break; |
| 2473 | default: |
| 2474 | printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| 2475 | req->opcode); |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2476 | return false; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2477 | } |
| 2478 | |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2479 | ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| 2480 | if (ret < 0) |
| 2481 | return false; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 2482 | return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2483 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2484 | #endif |
| 2485 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2486 | static bool io_rw_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2487 | { |
| 2488 | #ifdef CONFIG_BLOCK |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2489 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2490 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2491 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2492 | return false; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2493 | if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker()) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2494 | return false; |
| 2495 | |
Pavel Begunkov | 55e6ac1 | 2021-01-08 20:57:22 +0000 | [diff] [blame] | 2496 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2497 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 2498 | if (io_resubmit_prep(req)) { |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2499 | refcount_inc(&req->refs); |
| 2500 | io_queue_async_work(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2501 | return true; |
Jens Axboe | fdee946 | 2020-08-27 16:46:24 -0600 | [diff] [blame] | 2502 | } |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2503 | req_set_fail_links(req); |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2504 | #endif |
| 2505 | return false; |
| 2506 | } |
| 2507 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2508 | 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] | 2509 | unsigned int issue_flags) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2510 | { |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2511 | int cflags = 0; |
| 2512 | |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2513 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req)) |
| 2514 | return; |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2515 | if (res != req->result) |
| 2516 | req_set_fail_links(req); |
Pavel Begunkov | 23faba3 | 2021-02-11 18:28:22 +0000 | [diff] [blame] | 2517 | |
Pavel Begunkov | 2f8e45f | 2021-02-11 18:28:23 +0000 | [diff] [blame] | 2518 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2519 | kiocb_end_write(req); |
| 2520 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2521 | cflags = io_put_rw_kbuf(req); |
| 2522 | __io_req_complete(req, issue_flags, res, cflags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 2526 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2527 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2528 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2529 | __io_complete_rw(req, res, res2, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2532 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 2533 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2534 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2535 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2536 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2537 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2538 | |
Xiaoguang Wang | 2d7d679 | 2020-06-16 02:06:37 +0800 | [diff] [blame] | 2539 | if (res != -EAGAIN && res != req->result) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2540 | req_set_fail_links(req); |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2541 | |
| 2542 | WRITE_ONCE(req->result, res); |
| 2543 | /* order with io_poll_complete() checking ->result */ |
Pavel Begunkov | cd664b0 | 2020-06-25 12:37:10 +0300 | [diff] [blame] | 2544 | smp_wmb(); |
| 2545 | WRITE_ONCE(req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2550 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 2551 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 2552 | * accessing the kiocb cookie. |
| 2553 | */ |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2554 | 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] | 2555 | { |
| 2556 | struct io_ring_ctx *ctx = req->ctx; |
| 2557 | |
| 2558 | /* |
| 2559 | * Track whether we have multiple files in our lists. This will impact |
| 2560 | * how we do polling eventually, not spinning if we're on potentially |
| 2561 | * different devices. |
| 2562 | */ |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2563 | if (list_empty(&ctx->iopoll_list)) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2564 | ctx->poll_multi_file = false; |
| 2565 | } else if (!ctx->poll_multi_file) { |
| 2566 | struct io_kiocb *list_req; |
| 2567 | |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 2568 | list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2569 | inflight_entry); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2570 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2571 | ctx->poll_multi_file = true; |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | * For fast devices, IO may have already completed. If it has, add |
| 2576 | * it to the front so we find it first. |
| 2577 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2578 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2579 | list_add(&req->inflight_entry, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2580 | else |
Pavel Begunkov | d21ffe7 | 2020-07-13 23:37:10 +0300 | [diff] [blame] | 2581 | list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2582 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 2583 | /* |
| 2584 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread |
| 2585 | * task context or in io worker task context. If current task context is |
| 2586 | * sq thread, we don't need to check whether should wake up sq thread. |
| 2587 | */ |
| 2588 | if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) && |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 2589 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2590 | wake_up(&ctx->sq_data->wait); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
Pavel Begunkov | 9f13c35 | 2020-05-17 14:13:41 +0300 | [diff] [blame] | 2593 | static inline void io_state_file_put(struct io_submit_state *state) |
| 2594 | { |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2595 | if (state->file_refs) { |
| 2596 | fput_many(state->file, state->file_refs); |
| 2597 | state->file_refs = 0; |
| 2598 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * Get as many references to a file as we have IOs left in this submission, |
| 2603 | * assuming most submissions are for one file, or at least that each file |
| 2604 | * has more than one submission. |
| 2605 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2606 | 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] | 2607 | { |
| 2608 | if (!state) |
| 2609 | return fget(fd); |
| 2610 | |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2611 | if (state->file_refs) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2612 | if (state->fd == fd) { |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2613 | state->file_refs--; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2614 | return state->file; |
| 2615 | } |
Pavel Begunkov | 02b23a9 | 2021-01-19 13:32:41 +0000 | [diff] [blame] | 2616 | io_state_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2617 | } |
| 2618 | state->file = fget_many(fd, state->ios_left); |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2619 | if (unlikely(!state->file)) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2620 | return NULL; |
| 2621 | |
| 2622 | state->fd = fd; |
Pavel Begunkov | 6e1271e | 2020-11-20 15:50:50 +0000 | [diff] [blame] | 2623 | state->file_refs = state->ios_left - 1; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2624 | return state->file; |
| 2625 | } |
| 2626 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2627 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2628 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2629 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2630 | } |
| 2631 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2632 | /* |
| 2633 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2634 | * any file. For now, just ensure that anything potentially problematic is done |
| 2635 | * inline. |
| 2636 | */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2637 | static bool io_file_supports_async(struct file *file, int rw) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2638 | { |
| 2639 | umode_t mode = file_inode(file)->i_mode; |
| 2640 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2641 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2642 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2643 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2644 | return true; |
| 2645 | return false; |
| 2646 | } |
| 2647 | if (S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2648 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2649 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2650 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2651 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2652 | file->f_op != &io_uring_fops) |
| 2653 | return true; |
| 2654 | return false; |
| 2655 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2656 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2657 | /* any ->read/write should understand O_NONBLOCK */ |
| 2658 | if (file->f_flags & O_NONBLOCK) |
| 2659 | return true; |
| 2660 | |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 2661 | if (!(file->f_mode & FMODE_NOWAIT)) |
| 2662 | return false; |
| 2663 | |
| 2664 | if (rw == READ) |
| 2665 | return file->f_op->read_iter != NULL; |
| 2666 | |
| 2667 | return file->f_op->write_iter != NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 2670 | 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] | 2671 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2672 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2673 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2674 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2675 | unsigned ioprio; |
| 2676 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2677 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2678 | if (S_ISREG(file_inode(file)->i_mode)) |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2679 | req->flags |= REQ_F_ISREG; |
| 2680 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2681 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2682 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2683 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2684 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2685 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2686 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2687 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 2688 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2689 | if (unlikely(ret)) |
| 2690 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2691 | |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2692 | /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */ |
| 2693 | if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK)) |
| 2694 | req->flags |= REQ_F_NOWAIT; |
| 2695 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2696 | ioprio = READ_ONCE(sqe->ioprio); |
| 2697 | if (ioprio) { |
| 2698 | ret = ioprio_check_cap(ioprio); |
| 2699 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2700 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2701 | |
| 2702 | kiocb->ki_ioprio = ioprio; |
| 2703 | } else |
| 2704 | kiocb->ki_ioprio = get_current_ioprio(); |
| 2705 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2706 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2707 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 2708 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2709 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2710 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2711 | kiocb->ki_flags |= IOCB_HIPRI; |
| 2712 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2713 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2714 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2715 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2716 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2717 | kiocb->ki_complete = io_complete_rw; |
| 2718 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2719 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2720 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2721 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2722 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2723 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2727 | { |
| 2728 | switch (ret) { |
| 2729 | case -EIOCBQUEUED: |
| 2730 | break; |
| 2731 | case -ERESTARTSYS: |
| 2732 | case -ERESTARTNOINTR: |
| 2733 | case -ERESTARTNOHAND: |
| 2734 | case -ERESTART_RESTARTBLOCK: |
| 2735 | /* |
| 2736 | * We can't just restart the syscall, since previously |
| 2737 | * submitted sqes may already be in progress. Just fail this |
| 2738 | * IO with EINTR. |
| 2739 | */ |
| 2740 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2741 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2742 | default: |
| 2743 | kiocb->ki_complete(kiocb, ret, 0); |
| 2744 | } |
| 2745 | } |
| 2746 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2747 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2748 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2749 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2750 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2751 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2752 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2753 | /* add previously done IO, if any */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2754 | if (io && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2755 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2756 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2757 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2758 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2759 | } |
| 2760 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2761 | if (req->flags & REQ_F_CUR_POS) |
| 2762 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | bcaec08 | 2020-02-24 11:30:18 +0300 | [diff] [blame] | 2763 | if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2764 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2765 | else |
| 2766 | io_rw_done(kiocb, ret); |
| 2767 | } |
| 2768 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2769 | 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] | 2770 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2771 | struct io_ring_ctx *ctx = req->ctx; |
| 2772 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2773 | struct io_mapped_ubuf *imu; |
Pavel Begunkov | 4be1c61 | 2020-09-06 00:45:48 +0300 | [diff] [blame] | 2774 | u16 index, buf_index = req->buf_index; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2775 | size_t offset; |
| 2776 | u64 buf_addr; |
| 2777 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2778 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 2779 | return -EFAULT; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2780 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 2781 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2782 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2783 | |
| 2784 | /* overflow */ |
| 2785 | if (buf_addr + len < buf_addr) |
| 2786 | return -EFAULT; |
| 2787 | /* not inside the mapped region */ |
| 2788 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 2789 | return -EFAULT; |
| 2790 | |
| 2791 | /* |
| 2792 | * May not be a start of buffer, set size appropriately |
| 2793 | * and advance us to the beginning. |
| 2794 | */ |
| 2795 | offset = buf_addr - imu->ubuf; |
| 2796 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2797 | |
| 2798 | if (offset) { |
| 2799 | /* |
| 2800 | * Don't use iov_iter_advance() here, as it's really slow for |
| 2801 | * using the latter parts of a big fixed buffer - it iterates |
| 2802 | * over each segment manually. We can cheat a bit here, because |
| 2803 | * we know that: |
| 2804 | * |
| 2805 | * 1) it's a BVEC iter, we set it up |
| 2806 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 2807 | * first and last bvec |
| 2808 | * |
| 2809 | * So just find our index, and adjust the iterator afterwards. |
| 2810 | * If the offset is within the first bvec (or the whole first |
| 2811 | * bvec, just use iov_iter_advance(). This makes it easier |
| 2812 | * since we can just skip the first segment, which may not |
| 2813 | * be PAGE_SIZE aligned. |
| 2814 | */ |
| 2815 | const struct bio_vec *bvec = imu->bvec; |
| 2816 | |
| 2817 | if (offset <= bvec->bv_len) { |
| 2818 | iov_iter_advance(iter, offset); |
| 2819 | } else { |
| 2820 | unsigned long seg_skip; |
| 2821 | |
| 2822 | /* skip first vec */ |
| 2823 | offset -= bvec->bv_len; |
| 2824 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 2825 | |
| 2826 | iter->bvec = bvec + seg_skip; |
| 2827 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 2828 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2829 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2833 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2836 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2837 | { |
| 2838 | if (needs_lock) |
| 2839 | mutex_unlock(&ctx->uring_lock); |
| 2840 | } |
| 2841 | |
| 2842 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 2843 | { |
| 2844 | /* |
| 2845 | * "Normal" inline submissions always hold the uring_lock, since we |
| 2846 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 2847 | * The only exception is when we've detached the request and issue it |
| 2848 | * from an async worker thread, grab the lock for that case. |
| 2849 | */ |
| 2850 | if (needs_lock) |
| 2851 | mutex_lock(&ctx->uring_lock); |
| 2852 | } |
| 2853 | |
| 2854 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| 2855 | int bgid, struct io_buffer *kbuf, |
| 2856 | bool needs_lock) |
| 2857 | { |
| 2858 | struct io_buffer *head; |
| 2859 | |
| 2860 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 2861 | return kbuf; |
| 2862 | |
| 2863 | io_ring_submit_lock(req->ctx, needs_lock); |
| 2864 | |
| 2865 | lockdep_assert_held(&req->ctx->uring_lock); |
| 2866 | |
| 2867 | head = idr_find(&req->ctx->io_buffer_idr, bgid); |
| 2868 | if (head) { |
| 2869 | if (!list_empty(&head->list)) { |
| 2870 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 2871 | list); |
| 2872 | list_del(&kbuf->list); |
| 2873 | } else { |
| 2874 | kbuf = head; |
| 2875 | idr_remove(&req->ctx->io_buffer_idr, bgid); |
| 2876 | } |
| 2877 | if (*len > kbuf->len) |
| 2878 | *len = kbuf->len; |
| 2879 | } else { |
| 2880 | kbuf = ERR_PTR(-ENOBUFS); |
| 2881 | } |
| 2882 | |
| 2883 | io_ring_submit_unlock(req->ctx, needs_lock); |
| 2884 | |
| 2885 | return kbuf; |
| 2886 | } |
| 2887 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2888 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| 2889 | bool needs_lock) |
| 2890 | { |
| 2891 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2892 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2893 | |
| 2894 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2895 | bgid = req->buf_index; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2896 | kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| 2897 | if (IS_ERR(kbuf)) |
| 2898 | return kbuf; |
| 2899 | req->rw.addr = (u64) (unsigned long) kbuf; |
| 2900 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 2901 | return u64_to_user_ptr(kbuf->addr); |
| 2902 | } |
| 2903 | |
| 2904 | #ifdef CONFIG_COMPAT |
| 2905 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| 2906 | bool needs_lock) |
| 2907 | { |
| 2908 | struct compat_iovec __user *uiov; |
| 2909 | compat_ssize_t clen; |
| 2910 | void __user *buf; |
| 2911 | ssize_t len; |
| 2912 | |
| 2913 | uiov = u64_to_user_ptr(req->rw.addr); |
| 2914 | if (!access_ok(uiov, sizeof(*uiov))) |
| 2915 | return -EFAULT; |
| 2916 | if (__get_user(clen, &uiov->iov_len)) |
| 2917 | return -EFAULT; |
| 2918 | if (clen < 0) |
| 2919 | return -EINVAL; |
| 2920 | |
| 2921 | len = clen; |
| 2922 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2923 | if (IS_ERR(buf)) |
| 2924 | return PTR_ERR(buf); |
| 2925 | iov[0].iov_base = buf; |
| 2926 | iov[0].iov_len = (compat_size_t) len; |
| 2927 | return 0; |
| 2928 | } |
| 2929 | #endif |
| 2930 | |
| 2931 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2932 | bool needs_lock) |
| 2933 | { |
| 2934 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 2935 | void __user *buf; |
| 2936 | ssize_t len; |
| 2937 | |
| 2938 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 2939 | return -EFAULT; |
| 2940 | |
| 2941 | len = iov[0].iov_len; |
| 2942 | if (len < 0) |
| 2943 | return -EINVAL; |
| 2944 | buf = io_rw_buffer_select(req, &len, needs_lock); |
| 2945 | if (IS_ERR(buf)) |
| 2946 | return PTR_ERR(buf); |
| 2947 | iov[0].iov_base = buf; |
| 2948 | iov[0].iov_len = len; |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
| 2952 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| 2953 | bool needs_lock) |
| 2954 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2955 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 2956 | struct io_buffer *kbuf; |
| 2957 | |
| 2958 | kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| 2959 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 2960 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2961 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 2962 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 2963 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2964 | return -EINVAL; |
| 2965 | |
| 2966 | #ifdef CONFIG_COMPAT |
| 2967 | if (req->ctx->compat) |
| 2968 | return io_compat_import(req, iov, needs_lock); |
| 2969 | #endif |
| 2970 | |
| 2971 | return __io_iov_buffer_select(req, iov, needs_lock); |
| 2972 | } |
| 2973 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2974 | static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec, |
| 2975 | struct iov_iter *iter, bool needs_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2976 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2977 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2978 | size_t sqe_len = req->rw.len; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 2979 | u8 opcode = req->opcode; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2980 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2981 | |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2982 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2983 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2984 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2985 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2986 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2987 | /* buffer index only valid with fixed read/write, or buffer select */ |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2988 | if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2989 | return -EINVAL; |
| 2990 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2991 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2992 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2993 | buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 2994 | if (IS_ERR(buf)) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 2995 | return PTR_ERR(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 2996 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2999 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 3000 | *iovec = NULL; |
David Laight | 10fc72e | 2020-11-07 13:16:25 +0000 | [diff] [blame] | 3001 | return ret; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3002 | } |
| 3003 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3004 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 3005 | ret = io_iov_buffer_select(req, *iovec, needs_lock); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3006 | if (!ret) |
| 3007 | iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3008 | *iovec = NULL; |
| 3009 | return ret; |
| 3010 | } |
| 3011 | |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3012 | return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter, |
| 3013 | req->ctx->compat); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3016 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3017 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3018 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3019 | } |
| 3020 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3021 | /* |
| 3022 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3023 | * by looping over ->read() or ->write() manually. |
| 3024 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3025 | 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] | 3026 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3027 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3028 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3029 | ssize_t ret = 0; |
| 3030 | |
| 3031 | /* |
| 3032 | * Don't support polled IO through this interface, and we can't |
| 3033 | * support non-blocking either. For the latter, this just causes |
| 3034 | * the kiocb to be handled from an async context. |
| 3035 | */ |
| 3036 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3037 | return -EOPNOTSUPP; |
| 3038 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 3039 | return -EAGAIN; |
| 3040 | |
| 3041 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3042 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3043 | ssize_t nr; |
| 3044 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3045 | if (!iov_iter_is_bvec(iter)) { |
| 3046 | iovec = iov_iter_iovec(iter); |
| 3047 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3048 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3049 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3050 | } |
| 3051 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3052 | if (rw == READ) { |
| 3053 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3054 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3055 | } else { |
| 3056 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3057 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | if (nr < 0) { |
| 3061 | if (!ret) |
| 3062 | ret = nr; |
| 3063 | break; |
| 3064 | } |
| 3065 | ret += nr; |
| 3066 | if (nr != iovec.iov_len) |
| 3067 | break; |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3068 | req->rw.len -= nr; |
| 3069 | req->rw.addr += nr; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3070 | iov_iter_advance(iter, nr); |
| 3071 | } |
| 3072 | |
| 3073 | return ret; |
| 3074 | } |
| 3075 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3076 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3077 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3078 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3079 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3080 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3081 | memcpy(&rw->iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3082 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3083 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3084 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3085 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3086 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3087 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3088 | unsigned iov_off = 0; |
| 3089 | |
| 3090 | rw->iter.iov = rw->fast_iov; |
| 3091 | if (iter->iov != fast_iov) { |
| 3092 | iov_off = iter->iov - fast_iov; |
| 3093 | rw->iter.iov += iov_off; |
| 3094 | } |
| 3095 | if (rw->fast_iov != fast_iov) |
| 3096 | memcpy(rw->fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3097 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3098 | } else { |
| 3099 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3100 | } |
| 3101 | } |
| 3102 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3103 | static inline int __io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3104 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3105 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3106 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); |
| 3107 | return req->async_data == NULL; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3108 | } |
| 3109 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3110 | static int io_alloc_async_data(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3111 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3112 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 3113 | return 0; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3114 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3115 | return __io_alloc_async_data(req); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3116 | } |
| 3117 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3118 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3119 | const struct iovec *fast_iov, |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3120 | struct iov_iter *iter, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3121 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3122 | if (!force && !io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3123 | return 0; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3124 | if (!req->async_data) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3125 | if (__io_alloc_async_data(req)) { |
| 3126 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3127 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3128 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3129 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3130 | io_req_map_rw(req, iovec, fast_iov, iter); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3131 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3132 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3133 | } |
| 3134 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3135 | 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] | 3136 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3137 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | f4bff10 | 2020-09-06 00:45:45 +0300 | [diff] [blame] | 3138 | struct iovec *iov = iorw->fast_iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3139 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3140 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3141 | ret = io_import_iovec(rw, req, &iov, &iorw->iter, false); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3142 | if (unlikely(ret < 0)) |
| 3143 | return ret; |
| 3144 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3145 | iorw->bytes_done = 0; |
| 3146 | iorw->free_iovec = iov; |
| 3147 | if (iov) |
| 3148 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
| 3151 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3152 | 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] | 3153 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3154 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3155 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3156 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3159 | /* |
| 3160 | * This is our waitqueue callback handler, registered through lock_page_async() |
| 3161 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3162 | * This gets called when the page is unlocked, and we generally expect that to |
| 3163 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3164 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3165 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3166 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3167 | * slow path. |
| 3168 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3169 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3170 | int sync, void *arg) |
| 3171 | { |
| 3172 | struct wait_page_queue *wpq; |
| 3173 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3174 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3175 | |
| 3176 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3177 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3178 | if (!wake_page_match(wpq, key)) |
| 3179 | return 0; |
| 3180 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3181 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3182 | list_del_init(&wait->entry); |
| 3183 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3184 | /* submit ref gets dropped, acquire a new one */ |
| 3185 | refcount_inc(&req->refs); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3186 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3187 | return 1; |
| 3188 | } |
| 3189 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3190 | /* |
| 3191 | * This controls whether a given IO request should be armed for async page |
| 3192 | * based retry. If we return false here, the request is handed to the async |
| 3193 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3194 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3195 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3196 | * will register a callback when the page is unlocked at IO completion. Through |
| 3197 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3198 | * That retry will attempt the buffered read again. The retry will generally |
| 3199 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3200 | * async worker threads for a blocking retry. |
| 3201 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3202 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3203 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3204 | struct io_async_rw *rw = req->async_data; |
| 3205 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3206 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3207 | |
| 3208 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3209 | if (req->flags & REQ_F_NOWAIT) |
| 3210 | return false; |
| 3211 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3212 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3213 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3214 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3215 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3216 | /* |
| 3217 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3218 | * support callback based unlocks |
| 3219 | */ |
| 3220 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3221 | return false; |
| 3222 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3223 | wait->wait.func = io_async_buf_func; |
| 3224 | wait->wait.private = req; |
| 3225 | wait->wait.flags = 0; |
| 3226 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3227 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3228 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3229 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3230 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
| 3234 | { |
| 3235 | if (req->file->f_op->read_iter) |
| 3236 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3237 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3238 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3239 | else |
| 3240 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3241 | } |
| 3242 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3243 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3244 | { |
| 3245 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3246 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3247 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3248 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3249 | ssize_t io_size, ret, ret2; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3250 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3251 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3252 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3253 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3254 | iovec = NULL; |
| 3255 | } else { |
| 3256 | ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock); |
| 3257 | if (ret < 0) |
| 3258 | return ret; |
| 3259 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3260 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3261 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3262 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3263 | /* Ensure we clear previously set non-block flag */ |
| 3264 | if (!force_nonblock) |
Jens Axboe | 29de5f6 | 2020-02-20 09:56:08 -0700 | [diff] [blame] | 3265 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3266 | else |
| 3267 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 3268 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3269 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3270 | if (force_nonblock && !io_file_supports_async(req->file, READ)) { |
| 3271 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3272 | return ret ?: -EAGAIN; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3273 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3274 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3275 | 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] | 3276 | if (unlikely(ret)) { |
| 3277 | kfree(iovec); |
| 3278 | return ret; |
| 3279 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3280 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3281 | ret = io_iter_do_read(req, iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3282 | |
Pavel Begunkov | 57cd657 | 2021-02-01 18:59:56 +0000 | [diff] [blame] | 3283 | if (ret == -EIOCBQUEUED) { |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3284 | goto out_free; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3285 | } else if (ret == -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3286 | /* IOPOLL retry should happen for io-wq threads */ |
| 3287 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3288 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3289 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3290 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3291 | goto done; |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3292 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3293 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3294 | ret = 0; |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3295 | } else if (ret <= 0 || ret == io_size || !force_nonblock || |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3296 | (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3297 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3298 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3299 | } |
| 3300 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3301 | ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3302 | if (ret2) |
| 3303 | return ret2; |
| 3304 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3305 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3306 | rw = req->async_data; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3307 | /* now use our persistent iterator, if we aren't already */ |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3308 | iter = &rw->iter; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3309 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3310 | do { |
| 3311 | io_size -= ret; |
| 3312 | rw->bytes_done += ret; |
| 3313 | /* if we can retry, do so with the callbacks armed */ |
| 3314 | if (!io_rw_should_retry(req)) { |
| 3315 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3316 | return -EAGAIN; |
| 3317 | } |
| 3318 | |
| 3319 | /* |
| 3320 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3321 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3322 | * desired page gets unlocked. We can also get a partial read |
| 3323 | * here, and if we do, then just retry at the new offset. |
| 3324 | */ |
| 3325 | ret = io_iter_do_read(req, iter); |
| 3326 | if (ret == -EIOCBQUEUED) |
| 3327 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3328 | /* we got some bytes, but not all. retry. */ |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3329 | } while (ret > 0 && ret < io_size); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3330 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3331 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3332 | out_free: |
| 3333 | /* it's faster to check here then delegate to kfree */ |
| 3334 | if (iovec) |
| 3335 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3336 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3337 | } |
| 3338 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3339 | 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] | 3340 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3341 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3342 | return -EBADF; |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 3343 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3346 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3347 | { |
| 3348 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3349 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3350 | struct iov_iter __iter, *iter = &__iter; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3351 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3352 | ssize_t ret, ret2, io_size; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3353 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3354 | |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3355 | if (rw) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3356 | iter = &rw->iter; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3357 | iovec = NULL; |
| 3358 | } else { |
| 3359 | ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock); |
| 3360 | if (ret < 0) |
| 3361 | return ret; |
| 3362 | } |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3363 | io_size = iov_iter_count(iter); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3364 | req->result = io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3365 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3366 | /* Ensure we clear previously set non-block flag */ |
| 3367 | if (!force_nonblock) |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3368 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3369 | else |
| 3370 | kiocb->ki_flags |= IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 3371 | |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 3372 | /* If the file doesn't support async, just async punt */ |
Jens Axboe | af197f5 | 2020-04-28 13:15:06 -0600 | [diff] [blame] | 3373 | if (force_nonblock && !io_file_supports_async(req->file, WRITE)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3374 | goto copy_iov; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3375 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 3376 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3377 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3378 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3379 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3380 | |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3381 | 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] | 3382 | if (unlikely(ret)) |
| 3383 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3384 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3385 | /* |
| 3386 | * Open-code file_start_write here to grab freeze protection, |
| 3387 | * which will be released by another thread in |
| 3388 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3389 | * released so that it doesn't complain about the held lock when |
| 3390 | * we return to userspace. |
| 3391 | */ |
| 3392 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3393 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3394 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3395 | SB_FREEZE_WRITE); |
| 3396 | } |
| 3397 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3398 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3399 | if (req->file->f_op->write_iter) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3400 | ret2 = call_write_iter(req->file, kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3401 | else if (req->file->f_op->write) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3402 | ret2 = loop_rw_iter(WRITE, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3403 | else |
| 3404 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3405 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3406 | /* |
| 3407 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3408 | * retry them without IOCB_NOWAIT. |
| 3409 | */ |
| 3410 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3411 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3412 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3413 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3414 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3415 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3416 | /* IOPOLL retry should happen for io-wq threads */ |
| 3417 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN) |
| 3418 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3419 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3420 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3421 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3422 | copy_iov: |
Jens Axboe | 8421631 | 2020-08-24 11:45:26 -0600 | [diff] [blame] | 3423 | /* some cases will consume bytes even on error returns */ |
Pavel Begunkov | 632546c | 2020-11-07 13:16:26 +0000 | [diff] [blame] | 3424 | iov_iter_revert(iter, io_size - iov_iter_count(iter)); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3425 | ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3426 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3427 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3428 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3429 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3430 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3431 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3432 | return ret; |
| 3433 | } |
| 3434 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3435 | static int io_renameat_prep(struct io_kiocb *req, |
| 3436 | const struct io_uring_sqe *sqe) |
| 3437 | { |
| 3438 | struct io_rename *ren = &req->rename; |
| 3439 | const char __user *oldf, *newf; |
| 3440 | |
| 3441 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3442 | return -EBADF; |
| 3443 | |
| 3444 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3445 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3446 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3447 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3448 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3449 | |
| 3450 | ren->oldpath = getname(oldf); |
| 3451 | if (IS_ERR(ren->oldpath)) |
| 3452 | return PTR_ERR(ren->oldpath); |
| 3453 | |
| 3454 | ren->newpath = getname(newf); |
| 3455 | if (IS_ERR(ren->newpath)) { |
| 3456 | putname(ren->oldpath); |
| 3457 | return PTR_ERR(ren->newpath); |
| 3458 | } |
| 3459 | |
| 3460 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3461 | return 0; |
| 3462 | } |
| 3463 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3464 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3465 | { |
| 3466 | struct io_rename *ren = &req->rename; |
| 3467 | int ret; |
| 3468 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3469 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3470 | return -EAGAIN; |
| 3471 | |
| 3472 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3473 | ren->newpath, ren->flags); |
| 3474 | |
| 3475 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3476 | if (ret < 0) |
| 3477 | req_set_fail_links(req); |
| 3478 | io_req_complete(req, ret); |
| 3479 | return 0; |
| 3480 | } |
| 3481 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3482 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3483 | const struct io_uring_sqe *sqe) |
| 3484 | { |
| 3485 | struct io_unlink *un = &req->unlink; |
| 3486 | const char __user *fname; |
| 3487 | |
| 3488 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3489 | return -EBADF; |
| 3490 | |
| 3491 | un->dfd = READ_ONCE(sqe->fd); |
| 3492 | |
| 3493 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3494 | if (un->flags & ~AT_REMOVEDIR) |
| 3495 | return -EINVAL; |
| 3496 | |
| 3497 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3498 | un->filename = getname(fname); |
| 3499 | if (IS_ERR(un->filename)) |
| 3500 | return PTR_ERR(un->filename); |
| 3501 | |
| 3502 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3503 | return 0; |
| 3504 | } |
| 3505 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3506 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3507 | { |
| 3508 | struct io_unlink *un = &req->unlink; |
| 3509 | int ret; |
| 3510 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3511 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3512 | return -EAGAIN; |
| 3513 | |
| 3514 | if (un->flags & AT_REMOVEDIR) |
| 3515 | ret = do_rmdir(un->dfd, un->filename); |
| 3516 | else |
| 3517 | ret = do_unlinkat(un->dfd, un->filename); |
| 3518 | |
| 3519 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3520 | if (ret < 0) |
| 3521 | req_set_fail_links(req); |
| 3522 | io_req_complete(req, ret); |
| 3523 | return 0; |
| 3524 | } |
| 3525 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3526 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3527 | const struct io_uring_sqe *sqe) |
| 3528 | { |
| 3529 | #if defined(CONFIG_NET) |
| 3530 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3531 | return -EINVAL; |
| 3532 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3533 | sqe->buf_index) |
| 3534 | return -EINVAL; |
| 3535 | |
| 3536 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3537 | return 0; |
| 3538 | #else |
| 3539 | return -EOPNOTSUPP; |
| 3540 | #endif |
| 3541 | } |
| 3542 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3543 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3544 | { |
| 3545 | #if defined(CONFIG_NET) |
| 3546 | struct socket *sock; |
| 3547 | int ret; |
| 3548 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3549 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3550 | return -EAGAIN; |
| 3551 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3552 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3553 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 3554 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3555 | |
| 3556 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 3557 | if (ret < 0) |
| 3558 | req_set_fail_links(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3559 | io_req_complete(req, ret); |
| 3560 | return 0; |
| 3561 | #else |
| 3562 | return -EOPNOTSUPP; |
| 3563 | #endif |
| 3564 | } |
| 3565 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3566 | static int __io_splice_prep(struct io_kiocb *req, |
| 3567 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3568 | { |
| 3569 | struct io_splice* sp = &req->splice; |
| 3570 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3571 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3572 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3573 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3574 | |
| 3575 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3576 | sp->len = READ_ONCE(sqe->len); |
| 3577 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 3578 | |
| 3579 | if (unlikely(sp->flags & ~valid_flags)) |
| 3580 | return -EINVAL; |
| 3581 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 3582 | sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), |
| 3583 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3584 | if (!sp->file_in) |
| 3585 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3586 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3587 | |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3588 | if (!S_ISREG(file_inode(sp->file_in)->i_mode)) { |
| 3589 | /* |
| 3590 | * Splice operation will be punted aync, and here need to |
| 3591 | * modify io_wq_work.flags, so initialize io_wq_work firstly. |
| 3592 | */ |
| 3593 | io_req_init_async(req); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3594 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Xiaoguang Wang | 7cdaf58 | 2020-06-10 19:41:19 +0800 | [diff] [blame] | 3595 | } |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3596 | |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3600 | static int io_tee_prep(struct io_kiocb *req, |
| 3601 | const struct io_uring_sqe *sqe) |
| 3602 | { |
| 3603 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 3604 | return -EINVAL; |
| 3605 | return __io_splice_prep(req, sqe); |
| 3606 | } |
| 3607 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3608 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3609 | { |
| 3610 | struct io_splice *sp = &req->splice; |
| 3611 | struct file *in = sp->file_in; |
| 3612 | struct file *out = sp->file_out; |
| 3613 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3614 | long ret = 0; |
| 3615 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3616 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3617 | return -EAGAIN; |
| 3618 | if (sp->len) |
| 3619 | ret = do_tee(in, out, sp->len, flags); |
| 3620 | |
| 3621 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3622 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3623 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3624 | if (ret != sp->len) |
| 3625 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3626 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 3627 | return 0; |
| 3628 | } |
| 3629 | |
| 3630 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3631 | { |
| 3632 | struct io_splice* sp = &req->splice; |
| 3633 | |
| 3634 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 3635 | sp->off_out = READ_ONCE(sqe->off); |
| 3636 | return __io_splice_prep(req, sqe); |
| 3637 | } |
| 3638 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3639 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3640 | { |
| 3641 | struct io_splice *sp = &req->splice; |
| 3642 | struct file *in = sp->file_in; |
| 3643 | struct file *out = sp->file_out; |
| 3644 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 3645 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3646 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3647 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3648 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 3649 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3650 | |
| 3651 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 3652 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3653 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 3654 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 3655 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3656 | |
| 3657 | io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 3658 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3659 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3660 | if (ret != sp->len) |
| 3661 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3662 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 3663 | return 0; |
| 3664 | } |
| 3665 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3666 | /* |
| 3667 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 3668 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3669 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3670 | { |
| 3671 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3672 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3673 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 3674 | return -EINVAL; |
| 3675 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3676 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3677 | return 0; |
| 3678 | } |
| 3679 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 3680 | 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] | 3681 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3682 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3683 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3684 | if (!req->file) |
| 3685 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3686 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3687 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3688 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3689 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3690 | return -EINVAL; |
| 3691 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3692 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 3693 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 3694 | return -EINVAL; |
| 3695 | |
| 3696 | req->sync.off = READ_ONCE(sqe->off); |
| 3697 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3698 | return 0; |
| 3699 | } |
| 3700 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3701 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3702 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3703 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3704 | int ret; |
| 3705 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3706 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3707 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3708 | return -EAGAIN; |
| 3709 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3710 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3711 | end > 0 ? end : LLONG_MAX, |
| 3712 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 3713 | if (ret < 0) |
| 3714 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3715 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3716 | return 0; |
| 3717 | } |
| 3718 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3719 | static int io_fallocate_prep(struct io_kiocb *req, |
| 3720 | const struct io_uring_sqe *sqe) |
| 3721 | { |
| 3722 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 3723 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 3724 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3725 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3726 | |
| 3727 | req->sync.off = READ_ONCE(sqe->off); |
| 3728 | req->sync.len = READ_ONCE(sqe->addr); |
| 3729 | req->sync.mode = READ_ONCE(sqe->len); |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3733 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3734 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3735 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3736 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3737 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3738 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3739 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3740 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 3741 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 3742 | if (ret < 0) |
| 3743 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3744 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3745 | return 0; |
| 3746 | } |
| 3747 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3748 | 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] | 3749 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3750 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3751 | int ret; |
| 3752 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3753 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3754 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3755 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 3756 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3757 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3758 | /* open.how should be already initialised */ |
| 3759 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 3760 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3761 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 3762 | req->open.dfd = READ_ONCE(sqe->fd); |
| 3763 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 3764 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3765 | if (IS_ERR(req->open.filename)) { |
| 3766 | ret = PTR_ERR(req->open.filename); |
| 3767 | req->open.filename = NULL; |
| 3768 | return ret; |
| 3769 | } |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3770 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3771 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3772 | return 0; |
| 3773 | } |
| 3774 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3775 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3776 | { |
| 3777 | u64 flags, mode; |
| 3778 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3779 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3780 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3781 | mode = READ_ONCE(sqe->len); |
| 3782 | flags = READ_ONCE(sqe->open_flags); |
| 3783 | req->open.how = build_open_how(flags, mode); |
| 3784 | return __io_openat_prep(req, sqe); |
| 3785 | } |
| 3786 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3787 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 3788 | { |
| 3789 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3790 | size_t len; |
| 3791 | int ret; |
| 3792 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 3793 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 4eb8dde | 2020-09-18 19:36:24 -0600 | [diff] [blame] | 3794 | return -EINVAL; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3795 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3796 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3797 | if (len < OPEN_HOW_SIZE_VER0) |
| 3798 | return -EINVAL; |
| 3799 | |
| 3800 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 3801 | len); |
| 3802 | if (ret) |
| 3803 | return ret; |
| 3804 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 3805 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3806 | } |
| 3807 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3808 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3809 | { |
| 3810 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3811 | struct file *file; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3812 | bool nonblock_set; |
| 3813 | bool resolve_nonblock; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3814 | int ret; |
| 3815 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3816 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3817 | if (ret) |
| 3818 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3819 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 3820 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3821 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3822 | /* |
| 3823 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 3824 | * it'll always -EAGAIN |
| 3825 | */ |
| 3826 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 3827 | return -EAGAIN; |
| 3828 | op.lookup_flags |= LOOKUP_CACHED; |
| 3829 | op.open_flag |= O_NONBLOCK; |
| 3830 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3831 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 3832 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3833 | if (ret < 0) |
| 3834 | goto err; |
| 3835 | |
| 3836 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3837 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3838 | if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) && |
| 3839 | file == ERR_PTR(-EAGAIN)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3840 | /* |
| 3841 | * We could hang on to this 'fd', but seems like marginal |
| 3842 | * gain for something that is now known to be a slower path. |
| 3843 | * So just put it, and we'll get a new one when we retry. |
| 3844 | */ |
| 3845 | put_unused_fd(ret); |
| 3846 | return -EAGAIN; |
| 3847 | } |
| 3848 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3849 | if (IS_ERR(file)) { |
| 3850 | put_unused_fd(ret); |
| 3851 | ret = PTR_ERR(file); |
| 3852 | } else { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3853 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 3854 | file->f_flags &= ~O_NONBLOCK; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3855 | fsnotify_open(file); |
| 3856 | fd_install(ret, file); |
| 3857 | } |
| 3858 | err: |
| 3859 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 3860 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3861 | if (ret < 0) |
| 3862 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 3863 | io_req_complete(req, ret); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3864 | return 0; |
| 3865 | } |
| 3866 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3867 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3868 | { |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3869 | return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 3870 | } |
| 3871 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3872 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 3873 | const struct io_uring_sqe *sqe) |
| 3874 | { |
| 3875 | struct io_provide_buf *p = &req->pbuf; |
| 3876 | u64 tmp; |
| 3877 | |
| 3878 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off) |
| 3879 | return -EINVAL; |
| 3880 | |
| 3881 | tmp = READ_ONCE(sqe->fd); |
| 3882 | if (!tmp || tmp > USHRT_MAX) |
| 3883 | return -EINVAL; |
| 3884 | |
| 3885 | memset(p, 0, sizeof(*p)); |
| 3886 | p->nbufs = tmp; |
| 3887 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3888 | return 0; |
| 3889 | } |
| 3890 | |
| 3891 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 3892 | int bgid, unsigned nbufs) |
| 3893 | { |
| 3894 | unsigned i = 0; |
| 3895 | |
| 3896 | /* shouldn't happen */ |
| 3897 | if (!nbufs) |
| 3898 | return 0; |
| 3899 | |
| 3900 | /* the head kbuf is the list itself */ |
| 3901 | while (!list_empty(&buf->list)) { |
| 3902 | struct io_buffer *nxt; |
| 3903 | |
| 3904 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 3905 | list_del(&nxt->list); |
| 3906 | kfree(nxt); |
| 3907 | if (++i == nbufs) |
| 3908 | return i; |
| 3909 | } |
| 3910 | i++; |
| 3911 | kfree(buf); |
| 3912 | idr_remove(&ctx->io_buffer_idr, bgid); |
| 3913 | |
| 3914 | return i; |
| 3915 | } |
| 3916 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3917 | 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] | 3918 | { |
| 3919 | struct io_provide_buf *p = &req->pbuf; |
| 3920 | struct io_ring_ctx *ctx = req->ctx; |
| 3921 | struct io_buffer *head; |
| 3922 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3923 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3924 | |
| 3925 | io_ring_submit_lock(ctx, !force_nonblock); |
| 3926 | |
| 3927 | lockdep_assert_held(&ctx->uring_lock); |
| 3928 | |
| 3929 | ret = -ENOENT; |
| 3930 | head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 3931 | if (head) |
| 3932 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3933 | if (ret < 0) |
| 3934 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3935 | |
| 3936 | /* need to hold the lock to complete IOPOLL requests */ |
| 3937 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3938 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3939 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 3940 | } else { |
| 3941 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3942 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 3943 | } |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 3944 | return 0; |
| 3945 | } |
| 3946 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 3947 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 3948 | const struct io_uring_sqe *sqe) |
| 3949 | { |
| 3950 | struct io_provide_buf *p = &req->pbuf; |
| 3951 | u64 tmp; |
| 3952 | |
| 3953 | if (sqe->ioprio || sqe->rw_flags) |
| 3954 | return -EINVAL; |
| 3955 | |
| 3956 | tmp = READ_ONCE(sqe->fd); |
| 3957 | if (!tmp || tmp > USHRT_MAX) |
| 3958 | return -E2BIG; |
| 3959 | p->nbufs = tmp; |
| 3960 | p->addr = READ_ONCE(sqe->addr); |
| 3961 | p->len = READ_ONCE(sqe->len); |
| 3962 | |
Bijan Mottahedeh | efe68c1 | 2020-06-04 18:01:52 -0700 | [diff] [blame] | 3963 | 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] | 3964 | return -EFAULT; |
| 3965 | |
| 3966 | p->bgid = READ_ONCE(sqe->buf_group); |
| 3967 | tmp = READ_ONCE(sqe->off); |
| 3968 | if (tmp > USHRT_MAX) |
| 3969 | return -E2BIG; |
| 3970 | p->bid = tmp; |
| 3971 | return 0; |
| 3972 | } |
| 3973 | |
| 3974 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 3975 | { |
| 3976 | struct io_buffer *buf; |
| 3977 | u64 addr = pbuf->addr; |
| 3978 | int i, bid = pbuf->bid; |
| 3979 | |
| 3980 | for (i = 0; i < pbuf->nbufs; i++) { |
| 3981 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 3982 | if (!buf) |
| 3983 | break; |
| 3984 | |
| 3985 | buf->addr = addr; |
| 3986 | buf->len = pbuf->len; |
| 3987 | buf->bid = bid; |
| 3988 | addr += pbuf->len; |
| 3989 | bid++; |
| 3990 | if (!*head) { |
| 3991 | INIT_LIST_HEAD(&buf->list); |
| 3992 | *head = buf; |
| 3993 | } else { |
| 3994 | list_add_tail(&buf->list, &(*head)->list); |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | return i ? i : -ENOMEM; |
| 3999 | } |
| 4000 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4001 | 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] | 4002 | { |
| 4003 | struct io_provide_buf *p = &req->pbuf; |
| 4004 | struct io_ring_ctx *ctx = req->ctx; |
| 4005 | struct io_buffer *head, *list; |
| 4006 | int ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4007 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4008 | |
| 4009 | io_ring_submit_lock(ctx, !force_nonblock); |
| 4010 | |
| 4011 | lockdep_assert_held(&ctx->uring_lock); |
| 4012 | |
| 4013 | list = head = idr_find(&ctx->io_buffer_idr, p->bgid); |
| 4014 | |
| 4015 | ret = io_add_buffers(p, &head); |
| 4016 | if (ret < 0) |
| 4017 | goto out; |
| 4018 | |
| 4019 | if (!list) { |
| 4020 | ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1, |
| 4021 | GFP_KERNEL); |
| 4022 | if (ret < 0) { |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4023 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4024 | goto out; |
| 4025 | } |
| 4026 | } |
| 4027 | out: |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4028 | if (ret < 0) |
| 4029 | req_set_fail_links(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4030 | |
| 4031 | /* need to hold the lock to complete IOPOLL requests */ |
| 4032 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4033 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4034 | io_ring_submit_unlock(ctx, !force_nonblock); |
| 4035 | } else { |
| 4036 | io_ring_submit_unlock(ctx, !force_nonblock); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4037 | __io_req_complete(req, issue_flags, ret, 0); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4038 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4039 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4042 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4043 | const struct io_uring_sqe *sqe) |
| 4044 | { |
| 4045 | #if defined(CONFIG_EPOLL) |
| 4046 | if (sqe->ioprio || sqe->buf_index) |
| 4047 | return -EINVAL; |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4048 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4049 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4050 | |
| 4051 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4052 | req->epoll.op = READ_ONCE(sqe->len); |
| 4053 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4054 | |
| 4055 | if (ep_op_has_event(req->epoll.op)) { |
| 4056 | struct epoll_event __user *ev; |
| 4057 | |
| 4058 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4059 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4060 | return -EFAULT; |
| 4061 | } |
| 4062 | |
| 4063 | return 0; |
| 4064 | #else |
| 4065 | return -EOPNOTSUPP; |
| 4066 | #endif |
| 4067 | } |
| 4068 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4069 | 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] | 4070 | { |
| 4071 | #if defined(CONFIG_EPOLL) |
| 4072 | struct io_epoll *ie = &req->epoll; |
| 4073 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4074 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4075 | |
| 4076 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4077 | if (force_nonblock && ret == -EAGAIN) |
| 4078 | return -EAGAIN; |
| 4079 | |
| 4080 | if (ret < 0) |
| 4081 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4082 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4083 | return 0; |
| 4084 | #else |
| 4085 | return -EOPNOTSUPP; |
| 4086 | #endif |
| 4087 | } |
| 4088 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4089 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4090 | { |
| 4091 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4092 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 4093 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4094 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4095 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4096 | |
| 4097 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4098 | req->madvise.len = READ_ONCE(sqe->len); |
| 4099 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4100 | return 0; |
| 4101 | #else |
| 4102 | return -EOPNOTSUPP; |
| 4103 | #endif |
| 4104 | } |
| 4105 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4106 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4107 | { |
| 4108 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4109 | struct io_madvise *ma = &req->madvise; |
| 4110 | int ret; |
| 4111 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4112 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4113 | return -EAGAIN; |
| 4114 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4115 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4116 | if (ret < 0) |
| 4117 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4118 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4119 | return 0; |
| 4120 | #else |
| 4121 | return -EOPNOTSUPP; |
| 4122 | #endif |
| 4123 | } |
| 4124 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4125 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4126 | { |
| 4127 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 4128 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4129 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4130 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4131 | |
| 4132 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4133 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4134 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4135 | return 0; |
| 4136 | } |
| 4137 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4138 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4139 | { |
| 4140 | struct io_fadvise *fa = &req->fadvise; |
| 4141 | int ret; |
| 4142 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4143 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4144 | switch (fa->advice) { |
| 4145 | case POSIX_FADV_NORMAL: |
| 4146 | case POSIX_FADV_RANDOM: |
| 4147 | case POSIX_FADV_SEQUENTIAL: |
| 4148 | break; |
| 4149 | default: |
| 4150 | return -EAGAIN; |
| 4151 | } |
| 4152 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4153 | |
| 4154 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4155 | if (ret < 0) |
| 4156 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4157 | io_req_complete(req, ret); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4158 | return 0; |
| 4159 | } |
| 4160 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4161 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4162 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 4163 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL))) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4164 | return -EINVAL; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4165 | if (sqe->ioprio || sqe->buf_index) |
| 4166 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4167 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4168 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4169 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4170 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4171 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4172 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4173 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4174 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4175 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4176 | return 0; |
| 4177 | } |
| 4178 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4179 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4180 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4181 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4182 | int ret; |
| 4183 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4184 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4185 | /* only need file table for an actual valid fd */ |
| 4186 | if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD) |
| 4187 | req->flags |= REQ_F_NO_FILE_TABLE; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4188 | return -EAGAIN; |
Jens Axboe | 5b0bbee | 2020-04-27 10:41:22 -0600 | [diff] [blame] | 4189 | } |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4190 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4191 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4192 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4193 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4194 | if (ret < 0) |
| 4195 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4196 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4197 | return 0; |
| 4198 | } |
| 4199 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4200 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4201 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4202 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4203 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4204 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 4205 | sqe->rw_flags || sqe->buf_index) |
| 4206 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4207 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4208 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4209 | |
| 4210 | req->close.fd = READ_ONCE(sqe->fd); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4211 | return 0; |
| 4212 | } |
| 4213 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4214 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4215 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4216 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4217 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4218 | struct fdtable *fdt; |
| 4219 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4220 | int ret; |
| 4221 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4222 | file = NULL; |
| 4223 | ret = -EBADF; |
| 4224 | spin_lock(&files->file_lock); |
| 4225 | fdt = files_fdtable(files); |
| 4226 | if (close->fd >= fdt->max_fds) { |
| 4227 | spin_unlock(&files->file_lock); |
| 4228 | goto err; |
| 4229 | } |
| 4230 | file = fdt->fd[close->fd]; |
| 4231 | if (!file) { |
| 4232 | spin_unlock(&files->file_lock); |
| 4233 | goto err; |
| 4234 | } |
| 4235 | |
| 4236 | if (file->f_op == &io_uring_fops) { |
| 4237 | spin_unlock(&files->file_lock); |
| 4238 | file = NULL; |
| 4239 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4240 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4241 | |
| 4242 | /* 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] | 4243 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4244 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4245 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4246 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4247 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4248 | ret = __close_fd_get_file(close->fd, &file); |
| 4249 | spin_unlock(&files->file_lock); |
| 4250 | if (ret < 0) { |
| 4251 | if (ret == -ENOENT) |
| 4252 | ret = -EBADF; |
| 4253 | goto err; |
| 4254 | } |
| 4255 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4256 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4257 | ret = filp_close(file, current->files); |
| 4258 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4259 | if (ret < 0) |
| 4260 | req_set_fail_links(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4261 | if (file) |
| 4262 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4263 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4264 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4265 | } |
| 4266 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4267 | 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] | 4268 | { |
| 4269 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4270 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4271 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4272 | return -EINVAL; |
| 4273 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 4274 | return -EINVAL; |
| 4275 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4276 | req->sync.off = READ_ONCE(sqe->off); |
| 4277 | req->sync.len = READ_ONCE(sqe->len); |
| 4278 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4279 | return 0; |
| 4280 | } |
| 4281 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4282 | 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] | 4283 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4284 | int ret; |
| 4285 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4286 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4287 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4288 | return -EAGAIN; |
| 4289 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4290 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4291 | req->sync.flags); |
| 4292 | if (ret < 0) |
| 4293 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4294 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4295 | return 0; |
| 4296 | } |
| 4297 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4298 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4299 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4300 | struct io_async_msghdr *kmsg) |
| 4301 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4302 | struct io_async_msghdr *async_msg = req->async_data; |
| 4303 | |
| 4304 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4305 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4306 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4307 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4308 | return -ENOMEM; |
| 4309 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4310 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4311 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4312 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4313 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4314 | /* if were using fast_iov, set it to the new one */ |
| 4315 | if (!async_msg->free_iov) |
| 4316 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4317 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4318 | return -EAGAIN; |
| 4319 | } |
| 4320 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4321 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4322 | struct io_async_msghdr *iomsg) |
| 4323 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4324 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4325 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4326 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4327 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4328 | } |
| 4329 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4330 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4331 | { |
| 4332 | int ret; |
| 4333 | |
| 4334 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4335 | return 0; |
| 4336 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4337 | if (!ret) |
| 4338 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4339 | return ret; |
| 4340 | } |
| 4341 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4342 | 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] | 4343 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4344 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4345 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4346 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4347 | return -EINVAL; |
| 4348 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4349 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4350 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4351 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4352 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4353 | #ifdef CONFIG_COMPAT |
| 4354 | if (req->ctx->compat) |
| 4355 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4356 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4357 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4360 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4361 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4362 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4363 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4364 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4365 | int ret; |
| 4366 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4367 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4368 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4369 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4370 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4371 | kmsg = req->async_data; |
| 4372 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4373 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4374 | if (ret) |
| 4375 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4376 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4377 | } |
| 4378 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4379 | flags = req->sr_msg.msg_flags; |
| 4380 | if (flags & MSG_DONTWAIT) |
| 4381 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4382 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4383 | flags |= MSG_DONTWAIT; |
| 4384 | |
| 4385 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4386 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4387 | return io_setup_async_msg(req, kmsg); |
| 4388 | if (ret == -ERESTARTSYS) |
| 4389 | ret = -EINTR; |
| 4390 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4391 | /* fast path, check for non-NULL to avoid function call */ |
| 4392 | if (kmsg->free_iov) |
| 4393 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4394 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4395 | if (ret < 0) |
| 4396 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4397 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4398 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4399 | } |
| 4400 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4401 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4402 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4403 | struct io_sr_msg *sr = &req->sr_msg; |
| 4404 | struct msghdr msg; |
| 4405 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4406 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4407 | unsigned flags; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4408 | int ret; |
| 4409 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4410 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4411 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4412 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4413 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4414 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4415 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4416 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4417 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4418 | msg.msg_name = NULL; |
| 4419 | msg.msg_control = NULL; |
| 4420 | msg.msg_controllen = 0; |
| 4421 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4422 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4423 | flags = req->sr_msg.msg_flags; |
| 4424 | if (flags & MSG_DONTWAIT) |
| 4425 | req->flags |= REQ_F_NOWAIT; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4426 | else if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4427 | flags |= MSG_DONTWAIT; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4428 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4429 | msg.msg_flags = flags; |
| 4430 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4431 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4432 | return -EAGAIN; |
| 4433 | if (ret == -ERESTARTSYS) |
| 4434 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4435 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4436 | if (ret < 0) |
| 4437 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4438 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4439 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4442 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4443 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4444 | { |
| 4445 | struct io_sr_msg *sr = &req->sr_msg; |
| 4446 | struct iovec __user *uiov; |
| 4447 | size_t iov_len; |
| 4448 | int ret; |
| 4449 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4450 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4451 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4452 | if (ret) |
| 4453 | return ret; |
| 4454 | |
| 4455 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4456 | if (iov_len > 1) |
| 4457 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4458 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4459 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4460 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4461 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4462 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4463 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4464 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4465 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4466 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4467 | if (ret > 0) |
| 4468 | ret = 0; |
| 4469 | } |
| 4470 | |
| 4471 | return ret; |
| 4472 | } |
| 4473 | |
| 4474 | #ifdef CONFIG_COMPAT |
| 4475 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4476 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4477 | { |
| 4478 | struct compat_msghdr __user *msg_compat; |
| 4479 | struct io_sr_msg *sr = &req->sr_msg; |
| 4480 | struct compat_iovec __user *uiov; |
| 4481 | compat_uptr_t ptr; |
| 4482 | compat_size_t len; |
| 4483 | int ret; |
| 4484 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4485 | msg_compat = (struct compat_msghdr __user *) sr->umsg; |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4486 | ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4487 | &ptr, &len); |
| 4488 | if (ret) |
| 4489 | return ret; |
| 4490 | |
| 4491 | uiov = compat_ptr(ptr); |
| 4492 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4493 | compat_ssize_t clen; |
| 4494 | |
| 4495 | if (len > 1) |
| 4496 | return -EINVAL; |
| 4497 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4498 | return -EFAULT; |
| 4499 | if (__get_user(clen, &uiov->iov_len)) |
| 4500 | return -EFAULT; |
| 4501 | if (clen < 0) |
| 4502 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4503 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4504 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4505 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4506 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4507 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4508 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4509 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4510 | if (ret < 0) |
| 4511 | return ret; |
| 4512 | } |
| 4513 | |
| 4514 | return 0; |
| 4515 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4516 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4517 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4518 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4519 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4520 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4521 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4522 | |
| 4523 | #ifdef CONFIG_COMPAT |
| 4524 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4525 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4526 | #endif |
| 4527 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4528 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4531 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4532 | bool needs_lock) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4533 | { |
| 4534 | struct io_sr_msg *sr = &req->sr_msg; |
| 4535 | struct io_buffer *kbuf; |
| 4536 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4537 | kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock); |
| 4538 | if (IS_ERR(kbuf)) |
| 4539 | return kbuf; |
| 4540 | |
| 4541 | sr->kbuf = kbuf; |
| 4542 | req->flags |= REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4543 | return kbuf; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4544 | } |
| 4545 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4546 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4547 | { |
| 4548 | return io_put_kbuf(req, req->sr_msg.kbuf); |
| 4549 | } |
| 4550 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4551 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4552 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4553 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4554 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4555 | if (!io_op_defs[req->opcode].needs_async_data) |
| 4556 | return 0; |
| 4557 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 4558 | if (!ret) |
| 4559 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4560 | return ret; |
| 4561 | } |
| 4562 | |
| 4563 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4564 | { |
| 4565 | struct io_sr_msg *sr = &req->sr_msg; |
| 4566 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4567 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4568 | return -EINVAL; |
| 4569 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4570 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4571 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 4572 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4573 | sr->bgid = READ_ONCE(sqe->buf_group); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4574 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4575 | #ifdef CONFIG_COMPAT |
| 4576 | if (req->ctx->compat) |
| 4577 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4578 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4579 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4580 | } |
| 4581 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4582 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4583 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4584 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4585 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4586 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4587 | unsigned flags; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4588 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4589 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4590 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4591 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4592 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4593 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4594 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4595 | kmsg = req->async_data; |
| 4596 | if (!kmsg) { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4597 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 4598 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 4599 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4600 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4601 | } |
| 4602 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4603 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4604 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4605 | if (IS_ERR(kbuf)) |
| 4606 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4607 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4608 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 4609 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4610 | 1, req->sr_msg.len); |
| 4611 | } |
| 4612 | |
| 4613 | flags = req->sr_msg.msg_flags; |
| 4614 | if (flags & MSG_DONTWAIT) |
| 4615 | req->flags |= REQ_F_NOWAIT; |
| 4616 | else if (force_nonblock) |
| 4617 | flags |= MSG_DONTWAIT; |
| 4618 | |
| 4619 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 4620 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4621 | if (force_nonblock && ret == -EAGAIN) |
| 4622 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4623 | if (ret == -ERESTARTSYS) |
| 4624 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 4625 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4626 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4627 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4628 | /* fast path, check for non-NULL to avoid function call */ |
| 4629 | if (kmsg->free_iov) |
| 4630 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4631 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4632 | if (ret < 0) |
| 4633 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4634 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4635 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4636 | } |
| 4637 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4638 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4639 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4640 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4641 | struct io_sr_msg *sr = &req->sr_msg; |
| 4642 | struct msghdr msg; |
| 4643 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4644 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4645 | struct iovec iov; |
| 4646 | unsigned flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4647 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4648 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4649 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4650 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4651 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4652 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4653 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 4654 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4655 | kbuf = io_recv_buffer_select(req, !force_nonblock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4656 | if (IS_ERR(kbuf)) |
| 4657 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4658 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4661 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4662 | if (unlikely(ret)) |
| 4663 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4664 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4665 | msg.msg_name = NULL; |
| 4666 | msg.msg_control = NULL; |
| 4667 | msg.msg_controllen = 0; |
| 4668 | msg.msg_namelen = 0; |
| 4669 | msg.msg_iocb = NULL; |
| 4670 | msg.msg_flags = 0; |
| 4671 | |
| 4672 | flags = req->sr_msg.msg_flags; |
| 4673 | if (flags & MSG_DONTWAIT) |
| 4674 | req->flags |= REQ_F_NOWAIT; |
| 4675 | else if (force_nonblock) |
| 4676 | flags |= MSG_DONTWAIT; |
| 4677 | |
| 4678 | ret = sock_recvmsg(sock, &msg, flags); |
| 4679 | if (force_nonblock && ret == -EAGAIN) |
| 4680 | return -EAGAIN; |
| 4681 | if (ret == -ERESTARTSYS) |
| 4682 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 4683 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4684 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 4685 | cflags = io_put_recv_kbuf(req); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4686 | if (ret < 0) |
| 4687 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4688 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4689 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4690 | } |
| 4691 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4692 | 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] | 4693 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4694 | struct io_accept *accept = &req->accept; |
| 4695 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4696 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4697 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 4698 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4699 | return -EINVAL; |
| 4700 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 4701 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4702 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4703 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4704 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4705 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4706 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4707 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4708 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4709 | { |
| 4710 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4711 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4712 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4713 | int ret; |
| 4714 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 4715 | if (req->file->f_flags & O_NONBLOCK) |
| 4716 | req->flags |= REQ_F_NOWAIT; |
| 4717 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4718 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 4719 | accept->addr_len, accept->flags, |
| 4720 | accept->nofile); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4721 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4722 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4723 | if (ret < 0) { |
| 4724 | if (ret == -ERESTARTSYS) |
| 4725 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4726 | req_set_fail_links(req); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4727 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4728 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4729 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4730 | } |
| 4731 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4732 | static int io_connect_prep_async(struct io_kiocb *req) |
| 4733 | { |
| 4734 | struct io_async_connect *io = req->async_data; |
| 4735 | struct io_connect *conn = &req->connect; |
| 4736 | |
| 4737 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 4738 | } |
| 4739 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4740 | 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] | 4741 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4742 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4743 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4744 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4745 | return -EINVAL; |
| 4746 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 4747 | return -EINVAL; |
| 4748 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4749 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4750 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4751 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4752 | } |
| 4753 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4754 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4755 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4756 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4757 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4758 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4759 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4760 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4761 | if (req->async_data) { |
| 4762 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4763 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4764 | ret = move_addr_to_kernel(req->connect.addr, |
| 4765 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4766 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4767 | if (ret) |
| 4768 | goto out; |
| 4769 | io = &__io; |
| 4770 | } |
| 4771 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4772 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 4773 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4774 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 4775 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 4776 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4777 | if (req->async_data) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4778 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4779 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4780 | ret = -ENOMEM; |
| 4781 | goto out; |
| 4782 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4783 | io = req->async_data; |
| 4784 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4785 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4786 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4787 | if (ret == -ERESTARTSYS) |
| 4788 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4789 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4790 | if (ret < 0) |
| 4791 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4792 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4793 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4794 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4795 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4796 | #define IO_NETOP_FN(op) \ |
| 4797 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 4798 | { \ |
| 4799 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4800 | } |
| 4801 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4802 | #define IO_NETOP_PREP(op) \ |
| 4803 | IO_NETOP_FN(op) \ |
| 4804 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 4805 | { \ |
| 4806 | return -EOPNOTSUPP; \ |
| 4807 | } \ |
| 4808 | |
| 4809 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 4810 | IO_NETOP_PREP(op) \ |
| 4811 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 4812 | { \ |
| 4813 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4814 | } |
| 4815 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 4816 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 4817 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 4818 | IO_NETOP_PREP_ASYNC(connect); |
| 4819 | IO_NETOP_PREP(accept); |
| 4820 | IO_NETOP_FN(send); |
| 4821 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4822 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4823 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4824 | struct io_poll_table { |
| 4825 | struct poll_table_struct pt; |
| 4826 | struct io_kiocb *req; |
| 4827 | int error; |
| 4828 | }; |
| 4829 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4830 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
| 4831 | __poll_t mask, task_work_func_t func) |
| 4832 | { |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4833 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4834 | |
| 4835 | /* for instances that support it check for an event match first: */ |
| 4836 | if (mask && !(mask & poll->events)) |
| 4837 | return 0; |
| 4838 | |
| 4839 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 4840 | |
| 4841 | list_del_init(&poll->wait.entry); |
| 4842 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4843 | req->result = mask; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 4844 | req->task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4845 | percpu_ref_get(&req->ctx->refs); |
| 4846 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4847 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4848 | * If this fails, then the task is exiting. When a task exits, the |
| 4849 | * work gets canceled, so just cancel this request as well instead |
| 4850 | * of executing it. We can't safely execute it anyway, as we may not |
| 4851 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4852 | */ |
Jens Axboe | 355fb9e | 2020-10-22 20:19:35 -0600 | [diff] [blame] | 4853 | ret = io_req_task_work_add(req); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4854 | if (unlikely(ret)) { |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 4855 | WRITE_ONCE(poll->canceled, true); |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 4856 | io_req_task_work_add_fallback(req, func); |
Jens Axboe | aa96bf8 | 2020-04-03 11:26:26 -0600 | [diff] [blame] | 4857 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 4858 | return 1; |
| 4859 | } |
| 4860 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 4861 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 4862 | __acquires(&req->ctx->completion_lock) |
| 4863 | { |
| 4864 | struct io_ring_ctx *ctx = req->ctx; |
| 4865 | |
| 4866 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4867 | struct poll_table_struct pt = { ._key = poll->events }; |
| 4868 | |
| 4869 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 4870 | } |
| 4871 | |
| 4872 | spin_lock_irq(&ctx->completion_lock); |
| 4873 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 4874 | add_wait_queue(poll->head, &poll->wait); |
| 4875 | return true; |
| 4876 | } |
| 4877 | |
| 4878 | return false; |
| 4879 | } |
| 4880 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4881 | 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] | 4882 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4883 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4884 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4885 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4886 | return req->apoll->double_poll; |
| 4887 | } |
| 4888 | |
| 4889 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 4890 | { |
| 4891 | if (req->opcode == IORING_OP_POLL_ADD) |
| 4892 | return &req->poll; |
| 4893 | return &req->apoll->poll; |
| 4894 | } |
| 4895 | |
| 4896 | static void io_poll_remove_double(struct io_kiocb *req) |
| 4897 | { |
| 4898 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4899 | |
| 4900 | lockdep_assert_held(&req->ctx->completion_lock); |
| 4901 | |
| 4902 | if (poll && poll->head) { |
| 4903 | struct wait_queue_head *head = poll->head; |
| 4904 | |
| 4905 | spin_lock(&head->lock); |
| 4906 | list_del_init(&poll->wait.entry); |
| 4907 | if (poll->wait.private) |
| 4908 | refcount_dec(&req->refs); |
| 4909 | poll->head = NULL; |
| 4910 | spin_unlock(&head->lock); |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
| 4915 | { |
| 4916 | struct io_ring_ctx *ctx = req->ctx; |
| 4917 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4918 | io_poll_remove_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4919 | req->poll.done = true; |
| 4920 | io_cqring_fill_event(req, error ? error : mangle_poll(mask)); |
| 4921 | io_commit_cqring(ctx); |
| 4922 | } |
| 4923 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4924 | static void io_poll_task_func(struct callback_head *cb) |
| 4925 | { |
| 4926 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4927 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4928 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4929 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 4930 | if (io_poll_rewait(req, &req->poll)) { |
| 4931 | spin_unlock_irq(&ctx->completion_lock); |
| 4932 | } else { |
| 4933 | hash_del(&req->hash_node); |
| 4934 | io_poll_complete(req, req->result, 0); |
| 4935 | spin_unlock_irq(&ctx->completion_lock); |
| 4936 | |
| 4937 | nxt = io_put_req_find_next(req); |
| 4938 | io_cqring_ev_posted(ctx); |
| 4939 | if (nxt) |
| 4940 | __io_req_task_submit(nxt); |
| 4941 | } |
| 4942 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 4943 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 4947 | int sync, void *key) |
| 4948 | { |
| 4949 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4950 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4951 | __poll_t mask = key_to_poll(key); |
| 4952 | |
| 4953 | /* for instances that support it check for an event match first: */ |
| 4954 | if (mask && !(mask & poll->events)) |
| 4955 | return 0; |
| 4956 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 4957 | list_del_init(&wait->entry); |
| 4958 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4959 | if (poll && poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4960 | bool done; |
| 4961 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4962 | spin_lock(&poll->head->lock); |
| 4963 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4964 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4965 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 4966 | /* make sure double remove sees this as being gone */ |
| 4967 | wait->private = NULL; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 4968 | spin_unlock(&poll->head->lock); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 4969 | if (!done) { |
| 4970 | /* use wait func handler, so it matches the rq type */ |
| 4971 | poll->wait.func(&poll->wait, mode, sync, key); |
| 4972 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4973 | } |
| 4974 | refcount_dec(&req->refs); |
| 4975 | return 1; |
| 4976 | } |
| 4977 | |
| 4978 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 4979 | wait_queue_func_t wake_func) |
| 4980 | { |
| 4981 | poll->head = NULL; |
| 4982 | poll->done = false; |
| 4983 | poll->canceled = false; |
| 4984 | poll->events = events; |
| 4985 | INIT_LIST_HEAD(&poll->wait.entry); |
| 4986 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 4987 | } |
| 4988 | |
| 4989 | 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] | 4990 | struct wait_queue_head *head, |
| 4991 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 4992 | { |
| 4993 | struct io_kiocb *req = pt->req; |
| 4994 | |
| 4995 | /* |
| 4996 | * If poll->head is already set, it's because the file being polled |
| 4997 | * uses multiple waitqueues for poll handling (eg one for read, one |
| 4998 | * for write). Setup a separate io_poll_iocb if this happens. |
| 4999 | */ |
| 5000 | if (unlikely(poll->head)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5001 | struct io_poll_iocb *poll_one = poll; |
| 5002 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5003 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5004 | if (*poll_ptr) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5005 | pt->error = -EINVAL; |
| 5006 | return; |
| 5007 | } |
| 5008 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5009 | if (!poll) { |
| 5010 | pt->error = -ENOMEM; |
| 5011 | return; |
| 5012 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5013 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5014 | refcount_inc(&req->refs); |
| 5015 | poll->wait.private = req; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5016 | *poll_ptr = poll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | pt->error = 0; |
| 5020 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5021 | |
| 5022 | if (poll->events & EPOLLEXCLUSIVE) |
| 5023 | add_wait_queue_exclusive(head, &poll->wait); |
| 5024 | else |
| 5025 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5029 | struct poll_table_struct *p) |
| 5030 | { |
| 5031 | 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] | 5032 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5033 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5034 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5035 | } |
| 5036 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5037 | static void io_async_task_func(struct callback_head *cb) |
| 5038 | { |
| 5039 | struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| 5040 | struct async_poll *apoll = req->apoll; |
| 5041 | struct io_ring_ctx *ctx = req->ctx; |
| 5042 | |
| 5043 | trace_io_uring_task_run(req->ctx, req->opcode, req->user_data); |
| 5044 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5045 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5046 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5047 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5048 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5049 | } |
| 5050 | |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5051 | /* 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] | 5052 | if (hash_hashed(&req->hash_node)) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5053 | hash_del(&req->hash_node); |
Jens Axboe | 2bae047 | 2020-04-13 11:16:34 -0600 | [diff] [blame] | 5054 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5055 | io_poll_remove_double(req); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5056 | spin_unlock_irq(&ctx->completion_lock); |
| 5057 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5058 | if (!READ_ONCE(apoll->poll.canceled)) |
| 5059 | __io_req_task_submit(req); |
| 5060 | else |
| 5061 | __io_req_task_cancel(req, -ECANCELED); |
Dan Carpenter | aa34084 | 2020-07-08 21:47:11 +0300 | [diff] [blame] | 5062 | |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5063 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5064 | kfree(apoll->double_poll); |
Jens Axboe | 3106725 | 2020-05-17 17:43:31 -0600 | [diff] [blame] | 5065 | kfree(apoll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5069 | void *key) |
| 5070 | { |
| 5071 | struct io_kiocb *req = wait->private; |
| 5072 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5073 | |
| 5074 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5075 | key_to_poll(key)); |
| 5076 | |
| 5077 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5078 | } |
| 5079 | |
| 5080 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5081 | { |
| 5082 | struct io_ring_ctx *ctx = req->ctx; |
| 5083 | struct hlist_head *list; |
| 5084 | |
| 5085 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5086 | hlist_add_head(&req->hash_node, list); |
| 5087 | } |
| 5088 | |
| 5089 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5090 | struct io_poll_iocb *poll, |
| 5091 | struct io_poll_table *ipt, __poll_t mask, |
| 5092 | wait_queue_func_t wake_func) |
| 5093 | __acquires(&ctx->completion_lock) |
| 5094 | { |
| 5095 | struct io_ring_ctx *ctx = req->ctx; |
| 5096 | bool cancel = false; |
| 5097 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5098 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5099 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5100 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5101 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5102 | |
| 5103 | ipt->pt._key = mask; |
| 5104 | ipt->req = req; |
| 5105 | ipt->error = -EINVAL; |
| 5106 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5107 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5108 | |
| 5109 | spin_lock_irq(&ctx->completion_lock); |
| 5110 | if (likely(poll->head)) { |
| 5111 | spin_lock(&poll->head->lock); |
| 5112 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5113 | if (ipt->error) |
| 5114 | cancel = true; |
| 5115 | ipt->error = 0; |
| 5116 | mask = 0; |
| 5117 | } |
| 5118 | if (mask || ipt->error) |
| 5119 | list_del_init(&poll->wait.entry); |
| 5120 | else if (cancel) |
| 5121 | WRITE_ONCE(poll->canceled, true); |
| 5122 | else if (!poll->done) /* actually waiting for an event */ |
| 5123 | io_poll_req_insert(req); |
| 5124 | spin_unlock(&poll->head->lock); |
| 5125 | } |
| 5126 | |
| 5127 | return mask; |
| 5128 | } |
| 5129 | |
| 5130 | static bool io_arm_poll_handler(struct io_kiocb *req) |
| 5131 | { |
| 5132 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5133 | struct io_ring_ctx *ctx = req->ctx; |
| 5134 | struct async_poll *apoll; |
| 5135 | struct io_poll_table ipt; |
| 5136 | __poll_t mask, ret; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5137 | int rw; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5138 | |
| 5139 | if (!req->file || !file_can_poll(req->file)) |
| 5140 | return false; |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 5141 | if (req->flags & REQ_F_POLLED) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5142 | return false; |
Jens Axboe | 9dab14b | 2020-08-25 12:27:50 -0600 | [diff] [blame] | 5143 | if (def->pollin) |
| 5144 | rw = READ; |
| 5145 | else if (def->pollout) |
| 5146 | rw = WRITE; |
| 5147 | else |
| 5148 | return false; |
| 5149 | /* if we can't nonblock try, then no point in arming a poll handler */ |
| 5150 | if (!io_file_supports_async(req->file, rw)) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5151 | return false; |
| 5152 | |
| 5153 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5154 | if (unlikely(!apoll)) |
| 5155 | return false; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5156 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5157 | |
| 5158 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5159 | req->apoll = apoll; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5160 | |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5161 | mask = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5162 | if (def->pollin) |
Nathan Chancellor | 8755d97 | 2020-03-02 16:01:19 -0700 | [diff] [blame] | 5163 | mask |= POLLIN | POLLRDNORM; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5164 | if (def->pollout) |
| 5165 | mask |= POLLOUT | POLLWRNORM; |
Luke Hsiao | 901341b | 2020-08-21 21:41:05 -0700 | [diff] [blame] | 5166 | |
| 5167 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5168 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5169 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5170 | mask &= ~POLLIN; |
| 5171 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5172 | mask |= POLLERR | POLLPRI; |
| 5173 | |
| 5174 | ipt.pt._qproc = io_async_queue_proc; |
| 5175 | |
| 5176 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5177 | io_async_wake); |
Jens Axboe | a36da65 | 2020-08-11 09:50:19 -0600 | [diff] [blame] | 5178 | if (ret || ipt.error) { |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5179 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5180 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5181 | kfree(apoll->double_poll); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5182 | kfree(apoll); |
| 5183 | return false; |
| 5184 | } |
| 5185 | spin_unlock_irq(&ctx->completion_lock); |
| 5186 | trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask, |
| 5187 | apoll->poll.events); |
| 5188 | return true; |
| 5189 | } |
| 5190 | |
| 5191 | static bool __io_poll_remove_one(struct io_kiocb *req, |
| 5192 | struct io_poll_iocb *poll) |
| 5193 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5194 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5195 | |
| 5196 | spin_lock(&poll->head->lock); |
| 5197 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5198 | if (!list_empty(&poll->wait.entry)) { |
| 5199 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5200 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5201 | } |
| 5202 | spin_unlock(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5203 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5204 | return do_complete; |
| 5205 | } |
| 5206 | |
| 5207 | static bool io_poll_remove_one(struct io_kiocb *req) |
| 5208 | { |
| 5209 | bool do_complete; |
| 5210 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5211 | io_poll_remove_double(req); |
| 5212 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5213 | if (req->opcode == IORING_OP_POLL_ADD) { |
| 5214 | do_complete = __io_poll_remove_one(req, &req->poll); |
| 5215 | } else { |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5216 | struct async_poll *apoll = req->apoll; |
| 5217 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5218 | /* non-poll requests have submit ref still */ |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5219 | do_complete = __io_poll_remove_one(req, &apoll->poll); |
| 5220 | if (do_complete) { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5221 | io_put_req(req); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5222 | kfree(apoll->double_poll); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5223 | kfree(apoll); |
| 5224 | } |
Xiaoguang Wang | b1f573b | 2020-04-12 14:50:54 +0800 | [diff] [blame] | 5225 | } |
| 5226 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5227 | if (do_complete) { |
| 5228 | io_cqring_fill_event(req, -ECANCELED); |
| 5229 | io_commit_cqring(req->ctx); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5230 | req_set_fail_links(req); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 5231 | io_put_req_deferred(req, 1); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5232 | } |
| 5233 | |
| 5234 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5235 | } |
| 5236 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5237 | /* |
| 5238 | * Returns true if we found and killed one or more poll requests |
| 5239 | */ |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5240 | static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| 5241 | struct files_struct *files) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5242 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5243 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5244 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5245 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5246 | |
| 5247 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5248 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5249 | struct hlist_head *list; |
| 5250 | |
| 5251 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5252 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 5253 | if (io_match_task(req, tsk, files)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5254 | posted += io_poll_remove_one(req); |
| 5255 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5256 | } |
| 5257 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5258 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5259 | if (posted) |
| 5260 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5261 | |
| 5262 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5263 | } |
| 5264 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5265 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 5266 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5267 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5268 | struct io_kiocb *req; |
| 5269 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5270 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5271 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5272 | if (sqe_addr != req->user_data) |
| 5273 | continue; |
| 5274 | if (io_poll_remove_one(req)) |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5275 | return 0; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5276 | return -EALREADY; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | return -ENOENT; |
| 5280 | } |
| 5281 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5282 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 5283 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5284 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5285 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5286 | return -EINVAL; |
| 5287 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 5288 | sqe->poll_events) |
| 5289 | return -EINVAL; |
| 5290 | |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5291 | req->poll_remove.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5292 | return 0; |
| 5293 | } |
| 5294 | |
| 5295 | /* |
| 5296 | * Find a running poll command that matches one specified in sqe->addr, |
| 5297 | * and remove it if found. |
| 5298 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5299 | 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] | 5300 | { |
| 5301 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5302 | int ret; |
| 5303 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5304 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 5305 | ret = io_poll_cancel(ctx, req->poll_remove.addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5306 | spin_unlock_irq(&ctx->completion_lock); |
| 5307 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5308 | if (ret < 0) |
| 5309 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 5310 | io_req_complete(req, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5311 | return 0; |
| 5312 | } |
| 5313 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5314 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5315 | void *key) |
| 5316 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5317 | struct io_kiocb *req = wait->private; |
| 5318 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5319 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5320 | 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] | 5321 | } |
| 5322 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5323 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5324 | struct poll_table_struct *p) |
| 5325 | { |
| 5326 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5327 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5328 | __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] | 5329 | } |
| 5330 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5331 | 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] | 5332 | { |
| 5333 | struct io_poll_iocb *poll = &req->poll; |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5334 | u32 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5335 | |
| 5336 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5337 | return -EINVAL; |
| 5338 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 5339 | return -EINVAL; |
| 5340 | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 5341 | events = READ_ONCE(sqe->poll32_events); |
| 5342 | #ifdef __BIG_ENDIAN |
| 5343 | events = swahw32(events); |
| 5344 | #endif |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5345 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP | |
| 5346 | (events & EPOLLEXCLUSIVE); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5347 | return 0; |
| 5348 | } |
| 5349 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5350 | 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] | 5351 | { |
| 5352 | struct io_poll_iocb *poll = &req->poll; |
| 5353 | struct io_ring_ctx *ctx = req->ctx; |
| 5354 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5355 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5356 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5357 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5358 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5359 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5360 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5361 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5362 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5363 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5364 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5365 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5366 | spin_unlock_irq(&ctx->completion_lock); |
| 5367 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5368 | if (mask) { |
| 5369 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5370 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5371 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5372 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5373 | } |
| 5374 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5375 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5376 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5377 | struct io_timeout_data *data = container_of(timer, |
| 5378 | struct io_timeout_data, timer); |
| 5379 | struct io_kiocb *req = data->req; |
| 5380 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5381 | unsigned long flags; |
| 5382 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5383 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5384 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5385 | atomic_set(&req->ctx->cq_timeouts, |
| 5386 | atomic_read(&req->ctx->cq_timeouts) + 1); |
| 5387 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 5388 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5389 | io_commit_cqring(ctx); |
| 5390 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5391 | |
| 5392 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5393 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5394 | io_put_req(req); |
| 5395 | return HRTIMER_NORESTART; |
| 5396 | } |
| 5397 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5398 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5399 | __u64 user_data) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5400 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5401 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5402 | struct io_kiocb *req; |
| 5403 | int ret = -ENOENT; |
| 5404 | |
| 5405 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
| 5406 | if (user_data == req->user_data) { |
| 5407 | ret = 0; |
| 5408 | break; |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | if (ret == -ENOENT) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5413 | return ERR_PTR(ret); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5414 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5415 | io = req->async_data; |
| 5416 | ret = hrtimer_try_to_cancel(&io->timer); |
| 5417 | if (ret == -1) |
| 5418 | return ERR_PTR(-EALREADY); |
| 5419 | list_del_init(&req->timeout.list); |
| 5420 | return req; |
| 5421 | } |
| 5422 | |
| 5423 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 5424 | { |
| 5425 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5426 | |
| 5427 | if (IS_ERR(req)) |
| 5428 | return PTR_ERR(req); |
| 5429 | |
| 5430 | req_set_fail_links(req); |
| 5431 | io_cqring_fill_event(req, -ECANCELED); |
| 5432 | io_put_req_deferred(req, 1); |
| 5433 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5434 | } |
| 5435 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5436 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 5437 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 5438 | { |
| 5439 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 5440 | struct io_timeout_data *data; |
| 5441 | |
| 5442 | if (IS_ERR(req)) |
| 5443 | return PTR_ERR(req); |
| 5444 | |
| 5445 | req->timeout.off = 0; /* noseq */ |
| 5446 | data = req->async_data; |
| 5447 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
| 5448 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode); |
| 5449 | data->timer.function = io_timeout_fn; |
| 5450 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 5451 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5452 | } |
| 5453 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5454 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 5455 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5456 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5457 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 5458 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5459 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5460 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5461 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5462 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5463 | if (sqe->ioprio || sqe->buf_index || sqe->len) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5464 | return -EINVAL; |
| 5465 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5466 | tr->addr = READ_ONCE(sqe->addr); |
| 5467 | tr->flags = READ_ONCE(sqe->timeout_flags); |
| 5468 | if (tr->flags & IORING_TIMEOUT_UPDATE) { |
| 5469 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS)) |
| 5470 | return -EINVAL; |
| 5471 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 5472 | return -EFAULT; |
| 5473 | } else if (tr->flags) { |
| 5474 | /* timeout removal doesn't support flags */ |
| 5475 | return -EINVAL; |
| 5476 | } |
| 5477 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5478 | return 0; |
| 5479 | } |
| 5480 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5481 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 5482 | { |
| 5483 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 5484 | : HRTIMER_MODE_REL; |
| 5485 | } |
| 5486 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5487 | /* |
| 5488 | * Remove or update an existing timeout command |
| 5489 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5490 | 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] | 5491 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5492 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5493 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5494 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5495 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5496 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5497 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 5498 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5499 | else |
| 5500 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, |
| 5501 | io_translate_timeout_mode(tr->flags)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5502 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5503 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5504 | io_commit_cqring(ctx); |
| 5505 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5506 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5507 | if (ret < 0) |
| 5508 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 5509 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 5510 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5511 | } |
| 5512 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5513 | 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] | 5514 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5515 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5516 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5517 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5518 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5519 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5520 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5521 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5522 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5523 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 5524 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5525 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 5526 | flags = READ_ONCE(sqe->timeout_flags); |
| 5527 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5528 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 5529 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5530 | req->timeout.off = off; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5531 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5532 | if (!req->async_data && io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 5533 | return -ENOMEM; |
| 5534 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5535 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5536 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5537 | |
| 5538 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5539 | return -EFAULT; |
| 5540 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 5541 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5542 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 5543 | return 0; |
| 5544 | } |
| 5545 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5546 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5547 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5548 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5549 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5550 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5551 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5552 | |
Pavel Begunkov | 733f5c9 | 2020-05-26 20:34:03 +0300 | [diff] [blame] | 5553 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5554 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5555 | /* |
| 5556 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5557 | * timeout event to be satisfied. If it isn't set, then this is |
| 5558 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5559 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5560 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5561 | entry = ctx->timeout_list.prev; |
| 5562 | goto add; |
| 5563 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5564 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5565 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 5566 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5567 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 5568 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 5569 | * This is safe because ->completion_lock is held, and submissions |
| 5570 | * and completions are never mixed in the same ->completion_lock section. |
| 5571 | */ |
| 5572 | ctx->cq_last_tm_flush = tail; |
| 5573 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5574 | /* |
| 5575 | * Insertion sort, ensuring the first entry in the list is always |
| 5576 | * the one we need first. |
| 5577 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5578 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5579 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 5580 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5581 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 5582 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5583 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 5584 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 5585 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5586 | break; |
| 5587 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 5588 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 5589 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5590 | data->timer.function = io_timeout_fn; |
| 5591 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 5592 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5593 | return 0; |
| 5594 | } |
| 5595 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5596 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5597 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5598 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 5599 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5600 | return req->user_data == (unsigned long) data; |
| 5601 | } |
| 5602 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5603 | 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] | 5604 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5605 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5606 | int ret = 0; |
| 5607 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5608 | if (!tctx->io_wq) |
| 5609 | return -ENOENT; |
| 5610 | |
| 5611 | 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] | 5612 | switch (cancel_ret) { |
| 5613 | case IO_WQ_CANCEL_OK: |
| 5614 | ret = 0; |
| 5615 | break; |
| 5616 | case IO_WQ_CANCEL_RUNNING: |
| 5617 | ret = -EALREADY; |
| 5618 | break; |
| 5619 | case IO_WQ_CANCEL_NOTFOUND: |
| 5620 | ret = -ENOENT; |
| 5621 | break; |
| 5622 | } |
| 5623 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5624 | return ret; |
| 5625 | } |
| 5626 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5627 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 5628 | struct io_kiocb *req, __u64 sqe_addr, |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5629 | int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5630 | { |
| 5631 | unsigned long flags; |
| 5632 | int ret; |
| 5633 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 5634 | ret = io_async_cancel_one(req->task->io_uring, |
| 5635 | (void *) (unsigned long) sqe_addr); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5636 | if (ret != -ENOENT) { |
| 5637 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5638 | goto done; |
| 5639 | } |
| 5640 | |
| 5641 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 5642 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 5643 | if (ret != -ENOENT) |
| 5644 | goto done; |
| 5645 | ret = io_poll_cancel(ctx, sqe_addr); |
| 5646 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 5647 | if (!ret) |
| 5648 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5649 | io_cqring_fill_event(req, ret); |
| 5650 | io_commit_cqring(ctx); |
| 5651 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 5652 | io_cqring_ev_posted(ctx); |
| 5653 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5654 | if (ret < 0) |
| 5655 | req_set_fail_links(req); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5656 | io_put_req(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5657 | } |
| 5658 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5659 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 5660 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5661 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5662 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5663 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5664 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5665 | return -EINVAL; |
| 5666 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 5667 | return -EINVAL; |
| 5668 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5669 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5673 | 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] | 5674 | { |
| 5675 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5676 | |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 5677 | io_async_find_and_cancel(ctx, req, req->cancel.addr, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 5678 | return 0; |
| 5679 | } |
| 5680 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5681 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5682 | const struct io_uring_sqe *sqe) |
| 5683 | { |
Jens Axboe | 6ca56f8 | 2020-09-18 16:51:19 -0600 | [diff] [blame] | 5684 | if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL)) |
| 5685 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 5686 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 5687 | return -EINVAL; |
| 5688 | if (sqe->ioprio || sqe->rw_flags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5689 | return -EINVAL; |
| 5690 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5691 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 5692 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 5693 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5694 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5695 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5696 | return 0; |
| 5697 | } |
| 5698 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5699 | 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] | 5700 | { |
| 5701 | struct io_ring_ctx *ctx = req->ctx; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5702 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5703 | int ret; |
| 5704 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5705 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5706 | return -EAGAIN; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5707 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5708 | up.offset = req->rsrc_update.offset; |
| 5709 | up.data = req->rsrc_update.arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5710 | |
| 5711 | mutex_lock(&ctx->uring_lock); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5712 | ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5713 | mutex_unlock(&ctx->uring_lock); |
| 5714 | |
| 5715 | if (ret < 0) |
| 5716 | req_set_fail_links(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5717 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5718 | return 0; |
| 5719 | } |
| 5720 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5721 | 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] | 5722 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5723 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 5724 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5725 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5726 | case IORING_OP_READV: |
| 5727 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5728 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5729 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5730 | case IORING_OP_WRITEV: |
| 5731 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5732 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5733 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5734 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5735 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5736 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5737 | return io_poll_remove_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5738 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5739 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5740 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 5741 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5742 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5743 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5744 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5745 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5746 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5747 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5748 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5749 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5750 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5751 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 5752 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5753 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 5754 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5755 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 5756 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5757 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5758 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5759 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 5760 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5761 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 5762 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5763 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 5764 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5765 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5766 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 5767 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 5768 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5769 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 5770 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5771 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 5772 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5773 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 5774 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5775 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 5776 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5777 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 5778 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5779 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 5780 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5781 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 5782 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5783 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 5784 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5785 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 5786 | case IORING_OP_SHUTDOWN: |
| 5787 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5788 | case IORING_OP_RENAMEAT: |
| 5789 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5790 | case IORING_OP_UNLINKAT: |
| 5791 | return io_unlinkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 5792 | } |
| 5793 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 5794 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 5795 | req->opcode); |
| 5796 | return-EINVAL; |
| 5797 | } |
| 5798 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5799 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5800 | { |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5801 | switch (req->opcode) { |
| 5802 | case IORING_OP_READV: |
| 5803 | case IORING_OP_READ_FIXED: |
| 5804 | case IORING_OP_READ: |
| 5805 | return io_rw_prep_async(req, READ); |
| 5806 | case IORING_OP_WRITEV: |
| 5807 | case IORING_OP_WRITE_FIXED: |
| 5808 | case IORING_OP_WRITE: |
| 5809 | return io_rw_prep_async(req, WRITE); |
| 5810 | case IORING_OP_SENDMSG: |
| 5811 | case IORING_OP_SEND: |
| 5812 | return io_sendmsg_prep_async(req); |
| 5813 | case IORING_OP_RECVMSG: |
| 5814 | case IORING_OP_RECV: |
| 5815 | return io_recvmsg_prep_async(req); |
| 5816 | case IORING_OP_CONNECT: |
| 5817 | return io_connect_prep_async(req); |
| 5818 | } |
| 5819 | return 0; |
| 5820 | } |
| 5821 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5822 | static int io_req_defer_prep(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5823 | { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5824 | if (!io_op_defs[req->opcode].needs_async_data) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5825 | return 0; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5826 | /* some opcodes init it during the inital prep */ |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5827 | if (req->async_data) |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5828 | return 0; |
| 5829 | if (__io_alloc_async_data(req)) |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5830 | return -EAGAIN; |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5831 | return io_req_prep_async(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5832 | } |
| 5833 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5834 | static u32 io_get_sequence(struct io_kiocb *req) |
| 5835 | { |
| 5836 | struct io_kiocb *pos; |
| 5837 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5838 | u32 total_submitted, nr_reqs = 0; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5839 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 5840 | io_for_each_link(pos, req) |
| 5841 | nr_reqs++; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5842 | |
| 5843 | total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped; |
| 5844 | return total_submitted - nr_reqs; |
| 5845 | } |
| 5846 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5847 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5848 | { |
| 5849 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5850 | struct io_defer_entry *de; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5851 | int ret; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5852 | u32 seq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5853 | |
| 5854 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5855 | if (likely(list_empty_careful(&ctx->defer_list) && |
| 5856 | !(req->flags & REQ_F_IO_DRAIN))) |
| 5857 | return 0; |
| 5858 | |
| 5859 | seq = io_get_sequence(req); |
| 5860 | /* Still a chance to pass the sequence check */ |
| 5861 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5862 | return 0; |
| 5863 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 5864 | ret = io_req_defer_prep(req); |
| 5865 | if (ret) |
| 5866 | return ret; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 5867 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5868 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
| 5869 | if (!de) |
| 5870 | return -ENOMEM; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5871 | |
| 5872 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5873 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5874 | spin_unlock_irq(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5875 | kfree(de); |
Pavel Begunkov | ae34817 | 2020-07-23 20:25:20 +0300 | [diff] [blame] | 5876 | io_queue_async_work(req); |
| 5877 | return -EIOCBQUEUED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5881 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 5882 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 5883 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5884 | spin_unlock_irq(&ctx->completion_lock); |
| 5885 | return -EIOCBQUEUED; |
| 5886 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5887 | |
Pavel Begunkov | 3ca405e | 2020-07-13 23:37:08 +0300 | [diff] [blame] | 5888 | static void __io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5889 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5890 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
| 5891 | switch (req->opcode) { |
| 5892 | case IORING_OP_READV: |
| 5893 | case IORING_OP_READ_FIXED: |
| 5894 | case IORING_OP_READ: |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5895 | kfree((void *)(unsigned long)req->rw.addr); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5896 | break; |
| 5897 | case IORING_OP_RECVMSG: |
| 5898 | case IORING_OP_RECV: |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5899 | kfree(req->sr_msg.kbuf); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5900 | break; |
| 5901 | } |
| 5902 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5903 | } |
| 5904 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5905 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 5906 | switch (req->opcode) { |
| 5907 | case IORING_OP_READV: |
| 5908 | case IORING_OP_READ_FIXED: |
| 5909 | case IORING_OP_READ: |
| 5910 | case IORING_OP_WRITEV: |
| 5911 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5912 | case IORING_OP_WRITE: { |
| 5913 | struct io_async_rw *io = req->async_data; |
| 5914 | if (io->free_iovec) |
| 5915 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5916 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5917 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5918 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5919 | case IORING_OP_SENDMSG: { |
| 5920 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5921 | |
| 5922 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5923 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5924 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5925 | case IORING_OP_SPLICE: |
| 5926 | case IORING_OP_TEE: |
| 5927 | io_put_file(req, req->splice.file_in, |
| 5928 | (req->splice.flags & SPLICE_F_FD_IN_FIXED)); |
| 5929 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 5930 | case IORING_OP_OPENAT: |
| 5931 | case IORING_OP_OPENAT2: |
| 5932 | if (req->open.filename) |
| 5933 | putname(req->open.filename); |
| 5934 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 5935 | case IORING_OP_RENAMEAT: |
| 5936 | putname(req->rename.oldpath); |
| 5937 | putname(req->rename.newpath); |
| 5938 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 5939 | case IORING_OP_UNLINKAT: |
| 5940 | putname(req->unlink.filename); |
| 5941 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5942 | } |
| 5943 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 5944 | } |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5945 | } |
| 5946 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5947 | 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] | 5948 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5949 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5950 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5951 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 5952 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5953 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5954 | ret = io_nop(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 5955 | break; |
| 5956 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5957 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5958 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5959 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5960 | break; |
| 5961 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5962 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 5963 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5964 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5965 | break; |
| 5966 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5967 | ret = io_fsync(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5968 | break; |
| 5969 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5970 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5971 | break; |
| 5972 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5973 | ret = io_poll_remove(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5974 | break; |
| 5975 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5976 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5977 | break; |
| 5978 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5979 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 5980 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5981 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5982 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 5983 | break; |
| 5984 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5985 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 5986 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5987 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5988 | ret = io_recv(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5989 | break; |
| 5990 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5991 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5992 | break; |
| 5993 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5994 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5995 | break; |
| 5996 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5997 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5998 | break; |
| 5999 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6000 | ret = io_connect(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6001 | break; |
| 6002 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6003 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6004 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6005 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6006 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6007 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6008 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6009 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6010 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6011 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6012 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6013 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6014 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6015 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6016 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6017 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6018 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6019 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6020 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6021 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6022 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6023 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6024 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6025 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6026 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6027 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6028 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6029 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6030 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6031 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6032 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6033 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6034 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6035 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6036 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6037 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6038 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6039 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6040 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6041 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6042 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6043 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6044 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6045 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6046 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6047 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6048 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6049 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6050 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6051 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6052 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6053 | default: |
| 6054 | ret = -EINVAL; |
| 6055 | break; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6056 | } |
| 6057 | |
| 6058 | if (ret) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6059 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6060 | |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6061 | /* If the op doesn't have a file, we're not polling for it */ |
| 6062 | if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6063 | const bool in_async = io_wq_current_is_worker(); |
| 6064 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6065 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 6066 | if (in_async) |
| 6067 | mutex_lock(&ctx->uring_lock); |
| 6068 | |
Xiaoguang Wang | 2e9dbe9 | 2020-11-13 00:44:08 +0800 | [diff] [blame] | 6069 | io_iopoll_req_issued(req, in_async); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 6070 | |
| 6071 | if (in_async) |
| 6072 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | return 0; |
| 6076 | } |
| 6077 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6078 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6079 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6080 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6081 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6082 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6083 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6084 | timeout = io_prep_linked_timeout(req); |
| 6085 | if (timeout) |
| 6086 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6087 | |
Jens Axboe | 4014d94 | 2021-01-19 15:53:54 -0700 | [diff] [blame] | 6088 | if (work->flags & IO_WQ_WORK_CANCEL) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6089 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6090 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6091 | if (!ret) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6092 | do { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6093 | ret = io_issue_sqe(req, 0); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6094 | /* |
| 6095 | * We can get EAGAIN for polled IO even though we're |
| 6096 | * forcing a sync submission from here, since we can't |
| 6097 | * wait for request slots on the block side. |
| 6098 | */ |
| 6099 | if (ret != -EAGAIN) |
| 6100 | break; |
| 6101 | cond_resched(); |
| 6102 | } while (1); |
| 6103 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6104 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6105 | /* avoid locking problems by failing it from a clean context */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6106 | if (ret) { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6107 | /* io-wq is going to take one down */ |
| 6108 | refcount_inc(&req->refs); |
| 6109 | io_req_task_queue_fail(req, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6110 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6111 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6112 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6113 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6114 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6115 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6116 | struct fixed_rsrc_table *table; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6117 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6118 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
Xiaoming Ni | 8469508 | 2020-05-11 19:25:43 +0800 | [diff] [blame] | 6119 | return table->files[index & IORING_FILE_TABLE_MASK]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6120 | } |
| 6121 | |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6122 | static struct file *io_file_get(struct io_submit_state *state, |
| 6123 | struct io_kiocb *req, int fd, bool fixed) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6124 | { |
| 6125 | struct io_ring_ctx *ctx = req->ctx; |
| 6126 | struct file *file; |
| 6127 | |
| 6128 | if (fixed) { |
Pavel Begunkov | 479f517 | 2020-10-10 18:34:07 +0100 | [diff] [blame] | 6129 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6130 | return NULL; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6131 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6132 | file = io_file_from_index(ctx, fd); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 6133 | io_set_resource_node(req); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6134 | } else { |
| 6135 | trace_io_uring_file_get(ctx, fd); |
| 6136 | file = __io_file_get(state, fd); |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6137 | } |
| 6138 | |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 6139 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6140 | io_req_track_inflight(req); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6141 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6142 | } |
| 6143 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6144 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6145 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6146 | struct io_timeout_data *data = container_of(timer, |
| 6147 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6148 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6149 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6150 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6151 | |
| 6152 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6153 | prev = req->timeout.head; |
| 6154 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6155 | |
| 6156 | /* |
| 6157 | * We don't expect the list to be empty, that will only happen if we |
| 6158 | * race with the completion of the linked work. |
| 6159 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6160 | if (prev && refcount_inc_not_zero(&prev->refs)) |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6161 | io_remove_next_linked(prev); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6162 | else |
| 6163 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6164 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 6165 | |
| 6166 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6167 | req_set_fail_links(prev); |
Pavel Begunkov | 014db00 | 2020-03-03 21:33:12 +0300 | [diff] [blame] | 6168 | io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME); |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6169 | io_put_req_deferred(prev, 1); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6170 | } else { |
Pavel Begunkov | 9ae1f8d | 2021-02-01 18:59:51 +0000 | [diff] [blame] | 6171 | io_req_complete_post(req, -ETIME, 0); |
| 6172 | io_put_req_deferred(req, 1); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6173 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6174 | return HRTIMER_NORESTART; |
| 6175 | } |
| 6176 | |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6177 | static void __io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6178 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6179 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6180 | * If the back reference is NULL, then our linked request finished |
| 6181 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6182 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6183 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6184 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6185 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6186 | data->timer.function = io_link_timeout_fn; |
| 6187 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6188 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6189 | } |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | static void io_queue_linked_timeout(struct io_kiocb *req) |
| 6193 | { |
| 6194 | struct io_ring_ctx *ctx = req->ctx; |
| 6195 | |
| 6196 | spin_lock_irq(&ctx->completion_lock); |
| 6197 | __io_queue_linked_timeout(req); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6198 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6199 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6200 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6201 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6202 | } |
| 6203 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6204 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6205 | { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6206 | struct io_kiocb *nxt = req->link; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6207 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6208 | if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) || |
| 6209 | nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 6210 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6211 | |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6212 | nxt->timeout.head = req; |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 6213 | nxt->flags |= REQ_F_LTIMEOUT_ACTIVE; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6214 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6215 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6216 | } |
| 6217 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6218 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6219 | { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6220 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6221 | const struct cred *old_creds = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6222 | int ret; |
| 6223 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 6224 | if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds && |
| 6225 | req->work.creds != current_cred()) |
| 6226 | old_creds = override_creds(req->work.creds); |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6227 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6228 | 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] | 6229 | |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6230 | if (old_creds) |
| 6231 | revert_creds(old_creds); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 6232 | |
| 6233 | /* |
| 6234 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 6235 | * doesn't support non-blocking read/write attempts |
| 6236 | */ |
Pavel Begunkov | 24c7467 | 2020-06-21 13:09:51 +0300 | [diff] [blame] | 6237 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6238 | if (!io_arm_poll_handler(req)) { |
Pavel Begunkov | f063c54 | 2020-07-25 14:41:59 +0300 | [diff] [blame] | 6239 | /* |
| 6240 | * Queued up for async execution, worker will release |
| 6241 | * submit reference when the iocb is actually submitted. |
| 6242 | */ |
| 6243 | io_queue_async_work(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6244 | } |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6245 | } else if (likely(!ret)) { |
| 6246 | /* drop submission reference */ |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 6247 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6248 | struct io_ring_ctx *ctx = req->ctx; |
| 6249 | struct io_comp_state *cs = &ctx->submit_state.comp; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6250 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 6251 | cs->reqs[cs->nr++] = req; |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6252 | if (cs->nr == ARRAY_SIZE(cs->reqs)) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6253 | io_submit_flush_completions(cs, ctx); |
Pavel Begunkov | 9affd66 | 2021-01-19 13:32:46 +0000 | [diff] [blame] | 6254 | } else { |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6255 | io_put_req(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 6256 | } |
| 6257 | } else { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6258 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 6259 | io_put_req(req); |
Pavel Begunkov | 652532a | 2020-07-03 22:15:07 +0300 | [diff] [blame] | 6260 | io_req_complete(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6261 | } |
Pavel Begunkov | d3d7298 | 2021-02-12 03:23:51 +0000 | [diff] [blame] | 6262 | if (linked_timeout) |
| 6263 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6264 | } |
| 6265 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6266 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6267 | { |
| 6268 | int ret; |
| 6269 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6270 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6271 | if (ret) { |
| 6272 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 6273 | fail_req: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6274 | req_set_fail_links(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 6275 | io_put_req(req); |
| 6276 | io_req_complete(req, ret); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6277 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 6278 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6279 | ret = io_req_defer_prep(req); |
| 6280 | if (unlikely(ret)) |
| 6281 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6282 | io_queue_async_work(req); |
| 6283 | } else { |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 6284 | __io_queue_sqe(req); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 6285 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6286 | } |
| 6287 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6288 | /* |
| 6289 | * Check SQE restrictions (opcode and flags). |
| 6290 | * |
| 6291 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 6292 | */ |
| 6293 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 6294 | struct io_kiocb *req, |
| 6295 | unsigned int sqe_flags) |
| 6296 | { |
| 6297 | if (!ctx->restricted) |
| 6298 | return true; |
| 6299 | |
| 6300 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 6301 | return false; |
| 6302 | |
| 6303 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 6304 | ctx->restrictions.sqe_flags_required) |
| 6305 | return false; |
| 6306 | |
| 6307 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 6308 | ctx->restrictions.sqe_flags_required)) |
| 6309 | return false; |
| 6310 | |
| 6311 | return true; |
| 6312 | } |
| 6313 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6314 | 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] | 6315 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6316 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6317 | struct io_submit_state *state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6318 | unsigned int sqe_flags; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6319 | int id, ret = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6320 | |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6321 | req->opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6322 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 6323 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6324 | req->user_data = READ_ONCE(sqe->user_data); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6325 | req->async_data = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6326 | req->file = NULL; |
| 6327 | req->ctx = ctx; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6328 | req->link = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6329 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6330 | /* one is dropped after submission, the other at completion */ |
| 6331 | refcount_set(&req->refs, 2); |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 6332 | req->task = current; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6333 | req->result = 0; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6334 | |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6335 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6336 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
| 6337 | req->flags = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6338 | return -EINVAL; |
Pavel Begunkov | ebf4a5d | 2021-02-20 01:39:53 +0000 | [diff] [blame] | 6339 | } |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 6340 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6341 | if (unlikely(req->opcode >= IORING_OP_LAST)) |
| 6342 | return -EINVAL; |
| 6343 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 6344 | if (unlikely(!io_check_restriction(ctx, req, sqe_flags))) |
| 6345 | return -EACCES; |
| 6346 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6347 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
| 6348 | !io_op_defs[req->opcode].buffer_select) |
| 6349 | return -EOPNOTSUPP; |
| 6350 | |
| 6351 | id = READ_ONCE(sqe->personality); |
| 6352 | if (id) { |
Pavel Begunkov | ec99ca6 | 2020-10-18 10:17:38 +0100 | [diff] [blame] | 6353 | __io_req_init_async(req); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 6354 | req->work.creds = idr_find(&ctx->personality_idr, id); |
| 6355 | if (unlikely(!req->work.creds)) |
| 6356 | return -EINVAL; |
| 6357 | get_cred(req->work.creds); |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6358 | } |
| 6359 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6360 | state = &ctx->submit_state; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 6361 | |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 6362 | /* |
| 6363 | * Plug now if we have more than 1 IO left after this, and the target |
| 6364 | * is potentially a read/write to block based storage. |
| 6365 | */ |
| 6366 | if (!state->plug_started && state->ios_left > 1 && |
| 6367 | io_op_defs[req->opcode].plug) { |
| 6368 | blk_start_plug(&state->plug); |
| 6369 | state->plug_started = true; |
| 6370 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6371 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6372 | if (io_op_defs[req->opcode].needs_file) { |
| 6373 | bool fixed = req->flags & REQ_F_FIXED_FILE; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 6374 | |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6375 | req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 6376 | if (unlikely(!req->file)) |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 6377 | ret = -EBADF; |
| 6378 | } |
| 6379 | |
Pavel Begunkov | 71b547c | 2020-10-10 18:34:09 +0100 | [diff] [blame] | 6380 | state->ios_left--; |
| 6381 | return ret; |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 6382 | } |
| 6383 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6384 | 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] | 6385 | const struct io_uring_sqe *sqe) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6386 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6387 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6388 | int ret; |
| 6389 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6390 | ret = io_init_req(ctx, req, sqe); |
| 6391 | if (unlikely(ret)) { |
| 6392 | fail_req: |
| 6393 | io_put_req(req); |
| 6394 | io_req_complete(req, ret); |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6395 | if (link->head) { |
| 6396 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6397 | link->head->flags |= REQ_F_FAIL_LINK; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6398 | io_put_req(link->head); |
| 6399 | io_req_complete(link->head, -ECANCELED); |
| 6400 | link->head = NULL; |
| 6401 | } |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6402 | return ret; |
| 6403 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6404 | ret = io_req_prep(req, sqe); |
| 6405 | if (unlikely(ret)) |
| 6406 | goto fail_req; |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6407 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6408 | /* don't need @sqe from now on */ |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6409 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 6410 | true, ctx->flags & IORING_SETUP_SQPOLL); |
| 6411 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6412 | /* |
| 6413 | * If we already have a head request, queue this one for async |
| 6414 | * submittal once the head completes. If we don't have a head but |
| 6415 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 6416 | * submitted sync once the chain is complete. If none of those |
| 6417 | * conditions are true (normal request), then just queue it. |
| 6418 | */ |
| 6419 | if (link->head) { |
| 6420 | struct io_kiocb *head = link->head; |
| 6421 | |
| 6422 | /* |
| 6423 | * Taking sequential execution of a link, draining both sides |
| 6424 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 6425 | * requests in the link. So, it drains the head and the |
| 6426 | * next after the link request. The last one is done via |
| 6427 | * drain_next flag to persist the effect across calls. |
| 6428 | */ |
| 6429 | if (req->flags & REQ_F_IO_DRAIN) { |
| 6430 | head->flags |= REQ_F_IO_DRAIN; |
| 6431 | ctx->drain_next = 1; |
| 6432 | } |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6433 | ret = io_req_defer_prep(req); |
Pavel Begunkov | cf10960 | 2021-02-18 18:29:43 +0000 | [diff] [blame] | 6434 | if (unlikely(ret)) |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 6435 | goto fail_req; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6436 | trace_io_uring_link(ctx, req, head); |
| 6437 | link->last->link = req; |
| 6438 | link->last = req; |
| 6439 | |
| 6440 | /* last request of a link, enqueue the link */ |
| 6441 | if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6442 | io_queue_sqe(head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6443 | link->head = NULL; |
| 6444 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6445 | } else { |
| 6446 | if (unlikely(ctx->drain_next)) { |
| 6447 | req->flags |= REQ_F_IO_DRAIN; |
| 6448 | ctx->drain_next = 0; |
| 6449 | } |
| 6450 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6451 | link->head = req; |
| 6452 | link->last = req; |
| 6453 | } else { |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 6454 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 6455 | } |
| 6456 | } |
| 6457 | |
| 6458 | return 0; |
| 6459 | } |
| 6460 | |
| 6461 | /* |
| 6462 | * Batched submission is done, ensure local IO is flushed out. |
| 6463 | */ |
| 6464 | static void io_submit_state_end(struct io_submit_state *state, |
| 6465 | struct io_ring_ctx *ctx) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 6466 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6467 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 6468 | io_queue_sqe(state->link.head); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6469 | if (state->comp.nr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6470 | io_submit_flush_completions(&state->comp, ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 6471 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6472 | blk_finish_plug(&state->plug); |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6473 | io_state_file_put(state); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6474 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6475 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6476 | /* |
| 6477 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 6478 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6479 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6480 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6481 | { |
| 6482 | state->plug_started = false; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6483 | state->ios_left = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6484 | /* set only head, no need to init link_last in advance */ |
| 6485 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6486 | } |
| 6487 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6488 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 6489 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6490 | struct io_rings *rings = ctx->rings; |
| 6491 | |
| 6492 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 6493 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 6494 | * since once we write the new head, the application could |
| 6495 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 6496 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6497 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 6498 | } |
| 6499 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6500 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6501 | * 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] | 6502 | * that is mapped by userspace. This means that care needs to be taken to |
| 6503 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 6504 | * being a good citizen. If members of the sqe are validated and then later |
| 6505 | * 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] | 6506 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6507 | */ |
| 6508 | 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] | 6509 | { |
| 6510 | u32 *sq_array = ctx->sq_array; |
| 6511 | unsigned head; |
| 6512 | |
| 6513 | /* |
| 6514 | * The cached sq head (or cq tail) serves two purposes: |
| 6515 | * |
| 6516 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 6517 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6518 | * 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] | 6519 | * though the application is the one updating it. |
| 6520 | */ |
| 6521 | head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]); |
| 6522 | if (likely(head < ctx->sq_entries)) |
| 6523 | return &ctx->sq_sqes[head]; |
| 6524 | |
| 6525 | /* drop invalid entries */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 6526 | ctx->cached_sq_dropped++; |
| 6527 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
| 6528 | return NULL; |
| 6529 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 6530 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 6531 | 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] | 6532 | { |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6533 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6534 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 6535 | /* 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] | 6536 | if (test_bit(0, &ctx->sq_check_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6537 | if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 6538 | return -EBUSY; |
| 6539 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6540 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 6541 | /* make sure SQ entry isn't read before tail */ |
| 6542 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 6543 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 6544 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 6545 | return -EAGAIN; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6546 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6547 | percpu_counter_add(¤t->io_uring->inflight, nr); |
Jens Axboe | faf7b51 | 2020-10-07 12:48:53 -0600 | [diff] [blame] | 6548 | refcount_add(nr, ¤t->usage); |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 6549 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 6550 | |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 6551 | while (submitted < nr) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6552 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6553 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6554 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 6555 | req = io_alloc_req(ctx); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 6556 | if (unlikely(!req)) { |
| 6557 | if (!submitted) |
| 6558 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 6559 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 6560 | } |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 6561 | sqe = io_get_sqe(ctx); |
| 6562 | if (unlikely(!sqe)) { |
| 6563 | kmem_cache_free(req_cachep, req); |
| 6564 | break; |
| 6565 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6566 | /* will complete beyond this point, count as submitted */ |
| 6567 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6568 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6569 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6570 | } |
| 6571 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6572 | if (unlikely(submitted != nr)) { |
| 6573 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6574 | struct io_uring_task *tctx = current->io_uring; |
| 6575 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6576 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 6577 | percpu_ref_put_many(&ctx->refs, unused); |
| 6578 | percpu_counter_sub(&tctx->inflight, unused); |
| 6579 | put_task_struct_many(current, unused); |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 6580 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6581 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 6582 | io_submit_state_end(&ctx->submit_state, ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6583 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 6584 | io_commit_sqring(ctx); |
| 6585 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6586 | return submitted; |
| 6587 | } |
| 6588 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 6589 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 6590 | { |
| 6591 | /* Tell userspace we may need a wakeup call */ |
| 6592 | spin_lock_irq(&ctx->completion_lock); |
| 6593 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 6594 | spin_unlock_irq(&ctx->completion_lock); |
| 6595 | } |
| 6596 | |
| 6597 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 6598 | { |
| 6599 | spin_lock_irq(&ctx->completion_lock); |
| 6600 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 6601 | spin_unlock_irq(&ctx->completion_lock); |
| 6602 | } |
| 6603 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6604 | 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] | 6605 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6606 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 6607 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6608 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6609 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6610 | /* if we're handling multiple rings, cap submit size for fairness */ |
| 6611 | if (cap_entries && to_submit > 8) |
| 6612 | to_submit = 8; |
| 6613 | |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6614 | if (!list_empty(&ctx->iopoll_list) || to_submit) { |
| 6615 | unsigned nr_events = 0; |
| 6616 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6617 | mutex_lock(&ctx->uring_lock); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 6618 | if (!list_empty(&ctx->iopoll_list)) |
| 6619 | io_do_iopoll(ctx, &nr_events, 0); |
| 6620 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 6621 | if (to_submit && !ctx->sqo_dead && |
| 6622 | likely(!percpu_ref_is_dying(&ctx->refs))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6623 | ret = io_submit_sqes(ctx, to_submit); |
| 6624 | mutex_unlock(&ctx->uring_lock); |
| 6625 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 6626 | |
| 6627 | if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 6628 | wake_up(&ctx->sqo_sq_wait); |
| 6629 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6630 | return ret; |
| 6631 | } |
| 6632 | |
| 6633 | static void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
| 6634 | { |
| 6635 | struct io_ring_ctx *ctx; |
| 6636 | unsigned sq_thread_idle = 0; |
| 6637 | |
| 6638 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6639 | if (sq_thread_idle < ctx->sq_thread_idle) |
| 6640 | sq_thread_idle = ctx->sq_thread_idle; |
| 6641 | } |
| 6642 | |
| 6643 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6644 | } |
| 6645 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6646 | static void io_sqd_init_new(struct io_sq_data *sqd) |
| 6647 | { |
| 6648 | struct io_ring_ctx *ctx; |
| 6649 | |
| 6650 | while (!list_empty(&sqd->ctx_new_list)) { |
| 6651 | 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] | 6652 | list_move_tail(&ctx->sqd_list, &sqd->ctx_list); |
| 6653 | complete(&ctx->sq_thread_comp); |
| 6654 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6655 | |
| 6656 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6657 | } |
| 6658 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6659 | static bool io_sq_thread_should_stop(struct io_sq_data *sqd) |
| 6660 | { |
| 6661 | return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 6662 | } |
| 6663 | |
| 6664 | static bool io_sq_thread_should_park(struct io_sq_data *sqd) |
| 6665 | { |
| 6666 | return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 6667 | } |
| 6668 | |
| 6669 | static void io_sq_thread_parkme(struct io_sq_data *sqd) |
| 6670 | { |
| 6671 | for (;;) { |
| 6672 | /* |
| 6673 | * TASK_PARKED is a special state; we must serialize against |
| 6674 | * possible pending wakeups to avoid store-store collisions on |
| 6675 | * task->state. |
| 6676 | * |
| 6677 | * Such a collision might possibly result in the task state |
| 6678 | * changin from TASK_PARKED and us failing the |
| 6679 | * wait_task_inactive() in kthread_park(). |
| 6680 | */ |
| 6681 | set_special_state(TASK_PARKED); |
| 6682 | if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) |
| 6683 | break; |
| 6684 | |
| 6685 | /* |
| 6686 | * Thread is going to call schedule(), do not preempt it, |
| 6687 | * or the caller of kthread_park() may spend more time in |
| 6688 | * wait_task_inactive(). |
| 6689 | */ |
| 6690 | preempt_disable(); |
| 6691 | complete(&sqd->completion); |
| 6692 | schedule_preempt_disabled(); |
| 6693 | preempt_enable(); |
| 6694 | } |
| 6695 | __set_current_state(TASK_RUNNING); |
| 6696 | } |
| 6697 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6698 | static int io_sq_thread(void *data) |
| 6699 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6700 | struct io_sq_data *sqd = data; |
| 6701 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 6702 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6703 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6704 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6705 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6706 | sprintf(buf, "iou-sqp-%d", sqd->task_pid); |
| 6707 | set_task_comm(current, buf); |
| 6708 | sqd->thread = current; |
| 6709 | current->pf_io_worker = NULL; |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 6710 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6711 | if (sqd->sq_cpu != -1) |
| 6712 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 6713 | else |
| 6714 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 6715 | current->flags |= PF_NO_SETAFFINITY; |
| 6716 | |
| 6717 | complete(&sqd->completion); |
| 6718 | |
| 6719 | wait_for_completion(&sqd->startup); |
| 6720 | |
| 6721 | while (!io_sq_thread_should_stop(sqd)) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6722 | int ret; |
| 6723 | bool cap_entries, sqt_spin, needs_sched; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6724 | |
| 6725 | /* |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6726 | * Any changes to the sqd lists are synchronized through the |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6727 | * thread parking. This synchronizes the thread vs users, |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6728 | * the users are synchronized on the sqd->ctx_lock. |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6729 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6730 | if (io_sq_thread_should_park(sqd)) { |
| 6731 | io_sq_thread_parkme(sqd); |
| 6732 | continue; |
Xiaoguang Wang | 65b2b21 | 2020-11-19 17:44:46 +0800 | [diff] [blame] | 6733 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6734 | if (unlikely(!list_empty(&sqd->ctx_new_list))) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6735 | io_sqd_init_new(sqd); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6736 | timeout = jiffies + sqd->sq_thread_idle; |
| 6737 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6738 | if (fatal_signal_pending(current)) |
| 6739 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6740 | sqt_spin = false; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 6741 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6742 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6743 | ret = __io_sq_thread(ctx, cap_entries); |
| 6744 | if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list))) |
| 6745 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6746 | } |
| 6747 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6748 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 6749 | io_run_task_work(); |
| 6750 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6751 | if (sqt_spin) |
| 6752 | timeout = jiffies + sqd->sq_thread_idle; |
| 6753 | continue; |
| 6754 | } |
| 6755 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6756 | needs_sched = true; |
| 6757 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
| 6758 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
| 6759 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
| 6760 | !list_empty_careful(&ctx->iopoll_list)) { |
| 6761 | needs_sched = false; |
| 6762 | break; |
| 6763 | } |
| 6764 | if (io_sqring_entries(ctx)) { |
| 6765 | needs_sched = false; |
| 6766 | break; |
| 6767 | } |
| 6768 | } |
| 6769 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6770 | if (needs_sched && !io_sq_thread_should_park(sqd)) { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6771 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6772 | io_ring_set_wakeup_flag(ctx); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6773 | |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6774 | schedule(); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 6775 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6776 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6777 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 6778 | |
| 6779 | finish_wait(&sqd->wait, &wait); |
| 6780 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6781 | } |
| 6782 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6783 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 6784 | io_uring_cancel_sqpoll(ctx); |
| 6785 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6786 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6787 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6788 | /* |
| 6789 | * Clear thread under lock so that concurrent parks work correctly |
| 6790 | */ |
| 6791 | complete_all(&sqd->completion); |
| 6792 | mutex_lock(&sqd->lock); |
| 6793 | sqd->thread = NULL; |
| 6794 | mutex_unlock(&sqd->lock); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 6795 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 6796 | complete(&sqd->exited); |
| 6797 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6798 | } |
| 6799 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6800 | struct io_wait_queue { |
| 6801 | struct wait_queue_entry wq; |
| 6802 | struct io_ring_ctx *ctx; |
| 6803 | unsigned to_wait; |
| 6804 | unsigned nr_timeouts; |
| 6805 | }; |
| 6806 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6807 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6808 | { |
| 6809 | struct io_ring_ctx *ctx = iowq->ctx; |
| 6810 | |
| 6811 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 6812 | * 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] | 6813 | * started waiting. For timeouts, we always want to return to userspace, |
| 6814 | * regardless of event count. |
| 6815 | */ |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6816 | return io_cqring_events(ctx) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6817 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 6818 | } |
| 6819 | |
| 6820 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 6821 | int wake_flags, void *key) |
| 6822 | { |
| 6823 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 6824 | wq); |
| 6825 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6826 | /* |
| 6827 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 6828 | * the task, and the next invocation will do it. |
| 6829 | */ |
| 6830 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) |
| 6831 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 6832 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6833 | } |
| 6834 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6835 | static int io_run_task_work_sig(void) |
| 6836 | { |
| 6837 | if (io_run_task_work()) |
| 6838 | return 1; |
| 6839 | if (!signal_pending(current)) |
| 6840 | return 0; |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 6841 | if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL)) |
| 6842 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 6843 | return -EINTR; |
| 6844 | } |
| 6845 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6846 | /* when returns >0, the caller should retry */ |
| 6847 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 6848 | struct io_wait_queue *iowq, |
| 6849 | signed long *timeout) |
| 6850 | { |
| 6851 | int ret; |
| 6852 | |
| 6853 | /* make sure we run task_work before checking for signals */ |
| 6854 | ret = io_run_task_work_sig(); |
| 6855 | if (ret || io_should_wake(iowq)) |
| 6856 | return ret; |
| 6857 | /* let the caller flush overflows, retry */ |
| 6858 | if (test_bit(0, &ctx->cq_check_overflow)) |
| 6859 | return 1; |
| 6860 | |
| 6861 | *timeout = schedule_timeout(*timeout); |
| 6862 | return !*timeout ? -ETIME : 1; |
| 6863 | } |
| 6864 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6865 | /* |
| 6866 | * Wait until events become available, if we don't already have some. The |
| 6867 | * application must reap them itself, as they reside on the shared cq ring. |
| 6868 | */ |
| 6869 | 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] | 6870 | const sigset_t __user *sig, size_t sigsz, |
| 6871 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6872 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6873 | struct io_wait_queue iowq = { |
| 6874 | .wq = { |
| 6875 | .private = current, |
| 6876 | .func = io_wake_function, |
| 6877 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 6878 | }, |
| 6879 | .ctx = ctx, |
| 6880 | .to_wait = min_events, |
| 6881 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6882 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6883 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 6884 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6885 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6886 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6887 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| 6888 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6889 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 6890 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6891 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 6892 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6893 | |
| 6894 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6895 | #ifdef CONFIG_COMPAT |
| 6896 | if (in_compat_syscall()) |
| 6897 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6898 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6899 | else |
| 6900 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 6901 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 6902 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6903 | if (ret) |
| 6904 | return ret; |
| 6905 | } |
| 6906 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6907 | if (uts) { |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 6908 | struct timespec64 ts; |
| 6909 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 6910 | if (get_timespec64(&ts, uts)) |
| 6911 | return -EFAULT; |
| 6912 | timeout = timespec64_to_jiffies(&ts); |
| 6913 | } |
| 6914 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6915 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6916 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6917 | do { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 6918 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6919 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 6920 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 6921 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
| 6922 | finish_wait(&ctx->wait, &iowq.wq); |
| 6923 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 6924 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 6925 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6926 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6927 | 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] | 6928 | } |
| 6929 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6930 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 6931 | { |
| 6932 | #if defined(CONFIG_UNIX) |
| 6933 | if (ctx->ring_sock) { |
| 6934 | struct sock *sock = ctx->ring_sock->sk; |
| 6935 | struct sk_buff *skb; |
| 6936 | |
| 6937 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 6938 | kfree_skb(skb); |
| 6939 | } |
| 6940 | #else |
| 6941 | int i; |
| 6942 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6943 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 6944 | struct file *file; |
| 6945 | |
| 6946 | file = io_file_from_index(ctx, i); |
| 6947 | if (file) |
| 6948 | fput(file); |
| 6949 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6950 | #endif |
| 6951 | } |
| 6952 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 6953 | static void io_rsrc_data_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6954 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6955 | struct fixed_rsrc_data *data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6956 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6957 | data = container_of(ref, struct fixed_rsrc_data, refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6958 | complete(&data->done); |
| 6959 | } |
| 6960 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6961 | static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 6962 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6963 | spin_lock_bh(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6966 | static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6967 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6968 | spin_unlock_bh(&ctx->rsrc_ref_lock); |
| 6969 | } |
| 6970 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 6971 | static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx, |
| 6972 | struct fixed_rsrc_data *rsrc_data, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6973 | struct fixed_rsrc_ref_node *ref_node) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6974 | { |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6975 | io_rsrc_ref_lock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6976 | rsrc_data->node = ref_node; |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 6977 | list_add_tail(&ref_node->node, &ctx->rsrc_ref_list); |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6978 | io_rsrc_ref_unlock(ctx); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6979 | percpu_ref_get(&rsrc_data->refs); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6980 | } |
| 6981 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6982 | 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] | 6983 | { |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6984 | struct fixed_rsrc_ref_node *ref_node = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6985 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6986 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | 1e5d770 | 2020-11-18 14:56:25 +0000 | [diff] [blame] | 6987 | ref_node = data->node; |
Pavel Begunkov | e6cb007 | 2021-02-20 18:03:47 +0000 | [diff] [blame] | 6988 | data->node = NULL; |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 6989 | io_rsrc_ref_unlock(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6990 | if (ref_node) |
| 6991 | percpu_ref_kill(&ref_node->refs); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6992 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 6993 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6994 | static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data, |
| 6995 | struct io_ring_ctx *ctx, |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6996 | void (*rsrc_put)(struct io_ring_ctx *ctx, |
| 6997 | struct io_rsrc_put *prsrc)) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 6998 | { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 6999 | struct fixed_rsrc_ref_node *backup_node; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7000 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7001 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7002 | if (data->quiesce) |
| 7003 | return -ENXIO; |
| 7004 | |
| 7005 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7006 | do { |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7007 | ret = -ENOMEM; |
| 7008 | backup_node = alloc_fixed_rsrc_ref_node(ctx); |
| 7009 | if (!backup_node) |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7010 | break; |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7011 | backup_node->rsrc_data = data; |
| 7012 | backup_node->rsrc_put = rsrc_put; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7013 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7014 | io_sqe_rsrc_kill_node(ctx, data); |
| 7015 | percpu_ref_kill(&data->refs); |
| 7016 | flush_delayed_work(&ctx->rsrc_put_work); |
| 7017 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7018 | ret = wait_for_completion_interruptible(&data->done); |
Pavel Begunkov | 88f171a | 2021-02-20 18:03:50 +0000 | [diff] [blame] | 7019 | if (!ret || !io_refs_resurrect(&data->refs, &data->done)) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7020 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7021 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7022 | io_sqe_rsrc_set_node(ctx, data, backup_node); |
| 7023 | backup_node = NULL; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7024 | mutex_unlock(&ctx->uring_lock); |
| 7025 | ret = io_run_task_work_sig(); |
| 7026 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7027 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7028 | data->quiesce = false; |
| 7029 | |
| 7030 | if (backup_node) |
| 7031 | destroy_fixed_rsrc_ref_node(backup_node); |
| 7032 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7035 | static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx) |
| 7036 | { |
| 7037 | struct fixed_rsrc_data *data; |
| 7038 | |
| 7039 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7040 | if (!data) |
| 7041 | return NULL; |
| 7042 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7043 | if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero, |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7044 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 7045 | kfree(data); |
| 7046 | return NULL; |
| 7047 | } |
| 7048 | data->ctx = ctx; |
| 7049 | init_completion(&data->done); |
| 7050 | return data; |
| 7051 | } |
| 7052 | |
| 7053 | static void free_fixed_rsrc_data(struct fixed_rsrc_data *data) |
| 7054 | { |
| 7055 | percpu_ref_exit(&data->refs); |
| 7056 | kfree(data->table); |
| 7057 | kfree(data); |
| 7058 | } |
| 7059 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7060 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7061 | { |
| 7062 | struct fixed_rsrc_data *data = ctx->file_data; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7063 | unsigned nr_tables, i; |
| 7064 | int ret; |
| 7065 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7066 | /* |
| 7067 | * percpu_ref_is_dying() is to stop parallel files unregister |
| 7068 | * Since we possibly drop uring lock later in this function to |
| 7069 | * run task work. |
| 7070 | */ |
| 7071 | if (!data || percpu_ref_is_dying(&data->refs)) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7072 | return -ENXIO; |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7073 | ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put); |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7074 | if (ret) |
| 7075 | return ret; |
| 7076 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7077 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7078 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 7079 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7080 | kfree(data->table[i].files); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7081 | free_fixed_rsrc_data(data); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7082 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7083 | ctx->nr_user_files = 0; |
| 7084 | return 0; |
| 7085 | } |
| 7086 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7087 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
| 7088 | __releases(&sqd->lock) |
| 7089 | { |
| 7090 | if (!sqd->thread) |
| 7091 | return; |
| 7092 | if (sqd->thread == current) |
| 7093 | return; |
| 7094 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 7095 | wake_up_state(sqd->thread, TASK_PARKED); |
| 7096 | mutex_unlock(&sqd->lock); |
| 7097 | } |
| 7098 | |
| 7099 | static bool io_sq_thread_park(struct io_sq_data *sqd) |
| 7100 | __acquires(&sqd->lock) |
| 7101 | { |
| 7102 | if (sqd->thread == current) |
| 7103 | return true; |
| 7104 | mutex_lock(&sqd->lock); |
| 7105 | if (!sqd->thread) { |
| 7106 | mutex_unlock(&sqd->lock); |
| 7107 | return false; |
| 7108 | } |
| 7109 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
| 7110 | wake_up_process(sqd->thread); |
| 7111 | wait_for_completion(&sqd->completion); |
| 7112 | return true; |
| 7113 | } |
| 7114 | |
| 7115 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7116 | { |
| 7117 | if (!sqd->thread) |
| 7118 | return; |
| 7119 | |
| 7120 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7121 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)); |
| 7122 | wake_up_process(sqd->thread); |
| 7123 | wait_for_completion(&sqd->exited); |
| 7124 | } |
| 7125 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7126 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7127 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7128 | if (refcount_dec_and_test(&sqd->refs)) { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7129 | io_sq_thread_stop(sqd); |
| 7130 | kfree(sqd); |
| 7131 | } |
| 7132 | } |
| 7133 | |
| 7134 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 7135 | { |
| 7136 | struct io_sq_data *sqd = ctx->sq_data; |
| 7137 | |
| 7138 | if (sqd) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7139 | if (sqd->thread) { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7140 | wait_for_completion(&ctx->sq_thread_comp); |
| 7141 | io_sq_thread_park(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7142 | } |
| 7143 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7144 | mutex_lock(&sqd->ctx_lock); |
| 7145 | list_del(&ctx->sqd_list); |
| 7146 | io_sqd_update_thread_idle(sqd); |
| 7147 | mutex_unlock(&sqd->ctx_lock); |
| 7148 | |
| 7149 | if (sqd->thread) |
| 7150 | io_sq_thread_unpark(sqd); |
| 7151 | |
| 7152 | io_put_sq_data(sqd); |
| 7153 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7154 | } |
| 7155 | } |
| 7156 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7157 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 7158 | { |
| 7159 | struct io_ring_ctx *ctx_attach; |
| 7160 | struct io_sq_data *sqd; |
| 7161 | struct fd f; |
| 7162 | |
| 7163 | f = fdget(p->wq_fd); |
| 7164 | if (!f.file) |
| 7165 | return ERR_PTR(-ENXIO); |
| 7166 | if (f.file->f_op != &io_uring_fops) { |
| 7167 | fdput(f); |
| 7168 | return ERR_PTR(-EINVAL); |
| 7169 | } |
| 7170 | |
| 7171 | ctx_attach = f.file->private_data; |
| 7172 | sqd = ctx_attach->sq_data; |
| 7173 | if (!sqd) { |
| 7174 | fdput(f); |
| 7175 | return ERR_PTR(-EINVAL); |
| 7176 | } |
| 7177 | |
| 7178 | refcount_inc(&sqd->refs); |
| 7179 | fdput(f); |
| 7180 | return sqd; |
| 7181 | } |
| 7182 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7183 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p) |
| 7184 | { |
| 7185 | struct io_sq_data *sqd; |
| 7186 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7187 | if (p->flags & IORING_SETUP_ATTACH_WQ) |
| 7188 | return io_attach_sq_data(p); |
| 7189 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7190 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 7191 | if (!sqd) |
| 7192 | return ERR_PTR(-ENOMEM); |
| 7193 | |
| 7194 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7195 | INIT_LIST_HEAD(&sqd->ctx_list); |
| 7196 | INIT_LIST_HEAD(&sqd->ctx_new_list); |
| 7197 | mutex_init(&sqd->ctx_lock); |
| 7198 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7199 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7200 | init_completion(&sqd->startup); |
| 7201 | init_completion(&sqd->completion); |
| 7202 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7203 | return sqd; |
| 7204 | } |
| 7205 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7206 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7207 | /* |
| 7208 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 7209 | * the io_uring can be safely unregistered on process exit, even if we have |
| 7210 | * loops in the file referencing. |
| 7211 | */ |
| 7212 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 7213 | { |
| 7214 | struct sock *sk = ctx->ring_sock->sk; |
| 7215 | struct scm_fp_list *fpl; |
| 7216 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7217 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7218 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7219 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 7220 | if (!fpl) |
| 7221 | return -ENOMEM; |
| 7222 | |
| 7223 | skb = alloc_skb(0, GFP_KERNEL); |
| 7224 | if (!skb) { |
| 7225 | kfree(fpl); |
| 7226 | return -ENOMEM; |
| 7227 | } |
| 7228 | |
| 7229 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7230 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7231 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7232 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7233 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7234 | struct file *file = io_file_from_index(ctx, i + offset); |
| 7235 | |
| 7236 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7237 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7238 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7239 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 7240 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7241 | } |
| 7242 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7243 | if (nr_files) { |
| 7244 | fpl->max = SCM_MAX_FD; |
| 7245 | fpl->count = nr_files; |
| 7246 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7247 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7248 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 7249 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7250 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 7251 | for (i = 0; i < nr_files; i++) |
| 7252 | fput(fpl->fp[i]); |
| 7253 | } else { |
| 7254 | kfree_skb(skb); |
| 7255 | kfree(fpl); |
| 7256 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7257 | |
| 7258 | return 0; |
| 7259 | } |
| 7260 | |
| 7261 | /* |
| 7262 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 7263 | * causes regular reference counting to break down. We rely on the UNIX |
| 7264 | * garbage collection to take care of this problem for us. |
| 7265 | */ |
| 7266 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7267 | { |
| 7268 | unsigned left, total; |
| 7269 | int ret = 0; |
| 7270 | |
| 7271 | total = 0; |
| 7272 | left = ctx->nr_user_files; |
| 7273 | while (left) { |
| 7274 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7275 | |
| 7276 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 7277 | if (ret) |
| 7278 | break; |
| 7279 | left -= this_files; |
| 7280 | total += this_files; |
| 7281 | } |
| 7282 | |
| 7283 | if (!ret) |
| 7284 | return 0; |
| 7285 | |
| 7286 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7287 | struct file *file = io_file_from_index(ctx, total); |
| 7288 | |
| 7289 | if (file) |
| 7290 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7291 | total++; |
| 7292 | } |
| 7293 | |
| 7294 | return ret; |
| 7295 | } |
| 7296 | #else |
| 7297 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 7298 | { |
| 7299 | return 0; |
| 7300 | } |
| 7301 | #endif |
| 7302 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7303 | 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] | 7304 | unsigned nr_tables, unsigned nr_files) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7305 | { |
| 7306 | int i; |
| 7307 | |
| 7308 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7309 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7310 | unsigned this_files; |
| 7311 | |
| 7312 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 7313 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 7314 | GFP_KERNEL); |
| 7315 | if (!table->files) |
| 7316 | break; |
| 7317 | nr_files -= this_files; |
| 7318 | } |
| 7319 | |
| 7320 | if (i == nr_tables) |
| 7321 | return 0; |
| 7322 | |
| 7323 | for (i = 0; i < nr_tables; i++) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7324 | struct fixed_rsrc_table *table = &file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 7325 | kfree(table->files); |
| 7326 | } |
| 7327 | return 1; |
| 7328 | } |
| 7329 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7330 | 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] | 7331 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7332 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7333 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7334 | struct sock *sock = ctx->ring_sock->sk; |
| 7335 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 7336 | struct sk_buff *skb; |
| 7337 | int i; |
| 7338 | |
| 7339 | __skb_queue_head_init(&list); |
| 7340 | |
| 7341 | /* |
| 7342 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 7343 | * remove this entry and rearrange the file array. |
| 7344 | */ |
| 7345 | skb = skb_dequeue(head); |
| 7346 | while (skb) { |
| 7347 | struct scm_fp_list *fp; |
| 7348 | |
| 7349 | fp = UNIXCB(skb).fp; |
| 7350 | for (i = 0; i < fp->count; i++) { |
| 7351 | int left; |
| 7352 | |
| 7353 | if (fp->fp[i] != file) |
| 7354 | continue; |
| 7355 | |
| 7356 | unix_notinflight(fp->user, fp->fp[i]); |
| 7357 | left = fp->count - 1 - i; |
| 7358 | if (left) { |
| 7359 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 7360 | left * sizeof(struct file *)); |
| 7361 | } |
| 7362 | fp->count--; |
| 7363 | if (!fp->count) { |
| 7364 | kfree_skb(skb); |
| 7365 | skb = NULL; |
| 7366 | } else { |
| 7367 | __skb_queue_tail(&list, skb); |
| 7368 | } |
| 7369 | fput(file); |
| 7370 | file = NULL; |
| 7371 | break; |
| 7372 | } |
| 7373 | |
| 7374 | if (!file) |
| 7375 | break; |
| 7376 | |
| 7377 | __skb_queue_tail(&list, skb); |
| 7378 | |
| 7379 | skb = skb_dequeue(head); |
| 7380 | } |
| 7381 | |
| 7382 | if (skb_peek(&list)) { |
| 7383 | spin_lock_irq(&head->lock); |
| 7384 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 7385 | __skb_queue_tail(head, skb); |
| 7386 | spin_unlock_irq(&head->lock); |
| 7387 | } |
| 7388 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7389 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7390 | #endif |
| 7391 | } |
| 7392 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7393 | 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] | 7394 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7395 | struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data; |
| 7396 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 7397 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7398 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7399 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 7400 | list_del(&prsrc->list); |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7401 | ref_node->rsrc_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7402 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7403 | } |
| 7404 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7405 | percpu_ref_exit(&ref_node->refs); |
| 7406 | kfree(ref_node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7407 | percpu_ref_put(&rsrc_data->refs); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7408 | } |
| 7409 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7410 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7411 | { |
| 7412 | struct io_ring_ctx *ctx; |
| 7413 | struct llist_node *node; |
| 7414 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7415 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 7416 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7417 | |
| 7418 | while (node) { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7419 | struct fixed_rsrc_ref_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7420 | struct llist_node *next = node->next; |
| 7421 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7422 | ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist); |
| 7423 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7424 | node = next; |
| 7425 | } |
| 7426 | } |
| 7427 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7428 | static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data, |
| 7429 | unsigned i) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7430 | { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7431 | struct fixed_rsrc_table *table; |
| 7432 | |
| 7433 | table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 7434 | return &table->files[i & IORING_FILE_TABLE_MASK]; |
| 7435 | } |
| 7436 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7437 | static void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7438 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7439 | struct fixed_rsrc_ref_node *ref_node; |
| 7440 | struct fixed_rsrc_data *data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7441 | struct io_ring_ctx *ctx; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7442 | bool first_add = false; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7443 | int delay = HZ; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7444 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7445 | ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs); |
| 7446 | data = ref_node->rsrc_data; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7447 | ctx = data->ctx; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7448 | |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7449 | io_rsrc_ref_lock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7450 | ref_node->done = true; |
| 7451 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7452 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7453 | ref_node = list_first_entry(&ctx->rsrc_ref_list, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7454 | struct fixed_rsrc_ref_node, node); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7455 | /* recycle ref nodes in order */ |
| 7456 | if (!ref_node->done) |
| 7457 | break; |
| 7458 | list_del(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7459 | first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7460 | } |
Bijan Mottahedeh | 2a63b2d | 2021-01-15 17:37:47 +0000 | [diff] [blame] | 7461 | io_rsrc_ref_unlock(ctx); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7462 | |
| 7463 | if (percpu_ref_is_dying(&data->refs)) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7464 | delay = 0; |
| 7465 | |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7466 | if (!delay) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7467 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 7468 | else if (first_add) |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7469 | queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7470 | } |
| 7471 | |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7472 | static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node( |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7473 | struct io_ring_ctx *ctx) |
| 7474 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7475 | struct fixed_rsrc_ref_node *ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7476 | |
| 7477 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7478 | if (!ref_node) |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7479 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7480 | |
Bijan Mottahedeh | 00835dc | 2021-01-15 17:37:52 +0000 | [diff] [blame] | 7481 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7482 | 0, GFP_KERNEL)) { |
| 7483 | kfree(ref_node); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7484 | return NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7485 | } |
| 7486 | INIT_LIST_HEAD(&ref_node->node); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7487 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 7488 | ref_node->done = false; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7489 | return ref_node; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7490 | } |
| 7491 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7492 | static void init_fixed_file_ref_node(struct io_ring_ctx *ctx, |
| 7493 | struct fixed_rsrc_ref_node *ref_node) |
Bijan Mottahedeh | 6802535 | 2021-01-15 17:37:48 +0000 | [diff] [blame] | 7494 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7495 | ref_node->rsrc_data = ctx->file_data; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7496 | ref_node->rsrc_put = io_ring_file_put; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7497 | } |
| 7498 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7499 | 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] | 7500 | { |
| 7501 | percpu_ref_exit(&ref_node->refs); |
| 7502 | kfree(ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7503 | } |
| 7504 | |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7505 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7506 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 7507 | unsigned nr_args) |
| 7508 | { |
| 7509 | __s32 __user *fds = (__s32 __user *) arg; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7510 | unsigned nr_tables, i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7511 | struct file *file; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7512 | int fd, ret = -ENOMEM; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7513 | struct fixed_rsrc_ref_node *ref_node; |
| 7514 | struct fixed_rsrc_data *file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7515 | |
| 7516 | if (ctx->file_data) |
| 7517 | return -EBUSY; |
| 7518 | if (!nr_args) |
| 7519 | return -EINVAL; |
| 7520 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 7521 | return -EMFILE; |
| 7522 | |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7523 | file_data = alloc_fixed_rsrc_data(ctx); |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7524 | if (!file_data) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7525 | return -ENOMEM; |
Dan Carpenter | 13770a7 | 2021-02-01 15:23:42 +0300 | [diff] [blame] | 7526 | ctx->file_data = file_data; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7527 | |
| 7528 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
Colin Ian King | 035fbaf | 2020-10-12 15:03:41 +0100 | [diff] [blame] | 7529 | file_data->table = kcalloc(nr_tables, sizeof(*file_data->table), |
Pavel Begunkov | 5398ae6 | 2020-10-10 18:34:14 +0100 | [diff] [blame] | 7530 | GFP_KERNEL); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7531 | if (!file_data->table) |
| 7532 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7533 | |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7534 | if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args)) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7535 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7536 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7537 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7538 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
| 7539 | ret = -EFAULT; |
| 7540 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7541 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7542 | /* allow sparse sets */ |
| 7543 | if (fd == -1) |
| 7544 | continue; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7545 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7546 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7547 | ret = -EBADF; |
| 7548 | if (!file) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7549 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7550 | |
| 7551 | /* |
| 7552 | * Don't allow io_uring instances to be registered. If UNIX |
| 7553 | * isn't enabled, then this causes a reference cycle and this |
| 7554 | * instance can never get freed. If UNIX is enabled we'll |
| 7555 | * handle it just fine, but there's still no point in allowing |
| 7556 | * a ring fd as it doesn't support regular read/write anyway. |
| 7557 | */ |
| 7558 | if (file->f_op == &io_uring_fops) { |
| 7559 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7560 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7561 | } |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7562 | *io_fixed_file_slot(file_data, i) = file; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7563 | } |
| 7564 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7565 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7566 | if (ret) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7567 | io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7568 | return ret; |
| 7569 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7570 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7571 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7572 | if (!ref_node) { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7573 | io_sqe_files_unregister(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7574 | return -ENOMEM; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7575 | } |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7576 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7577 | |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7578 | io_sqe_rsrc_set_node(ctx, file_data, ref_node); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7579 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7580 | out_fput: |
| 7581 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7582 | file = io_file_from_index(ctx, i); |
| 7583 | if (file) |
| 7584 | fput(file); |
| 7585 | } |
| 7586 | for (i = 0; i < nr_tables; i++) |
| 7587 | kfree(file_data->table[i].files); |
| 7588 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 7589 | out_free: |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7590 | free_fixed_rsrc_data(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 7591 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7592 | return ret; |
| 7593 | } |
| 7594 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7595 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 7596 | int index) |
| 7597 | { |
| 7598 | #if defined(CONFIG_UNIX) |
| 7599 | struct sock *sock = ctx->ring_sock->sk; |
| 7600 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 7601 | struct sk_buff *skb; |
| 7602 | |
| 7603 | /* |
| 7604 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 7605 | * file set. If there's no room, fall back to allocating a new skb |
| 7606 | * and filling it in. |
| 7607 | */ |
| 7608 | spin_lock_irq(&head->lock); |
| 7609 | skb = skb_peek(head); |
| 7610 | if (skb) { |
| 7611 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 7612 | |
| 7613 | if (fpl->count < SCM_MAX_FD) { |
| 7614 | __skb_unlink(skb, head); |
| 7615 | spin_unlock_irq(&head->lock); |
| 7616 | fpl->fp[fpl->count] = get_file(file); |
| 7617 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 7618 | fpl->count++; |
| 7619 | spin_lock_irq(&head->lock); |
| 7620 | __skb_queue_head(head, skb); |
| 7621 | } else { |
| 7622 | skb = NULL; |
| 7623 | } |
| 7624 | } |
| 7625 | spin_unlock_irq(&head->lock); |
| 7626 | |
| 7627 | if (skb) { |
| 7628 | fput(file); |
| 7629 | return 0; |
| 7630 | } |
| 7631 | |
| 7632 | return __io_sqe_files_scm(ctx, 1, index); |
| 7633 | #else |
| 7634 | return 0; |
| 7635 | #endif |
| 7636 | } |
| 7637 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7638 | 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] | 7639 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7640 | struct io_rsrc_put *prsrc; |
| 7641 | struct fixed_rsrc_ref_node *ref_node = data->node; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7642 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7643 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 7644 | if (!prsrc) |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7645 | return -ENOMEM; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7646 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7647 | prsrc->rsrc = rsrc; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7648 | list_add(&prsrc->list, &ref_node->rsrc_list); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7649 | |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7650 | return 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7651 | } |
| 7652 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7653 | static inline int io_queue_file_removal(struct fixed_rsrc_data *data, |
| 7654 | struct file *file) |
| 7655 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 7656 | return io_queue_rsrc_removal(data, (void *)file); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7657 | } |
| 7658 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7659 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7660 | struct io_uring_rsrc_update *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7661 | unsigned nr_args) |
| 7662 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7663 | struct fixed_rsrc_data *data = ctx->file_data; |
| 7664 | struct fixed_rsrc_ref_node *ref_node; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7665 | struct file *file, **file_slot; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7666 | __s32 __user *fds; |
| 7667 | int fd, i, err; |
| 7668 | __u32 done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7669 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7670 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7671 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7672 | return -EOVERFLOW; |
| 7673 | if (done > ctx->nr_user_files) |
| 7674 | return -EINVAL; |
| 7675 | |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7676 | ref_node = alloc_fixed_rsrc_ref_node(ctx); |
Matthew Wilcox (Oracle) | 3e2224c | 2021-01-06 16:09:26 +0000 | [diff] [blame] | 7677 | if (!ref_node) |
| 7678 | return -ENOMEM; |
Pavel Begunkov | bc9744c | 2021-01-15 17:37:49 +0000 | [diff] [blame] | 7679 | init_fixed_file_ref_node(ctx, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7680 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7681 | fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7682 | for (done = 0; done < nr_args; done++) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7683 | err = 0; |
| 7684 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 7685 | err = -EFAULT; |
| 7686 | break; |
| 7687 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 7688 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 7689 | continue; |
| 7690 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 7691 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7692 | file_slot = io_fixed_file_slot(ctx->file_data, i); |
| 7693 | |
| 7694 | if (*file_slot) { |
| 7695 | err = io_queue_file_removal(data, *file_slot); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 7696 | if (err) |
| 7697 | break; |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 7698 | *file_slot = NULL; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7699 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7700 | } |
| 7701 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7702 | file = fget(fd); |
| 7703 | if (!file) { |
| 7704 | err = -EBADF; |
| 7705 | break; |
| 7706 | } |
| 7707 | /* |
| 7708 | * Don't allow io_uring instances to be registered. If |
| 7709 | * UNIX isn't enabled, then this causes a reference |
| 7710 | * cycle and this instance can never get freed. If UNIX |
| 7711 | * is enabled we'll handle it just fine, but there's |
| 7712 | * still no point in allowing a ring fd as it doesn't |
| 7713 | * support regular read/write anyway. |
| 7714 | */ |
| 7715 | if (file->f_op == &io_uring_fops) { |
| 7716 | fput(file); |
| 7717 | err = -EBADF; |
| 7718 | break; |
| 7719 | } |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 7720 | *file_slot = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7721 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7722 | if (err) { |
Jens Axboe | e68a3ff | 2021-02-11 07:45:08 -0700 | [diff] [blame] | 7723 | *file_slot = NULL; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7724 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7725 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 7726 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7727 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7728 | } |
| 7729 | |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7730 | if (needs_switch) { |
Pavel Begunkov | b2e9685 | 2020-10-10 18:34:16 +0100 | [diff] [blame] | 7731 | percpu_ref_kill(&data->node->refs); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 7732 | io_sqe_rsrc_set_node(ctx, data, ref_node); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7733 | } else |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7734 | destroy_fixed_rsrc_ref_node(ref_node); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7735 | |
| 7736 | return done ? done : err; |
| 7737 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7738 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7739 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 7740 | unsigned nr_args) |
| 7741 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7742 | struct io_uring_rsrc_update up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7743 | |
| 7744 | if (!ctx->file_data) |
| 7745 | return -ENXIO; |
| 7746 | if (!nr_args) |
| 7747 | return -EINVAL; |
| 7748 | if (copy_from_user(&up, arg, sizeof(up))) |
| 7749 | return -EFAULT; |
| 7750 | if (up.resv) |
| 7751 | return -EINVAL; |
| 7752 | |
| 7753 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 7754 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 7755 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7756 | 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] | 7757 | { |
| 7758 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 7759 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 7760 | req = io_put_req_find_next(req); |
| 7761 | return req ? &req->work : NULL; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 7762 | } |
| 7763 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7764 | 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] | 7765 | { |
| 7766 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7767 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7768 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 7769 | data.free_work = io_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 7770 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7771 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7772 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 7773 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7774 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7775 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 7776 | } |
| 7777 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7778 | static int io_uring_alloc_task_context(struct task_struct *task, |
| 7779 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7780 | { |
| 7781 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7782 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7783 | |
| 7784 | tctx = kmalloc(sizeof(*tctx), GFP_KERNEL); |
| 7785 | if (unlikely(!tctx)) |
| 7786 | return -ENOMEM; |
| 7787 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7788 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 7789 | if (unlikely(ret)) { |
| 7790 | kfree(tctx); |
| 7791 | return ret; |
| 7792 | } |
| 7793 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7794 | tctx->io_wq = io_init_wq_offload(ctx); |
| 7795 | if (IS_ERR(tctx->io_wq)) { |
| 7796 | ret = PTR_ERR(tctx->io_wq); |
| 7797 | percpu_counter_destroy(&tctx->inflight); |
| 7798 | kfree(tctx); |
| 7799 | return ret; |
| 7800 | } |
| 7801 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7802 | xa_init(&tctx->xa); |
| 7803 | init_waitqueue_head(&tctx->wait); |
| 7804 | tctx->last = NULL; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 7805 | atomic_set(&tctx->in_idle, 0); |
| 7806 | tctx->sqpoll = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7807 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 7808 | spin_lock_init(&tctx->task_lock); |
| 7809 | INIT_WQ_LIST(&tctx->task_list); |
| 7810 | tctx->task_state = 0; |
| 7811 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7812 | return 0; |
| 7813 | } |
| 7814 | |
| 7815 | void __io_uring_free(struct task_struct *tsk) |
| 7816 | { |
| 7817 | struct io_uring_task *tctx = tsk->io_uring; |
| 7818 | |
| 7819 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7820 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7821 | kfree(tctx); |
| 7822 | tsk->io_uring = NULL; |
| 7823 | } |
| 7824 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7825 | static int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 7826 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7827 | { |
| 7828 | int ret; |
| 7829 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 7830 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 7831 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 7832 | IORING_SETUP_ATTACH_WQ) { |
| 7833 | struct fd f; |
| 7834 | |
| 7835 | f = fdget(p->wq_fd); |
| 7836 | if (!f.file) |
| 7837 | return -ENXIO; |
| 7838 | if (f.file->f_op != &io_uring_fops) { |
| 7839 | fdput(f); |
| 7840 | return -EINVAL; |
| 7841 | } |
| 7842 | fdput(f); |
| 7843 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7844 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7845 | struct io_sq_data *sqd; |
| 7846 | |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7847 | ret = -EPERM; |
Jens Axboe | ce59fc6 | 2020-09-02 13:28:09 -0600 | [diff] [blame] | 7848 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 7849 | goto err; |
| 7850 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7851 | sqd = io_get_sq_data(p); |
| 7852 | if (IS_ERR(sqd)) { |
| 7853 | ret = PTR_ERR(sqd); |
| 7854 | goto err; |
| 7855 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7856 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7857 | ctx->sq_data = sqd; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7858 | io_sq_thread_park(sqd); |
| 7859 | mutex_lock(&sqd->ctx_lock); |
| 7860 | list_add(&ctx->sqd_list, &sqd->ctx_new_list); |
| 7861 | mutex_unlock(&sqd->ctx_lock); |
| 7862 | io_sq_thread_unpark(sqd); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7863 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7864 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 7865 | if (!ctx->sq_thread_idle) |
| 7866 | ctx->sq_thread_idle = HZ; |
| 7867 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7868 | if (sqd->thread) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7869 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 7870 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7871 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7872 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7873 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7874 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 7875 | if (cpu >= nr_cpu_ids) |
| 7876 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 7877 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 7878 | goto err; |
| 7879 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7880 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7881 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7882 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7883 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7884 | |
| 7885 | sqd->task_pid = current->pid; |
| 7886 | current->flags |= PF_IO_WORKER; |
| 7887 | ret = io_wq_fork_thread(io_sq_thread, sqd); |
| 7888 | current->flags &= ~PF_IO_WORKER; |
| 7889 | if (ret < 0) { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7890 | sqd->thread = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7891 | goto err; |
| 7892 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7893 | wait_for_completion(&sqd->completion); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 7894 | ret = io_uring_alloc_task_context(sqd->thread, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7895 | if (ret) |
| 7896 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7897 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 7898 | /* Can't have SQ_AFF without SQPOLL */ |
| 7899 | ret = -EINVAL; |
| 7900 | goto err; |
| 7901 | } |
| 7902 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7903 | return 0; |
| 7904 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7905 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7906 | return ret; |
| 7907 | } |
| 7908 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7909 | static void io_sq_offload_start(struct io_ring_ctx *ctx) |
| 7910 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 7911 | struct io_sq_data *sqd = ctx->sq_data; |
| 7912 | |
| 7913 | if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7914 | complete(&sqd->startup); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 7915 | } |
| 7916 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7917 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 7918 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7919 | { |
| 7920 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 7921 | } |
| 7922 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7923 | static inline int __io_account_mem(struct user_struct *user, |
| 7924 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7925 | { |
| 7926 | unsigned long page_limit, cur_pages, new_pages; |
| 7927 | |
| 7928 | /* Don't allow more pages than we can safely lock */ |
| 7929 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 7930 | |
| 7931 | do { |
| 7932 | cur_pages = atomic_long_read(&user->locked_vm); |
| 7933 | new_pages = cur_pages + nr_pages; |
| 7934 | if (new_pages > page_limit) |
| 7935 | return -ENOMEM; |
| 7936 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 7937 | new_pages) != cur_pages); |
| 7938 | |
| 7939 | return 0; |
| 7940 | } |
| 7941 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7942 | 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] | 7943 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7944 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7945 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7946 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7947 | if (ctx->mm_account) |
| 7948 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7949 | } |
| 7950 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7951 | 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] | 7952 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7953 | int ret; |
| 7954 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 7955 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 7956 | ret = __io_account_mem(ctx->user, nr_pages); |
| 7957 | if (ret) |
| 7958 | return ret; |
| 7959 | } |
| 7960 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7961 | if (ctx->mm_account) |
| 7962 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 7963 | |
| 7964 | return 0; |
| 7965 | } |
| 7966 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7967 | static void io_mem_free(void *ptr) |
| 7968 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7969 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7970 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 7971 | if (!ptr) |
| 7972 | return; |
| 7973 | |
| 7974 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7975 | if (put_page_testzero(page)) |
| 7976 | free_compound_page(page); |
| 7977 | } |
| 7978 | |
| 7979 | static void *io_mem_alloc(size_t size) |
| 7980 | { |
| 7981 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 7982 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7983 | |
| 7984 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 7985 | } |
| 7986 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7987 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 7988 | size_t *sq_offset) |
| 7989 | { |
| 7990 | struct io_rings *rings; |
| 7991 | size_t off, sq_array_size; |
| 7992 | |
| 7993 | off = struct_size(rings, cqes, cq_entries); |
| 7994 | if (off == SIZE_MAX) |
| 7995 | return SIZE_MAX; |
| 7996 | |
| 7997 | #ifdef CONFIG_SMP |
| 7998 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 7999 | if (off == 0) |
| 8000 | return SIZE_MAX; |
| 8001 | #endif |
| 8002 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8003 | if (sq_offset) |
| 8004 | *sq_offset = off; |
| 8005 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8006 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8007 | if (sq_array_size == SIZE_MAX) |
| 8008 | return SIZE_MAX; |
| 8009 | |
| 8010 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8011 | return SIZE_MAX; |
| 8012 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8013 | return off; |
| 8014 | } |
| 8015 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8016 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8017 | { |
| 8018 | int i, j; |
| 8019 | |
| 8020 | if (!ctx->user_bufs) |
| 8021 | return -ENXIO; |
| 8022 | |
| 8023 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8024 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8025 | |
| 8026 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 8027 | unpin_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8028 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8029 | if (imu->acct_pages) |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8030 | io_unaccount_mem(ctx, imu->acct_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 8031 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8032 | imu->nr_bvecs = 0; |
| 8033 | } |
| 8034 | |
| 8035 | kfree(ctx->user_bufs); |
| 8036 | ctx->user_bufs = NULL; |
| 8037 | ctx->nr_user_bufs = 0; |
| 8038 | return 0; |
| 8039 | } |
| 8040 | |
| 8041 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8042 | void __user *arg, unsigned index) |
| 8043 | { |
| 8044 | struct iovec __user *src; |
| 8045 | |
| 8046 | #ifdef CONFIG_COMPAT |
| 8047 | if (ctx->compat) { |
| 8048 | struct compat_iovec __user *ciovs; |
| 8049 | struct compat_iovec ciov; |
| 8050 | |
| 8051 | ciovs = (struct compat_iovec __user *) arg; |
| 8052 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8053 | return -EFAULT; |
| 8054 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8055 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8056 | dst->iov_len = ciov.iov_len; |
| 8057 | return 0; |
| 8058 | } |
| 8059 | #endif |
| 8060 | src = (struct iovec __user *) arg; |
| 8061 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8062 | return -EFAULT; |
| 8063 | return 0; |
| 8064 | } |
| 8065 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8066 | /* |
| 8067 | * Not super efficient, but this is just a registration time. And we do cache |
| 8068 | * the last compound head, so generally we'll only do a full search if we don't |
| 8069 | * match that one. |
| 8070 | * |
| 8071 | * We check if the given compound head page has already been accounted, to |
| 8072 | * avoid double accounting it. This allows us to account the full size of the |
| 8073 | * page, not just the constituent pages of a huge page. |
| 8074 | */ |
| 8075 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8076 | int nr_pages, struct page *hpage) |
| 8077 | { |
| 8078 | int i, j; |
| 8079 | |
| 8080 | /* check current page array */ |
| 8081 | for (i = 0; i < nr_pages; i++) { |
| 8082 | if (!PageCompound(pages[i])) |
| 8083 | continue; |
| 8084 | if (compound_head(pages[i]) == hpage) |
| 8085 | return true; |
| 8086 | } |
| 8087 | |
| 8088 | /* check previously registered pages */ |
| 8089 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 8090 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 8091 | |
| 8092 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8093 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8094 | continue; |
| 8095 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8096 | return true; |
| 8097 | } |
| 8098 | } |
| 8099 | |
| 8100 | return false; |
| 8101 | } |
| 8102 | |
| 8103 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8104 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8105 | struct page **last_hpage) |
| 8106 | { |
| 8107 | int i, ret; |
| 8108 | |
| 8109 | for (i = 0; i < nr_pages; i++) { |
| 8110 | if (!PageCompound(pages[i])) { |
| 8111 | imu->acct_pages++; |
| 8112 | } else { |
| 8113 | struct page *hpage; |
| 8114 | |
| 8115 | hpage = compound_head(pages[i]); |
| 8116 | if (hpage == *last_hpage) |
| 8117 | continue; |
| 8118 | *last_hpage = hpage; |
| 8119 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 8120 | continue; |
| 8121 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 8122 | } |
| 8123 | } |
| 8124 | |
| 8125 | if (!imu->acct_pages) |
| 8126 | return 0; |
| 8127 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8128 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8129 | if (ret) |
| 8130 | imu->acct_pages = 0; |
| 8131 | return ret; |
| 8132 | } |
| 8133 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8134 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
| 8135 | struct io_mapped_ubuf *imu, |
| 8136 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8137 | { |
| 8138 | struct vm_area_struct **vmas = NULL; |
| 8139 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8140 | unsigned long off, start, end, ubuf; |
| 8141 | size_t size; |
| 8142 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8143 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8144 | ubuf = (unsigned long) iov->iov_base; |
| 8145 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 8146 | start = ubuf >> PAGE_SHIFT; |
| 8147 | nr_pages = end - start; |
| 8148 | |
| 8149 | ret = -ENOMEM; |
| 8150 | |
| 8151 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 8152 | if (!pages) |
| 8153 | goto done; |
| 8154 | |
| 8155 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 8156 | GFP_KERNEL); |
| 8157 | if (!vmas) |
| 8158 | goto done; |
| 8159 | |
| 8160 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 8161 | GFP_KERNEL); |
| 8162 | if (!imu->bvec) |
| 8163 | goto done; |
| 8164 | |
| 8165 | ret = 0; |
| 8166 | mmap_read_lock(current->mm); |
| 8167 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 8168 | pages, vmas); |
| 8169 | if (pret == nr_pages) { |
| 8170 | /* don't support file backed memory */ |
| 8171 | for (i = 0; i < nr_pages; i++) { |
| 8172 | struct vm_area_struct *vma = vmas[i]; |
| 8173 | |
| 8174 | if (vma->vm_file && |
| 8175 | !is_file_hugepages(vma->vm_file)) { |
| 8176 | ret = -EOPNOTSUPP; |
| 8177 | break; |
| 8178 | } |
| 8179 | } |
| 8180 | } else { |
| 8181 | ret = pret < 0 ? pret : -EFAULT; |
| 8182 | } |
| 8183 | mmap_read_unlock(current->mm); |
| 8184 | if (ret) { |
| 8185 | /* |
| 8186 | * if we did partial map, or found file backed vmas, |
| 8187 | * release any pages we did get |
| 8188 | */ |
| 8189 | if (pret > 0) |
| 8190 | unpin_user_pages(pages, pret); |
| 8191 | kvfree(imu->bvec); |
| 8192 | goto done; |
| 8193 | } |
| 8194 | |
| 8195 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 8196 | if (ret) { |
| 8197 | unpin_user_pages(pages, pret); |
| 8198 | kvfree(imu->bvec); |
| 8199 | goto done; |
| 8200 | } |
| 8201 | |
| 8202 | off = ubuf & ~PAGE_MASK; |
| 8203 | size = iov->iov_len; |
| 8204 | for (i = 0; i < nr_pages; i++) { |
| 8205 | size_t vec_len; |
| 8206 | |
| 8207 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 8208 | imu->bvec[i].bv_page = pages[i]; |
| 8209 | imu->bvec[i].bv_len = vec_len; |
| 8210 | imu->bvec[i].bv_offset = off; |
| 8211 | off = 0; |
| 8212 | size -= vec_len; |
| 8213 | } |
| 8214 | /* store original address for later verification */ |
| 8215 | imu->ubuf = ubuf; |
| 8216 | imu->len = iov->iov_len; |
| 8217 | imu->nr_bvecs = nr_pages; |
| 8218 | ret = 0; |
| 8219 | done: |
| 8220 | kvfree(pages); |
| 8221 | kvfree(vmas); |
| 8222 | return ret; |
| 8223 | } |
| 8224 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8225 | 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] | 8226 | { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8227 | if (ctx->user_bufs) |
| 8228 | return -EBUSY; |
| 8229 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 8230 | return -EINVAL; |
| 8231 | |
| 8232 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 8233 | GFP_KERNEL); |
| 8234 | if (!ctx->user_bufs) |
| 8235 | return -ENOMEM; |
| 8236 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8237 | return 0; |
| 8238 | } |
| 8239 | |
| 8240 | static int io_buffer_validate(struct iovec *iov) |
| 8241 | { |
| 8242 | /* |
| 8243 | * Don't impose further limits on the size and buffer |
| 8244 | * constraints here, we'll -EINVAL later when IO is |
| 8245 | * submitted if they are wrong. |
| 8246 | */ |
| 8247 | if (!iov->iov_base || !iov->iov_len) |
| 8248 | return -EFAULT; |
| 8249 | |
| 8250 | /* arbitrary limit, but we need something */ |
| 8251 | if (iov->iov_len > SZ_1G) |
| 8252 | return -EFAULT; |
| 8253 | |
| 8254 | return 0; |
| 8255 | } |
| 8256 | |
| 8257 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 8258 | unsigned int nr_args) |
| 8259 | { |
| 8260 | int i, ret; |
| 8261 | struct iovec iov; |
| 8262 | struct page *last_hpage = NULL; |
| 8263 | |
| 8264 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 8265 | if (ret) |
| 8266 | return ret; |
| 8267 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8268 | for (i = 0; i < nr_args; i++) { |
| 8269 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8270 | |
| 8271 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 8272 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8273 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8274 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 8275 | ret = io_buffer_validate(&iov); |
| 8276 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8277 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8278 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8279 | ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage); |
| 8280 | if (ret) |
| 8281 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8282 | |
| 8283 | ctx->nr_user_bufs++; |
| 8284 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8285 | |
| 8286 | if (ret) |
| 8287 | io_sqe_buffers_unregister(ctx); |
| 8288 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8289 | return ret; |
| 8290 | } |
| 8291 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8292 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 8293 | { |
| 8294 | __s32 __user *fds = arg; |
| 8295 | int fd; |
| 8296 | |
| 8297 | if (ctx->cq_ev_fd) |
| 8298 | return -EBUSY; |
| 8299 | |
| 8300 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 8301 | return -EFAULT; |
| 8302 | |
| 8303 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 8304 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 8305 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 8306 | ctx->cq_ev_fd = NULL; |
| 8307 | return ret; |
| 8308 | } |
| 8309 | |
| 8310 | return 0; |
| 8311 | } |
| 8312 | |
| 8313 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 8314 | { |
| 8315 | if (ctx->cq_ev_fd) { |
| 8316 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 8317 | ctx->cq_ev_fd = NULL; |
| 8318 | return 0; |
| 8319 | } |
| 8320 | |
| 8321 | return -ENXIO; |
| 8322 | } |
| 8323 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8324 | static int __io_destroy_buffers(int id, void *p, void *data) |
| 8325 | { |
| 8326 | struct io_ring_ctx *ctx = data; |
| 8327 | struct io_buffer *buf = p; |
| 8328 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 8329 | __io_remove_buffers(ctx, buf, id, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8330 | return 0; |
| 8331 | } |
| 8332 | |
| 8333 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 8334 | { |
| 8335 | idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx); |
| 8336 | idr_destroy(&ctx->io_buffer_idr); |
| 8337 | } |
| 8338 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8339 | 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] | 8340 | { |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8341 | struct io_kiocb *req, *nxt; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8342 | |
Jens Axboe | 68e68ee | 2021-02-13 09:00:02 -0700 | [diff] [blame] | 8343 | list_for_each_entry_safe(req, nxt, list, compl.list) { |
| 8344 | if (tsk && req->task != tsk) |
| 8345 | continue; |
Jens Axboe | 1b4c351 | 2021-02-10 00:03:19 +0000 | [diff] [blame] | 8346 | list_del(&req->compl.list); |
| 8347 | kmem_cache_free(req_cachep, req); |
| 8348 | } |
| 8349 | } |
| 8350 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8351 | static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8352 | { |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 8353 | struct io_submit_state *submit_state = &ctx->submit_state; |
| 8354 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8355 | mutex_lock(&ctx->uring_lock); |
| 8356 | |
| 8357 | if (submit_state->free_reqs) |
| 8358 | kmem_cache_free_bulk(req_cachep, submit_state->free_reqs, |
| 8359 | submit_state->reqs); |
| 8360 | |
| 8361 | io_req_cache_free(&submit_state->comp.free_list, NULL); |
| 8362 | |
| 8363 | spin_lock_irq(&ctx->completion_lock); |
| 8364 | io_req_cache_free(&submit_state->comp.locked_free_list, NULL); |
| 8365 | spin_unlock_irq(&ctx->completion_lock); |
| 8366 | |
| 8367 | mutex_unlock(&ctx->uring_lock); |
| 8368 | } |
| 8369 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8370 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 8371 | { |
Pavel Begunkov | 04fc6c8 | 2021-02-12 03:23:54 +0000 | [diff] [blame] | 8372 | /* |
| 8373 | * Some may use context even when all refs and requests have been put, |
| 8374 | * and they are free to do so while still holding uring_lock, see |
| 8375 | * __io_req_task_submit(). Wait for them to finish. |
| 8376 | */ |
| 8377 | mutex_lock(&ctx->uring_lock); |
| 8378 | mutex_unlock(&ctx->uring_lock); |
| 8379 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8380 | io_sq_thread_finish(ctx); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 8381 | io_sqe_buffers_unregister(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8382 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8383 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 8384 | mmdrop(ctx->mm_account); |
| 8385 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8386 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8387 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8388 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8389 | io_sqe_files_unregister(ctx); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 8390 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 8391 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 8392 | io_destroy_buffers(ctx); |
Jens Axboe | 41726c9 | 2020-02-23 13:11:42 -0700 | [diff] [blame] | 8393 | idr_destroy(&ctx->personality_idr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 8394 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8395 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8396 | if (ctx->ring_sock) { |
| 8397 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8398 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 8399 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8400 | #endif |
| 8401 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8402 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8403 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8404 | |
| 8405 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8406 | free_uid(ctx->user); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 8407 | io_req_caches_free(ctx, NULL); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 8408 | kfree(ctx->cancel_hash); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8409 | kfree(ctx); |
| 8410 | } |
| 8411 | |
| 8412 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 8413 | { |
| 8414 | struct io_ring_ctx *ctx = file->private_data; |
| 8415 | __poll_t mask = 0; |
| 8416 | |
| 8417 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 8418 | /* |
| 8419 | * synchronizes with barrier from wq_has_sleeper call in |
| 8420 | * io_commit_cqring |
| 8421 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8422 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 8423 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8424 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 8425 | |
| 8426 | /* |
| 8427 | * Don't flush cqring overflow list here, just do a simple check. |
| 8428 | * Otherwise there could possible be ABBA deadlock: |
| 8429 | * CPU0 CPU1 |
| 8430 | * ---- ---- |
| 8431 | * lock(&ctx->uring_lock); |
| 8432 | * lock(&ep->mtx); |
| 8433 | * lock(&ctx->uring_lock); |
| 8434 | * lock(&ep->mtx); |
| 8435 | * |
| 8436 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 8437 | * pushs them to do the flush. |
| 8438 | */ |
| 8439 | if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8440 | mask |= EPOLLIN | EPOLLRDNORM; |
| 8441 | |
| 8442 | return mask; |
| 8443 | } |
| 8444 | |
| 8445 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 8446 | { |
| 8447 | struct io_ring_ctx *ctx = file->private_data; |
| 8448 | |
| 8449 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 8450 | } |
| 8451 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8452 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8453 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8454 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8455 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 8456 | creds = idr_remove(&ctx->personality_idr, id); |
| 8457 | if (creds) { |
| 8458 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8459 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 8460 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 8461 | |
| 8462 | return -EINVAL; |
| 8463 | } |
| 8464 | |
| 8465 | static int io_remove_personalities(int id, void *p, void *data) |
| 8466 | { |
| 8467 | struct io_ring_ctx *ctx = data; |
| 8468 | |
| 8469 | io_unregister_personality(ctx, id); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 8470 | return 0; |
| 8471 | } |
| 8472 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8473 | static void io_run_ctx_fallback(struct io_ring_ctx *ctx) |
| 8474 | { |
| 8475 | struct callback_head *work, *head, *next; |
| 8476 | |
| 8477 | do { |
| 8478 | do { |
| 8479 | head = NULL; |
| 8480 | work = READ_ONCE(ctx->exit_task_work); |
| 8481 | } while (cmpxchg(&ctx->exit_task_work, work, head) != work); |
| 8482 | |
| 8483 | if (!work) |
| 8484 | break; |
| 8485 | |
| 8486 | do { |
| 8487 | next = work->next; |
| 8488 | work->func(work); |
| 8489 | work = next; |
| 8490 | cond_resched(); |
| 8491 | } while (work); |
| 8492 | } while (1); |
| 8493 | } |
| 8494 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8495 | static void io_ring_exit_work(struct work_struct *work) |
| 8496 | { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8497 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 8498 | exit_work); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8499 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 8500 | /* |
| 8501 | * If we're doing polled IO and end up having requests being |
| 8502 | * submitted async (out-of-line), then completions can come in while |
| 8503 | * we're waiting for refs to drop. We need to reap these manually, |
| 8504 | * as nobody else will be looking for them. |
| 8505 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8506 | do { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8507 | io_uring_try_cancel_requests(ctx, NULL, NULL); |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8508 | io_run_ctx_fallback(ctx); |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 8509 | } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)); |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8510 | io_ring_ctx_free(ctx); |
| 8511 | } |
| 8512 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8513 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 8514 | { |
| 8515 | mutex_lock(&ctx->uring_lock); |
| 8516 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8517 | |
| 8518 | if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead)) |
| 8519 | ctx->sqo_dead = 1; |
| 8520 | |
Pavel Begunkov | cda286f | 2020-12-17 00:24:35 +0000 | [diff] [blame] | 8521 | /* if force is set, the ring is going away. always drop after that */ |
| 8522 | ctx->cq_overflow_flushed = 1; |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 8523 | if (ctx->rings) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 8524 | __io_cqring_overflow_flush(ctx, true, NULL, NULL); |
Pavel Begunkov | 5c766a9 | 2021-01-19 13:32:36 +0000 | [diff] [blame] | 8525 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8526 | mutex_unlock(&ctx->uring_lock); |
| 8527 | |
Pavel Begunkov | 6b81928 | 2020-11-06 13:00:25 +0000 | [diff] [blame] | 8528 | io_kill_timeouts(ctx, NULL, NULL); |
| 8529 | io_poll_remove_all(ctx, NULL, NULL); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 8530 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 8531 | /* 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] | 8532 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 8533 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 8534 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 8535 | /* |
| 8536 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 8537 | * if we're exiting a ton of rings at the same time. It just adds |
| 8538 | * noise and overhead, there's no discernable change in runtime |
| 8539 | * over using system_wq. |
| 8540 | */ |
| 8541 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8542 | } |
| 8543 | |
| 8544 | static int io_uring_release(struct inode *inode, struct file *file) |
| 8545 | { |
| 8546 | struct io_ring_ctx *ctx = file->private_data; |
| 8547 | |
| 8548 | file->private_data = NULL; |
| 8549 | io_ring_ctx_wait_and_kill(ctx); |
| 8550 | return 0; |
| 8551 | } |
| 8552 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8553 | struct io_task_cancel { |
| 8554 | struct task_struct *task; |
| 8555 | struct files_struct *files; |
| 8556 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 8557 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8558 | 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] | 8559 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8560 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8561 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8562 | bool ret; |
| 8563 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8564 | if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8565 | unsigned long flags; |
| 8566 | struct io_ring_ctx *ctx = req->ctx; |
| 8567 | |
| 8568 | /* protect against races with linked timeouts */ |
| 8569 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8570 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8571 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 8572 | } else { |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8573 | ret = io_match_task(req, cancel->task, cancel->files); |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 8574 | } |
| 8575 | return ret; |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 8576 | } |
| 8577 | |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8578 | static void io_cancel_defer_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | ef9865a | 2020-11-05 14:06:19 +0000 | [diff] [blame] | 8579 | struct task_struct *task, |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8580 | struct files_struct *files) |
| 8581 | { |
| 8582 | struct io_defer_entry *de = NULL; |
| 8583 | LIST_HEAD(list); |
| 8584 | |
| 8585 | spin_lock_irq(&ctx->completion_lock); |
| 8586 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 8587 | if (io_match_task(de->req, task, files)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 8588 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 8589 | break; |
| 8590 | } |
| 8591 | } |
| 8592 | spin_unlock_irq(&ctx->completion_lock); |
| 8593 | |
| 8594 | while (!list_empty(&list)) { |
| 8595 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 8596 | list_del_init(&de->list); |
| 8597 | req_set_fail_links(de->req); |
| 8598 | io_put_req(de->req); |
| 8599 | io_req_complete(de->req, -ECANCELED); |
| 8600 | kfree(de); |
| 8601 | } |
| 8602 | } |
| 8603 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8604 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 8605 | struct task_struct *task, |
| 8606 | struct files_struct *files) |
| 8607 | { |
| 8608 | struct io_task_cancel cancel = { .task = task, .files = files, }; |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8609 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8610 | |
| 8611 | while (1) { |
| 8612 | enum io_wq_cancel cret; |
| 8613 | bool ret = false; |
| 8614 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8615 | if (tctx && tctx->io_wq) { |
| 8616 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8617 | &cancel, true); |
| 8618 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 8619 | } |
| 8620 | |
| 8621 | /* SQPOLL thread does its own polling */ |
| 8622 | if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) { |
| 8623 | while (!list_empty_careful(&ctx->iopoll_list)) { |
| 8624 | io_iopoll_try_reap_events(ctx); |
| 8625 | ret = true; |
| 8626 | } |
| 8627 | } |
| 8628 | |
| 8629 | ret |= io_poll_remove_all(ctx, task, files); |
| 8630 | ret |= io_kill_timeouts(ctx, task, files); |
| 8631 | ret |= io_run_task_work(); |
| 8632 | io_cqring_overflow_flush(ctx, true, task, files); |
| 8633 | if (!ret) |
| 8634 | break; |
| 8635 | cond_resched(); |
| 8636 | } |
| 8637 | } |
| 8638 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8639 | static int io_uring_count_inflight(struct io_ring_ctx *ctx, |
| 8640 | struct task_struct *task, |
| 8641 | struct files_struct *files) |
| 8642 | { |
| 8643 | struct io_kiocb *req; |
| 8644 | int cnt = 0; |
| 8645 | |
| 8646 | spin_lock_irq(&ctx->inflight_lock); |
| 8647 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) |
| 8648 | cnt += io_match_task(req, task, files); |
| 8649 | spin_unlock_irq(&ctx->inflight_lock); |
| 8650 | return cnt; |
| 8651 | } |
| 8652 | |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8653 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8654 | struct task_struct *task, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8655 | struct files_struct *files) |
| 8656 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8657 | while (!list_empty_careful(&ctx->inflight_list)) { |
Xiaoguang Wang | d8f1b97 | 2020-04-26 15:54:43 +0800 | [diff] [blame] | 8658 | DEFINE_WAIT(wait); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8659 | int inflight; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8660 | |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8661 | inflight = io_uring_count_inflight(ctx, task, files); |
| 8662 | if (!inflight) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8663 | break; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 8664 | |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8665 | io_uring_try_cancel_requests(ctx, task, files); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8666 | |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 8667 | if (ctx->sq_data) |
| 8668 | io_sq_thread_unpark(ctx->sq_data); |
Pavel Begunkov | ca70f00 | 2021-01-26 15:28:27 +0000 | [diff] [blame] | 8669 | prepare_to_wait(&task->io_uring->wait, &wait, |
| 8670 | TASK_UNINTERRUPTIBLE); |
| 8671 | if (inflight == io_uring_count_inflight(ctx, task, files)) |
| 8672 | schedule(); |
Pavel Begunkov | c98de08 | 2020-11-15 12:56:32 +0000 | [diff] [blame] | 8673 | finish_wait(&task->io_uring->wait, &wait); |
Pavel Begunkov | 3434378 | 2021-02-10 11:45:42 +0000 | [diff] [blame] | 8674 | if (ctx->sq_data) |
| 8675 | io_sq_thread_park(ctx->sq_data); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8676 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8677 | } |
| 8678 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8679 | static void io_disable_sqo_submit(struct io_ring_ctx *ctx) |
| 8680 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8681 | mutex_lock(&ctx->uring_lock); |
| 8682 | ctx->sqo_dead = 1; |
| 8683 | mutex_unlock(&ctx->uring_lock); |
| 8684 | |
| 8685 | /* make sure callers enter the ring to get error */ |
Pavel Begunkov | b441161 | 2021-01-13 12:42:24 +0000 | [diff] [blame] | 8686 | if (ctx->rings) |
| 8687 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8688 | } |
| 8689 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8690 | /* |
| 8691 | * We need to iteratively cancel requests, in case a request has dependent |
| 8692 | * hard links. These persist even for failure of cancelations, hence keep |
| 8693 | * looping until none are found. |
| 8694 | */ |
| 8695 | static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, |
| 8696 | struct files_struct *files) |
| 8697 | { |
| 8698 | struct task_struct *task = current; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8699 | bool did_park = false; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8700 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8701 | if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8702 | io_disable_sqo_submit(ctx); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8703 | did_park = io_sq_thread_park(ctx->sq_data); |
| 8704 | if (did_park) { |
| 8705 | task = ctx->sq_data->thread; |
| 8706 | atomic_inc(&task->io_uring->in_idle); |
| 8707 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8708 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8709 | |
Pavel Begunkov | df9923f | 2020-11-06 13:00:23 +0000 | [diff] [blame] | 8710 | io_cancel_defer_files(ctx, task, files); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8711 | |
Pavel Begunkov | 3a7efd1 | 2021-01-28 23:23:42 +0000 | [diff] [blame] | 8712 | io_uring_cancel_files(ctx, task, files); |
Pavel Begunkov | b52fda0 | 2020-11-06 13:00:24 +0000 | [diff] [blame] | 8713 | if (!files) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 8714 | io_uring_try_cancel_requests(ctx, task, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8715 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8716 | if (did_park) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8717 | atomic_dec(&task->io_uring->in_idle); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8718 | io_sq_thread_unpark(ctx->sq_data); |
| 8719 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8720 | } |
| 8721 | |
| 8722 | /* |
| 8723 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 8724 | */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8725 | 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] | 8726 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8727 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8728 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8729 | |
| 8730 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8731 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8732 | if (unlikely(ret)) |
| 8733 | return ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8734 | tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8735 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8736 | if (tctx->last != file) { |
| 8737 | void *old = xa_load(&tctx->xa, (unsigned long)file); |
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 (!old) { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8740 | get_file(file); |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 8741 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)file, |
| 8742 | file, GFP_KERNEL)); |
| 8743 | if (ret) { |
| 8744 | fput(file); |
| 8745 | return ret; |
| 8746 | } |
Pavel Begunkov | ecfc849 | 2021-01-25 11:42:20 +0000 | [diff] [blame] | 8747 | |
| 8748 | /* one and only SQPOLL file note, held by sqo_task */ |
| 8749 | WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && |
| 8750 | current != ctx->sqo_task); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8751 | } |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 8752 | tctx->last = file; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8753 | } |
| 8754 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8755 | /* |
| 8756 | * This is race safe in that the task itself is doing this, hence it |
| 8757 | * cannot be going through the exit/cancel paths at the same time. |
| 8758 | * This cannot be modified while exit/cancel is running. |
| 8759 | */ |
| 8760 | if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 8761 | tctx->sqpoll = true; |
| 8762 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8763 | return 0; |
| 8764 | } |
| 8765 | |
| 8766 | /* |
| 8767 | * Remove this io_uring_file -> task mapping. |
| 8768 | */ |
| 8769 | static void io_uring_del_task_file(struct file *file) |
| 8770 | { |
| 8771 | struct io_uring_task *tctx = current->io_uring; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8772 | |
| 8773 | if (tctx->last == file) |
| 8774 | tctx->last = NULL; |
Matthew Wilcox (Oracle) | 5e2ed8c | 2020-10-09 13:49:53 +0100 | [diff] [blame] | 8775 | file = xa_erase(&tctx->xa, (unsigned long)file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8776 | if (file) |
| 8777 | fput(file); |
| 8778 | } |
| 8779 | |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8780 | static void io_uring_remove_task_files(struct io_uring_task *tctx) |
| 8781 | { |
| 8782 | struct file *file; |
| 8783 | unsigned long index; |
| 8784 | |
| 8785 | xa_for_each(&tctx->xa, index, file) |
| 8786 | io_uring_del_task_file(file); |
| 8787 | } |
| 8788 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8789 | void __io_uring_files_cancel(struct files_struct *files) |
| 8790 | { |
| 8791 | struct io_uring_task *tctx = current->io_uring; |
Matthew Wilcox (Oracle) | ce76537 | 2020-10-09 13:49:51 +0100 | [diff] [blame] | 8792 | struct file *file; |
| 8793 | unsigned long index; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8794 | |
| 8795 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8796 | atomic_inc(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8797 | xa_for_each(&tctx->xa, index, file) |
| 8798 | io_uring_cancel_task_requests(file->private_data, files); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8799 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8800 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8801 | if (files) { |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8802 | io_uring_remove_task_files(tctx); |
Jens Axboe | 8a378fb | 2021-02-23 12:27:49 -0700 | [diff] [blame] | 8803 | if (tctx->io_wq) { |
| 8804 | io_wq_destroy(tctx->io_wq); |
| 8805 | tctx->io_wq = NULL; |
| 8806 | } |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8807 | } |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8808 | } |
| 8809 | |
| 8810 | static s64 tctx_inflight(struct io_uring_task *tctx) |
| 8811 | { |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8812 | return percpu_counter_sum(&tctx->inflight); |
| 8813 | } |
| 8814 | |
| 8815 | static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx) |
| 8816 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8817 | struct io_sq_data *sqd = ctx->sq_data; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8818 | struct io_uring_task *tctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8819 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8820 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8821 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8822 | if (!sqd) |
| 8823 | return; |
| 8824 | io_disable_sqo_submit(ctx); |
| 8825 | if (!io_sq_thread_park(sqd)) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8826 | return; |
| 8827 | tctx = ctx->sq_data->thread->io_uring; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8828 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8829 | atomic_inc(&tctx->in_idle); |
| 8830 | do { |
| 8831 | /* read completions before cancelations */ |
| 8832 | inflight = tctx_inflight(tctx); |
| 8833 | if (!inflight) |
| 8834 | break; |
| 8835 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8836 | |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8837 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8838 | /* |
| 8839 | * If we've seen completions, retry without waiting. This |
| 8840 | * avoids a race where a completion comes in before we did |
| 8841 | * prepare_to_wait(). |
| 8842 | */ |
| 8843 | if (inflight == tctx_inflight(tctx)) |
| 8844 | schedule(); |
| 8845 | finish_wait(&tctx->wait, &wait); |
| 8846 | } while (1); |
| 8847 | atomic_dec(&tctx->in_idle); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8848 | io_sq_thread_unpark(sqd); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8849 | } |
| 8850 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8851 | /* |
| 8852 | * Find any io_uring fd that this task has registered or done IO on, and cancel |
| 8853 | * requests. |
| 8854 | */ |
| 8855 | void __io_uring_task_cancel(void) |
| 8856 | { |
| 8857 | struct io_uring_task *tctx = current->io_uring; |
| 8858 | DEFINE_WAIT(wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8859 | s64 inflight; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8860 | |
| 8861 | /* make sure overflow events are dropped */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8862 | atomic_inc(&tctx->in_idle); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8863 | |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 8864 | /* trigger io_disable_sqo_submit() */ |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 8865 | if (tctx->sqpoll) { |
| 8866 | struct file *file; |
| 8867 | unsigned long index; |
| 8868 | |
| 8869 | xa_for_each(&tctx->xa, index, file) |
| 8870 | io_uring_cancel_sqpoll(file->private_data); |
| 8871 | } |
Pavel Begunkov | 0b5cd6c | 2021-01-17 02:29:56 +0000 | [diff] [blame] | 8872 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8873 | do { |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8874 | /* read completions before cancelations */ |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8875 | inflight = tctx_inflight(tctx); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8876 | if (!inflight) |
| 8877 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8878 | __io_uring_files_cancel(NULL); |
| 8879 | |
| 8880 | prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 8881 | |
| 8882 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 8883 | * If we've seen completions, retry without waiting. This |
| 8884 | * avoids a race where a completion comes in before we did |
| 8885 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8886 | */ |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 8887 | if (inflight == tctx_inflight(tctx)) |
| 8888 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 8889 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8890 | } while (1); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8891 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8892 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 8893 | |
| 8894 | io_uring_remove_task_files(tctx); |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 8895 | } |
| 8896 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8897 | static int io_uring_flush(struct file *file, void *data) |
| 8898 | { |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8899 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8900 | struct io_ring_ctx *ctx = file->private_data; |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8901 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 8902 | /* Ignore helper thread files exit */ |
| 8903 | if (current->flags & PF_IO_WORKER) |
| 8904 | return 0; |
| 8905 | |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 8906 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 8907 | io_uring_cancel_task_requests(ctx, NULL); |
Jens Axboe | 41be53e | 2021-02-13 09:11:04 -0700 | [diff] [blame] | 8908 | io_req_caches_free(ctx, current); |
| 8909 | } |
Jens Axboe | 84965ff | 2021-01-23 15:51:11 -0700 | [diff] [blame] | 8910 | |
Jens Axboe | 7c25c0d | 2021-02-16 07:17:00 -0700 | [diff] [blame] | 8911 | io_run_ctx_fallback(ctx); |
| 8912 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8913 | if (!tctx) |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 8914 | return 0; |
| 8915 | |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8916 | /* we should have cancelled and erased it before PF_EXITING */ |
| 8917 | WARN_ON_ONCE((current->flags & PF_EXITING) && |
| 8918 | xa_load(&tctx->xa, (unsigned long)file)); |
| 8919 | |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 8920 | /* |
| 8921 | * fput() is pending, will be 2 if the only other ref is our potential |
| 8922 | * task file note. If the task is exiting, drop regardless of count. |
| 8923 | */ |
Pavel Begunkov | 6b5733e | 2021-01-08 20:57:24 +0000 | [diff] [blame] | 8924 | if (atomic_long_read(&file->f_count) != 2) |
| 8925 | return 0; |
Pavel Begunkov | 4f793dc | 2021-01-08 20:57:23 +0000 | [diff] [blame] | 8926 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8927 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 8928 | /* there is only one file note, which is owned by sqo_task */ |
Pavel Begunkov | 4325cb4 | 2021-01-16 05:32:30 +0000 | [diff] [blame] | 8929 | WARN_ON_ONCE(ctx->sqo_task != current && |
| 8930 | xa_load(&tctx->xa, (unsigned long)file)); |
| 8931 | /* sqo_dead check is for when this happens after cancellation */ |
| 8932 | WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead && |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 8933 | !xa_load(&tctx->xa, (unsigned long)file)); |
| 8934 | |
| 8935 | io_disable_sqo_submit(ctx); |
| 8936 | } |
| 8937 | |
| 8938 | if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current) |
| 8939 | io_uring_del_task_file(file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 8940 | return 0; |
| 8941 | } |
| 8942 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8943 | static void *io_uring_validate_mmap_request(struct file *file, |
| 8944 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8945 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8946 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8947 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8948 | struct page *page; |
| 8949 | void *ptr; |
| 8950 | |
| 8951 | switch (offset) { |
| 8952 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8953 | case IORING_OFF_CQ_RING: |
| 8954 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8955 | break; |
| 8956 | case IORING_OFF_SQES: |
| 8957 | ptr = ctx->sq_sqes; |
| 8958 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8959 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8960 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8961 | } |
| 8962 | |
| 8963 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 8964 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8965 | return ERR_PTR(-EINVAL); |
| 8966 | |
| 8967 | return ptr; |
| 8968 | } |
| 8969 | |
| 8970 | #ifdef CONFIG_MMU |
| 8971 | |
| 8972 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8973 | { |
| 8974 | size_t sz = vma->vm_end - vma->vm_start; |
| 8975 | unsigned long pfn; |
| 8976 | void *ptr; |
| 8977 | |
| 8978 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 8979 | if (IS_ERR(ptr)) |
| 8980 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8981 | |
| 8982 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 8983 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 8984 | } |
| 8985 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 8986 | #else /* !CONFIG_MMU */ |
| 8987 | |
| 8988 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 8989 | { |
| 8990 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 8991 | } |
| 8992 | |
| 8993 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 8994 | { |
| 8995 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 8996 | } |
| 8997 | |
| 8998 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 8999 | unsigned long addr, unsigned long len, |
| 9000 | unsigned long pgoff, unsigned long flags) |
| 9001 | { |
| 9002 | void *ptr; |
| 9003 | |
| 9004 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9005 | if (IS_ERR(ptr)) |
| 9006 | return PTR_ERR(ptr); |
| 9007 | |
| 9008 | return (unsigned long) ptr; |
| 9009 | } |
| 9010 | |
| 9011 | #endif /* !CONFIG_MMU */ |
| 9012 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9013 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9014 | { |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9015 | int ret = 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9016 | DEFINE_WAIT(wait); |
| 9017 | |
| 9018 | do { |
| 9019 | if (!io_sqring_full(ctx)) |
| 9020 | break; |
| 9021 | |
| 9022 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9023 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9024 | if (unlikely(ctx->sqo_dead)) { |
| 9025 | ret = -EOWNERDEAD; |
| 9026 | goto out; |
| 9027 | } |
| 9028 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9029 | if (!io_sqring_full(ctx)) |
| 9030 | break; |
| 9031 | |
| 9032 | schedule(); |
| 9033 | } while (!signal_pending(current)); |
| 9034 | |
| 9035 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9036 | out: |
| 9037 | return ret; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9038 | } |
| 9039 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9040 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9041 | struct __kernel_timespec __user **ts, |
| 9042 | const sigset_t __user **sig) |
| 9043 | { |
| 9044 | struct io_uring_getevents_arg arg; |
| 9045 | |
| 9046 | /* |
| 9047 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9048 | * is just a pointer to the sigset_t. |
| 9049 | */ |
| 9050 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9051 | *sig = (const sigset_t __user *) argp; |
| 9052 | *ts = NULL; |
| 9053 | return 0; |
| 9054 | } |
| 9055 | |
| 9056 | /* |
| 9057 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 9058 | * timespec and sigset_t pointers if good. |
| 9059 | */ |
| 9060 | if (*argsz != sizeof(arg)) |
| 9061 | return -EINVAL; |
| 9062 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 9063 | return -EFAULT; |
| 9064 | *sig = u64_to_user_ptr(arg.sigmask); |
| 9065 | *argsz = arg.sigmask_sz; |
| 9066 | *ts = u64_to_user_ptr(arg.ts); |
| 9067 | return 0; |
| 9068 | } |
| 9069 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9070 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9071 | u32, min_complete, u32, flags, const void __user *, argp, |
| 9072 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9073 | { |
| 9074 | struct io_ring_ctx *ctx; |
| 9075 | long ret = -EBADF; |
| 9076 | int submitted = 0; |
| 9077 | struct fd f; |
| 9078 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 9079 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 9080 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9081 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9082 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9083 | return -EINVAL; |
| 9084 | |
| 9085 | f = fdget(fd); |
| 9086 | if (!f.file) |
| 9087 | return -EBADF; |
| 9088 | |
| 9089 | ret = -EOPNOTSUPP; |
| 9090 | if (f.file->f_op != &io_uring_fops) |
| 9091 | goto out_fput; |
| 9092 | |
| 9093 | ret = -ENXIO; |
| 9094 | ctx = f.file->private_data; |
| 9095 | if (!percpu_ref_tryget(&ctx->refs)) |
| 9096 | goto out_fput; |
| 9097 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9098 | ret = -EBADFD; |
| 9099 | if (ctx->flags & IORING_SETUP_R_DISABLED) |
| 9100 | goto out; |
| 9101 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9102 | /* |
| 9103 | * For SQ polling, the thread will do all submissions and completions. |
| 9104 | * Just return the requested submit count, and wake the thread if |
| 9105 | * we were asked to. |
| 9106 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9107 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9108 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 9109 | io_cqring_overflow_flush(ctx, false, NULL, NULL); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 9110 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9111 | ret = -EOWNERDEAD; |
| 9112 | if (unlikely(ctx->sqo_dead)) |
| 9113 | goto out; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9114 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 9115 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9116 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 9117 | ret = io_sqpoll_wait_sq(ctx); |
| 9118 | if (ret) |
| 9119 | goto out; |
| 9120 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9121 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 9122 | } else if (to_submit) { |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9123 | ret = io_uring_add_task_file(ctx, f.file); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9124 | if (unlikely(ret)) |
| 9125 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9126 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9127 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9128 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9129 | |
| 9130 | if (submitted != to_submit) |
| 9131 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9132 | } |
| 9133 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9134 | const sigset_t __user *sig; |
| 9135 | struct __kernel_timespec __user *ts; |
| 9136 | |
| 9137 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 9138 | if (unlikely(ret)) |
| 9139 | goto out; |
| 9140 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9141 | min_complete = min(min_complete, ctx->cq_entries); |
| 9142 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 9143 | /* |
| 9144 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 9145 | * space applications don't need to do io completion events |
| 9146 | * polling again, they can rely on io_sq_thread to do polling |
| 9147 | * work, which can reduce cpu usage and uring_lock contention. |
| 9148 | */ |
| 9149 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 9150 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 9151 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9152 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9153 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9154 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9155 | } |
| 9156 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 9157 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 9158 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9159 | out_fput: |
| 9160 | fdput(f); |
| 9161 | return submitted ? submitted : ret; |
| 9162 | } |
| 9163 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9164 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9165 | static int io_uring_show_cred(int id, void *p, void *data) |
| 9166 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9167 | const struct cred *cred = p; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9168 | struct seq_file *m = data; |
| 9169 | struct user_namespace *uns = seq_user_ns(m); |
| 9170 | struct group_info *gi; |
| 9171 | kernel_cap_t cap; |
| 9172 | unsigned __capi; |
| 9173 | int g; |
| 9174 | |
| 9175 | seq_printf(m, "%5d\n", id); |
| 9176 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 9177 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 9178 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 9179 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 9180 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 9181 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 9182 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 9183 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 9184 | seq_puts(m, "\n\tGroups:\t"); |
| 9185 | gi = cred->group_info; |
| 9186 | for (g = 0; g < gi->ngroups; g++) { |
| 9187 | seq_put_decimal_ull(m, g ? " " : "", |
| 9188 | from_kgid_munged(uns, gi->gid[g])); |
| 9189 | } |
| 9190 | seq_puts(m, "\n\tCapEff:\t"); |
| 9191 | cap = cred->cap_effective; |
| 9192 | CAP_FOR_EACH_U32(__capi) |
| 9193 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 9194 | seq_putc(m, '\n'); |
| 9195 | return 0; |
| 9196 | } |
| 9197 | |
| 9198 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) |
| 9199 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9200 | struct io_sq_data *sq = NULL; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9201 | bool has_lock; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9202 | int i; |
| 9203 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9204 | /* |
| 9205 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 9206 | * since fdinfo case grabs it in the opposite direction of normal use |
| 9207 | * cases. If we fail to get the lock, we just don't iterate any |
| 9208 | * structures that could be going away outside the io_uring mutex. |
| 9209 | */ |
| 9210 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 9211 | |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 9212 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) |
| 9213 | sq = ctx->sq_data; |
| 9214 | |
| 9215 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 9216 | 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] | 9217 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9218 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 9219 | struct file *f = *io_fixed_file_slot(ctx->file_data, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9220 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9221 | if (f) |
| 9222 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 9223 | else |
| 9224 | seq_printf(m, "%5u: <none>\n", i); |
| 9225 | } |
| 9226 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9227 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9228 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; |
| 9229 | |
| 9230 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, |
| 9231 | (unsigned int) buf->len); |
| 9232 | } |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9233 | if (has_lock && !idr_is_empty(&ctx->personality_idr)) { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9234 | seq_printf(m, "Personalities:\n"); |
| 9235 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); |
| 9236 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 9237 | seq_printf(m, "PollList:\n"); |
| 9238 | spin_lock_irq(&ctx->completion_lock); |
| 9239 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 9240 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 9241 | struct io_kiocb *req; |
| 9242 | |
| 9243 | hlist_for_each_entry(req, list, hash_node) |
| 9244 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 9245 | req->task->task_works != NULL); |
| 9246 | } |
| 9247 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 9248 | if (has_lock) |
| 9249 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9250 | } |
| 9251 | |
| 9252 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
| 9253 | { |
| 9254 | struct io_ring_ctx *ctx = f->private_data; |
| 9255 | |
| 9256 | if (percpu_ref_tryget(&ctx->refs)) { |
| 9257 | __io_uring_show_fdinfo(ctx, m); |
| 9258 | percpu_ref_put(&ctx->refs); |
| 9259 | } |
| 9260 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9261 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9262 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9263 | static const struct file_operations io_uring_fops = { |
| 9264 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 9265 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9266 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9267 | #ifndef CONFIG_MMU |
| 9268 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 9269 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 9270 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9271 | .poll = io_uring_poll, |
| 9272 | .fasync = io_uring_fasync, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9273 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 9274 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 9275 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9276 | }; |
| 9277 | |
| 9278 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 9279 | struct io_uring_params *p) |
| 9280 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9281 | struct io_rings *rings; |
| 9282 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9283 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 9284 | /* make sure these are sane, as we already accounted them */ |
| 9285 | ctx->sq_entries = p->sq_entries; |
| 9286 | ctx->cq_entries = p->cq_entries; |
| 9287 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9288 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 9289 | if (size == SIZE_MAX) |
| 9290 | return -EOVERFLOW; |
| 9291 | |
| 9292 | rings = io_mem_alloc(size); |
| 9293 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9294 | return -ENOMEM; |
| 9295 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9296 | ctx->rings = rings; |
| 9297 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 9298 | rings->sq_ring_mask = p->sq_entries - 1; |
| 9299 | rings->cq_ring_mask = p->cq_entries - 1; |
| 9300 | rings->sq_ring_entries = p->sq_entries; |
| 9301 | rings->cq_ring_entries = p->cq_entries; |
| 9302 | ctx->sq_mask = rings->sq_ring_mask; |
| 9303 | ctx->cq_mask = rings->cq_ring_mask; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9304 | |
| 9305 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9306 | if (size == SIZE_MAX) { |
| 9307 | io_mem_free(ctx->rings); |
| 9308 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9309 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9310 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9311 | |
| 9312 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9313 | if (!ctx->sq_sqes) { |
| 9314 | io_mem_free(ctx->rings); |
| 9315 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9316 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 9317 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9318 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9319 | return 0; |
| 9320 | } |
| 9321 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9322 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 9323 | { |
| 9324 | int ret, fd; |
| 9325 | |
| 9326 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 9327 | if (fd < 0) |
| 9328 | return fd; |
| 9329 | |
| 9330 | ret = io_uring_add_task_file(ctx, file); |
| 9331 | if (ret) { |
| 9332 | put_unused_fd(fd); |
| 9333 | return ret; |
| 9334 | } |
| 9335 | fd_install(fd, file); |
| 9336 | return fd; |
| 9337 | } |
| 9338 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9339 | /* |
| 9340 | * Allocate an anonymous fd, this is what constitutes the application |
| 9341 | * visible backing of an io_uring instance. The application mmaps this |
| 9342 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 9343 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 9344 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9345 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9346 | { |
| 9347 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9348 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9349 | int ret; |
| 9350 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9351 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 9352 | &ctx->ring_sock); |
| 9353 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9354 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9355 | #endif |
| 9356 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9357 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 9358 | O_RDWR | O_CLOEXEC); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9359 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9360 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9361 | sock_release(ctx->ring_sock); |
| 9362 | ctx->ring_sock = NULL; |
| 9363 | } else { |
| 9364 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9365 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9366 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9367 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9368 | } |
| 9369 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9370 | static int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 9371 | struct io_uring_params __user *params) |
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 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9374 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9375 | int ret; |
| 9376 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9377 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9378 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9379 | if (entries > IORING_MAX_ENTRIES) { |
| 9380 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9381 | return -EINVAL; |
| 9382 | entries = IORING_MAX_ENTRIES; |
| 9383 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9384 | |
| 9385 | /* |
| 9386 | * Use twice as many entries for the CQ ring. It's possible for the |
| 9387 | * application to drive a higher depth than the size of the SQ ring, |
| 9388 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9389 | * some flexibility in overcommitting a bit. If the application has |
| 9390 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 9391 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9392 | */ |
| 9393 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9394 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 9395 | /* |
| 9396 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 9397 | * to a power-of-two, if it isn't already. We do NOT impose |
| 9398 | * any cq vs sq ring sizing. |
| 9399 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9400 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9401 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9402 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 9403 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 9404 | return -EINVAL; |
| 9405 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 9406 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 9407 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 9408 | if (p->cq_entries < p->sq_entries) |
| 9409 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 9410 | } else { |
| 9411 | p->cq_entries = 2 * p->sq_entries; |
| 9412 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9413 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9414 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9415 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9416 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9417 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 9418 | if (!capable(CAP_IPC_LOCK)) |
| 9419 | ctx->user = get_uid(current_user()); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9420 | ctx->sqo_task = current; |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9421 | |
| 9422 | /* |
| 9423 | * This is just grabbed for accounting purposes. When a process exits, |
| 9424 | * the mm is exited and dropped before the files, hence we need to hang |
| 9425 | * on to this mm purely for the purposes of being able to unaccount |
| 9426 | * memory (locked/pinned vm). It's not used for anything else. |
| 9427 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9428 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9429 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 9430 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9431 | ret = io_allocate_scq_urings(ctx, p); |
| 9432 | if (ret) |
| 9433 | goto err; |
| 9434 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9435 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9436 | if (ret) |
| 9437 | goto err; |
| 9438 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9439 | if (!(p->flags & IORING_SETUP_R_DISABLED)) |
| 9440 | io_sq_offload_start(ctx); |
| 9441 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9442 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9443 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 9444 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 9445 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 9446 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 9447 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 9448 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 9449 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9450 | |
| 9451 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9452 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 9453 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 9454 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 9455 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 9456 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 9457 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 9458 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 9459 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9460 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 9461 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9462 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9463 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Jens Axboe | 1c0aa1f | 2021-02-20 11:55:28 -0700 | [diff] [blame] | 9464 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9465 | |
| 9466 | if (copy_to_user(params, p, sizeof(*p))) { |
| 9467 | ret = -EFAULT; |
| 9468 | goto err; |
| 9469 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9470 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9471 | file = io_uring_get_file(ctx); |
| 9472 | if (IS_ERR(file)) { |
| 9473 | ret = PTR_ERR(file); |
| 9474 | goto err; |
| 9475 | } |
| 9476 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 9477 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9478 | * Install ring fd as the very last thing, so we don't risk someone |
| 9479 | * having closed it before we finish setup |
| 9480 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9481 | ret = io_uring_install_fd(ctx, file); |
| 9482 | if (ret < 0) { |
Pavel Begunkov | 06585c4 | 2021-01-13 12:42:25 +0000 | [diff] [blame] | 9483 | io_disable_sqo_submit(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 9484 | /* fput will clean it up */ |
| 9485 | fput(file); |
| 9486 | return ret; |
| 9487 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 9488 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9489 | 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] | 9490 | return ret; |
| 9491 | err: |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9492 | io_disable_sqo_submit(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9493 | io_ring_ctx_wait_and_kill(ctx); |
| 9494 | return ret; |
| 9495 | } |
| 9496 | |
| 9497 | /* |
| 9498 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 9499 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 9500 | * params structure passed in. |
| 9501 | */ |
| 9502 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 9503 | { |
| 9504 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9505 | int i; |
| 9506 | |
| 9507 | if (copy_from_user(&p, params, sizeof(p))) |
| 9508 | return -EFAULT; |
| 9509 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 9510 | if (p.resv[i]) |
| 9511 | return -EINVAL; |
| 9512 | } |
| 9513 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 9514 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 9515 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9516 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 9517 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9518 | return -EINVAL; |
| 9519 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 9520 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9521 | } |
| 9522 | |
| 9523 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 9524 | struct io_uring_params __user *, params) |
| 9525 | { |
| 9526 | return io_uring_setup(entries, params); |
| 9527 | } |
| 9528 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9529 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 9530 | { |
| 9531 | struct io_uring_probe *p; |
| 9532 | size_t size; |
| 9533 | int i, ret; |
| 9534 | |
| 9535 | size = struct_size(p, ops, nr_args); |
| 9536 | if (size == SIZE_MAX) |
| 9537 | return -EOVERFLOW; |
| 9538 | p = kzalloc(size, GFP_KERNEL); |
| 9539 | if (!p) |
| 9540 | return -ENOMEM; |
| 9541 | |
| 9542 | ret = -EFAULT; |
| 9543 | if (copy_from_user(p, arg, size)) |
| 9544 | goto out; |
| 9545 | ret = -EINVAL; |
| 9546 | if (memchr_inv(p, 0, size)) |
| 9547 | goto out; |
| 9548 | |
| 9549 | p->last_op = IORING_OP_LAST - 1; |
| 9550 | if (nr_args > IORING_OP_LAST) |
| 9551 | nr_args = IORING_OP_LAST; |
| 9552 | |
| 9553 | for (i = 0; i < nr_args; i++) { |
| 9554 | p->ops[i].op = i; |
| 9555 | if (!io_op_defs[i].not_supported) |
| 9556 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 9557 | } |
| 9558 | p->ops_len = i; |
| 9559 | |
| 9560 | ret = 0; |
| 9561 | if (copy_to_user(arg, p, size)) |
| 9562 | ret = -EFAULT; |
| 9563 | out: |
| 9564 | kfree(p); |
| 9565 | return ret; |
| 9566 | } |
| 9567 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9568 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 9569 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9570 | const struct cred *creds; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9571 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9572 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9573 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9574 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9575 | ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 9576 | USHRT_MAX, GFP_KERNEL); |
| 9577 | if (ret < 0) |
| 9578 | put_cred(creds); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9579 | return ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9580 | } |
| 9581 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9582 | static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, |
| 9583 | unsigned int nr_args) |
| 9584 | { |
| 9585 | struct io_uring_restriction *res; |
| 9586 | size_t size; |
| 9587 | int i, ret; |
| 9588 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9589 | /* Restrictions allowed only if rings started disabled */ |
| 9590 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9591 | return -EBADFD; |
| 9592 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9593 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9594 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9595 | return -EBUSY; |
| 9596 | |
| 9597 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 9598 | return -EINVAL; |
| 9599 | |
| 9600 | size = array_size(nr_args, sizeof(*res)); |
| 9601 | if (size == SIZE_MAX) |
| 9602 | return -EOVERFLOW; |
| 9603 | |
| 9604 | res = memdup_user(arg, size); |
| 9605 | if (IS_ERR(res)) |
| 9606 | return PTR_ERR(res); |
| 9607 | |
| 9608 | ret = 0; |
| 9609 | |
| 9610 | for (i = 0; i < nr_args; i++) { |
| 9611 | switch (res[i].opcode) { |
| 9612 | case IORING_RESTRICTION_REGISTER_OP: |
| 9613 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 9614 | ret = -EINVAL; |
| 9615 | goto out; |
| 9616 | } |
| 9617 | |
| 9618 | __set_bit(res[i].register_op, |
| 9619 | ctx->restrictions.register_op); |
| 9620 | break; |
| 9621 | case IORING_RESTRICTION_SQE_OP: |
| 9622 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 9623 | ret = -EINVAL; |
| 9624 | goto out; |
| 9625 | } |
| 9626 | |
| 9627 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 9628 | break; |
| 9629 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 9630 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 9631 | break; |
| 9632 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 9633 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 9634 | break; |
| 9635 | default: |
| 9636 | ret = -EINVAL; |
| 9637 | goto out; |
| 9638 | } |
| 9639 | } |
| 9640 | |
| 9641 | out: |
| 9642 | /* Reset all restrictions if an error happened */ |
| 9643 | if (ret != 0) |
| 9644 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 9645 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9646 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9647 | |
| 9648 | kfree(res); |
| 9649 | return ret; |
| 9650 | } |
| 9651 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9652 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 9653 | { |
| 9654 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 9655 | return -EBADFD; |
| 9656 | |
| 9657 | if (ctx->restrictions.registered) |
| 9658 | ctx->restricted = 1; |
| 9659 | |
| 9660 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 9661 | |
| 9662 | io_sq_offload_start(ctx); |
| 9663 | |
| 9664 | return 0; |
| 9665 | } |
| 9666 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9667 | static bool io_register_op_must_quiesce(int op) |
| 9668 | { |
| 9669 | switch (op) { |
| 9670 | case IORING_UNREGISTER_FILES: |
| 9671 | case IORING_REGISTER_FILES_UPDATE: |
| 9672 | case IORING_REGISTER_PROBE: |
| 9673 | case IORING_REGISTER_PERSONALITY: |
| 9674 | case IORING_UNREGISTER_PERSONALITY: |
| 9675 | return false; |
| 9676 | default: |
| 9677 | return true; |
| 9678 | } |
| 9679 | } |
| 9680 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9681 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 9682 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9683 | __releases(ctx->uring_lock) |
| 9684 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9685 | { |
| 9686 | int ret; |
| 9687 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 9688 | /* |
| 9689 | * We're inside the ring mutex, if the ref is already dying, then |
| 9690 | * someone else killed the ctx or is already going through |
| 9691 | * io_uring_register(). |
| 9692 | */ |
| 9693 | if (percpu_ref_is_dying(&ctx->refs)) |
| 9694 | return -ENXIO; |
| 9695 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9696 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9697 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 9698 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9699 | /* |
| 9700 | * Drop uring mutex before waiting for references to exit. If |
| 9701 | * another thread is currently inside io_uring_enter() it might |
| 9702 | * need to grab the uring_lock to make progress. If we hold it |
| 9703 | * here across the drain wait, then we can deadlock. It's safe |
| 9704 | * to drop the mutex here, since no new references will come in |
| 9705 | * after we've killed the percpu ref. |
| 9706 | */ |
| 9707 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9708 | do { |
| 9709 | ret = wait_for_completion_interruptible(&ctx->ref_comp); |
| 9710 | if (!ret) |
| 9711 | break; |
Jens Axboe | ed6930c | 2020-10-08 19:09:46 -0600 | [diff] [blame] | 9712 | ret = io_run_task_work_sig(); |
| 9713 | if (ret < 0) |
| 9714 | break; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9715 | } while (1); |
| 9716 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9717 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 9718 | |
Pavel Begunkov | 88f171a | 2021-02-20 18:03:50 +0000 | [diff] [blame] | 9719 | if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp)) |
| 9720 | return ret; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9721 | } |
| 9722 | |
| 9723 | if (ctx->restricted) { |
| 9724 | if (opcode >= IORING_REGISTER_LAST) { |
| 9725 | ret = -EINVAL; |
| 9726 | goto out; |
| 9727 | } |
| 9728 | |
| 9729 | if (!test_bit(opcode, ctx->restrictions.register_op)) { |
| 9730 | ret = -EACCES; |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 9731 | goto out; |
| 9732 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9733 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9734 | |
| 9735 | switch (opcode) { |
| 9736 | case IORING_REGISTER_BUFFERS: |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9737 | ret = io_sqe_buffers_register(ctx, arg, nr_args); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9738 | break; |
| 9739 | case IORING_UNREGISTER_BUFFERS: |
| 9740 | ret = -EINVAL; |
| 9741 | if (arg || nr_args) |
| 9742 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9743 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9744 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 9745 | case IORING_REGISTER_FILES: |
| 9746 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 9747 | break; |
| 9748 | case IORING_UNREGISTER_FILES: |
| 9749 | ret = -EINVAL; |
| 9750 | if (arg || nr_args) |
| 9751 | break; |
| 9752 | ret = io_sqe_files_unregister(ctx); |
| 9753 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 9754 | case IORING_REGISTER_FILES_UPDATE: |
| 9755 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 9756 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9757 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9758 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9759 | ret = -EINVAL; |
| 9760 | if (nr_args != 1) |
| 9761 | break; |
| 9762 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 9763 | if (ret) |
| 9764 | break; |
| 9765 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 9766 | ctx->eventfd_async = 1; |
| 9767 | else |
| 9768 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9769 | break; |
| 9770 | case IORING_UNREGISTER_EVENTFD: |
| 9771 | ret = -EINVAL; |
| 9772 | if (arg || nr_args) |
| 9773 | break; |
| 9774 | ret = io_eventfd_unregister(ctx); |
| 9775 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 9776 | case IORING_REGISTER_PROBE: |
| 9777 | ret = -EINVAL; |
| 9778 | if (!arg || nr_args > 256) |
| 9779 | break; |
| 9780 | ret = io_probe(ctx, arg, nr_args); |
| 9781 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9782 | case IORING_REGISTER_PERSONALITY: |
| 9783 | ret = -EINVAL; |
| 9784 | if (arg || nr_args) |
| 9785 | break; |
| 9786 | ret = io_register_personality(ctx); |
| 9787 | break; |
| 9788 | case IORING_UNREGISTER_PERSONALITY: |
| 9789 | ret = -EINVAL; |
| 9790 | if (arg) |
| 9791 | break; |
| 9792 | ret = io_unregister_personality(ctx, nr_args); |
| 9793 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 9794 | case IORING_REGISTER_ENABLE_RINGS: |
| 9795 | ret = -EINVAL; |
| 9796 | if (arg || nr_args) |
| 9797 | break; |
| 9798 | ret = io_register_enable_rings(ctx); |
| 9799 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9800 | case IORING_REGISTER_RESTRICTIONS: |
| 9801 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 9802 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9803 | default: |
| 9804 | ret = -EINVAL; |
| 9805 | break; |
| 9806 | } |
| 9807 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 9808 | out: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9809 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9810 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9811 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 9812 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 9813 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9814 | return ret; |
| 9815 | } |
| 9816 | |
| 9817 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 9818 | void __user *, arg, unsigned int, nr_args) |
| 9819 | { |
| 9820 | struct io_ring_ctx *ctx; |
| 9821 | long ret = -EBADF; |
| 9822 | struct fd f; |
| 9823 | |
| 9824 | f = fdget(fd); |
| 9825 | if (!f.file) |
| 9826 | return -EBADF; |
| 9827 | |
| 9828 | ret = -EOPNOTSUPP; |
| 9829 | if (f.file->f_op != &io_uring_fops) |
| 9830 | goto out_fput; |
| 9831 | |
| 9832 | ctx = f.file->private_data; |
| 9833 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 9834 | io_run_task_work(); |
| 9835 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9836 | mutex_lock(&ctx->uring_lock); |
| 9837 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 9838 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 9839 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 9840 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9841 | out_fput: |
| 9842 | fdput(f); |
| 9843 | return ret; |
| 9844 | } |
| 9845 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9846 | static int __init io_uring_init(void) |
| 9847 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9848 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 9849 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 9850 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 9851 | } while (0) |
| 9852 | |
| 9853 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 9854 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 9855 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 9856 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 9857 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 9858 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 9859 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 9860 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 9861 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 9862 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9863 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9864 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 9865 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 9866 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 9867 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 9868 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 9869 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 9870 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9871 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 9872 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 9873 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 9874 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 9875 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 9876 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 9877 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 9878 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9879 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9880 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 9881 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
| 9882 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 9883 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 9884 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 9885 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 9886 | BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int)); |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 9887 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 9888 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9889 | return 0; |
| 9890 | }; |
| 9891 | __initcall(io_uring_init); |